{"record":{"id":"5f00b65b00365f15","repo":"twigphp/Twig","slug":"s-is-an-empty-enum","errorCode":null,"errorMessage":"\"%s\" is an empty enum.","messagePattern":"\"(.+?)\" is an empty enum\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":1657,"sourceCode":"    /**\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.)\n     *               When $checkDefined is true, returns true when the constant is defined, false otherwise\n     *\n     * @internal\n     */","sourceCodeStart":1639,"sourceCodeEnd":1675,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L1639-L1675","documentation":"Twig's internal CoreExtension::enum() resolves an enum class name (used by the `enum` function in templates) and returns its first case. The library throws this RuntimeError when the given class IS a valid enum but declares no cases (`enum Foo {}` with zero cases), so there is no first case to return. Since an empty enum has no representable value, Twig refuses to return anything rather than returning null silently.","triggerScenarios":"Calling `enum('App\\\\Status')` (or `enum` with a variable class name) in a template where App\\\\Status is declared as an enum with zero cases. Also occurs with the `enum` function applied to a class string built dynamically, e.g. `enum('App\\\\Enums\\\\' ~ name)`, when that specific enum class is empty.","commonSituations":"Developers define an enum as a placeholder/skeleton before adding cases (e.g. generated code stubs), or a class-name map/config points at an enum that was emptied during a refactor while templates still reference it via the `enum` function.","solutions":["Add at least one case to the enum class referenced in the template","Verify the class name passed to the `enum` function points at the intended enum (not a placeholder)","If the enum can legitimately be empty, guard the template with `enum(...)` usage only after checking the enum has cases, or use `constant('App\\\\Status::class')` style access to a specific case instead"],"exampleFix":"// before (App\\Status.php)\nenum Status {}\n// after\nenum Status { case Draft; case Published; }","handlingStrategy":"validation","validationCode":"// PHP, before rendering or before calling the enum function\nif (enum_exists($class) && [] !== $class::cases()) {\n    // safe: enum has at least one case\n}","typeGuard":"function hasEnumCases(string $class): bool\n{\n    return enum_exists($class) && [] !== $class::cases();\n}","tryCatchPattern":"try {\n    $case = \\Twig\\Extension\\CoreExtension::enum($class);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    // empty enum: fall back to null/default case\n    $case = null;\n}","preventionTips":["Never declare enums with zero cases; add a placeholder case if needed temporarily","Add a static analysis/custom rule flagging case-less enums","Unit-test template helpers that resolve enums by class name","Keep class-name-to-enum maps in sync with the enum definitions"],"tags":["php","enum","twig","runtime"],"backgroundTag":"empty-result-set","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"}