twigphp/Twig · error · SyntaxError
The last parameter of
Error message
The last parameter of "%s" for %s "%s" must be an array with default value, eg. "array $arg = []".
What it means
Twig's callable arguments extractor inspects the signature of a PHP function/method registered as a Twig function or filter via Reflection. When the Twig callable is declared as variadic, Twig expects the LAST reflected parameter to either be an array with an empty-array default ("array $arg = []") or a native PHP variadic (...$args). If neither holds, it throws this SyntaxError because Twig cannot map arbitrary extra template arguments onto the signature at compile time.
Solutions
- Change the last parameter of the callable to an array with an empty default: "array $arg = []".
- Alternatively use a native PHP variadic: "...$args" so the isVariadic() branch pops it.
- If the callable is not meant to accept extra arguments, remove the 'is_variadic' => true option from the TwigFunction/TwigFilter options.
Example fix
// before
TwigFunction('sum', function (int $a, array $rest = null) { ... }, ['is_variadic' => true]);
// after
TwigFunction('sum', function (int $a, array $rest = []) { ... }, ['is_variadic' => true]); Defensive patterns
Strategy: validation
Validate before calling
$r = new ReflectionFunction($callable);
$last = $r->getNumberOfParameters() ? $r->getParameters()[count($r->getParameters()) - 1] : null;
$ok = $last && ($last->isVariadic() || ($last->getType()?->getName() === 'array' && $last->isDefaultValueAvailable() && $last->getDefaultValue() === []));
if (!$ok) throw new LogicException('Callable is not a valid Twig variadic callable.'); Prevention
- Always end variadic Twig callables with "array $arg = []" or "...$args".
- Keep the default value exactly [] — non-empty defaults also fail.
- Add a unit test registering every TwigFunction/TwigFilter you define so registration failures surface early (errors are compile-time SyntaxErrors).
When it happens
Trigger: Registering a Twig function/filter whose callable has a trailing parameter that is neither "array $x = []" nor a native variadic, while the Twig callable is registered with is_variadic=true (or Twig infers variadic support), e.g. new TwigFunction('foo', 'foo', ['is_variadic' => true]) pointing at fn($a, string $b).
Common situations: Wrapping legacy PHP helpers as Twig filters with is_variadic enabled; typos like "array $arg = null" or a non-array typed last parameter; upgrading Twig after refactoring a callable signature and forgetting the variadic convention; passing a closure whose last param has a non-empty default like "array $opts = ['x' => 1]".
Related errors
- The variadic argument
- Callback for " " is not callable in the current scope.
- Unknown " " configuration.
- The "format_list" filter requires the "IntlListFormatter"…
- SpanishInflector is not available.
AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13).
Data as JSON: /api/errors/45a4afc1c3f4dd03.
Report an issue: GitHub.
Appendix: source
Thrown at src/Util/CallableArgumentsExtractor.php:198
{
return strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z0-9])([A-Z])/'], '\1_\2', $name));
}
private function getCallableParameters(): array
{
$parameters = $this->rc->getTwigParameters($this->node->hasNode('node'));
$isPhpVariadic = false;
if ($this->twigCallable->isVariadic()) {
$argument = end($parameters);
$isArray = $argument && $argument->hasType() && $argument->getType() instanceof \ReflectionNamedType && 'array' === $argument->getType()->getName();
if ($isArray && $argument->isDefaultValueAvailable() && [] === $argument->getDefaultValue()) {
array_pop($parameters);
} elseif ($argument && $argument->isVariadic()) {
array_pop($parameters);
$isPhpVariadic = true;
} else {
throw new SyntaxError(\sprintf('The last parameter of "%s" for %s "%s" must be an array with default value, eg. "array $arg = []".', $this->rc->getName(), $this->twigCallable->getType(), $this->twigCallable->getName()));
}
}
return [$parameters, $isPhpVariadic];
}
}
View on GitHub (pinned to a414c3a491)