twigphp/Twig · error · SyntaxError

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

Error message

The first argument of the "enum" function must be a non-empty enum, "%s" given.

What it means

enum() compiles to a constant reference of the enum's first case. If $value::cases() returns an empty array the previous error fires; this error covers the (defensive) non-empty requirement surfaced when cases exist check fails, i.e. the enum passed has no cases and the function cannot produce a first-case constant.

Solutions

  1. Declare at least one case in the enum
  2. Target an enum known to have cases
  3. Avoid enum() for empty enums; use enum_cases() which handles empties (returns empty list)
  4. Review the enum definition in your codebase

Example fix

// before (PHP)
enum Level {}
// after
enum Level { case Low; case High; }
Defensive patterns

Strategy: validation

Validate before calling

<?php
foreach (['App\\Level'] as $e) {
    assert(\enum_exists($e) && \count(call_user_func($e.'::cases')) > 0);
}

Type guard

function hasCases(string $enumName): bool { return \enum_exists($enumName) && !empty(call_user_func($enumName.'::cases')); }

Try / catch

try {
    echo $twig->render($template, $ctx);
} catch (\Twig\Error\SyntaxError $e) {
    if (str_contains($e->getMessage(), 'non-empty enum')) { /* declare a case */ }
}

Prevention

When it happens

Trigger: Calling {{ enum('SomeEnum') }} where SomeEnum exists as an enum but declares no cases, making cases() return [].

Common situations: Empty enums used purely as type markers; scaffolding enums before cases are added; generated code omitting case statements.

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/f3e5f2d82f90a917. Report an issue: GitHub.

Appendix: source

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

        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)