twigphp/Twig · error · SyntaxError

The first argument of the "enum" function must be a string.

Error message

The first argument of the "enum" function must be a string.

What it means

The enum() Twig function returns the first case of an enum. At compile time, its first constant argument must be a string; if the constant node's value is not a string (number, boolean, etc.), compilation fails with this SyntaxError.

Solutions

  1. Pass the enum FQCN as a quoted string, e.g. {{ enum('App\\Status') }}
  2. If you already have an enum instance, you don't need the enum() function — use it directly
  3. Quote the value: '123' not 123

Example fix

// before (template)
{{ enum(42) }}
// after
{{ enum('App\\Status') }}
Defensive patterns

Strategy: type-guard

Validate before calling

<?php
$value = 42;
if (!is_string($value)) throw new \InvalidArgumentException('enum() first arg must be a string');

Type guard

function isEnumClassString(mixed $v): bool { return \is_string($v) && \enum_exists($v); }

Try / catch

try {
    echo $twig->render($template, $ctx);
} catch (\Twig\Error\SyntaxError $e) {
    if (str_contains($e->getMessage(), '"enum" function must be a string')) { /* quote the arg */ }
}

Prevention

When it happens

Trigger: Writing {{ enum(42) }} or {{ enum(false) }} — the first argument is a constant expression that is not a string literal.

Common situations: Passing a numeric or boolean literal instead of the quoted enum class name; misunderstanding that enum() takes an enum class name string, not an enum instance.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

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

        $arguments = $this->getNode('arguments');
        if ($arguments->hasNode('enum')) {
            $firstArgument = $arguments->getNode('enum');
        } elseif ($arguments->hasNode('0')) {
            $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)