twigphp/Twig · error · RuntimeError

" " is an empty enum.

Error message

"%s" is an empty enum.

What it means

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.

Solutions

  1. Add at least one case to the enum class referenced in the template
  2. Verify the class name passed to the `enum` function points at the intended enum (not a placeholder)
  3. 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

Example fix

// before (App\Status.php)
enum Status {}
// after
enum Status { case Draft; case Published; }
Defensive patterns

Strategy: validation

Validate before calling

// PHP, before rendering or before calling the enum function
if (enum_exists($class) && [] !== $class::cases()) {
    // safe: enum has at least one case
}

Type guard

function hasEnumCases(string $class): bool
{
    return enum_exists($class) && [] !== $class::cases();
}

Try / catch

try {
    $case = \Twig\Extension\CoreExtension::enum($class);
} catch (\Twig\Error\RuntimeError $e) {
    // empty enum: fall back to null/default case
    $case = null;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13). Data as JSON: /api/errors/5f00b65b00365f15. Report an issue: GitHub.

Appendix: source

Thrown at src/Extension/CoreExtension.php:1657

    /**
     * Provides the ability to access enums by their class names.
     *
     * @template T of \UnitEnum
     *
     * @param class-string<T> $enum
     *
     * @return T
     *
     * @internal
     */
    public static function enum(string $enum): \UnitEnum
    {
        if (!enum_exists($enum)) {
            throw new RuntimeError(\sprintf('"%s" is not an enum.', $enum));
        }

        if (!$cases = $enum::cases()) {
            throw new RuntimeError(\sprintf('"%s" is an empty enum.', $enum));
        }

        return $cases[0];
    }

    /**
     * Provides the ability to get constants from instances as well as class/global constants.
     *
     * @param string      $constant     The name of the constant
     * @param object|null $object       The object to get the constant from
     * @param bool        $checkDefined Whether to check if the constant is defined or not
     *
     * @return mixed Class constants can return many types like scalars, arrays, and
     *               objects depending on the PHP version (\BackedEnum, \UnitEnum, etc.)
     *               When $checkDefined is true, returns true when the constant is defined, false otherwise
     *
     * @internal
     */

View on GitHub (pinned to a414c3a491)