{"record":{"id":"9b183aa7bbd016d9","repo":"twigphp/Twig","slug":"the-arrow-function-argument-must-be-a-list-of-variables-or-a","errorCode":null,"errorMessage":"The arrow function argument must be a list of variables or a single variable.","messagePattern":"The arrow function argument must be a list of variables or a single variable\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/Node/Expression/ArrowFunctionExpression.php","lineNumber":34,"sourceCode":"use Twig\\Node\\Expression\\Variable\\AssignContextVariable;\nuse Twig\\Node\\Expression\\Variable\\ContextVariable;\nuse Twig\\Node\\Node;\n\n/**\n * Represents an arrow function.\n *\n * @author Fabien Potencier <fabien@symfony.com>\n */\nclass ArrowFunctionExpression extends AbstractExpression\n{\n    public function __construct(AbstractExpression $expr, Node $names, $lineno)\n    {\n        if ($names instanceof ContextVariable) {\n            $names = new ListExpression([new AssignContextVariable($names->getAttribute('name'), $names->getTemplateLine())], $lineno);\n        }\n\n        if (!$names instanceof ListExpression) {\n            throw new SyntaxError('The arrow function argument must be a list of variables or a single variable.', $names->getTemplateLine(), $names->getSourceContext());\n        }\n\n        parent::__construct(['expr' => $expr, 'names' => $names], [], $lineno);\n    }\n\n    public function compile(Compiler $compiler): void\n    {\n        $compiler\n            ->addDebugInfo($this)\n            ->raw('function (')\n            ->subcompile($this->getNode('names'))\n            ->raw(') use ($context, $macros) { ')\n        ;\n        foreach ($this->getNode('names') as $name) {\n            $compiler\n                ->raw('$context[\"')\n                ->raw($name->getAttribute('name'))\n                ->raw('\"] = $__')","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Node/Expression/ArrowFunctionExpression.php#L16-L52","documentation":"ArrowFunctionExpression's constructor requires its parameter list to be a ListExpression of AssignContextVariable nodes (or a single ContextVariable, which it wraps automatically). Anything else — a constant, expression, or malformed node — throws this SyntaxError, because arrow function arguments must be plain variables to bind.","triggerScenarios":"In a template, writing an arrow function whose left side of => is not a variable or variable list, e.g. map(1 => v) or map('x' => v); multi-argument callbacks without parentheses (a, b => ...); programmatically constructing ArrowFunctionExpression with a non-list/non-variable $names node.","commonSituations":"Typos in Twig arrow-function callbacks passed to filters like sort/filter/map/reduce; forgotten parentheses around multiple parameters; PHP extensions building arrow function nodes with wrong argument nodes.","solutions":["Ensure the left side of => is a variable or a parenthesized list of variables: sort((a, b) => a - b), filter(v => v > 1).","Wrap multiple arguments in parentheses: (a, b) => ..., not a, b => ....","Remove any literal or expression used as an arrow-function parameter; only variable names are allowed.","If constructing ArrowFunctionExpression in PHP, pass a ListExpression of AssignContextVariable (or a ContextVariable) as $names."],"exampleFix":"// before (Twig template)\n{{ numbers|sort(1 => a) }} {# SyntaxError #}\n// after\n{{ numbers|sort((a, b) => a <=> b) }}","handlingStrategy":"validation","validationCode":"// validate arrow-function callbacks in template source before render\nif (preg_match('/\\d+\\s*=>|[\"\\']\\s*=>/', $templateSource)) {\n    throw new InvalidArgumentException('Arrow function argument must be variable(s).');\n}","typeGuard":"function isValidArrowFnArgs($names): bool {\n    return $names instanceof \\Twig\\Node\\Expression\\ListExpression\n        || $names instanceof \\Twig\\Node\\Expression\\ContextVariable;\n}","tryCatchPattern":"try {\n    $twig->parse($twig->tokenize(new \\Twig\\Source($code, 'tpl')));\n} catch (\\Twig\\Error\\SyntaxError $e) {\n    if (str_contains($e->getMessage(), 'arrow function argument')) {\n        // fix the callback to use variable(s) left of =>\n    }\n    throw $e;\n}","preventionTips":["Only use variable(s) left of => in Twig arrow functions: v => ..., (a, b) => ....","Parenthesize multiple arrow-function parameters.","Lint templates in CI to catch syntax errors early.","When building arrow nodes in PHP, pass a ListExpression of AssignContextVariable."],"tags":["template","syntax","arrow-function","compile-time"],"backgroundTag":"invalid-argument-format","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}