{"record":{"id":"c2407570a4f70a92","repo":"twigphp/Twig","slug":"enum-s-does-not-exist","errorCode":null,"errorMessage":"Enum \"%s\" does not exist.","messagePattern":"Enum \"(.+?)\" does not exist\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":1633,"sourceCode":"            return '';\n        }\n    }\n\n    /**\n     * Returns the list of cases of the enum.\n     *\n     * @template T of \\UnitEnum\n     *\n     * @param class-string<T> $enum\n     *\n     * @return list<T>\n     *\n     * @internal\n     */\n    public static function enumCases(string $enum): array\n    {\n        if (!enum_exists($enum)) {\n            throw new RuntimeError(\\sprintf('Enum \"%s\" does not exist.', $enum));\n        }\n\n        return $enum::cases();\n    }\n\n    /**\n     * Provides the ability to access enums by their class names.\n     *\n     * @template T of \\UnitEnum\n     *\n     * @param class-string<T> $enum\n     *\n     * @return T\n     *\n     * @internal\n     */\n    public static function enum(string $enum): \\UnitEnum\n    {","sourceCodeStart":1615,"sourceCodeEnd":1651,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L1615-L1651","documentation":"Thrown by CoreExtension::enumCases, which backs Twig's `enum_cases` function. It validates its argument with enum_exists() and throws this RuntimeError when the given class string is not an existing enum — either the class does not exist at all or it exists but is a regular class/interface, not an enum.","triggerScenarios":"Calling `enum_cases('App\\Status')` in a template where the FQCN is wrong (missing namespace, typo, leading backslash handling), the enum class has not been autoloaded, or a non-enum class is passed. Also when passing a non-string (e.g. an object) that stringifies incorrectly.","commonSituations":"Refactoring/moving an enum without updating the template string; using a short class name without namespace; autoloading misconfiguration (composer dump-autoload not run); passing a class that is an enum in newer PHP but a class in older versions of your codebase; PHP < 8.1 environments where enums do not exist.","solutions":["Pass the correct fully-qualified enum class name (e.g. 'App\\Enum\\Status'), verifying it with var_dump(enum_exists('App\\Enum\\Status'))","Run composer dump-autoload if the class exists but is not autoloaded","Check the argument is a string, not a variable holding an instance — for instances, pass the FQCN or use `enum_cases(class-string)`","Require PHP >= 8.1 and make the referenced type actually a backed/unit enum"],"exampleFix":"// before (Twig)\n{% for case in enum_cases('Status') %}...{% endfor %}\n// after\n{% for case in enum_cases('App\\\\Enum\\\\Status') %}...{% endfor %}","handlingStrategy":"type-guard","validationCode":"if (!\\is_string($enum) || !\\enum_exists($enum)) {\n    throw new \\InvalidArgumentException(\"Not a valid enum class: \" . print_r($enum, true));\n}","typeGuard":"function isValidEnumClass(mixed $class): bool {\n    return \\is_string($class) && \\enum_exists($class);\n}","tryCatchPattern":"try {\n    $cases = $twig->render('t.html', ['enumClass' => $enumClass]);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'does not exist')) {\n        // fall back to a hardcoded case list or log misconfiguration\n    } else { throw $e; }\n}","preventionTips":["Always pass fully-qualified class-name strings ('App\\Enum\\Status') into enum_cases","Run composer dump-autoload after adding/moving enums","Pin PHP >= 8.1 where enums exist and add a static analysis rule banning non-enum class strings","Keep enum FQCNs in PHP constants shared between templates and code instead of inline strings"],"tags":["twig","enum","class-not-found","argument-validation"],"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"}