{"record":{"id":"43dfcfc2888c2301","repo":"twigphp/Twig","slug":"cannot-assign-to-s-only-variables-can-be-assigned-in","errorCode":null,"errorMessage":"Cannot assign to \"%s\", only variables can be assigned in sequence destructuring.","messagePattern":"Cannot assign to \"(.+?)\", only variables can be assigned in sequence destructuring\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/Node/Expression/Binary/SequenceDestructuringSetBinary.php","lineNumber":41,"sourceCode":" * @internal\n */\nclass SequenceDestructuringSetBinary extends AbstractBinary\n{\n    private array $variables = [];\n\n    /**\n     * @param ArrayExpression    $left  The array expression containing variables to assign to\n     * @param AbstractExpression $right The expression providing values for assignment\n     */\n    public function __construct(Node $left, Node $right, int $lineno)\n    {\n        foreach ($left->getKeyValuePairs() as $pair) {\n            if ($pair['value'] instanceof EmptyExpression) {\n                $this->variables[] = null;\n            } elseif ($pair['value'] instanceof AssignContextVariable) {\n                $this->variables[] = $pair['value']->getAttribute('name');\n            } else {\n                throw new SyntaxError(\\sprintf('Cannot assign to \"%s\", only variables can be assigned in sequence destructuring.', $pair['value']::class), $lineno);\n            }\n        }\n\n        parent::__construct($left, $right, $lineno);\n    }\n\n    public function compile(Compiler $compiler): void\n    {\n        $compiler->addDebugInfo($this);\n        $var = '$'.$compiler->getVarName();\n        $compiler\n            ->raw('[(('.$var.' = ')\n            ->subcompile($this->getNode('right'))\n            ->raw(') instanceof \\Traversable ? CoreExtension::destructureSequence($context, ')\n            ->repr($this->variables)\n            ->raw(', '.$var.') : ([')\n        ;\n        foreach ($this->variables as $i => $name) {","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Node/Expression/Binary/SequenceDestructuringSetBinary.php#L23-L59","documentation":"Twig's sequence destructuring (`[a, b] = value`) only accepts variables (or EmptyExpression placeholders for skipped elements) on the left side. The constructor throws this SyntaxError when any element of the sequence is a non-variable expression such as a constant or function call.","triggerScenarios":"Compiling `{ ['literal', b] = arr }` or `{ [first(), b] = arr }`; direct PHP construction of SequenceDestructuringSetBinary whose left ArrayExpression contains a pair value that is neither EmptyExpression nor AssignContextVariable. The message contains the offending node's class name.","commonSituations":"Trying to destructure into array offsets or function results out of habit from other languages; templates generated by code that didn't normalize targets to variables; typos that turn a variable name into an expression.","solutions":["Replace the non-variable element with a plain variable name, or an empty placeholder (`[_, b]`-style skip via EmptyExpression) if the value is unused.","Destructure into variables first, then index the original value for any computed targets.","In PHP-generated templates, emit AssignContextVariable (or EmptyExpression) for every sequence element."],"exampleFix":"// before (template)\n{% set [config['key'], b] = pair %}\n// after\n{% set [cfg, b] = pair %}\n{% set keyValue = cfg['key'] %}","handlingStrategy":"validation","validationCode":"// Every element of a Twig sequence destructuring pattern must be a bare variable or a skip placeholder.\n// When generating nodes in PHP:\nforeach ($pairs as $p) {\n    $ok = $p['value'] instanceof \\Twig\\Node\\Expression\\AssignContextVariable\n        || $p['value'] instanceof \\Twig\\Node\\Expression\\EmptyExpression;\n    if (!$ok) {\n        throw new \\InvalidArgumentException('Sequence destructuring target must be a variable or empty');\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use bare variable names for each element you want to capture; use the skip placeholder for ignored elements.","Never put array offsets, property accesses, or function calls inside a destructuring pattern.","Compute derived values in separate statements after destructuring into plain variables."],"tags":["twig","destructuring","syntax-error","template-compilation"],"backgroundTag":"unsupported-operation","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"}