{"record":{"id":"80d2ec3ae2d2e2da","repo":"twigphp/Twig","slug":"the-first-argument-of-the-enum-function-must-be-a-string","errorCode":null,"errorMessage":"The first argument of the \"enum\" function must be a string.","messagePattern":"The first argument of the \"enum\" function must be a string\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/Node/Expression/FunctionNode/EnumFunction.php","lineNumber":41,"sourceCode":"        $arguments = $this->getNode('arguments');\n        if ($arguments->hasNode('enum')) {\n            $firstArgument = $arguments->getNode('enum');\n        } elseif ($arguments->hasNode('0')) {\n            $firstArgument = $arguments->getNode('0');\n        } else {\n            $firstArgument = null;\n        }\n\n        if (!$firstArgument instanceof ConstantExpression || 1 !== \\count($arguments)) {\n            parent::compile($compiler);\n\n            return;\n        }\n\n        $value = $firstArgument->getAttribute('value');\n\n        if (!\\is_string($value)) {\n            throw new SyntaxError('The first argument of the \"enum\" function must be a string.', $this->getTemplateLine(), $this->getSourceContext());\n        }\n\n        if (!enum_exists($value)) {\n            throw new SyntaxError(\\sprintf('The first argument of the \"enum\" function must be the name of an enum, \"%s\" given.', $value), $this->getTemplateLine(), $this->getSourceContext());\n        }\n\n        if (!$cases = $value::cases()) {\n            throw new SyntaxError(\\sprintf('The first argument of the \"enum\" function must be a non-empty enum, \"%s\" given.', $value), $this->getTemplateLine(), $this->getSourceContext());\n        }\n\n        $compiler->raw(\\sprintf('%s::%s', $value, $cases[0]->name));\n    }\n}\n","sourceCodeStart":23,"sourceCodeEnd":55,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Node/Expression/FunctionNode/EnumFunction.php#L23-L55","documentation":"The enum() Twig function returns the first case of an enum. At compile time, its first constant argument must be a string; if the constant node's value is not a string (number, boolean, etc.), compilation fails with this SyntaxError.","triggerScenarios":"Writing {{ enum(42) }} or {{ enum(false) }} — the first argument is a constant expression that is not a string literal.","commonSituations":"Passing a numeric or boolean literal instead of the quoted enum class name; misunderstanding that enum() takes an enum class name string, not an enum instance.","solutions":["Pass the enum FQCN as a quoted string, e.g. {{ enum('App\\\\Status') }}","If you already have an enum instance, you don't need the enum() function — use it directly","Quote the value: '123' not 123"],"exampleFix":"// before (template)\n{{ enum(42) }}\n// after\n{{ enum('App\\\\Status') }}","handlingStrategy":"type-guard","validationCode":"<?php\n$value = 42;\nif (!is_string($value)) throw new \\InvalidArgumentException('enum() first arg must be a string');","typeGuard":"function isEnumClassString(mixed $v): bool { return \\is_string($v) && \\enum_exists($v); }","tryCatchPattern":"try {\n    echo $twig->render($template, $ctx);\n} catch (\\Twig\\Error\\SyntaxError $e) {\n    if (str_contains($e->getMessage(), '\"enum\" function must be a string')) { /* quote the arg */ }\n}","preventionTips":["Pass quoted enum FQCN strings to enum()","Don't pass enum instances to enum(); it expects a class-name string","Lint templates that use PHP 8.1 enum functions"],"tags":["twig","enum","type-mismatch","compile-time"],"backgroundTag":"type-mismatch","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}