{"record":{"id":"298f198de07b373f","repo":"twigphp/Twig","slug":"the-first-argument-of-the-enum-function-must-be-the-name-of","errorCode":null,"errorMessage":"The first argument of the \"enum\" function must be the name of an enum, \"%s\" given.","messagePattern":"The first argument of the \"enum\" function must be the name of an enum, \"(.+?)\" given\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/Node/Expression/FunctionNode/EnumFunction.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\" 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":27,"sourceCodeEnd":55,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Node/Expression/FunctionNode/EnumFunction.php#L27-L55","documentation":"After confirming the first argument of enum() is a string naming an existing enum (via enum_exists), Twig fetches $value::cases(). If the enum has zero cases, the function cannot return a 'first case', so compilation fails with this SyntaxError naming the enum.","triggerScenarios":"Using {{ enum('App\\\\EmptyEnum') }} where EmptyEnum is declared as enum with no case statements. Pure enums or backed enums with no cases trigger this at compile time.","commonSituations":"Defining placeholder enums with no cases during development; code generation producing empty enums; importing enums from packages that declare empty enums for typing purposes.","solutions":["Add at least one case to the enum class","Use a different enum that has cases","If an empty enum is intentional, do not use the enum() function with it; reference cases explicitly","Check the enum source file for missing case declarations"],"exampleFix":"// before (PHP)\nenum Status {}\n// after\nenum Status { case Active; case Inactive; }","handlingStrategy":"validation","validationCode":"<?php\n$name = 'App\\\\EmptyEnum';\nif (\\enum_exists($name) && !call_user_func($name.'::cases')) {\n    throw new \\LogicException(\"$name has no cases; cannot use enum()\");\n}","typeGuard":"function isNonEmptyEnum(string $name): bool { return \\enum_exists($name) && \\count(call_user_func($name.'::cases')) > 0; }","tryCatchPattern":"try {\n    echo $twig->render($template, $ctx);\n} catch (\\Twig\\Error\\SyntaxError $e) {\n    if (str_contains($e->getMessage(), 'non-empty enum')) { /* add cases or pick another enum */ }\n}","preventionTips":["Never ship enums without at least one case if used by enum()","Add a unit test asserting case lists for template-referenced enums","Review code-generated enums for missing case declarations"],"tags":["twig","enum","empty-enum","compile-time"],"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"}