twigphp/Twig · error · SyntaxError

The first argument of the "enum" function must be the name…

Error message

The first argument of the "enum" function must be the name of an enum, "%s" given.

What it means

After confirming the first argument of enum() is a string naming an existing enum (via enum_exists), Twig fetches $value::cases(). If the enum has zero cases, the function cannot return a 'first case', so compilation fails with this SyntaxError naming the enum.

Solutions

  1. Add at least one case to the enum class
  2. Use a different enum that has cases
  3. If an empty enum is intentional, do not use the enum() function with it; reference cases explicitly
  4. Check the enum source file for missing case declarations

Example fix

// before (PHP)
enum Status {}
// after
enum Status { case Active; case Inactive; }
Defensive patterns

Strategy: validation

Validate before calling

<?php
$name = 'App\\EmptyEnum';
if (\enum_exists($name) && !call_user_func($name.'::cases')) {
    throw new \LogicException("$name has no cases; cannot use enum()");
}

Type guard

function isNonEmptyEnum(string $name): bool { return \enum_exists($name) && \count(call_user_func($name.'::cases')) > 0; }

Try / catch

try {
    echo $twig->render($template, $ctx);
} catch (\Twig\Error\SyntaxError $e) {
    if (str_contains($e->getMessage(), 'non-empty enum')) { /* add cases or pick another enum */ }
}

Prevention

When it happens

Trigger: Using {{ enum('App\\EmptyEnum') }} where EmptyEnum is declared as enum with no case statements. Pure enums or backed enums with no cases trigger this at compile time.

Common situations: Defining placeholder enums with no cases during development; code generation producing empty enums; importing enums from packages that declare empty enums for typing purposes.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Node/Expression/FunctionNode/EnumFunction.php:45

            $firstArgument = $arguments->getNode('0');
        } else {
            $firstArgument = null;
        }

        if (!$firstArgument instanceof ConstantExpression || 1 !== \count($arguments)) {
            parent::compile($compiler);

            return;
        }

        $value = $firstArgument->getAttribute('value');

        if (!\is_string($value)) {
            throw new SyntaxError('The first argument of the "enum" function must be a string.', $this->getTemplateLine(), $this->getSourceContext());
        }

        if (!enum_exists($value)) {
            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());
        }

        if (!$cases = $value::cases()) {
            throw new SyntaxError(\sprintf('The first argument of the "enum" function must be a non-empty enum, "%s" given.', $value), $this->getTemplateLine(), $this->getSourceContext());
        }

        $compiler->raw(\sprintf('%s::%s', $value, $cases[0]->name));
    }
}

View on GitHub (pinned to a414c3a491)