{"record":{"id":"2091d1fc224ab4c0","repo":"twigphp/Twig","slug":"the-first-argument-of-the-enum-cases-function-must-be-the","errorCode":null,"errorMessage":"The first argument of the \"enum_cases\" function must be the name of an enum, \"%s\" given.","messagePattern":"The first argument of the \"enum_cases\" function must be the name of an enum, \"(.+?)\" given\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/Node/Expression/FunctionNode/EnumCasesFunction.php","lineNumber":45,"sourceCode":"            $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":27,"sourceCodeEnd":51,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Node/Expression/FunctionNode/EnumCasesFunction.php#L27-L51","documentation":"enum_cases() requires its first constant argument to name an existing PHP enum class. After confirming it is a string, Twig calls enum_exists(); if the string is not a loaded enum class, compilation fails with this SyntaxError showing the given value.","triggerScenarios":"{{ enum_cases('App\\\\Status') }} where the class does not exist, is not an enum, or is not yet loaded/autoloader-misspelled, e.g. {{ enum_cases('Status') }} without namespace, or passing an enum-like class name that is actually a plain class.","commonSituations":"Wrong namespace or typo in enum class name; enum defined in a file not autoloadable at compile time; confusing a class or interface with an enum; PHP < 8.1 environments (feature unavailable).","solutions":["Use the fully qualified enum class name (FQCN) as a string","Verify the class exists, is autoloadable, and is declared with 'enum' (PHP 8.1+)","Run the class name through class_exists/enum_exists in PHP to confirm before using in templates","Check for typos in backslash namespaces within template strings"],"exampleFix":"// before (template)\n{{ enum_cases('Status') }}\n// after\n{{ enum_cases('App\\\\Enum\\\\Status') }}","handlingStrategy":"validation","validationCode":"<?php\n$name = 'App\\\\Status';\nif (!\\enum_exists($name)) {\n    throw new \\InvalidArgumentException(\"$name is not a loaded enum (check namespace/autoloader)\");\n}","typeGuard":"function isValidEnum(?string $name): bool { return $name !== null && \\enum_exists($name); }","tryCatchPattern":"try {\n    echo $twig->render($template, $ctx);\n} catch (\\Twig\\Error\\SyntaxError $e) {\n    if (str_contains($e->getMessage(), 'name of an enum')) {\n        // verify FQCN, autoload, and PHP >= 8.1\n    }\n}","preventionTips":["Use fully qualified enum class names in templates","Ensure PHP >= 8.1 and the enum file is autoloadable","Sanity-check with enum_exists() in a unit test per template-facing enum"],"tags":["twig","enum","compile-time","class-not-found"],"backgroundTag":"invalid-enum-value","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"}