{"record":{"id":"a92e857d54be5e12","repo":"twigphp/Twig","slug":"the-first-argument-of-the-enum-cases-function-must-be-a","errorCode":null,"errorMessage":"The first argument of the \"enum_cases\" function must be a string.","messagePattern":"The first argument of the \"enum_cases\" function must be a string\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/Node/Expression/FunctionNode/EnumCasesFunction.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_cases\" 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_cases\" function must be the name of an enum, \"%s\" given.', $value), $this->getTemplateLine(), $this->getSourceContext());\n        }\n\n        $compiler->raw(\\sprintf('%s::cases()', $value));\n    }\n}\n","sourceCodeStart":23,"sourceCodeEnd":51,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Node/Expression/FunctionNode/EnumCasesFunction.php#L23-L51","documentation":"The enum_cases() Twig function compiles to <Enum>::cases(). At compile time it checks that its first argument is a constant string literal. If the constant node's value is not a string (e.g. a number or boolean literal), compilation fails with this SyntaxError.","triggerScenarios":"Writing {{ enum_cases(123) }} or {{ enum_cases(true) }} — the first argument is a constant expression but not a string. Note the check only applies to constant arguments; dynamic expressions are evaluated at runtime.","commonSituations":"Misunderstanding enum_cases as accepting any value; accidentally passing a numeric literal instead of a quoted enum class string.","solutions":["Pass the enum class name as a quoted string, e.g. {{ enum_cases('App\\\\Status') }}","If the enum name is dynamic, pass a runtime variable instead of a literal (runtime validation applies)","Check quoting: '123' vs 123"],"exampleFix":"// before (template)\n{{ enum_cases(123) }}\n// after\n{{ enum_cases('App\\\\Status') }}","handlingStrategy":"type-guard","validationCode":"<?php\n// before rendering\n$value = 123;\nif (!is_string($value)) throw new \\InvalidArgumentException('enum_cases arg must be a string');","typeGuard":"function isEnumName(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_cases')) { /* fix literal type */ }\n}","preventionTips":["Always quote enum class names in templates","Remember enum_cases expects an FQCN string literal, not arbitrary values","Validate enum names with enum_exists() in PHP before templating"],"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"}