{"record":{"id":"f3e5f2d82f90a917","repo":"twigphp/Twig","slug":"the-first-argument-of-the-enum-function-must-be-a-non-empty","errorCode":null,"errorMessage":"The first argument of the \"enum\" function must be a non-empty enum, \"%s\" given.","messagePattern":"The first argument of the \"enum\" function must be a non-empty enum, \"(.+?)\" given\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/Node/Expression/FunctionNode/EnumFunction.php","lineNumber":49,"sourceCode":"\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":31,"sourceCodeEnd":55,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Node/Expression/FunctionNode/EnumFunction.php#L31-L55","documentation":"enum() compiles to a constant reference of the enum's first case. If $value::cases() returns an empty array the previous error fires; this error covers the (defensive) non-empty requirement surfaced when cases exist check fails, i.e. the enum passed has no cases and the function cannot produce a first-case constant.","triggerScenarios":"Calling {{ enum('SomeEnum') }} where SomeEnum exists as an enum but declares no cases, making cases() return [].","commonSituations":"Empty enums used purely as type markers; scaffolding enums before cases are added; generated code omitting case statements.","solutions":["Declare at least one case in the enum","Target an enum known to have cases","Avoid enum() for empty enums; use enum_cases() which handles empties (returns empty list)","Review the enum definition in your codebase"],"exampleFix":"// before (PHP)\nenum Level {}\n// after\nenum Level { case Low; case High; }","handlingStrategy":"validation","validationCode":"<?php\nforeach (['App\\\\Level'] as $e) {\n    assert(\\enum_exists($e) && \\count(call_user_func($e.'::cases')) > 0);\n}","typeGuard":"function hasCases(string $enumName): bool { return \\enum_exists($enumName) && !empty(call_user_func($enumName.'::cases')); }","tryCatchPattern":"try {\n    echo $twig->render($template, $ctx);\n} catch (\\Twig\\Error\\SyntaxError $e) {\n    if (str_contains($e->getMessage(), 'non-empty enum')) { /* declare a case */ }\n}","preventionTips":["Keep an inventory of enums used in templates and assert non-emptiness","Use enum_cases() when emptiness is possible (returns an empty list safely)","Code-review enum definitions for at least one case"],"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"}