{"record":{"id":"63d6ae0352e73d3d","repo":"twigphp/Twig","slug":"s-is-not-an-enum","errorCode":null,"errorMessage":"\"%s\" is not an enum.","messagePattern":"\"(.+?)\" is not an enum\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":1653,"sourceCode":"\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    {\n        if (!enum_exists($enum)) {\n            throw new RuntimeError(\\sprintf('\"%s\" is not an enum.', $enum));\n        }\n\n        if (!$cases = $enum::cases()) {\n            throw new RuntimeError(\\sprintf('\"%s\" is an empty enum.', $enum));\n        }\n\n        return $cases[0];\n    }\n\n    /**\n     * Provides the ability to get constants from instances as well as class/global constants.\n     *\n     * @param string      $constant     The name of the constant\n     * @param object|null $object       The object to get the constant from\n     * @param bool        $checkDefined Whether to check if the constant is defined or not\n     *\n     * @return mixed Class constants can return many types like scalars, arrays, and\n     *               objects depending on the PHP version (\\BackedEnum, \\UnitEnum, etc.)","sourceCodeStart":1635,"sourceCodeEnd":1671,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L1635-L1671","documentation":"Thrown by CoreExtension::enum, which backs Twig's `enum` function for fetching a single enum case. When the given class string is not an existing enum (unknown class or non-enum class), it throws this RuntimeError. This is distinct from the follow-up 'is an empty enum' error thrown when the enum exists but has no cases.","triggerScenarios":"Calling `enum('App\\Color', 'Red')` in a Twig template with a wrong or non-autoloaded class name, passing a plain class or interface instead of an enum, or passing an instance/short name where the FQCN string is required.","commonSituations":"Typos in namespace; using an enum that was renamed or removed; referencing a const-class of a non-enum; tests rendering templates before the enum autoloads; supporting PHP < 8.1 where enum_exists is always false.","solutions":["Pass the exact fully-qualified enum class name and confirm with var_dump(enum_exists('App\\Color'))","Ensure composer autoloading is up to date (composer dump-autoload)","If you have an enum instance already, you do not need this function — access ->name/->value directly","Upgrade to PHP 8.1+ and convert the referenced type to a real enum if it currently is a plain class"],"exampleFix":"// before (Twig)\n{{ enum('Color', 'Red').value }}\n// after\n{{ enum('App\\\\Enum\\\\Color', 'Red').value }}","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 isUsableEnumClass(mixed $class): bool {\n    return \\is_string($class) && \\enum_exists($class) && \\count($class::cases()) > 0;\n}","tryCatchPattern":"try {\n    $case = $twig->render('t.html', ['enumClass' => $enumClass, 'case' => $caseName]);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'is not an enum') || str_contains($e->getMessage(), 'is an empty enum')) {\n        // correct the class string or choose a default enum\n    } else { throw $e; }\n}","preventionTips":["Pass exact FQCN strings to the `enum` Twig function, never short names","Reference enum classes via shared PHP constants to avoid string drift after renames","Ensure PHP >= 8.1 and composer autoload are correct","Remember the sibling check: enums with zero cases throw 'is an empty enum' — guard with count($enum::cases()) > 0"],"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"}