diff --git a/src/Node/Expression/Binary/ObjectDestructuringSetBinary.php b/src/Node/Expression/Binary/ObjectDestructuringSetBinary.php index 1ce150a52..217266dc8 100644 --- a/src/Node/Expression/Binary/ObjectDestructuringSetBinary.php +++ b/src/Node/Expression/Binary/ObjectDestructuringSetBinary.php @@ -54,7 +54,7 @@ class ObjectDestructuringSetBinary extends AbstractBinary { $compiler->addDebugInfo($this); $var = '$'.$compiler->getVarName(); - $compiler->raw('['); + $compiler->raw('[['); foreach ($this->mappings as $i => $mapping) { if ($i) { $compiler->raw(', '); @@ -74,7 +74,7 @@ class ObjectDestructuringSetBinary extends AbstractBinary } $compiler->raw(', ')->repr($mapping['property'])->raw(', [], \\Twig\\Template::ANY_CALL, false, false, ')->repr($compiler->getEnvironment()->hasExtension(SandboxExtension::class))->raw(', ')->repr($this->getNode('right')->getTemplateLine())->raw(')'); } - $compiler->raw(']'); + $compiler->raw('], '.$var.' = null][0]'); } public function operator(Compiler $compiler): Compiler diff --git a/src/Node/Expression/Binary/SequenceDestructuringSetBinary.php b/src/Node/Expression/Binary/SequenceDestructuringSetBinary.php index 326af2bc1..b3f8179f9 100644 --- a/src/Node/Expression/Binary/SequenceDestructuringSetBinary.php +++ b/src/Node/Expression/Binary/SequenceDestructuringSetBinary.php @@ -50,7 +50,7 @@ class SequenceDestructuringSetBinary extends AbstractBinary $compiler->addDebugInfo($this); $var = '$'.$compiler->getVarName(); $compiler - ->raw('(('.$var.' = ') + ->raw('[(('.$var.' = ') ->subcompile($this->getNode('right')) ->raw(') instanceof \Traversable ? CoreExtension::destructureSequence($context, ') ->repr($this->variables) @@ -67,7 +67,7 @@ class SequenceDestructuringSetBinary extends AbstractBinary $compiler ->raw('] = array_pad('.$var.', ') ->repr(\count($this->variables)) - ->raw(', null)))') + ->raw(', null))), '.$var.' = null][0]') ; } diff --git a/tests/Node/Expression/Binary/ObjectDestructuringSetTest.php b/tests/Node/Expression/Binary/ObjectDestructuringSetTest.php new file mode 100644 index 000000000..6cf3b9441 --- /dev/null +++ b/tests/Node/Expression/Binary/ObjectDestructuringSetTest.php @@ -0,0 +1,38 @@ +addElement(new AssignContextVariable('name', 1), new ConstantExpression('name', 1)); + $left->addElement(new AssignContextVariable('address', 1), new ConstantExpression('email', 1)); + $node = new ObjectDestructuringSetBinary($left, new ContextVariable('user', 1), 1); + + return [ + [$node, <<<'EOF' +// line 1 +[[$context["name"], $context["address"]] = [CoreExtension::getAttribute($this->env, $this->source, ($_v0 = ($context["user"] ?? null)), "name", [], \Twig\Template::ANY_CALL, false, false, false, 1), CoreExtension::getAttribute($this->env, $this->source, $_v0, "email", [], \Twig\Template::ANY_CALL, false, false, false, 1)], $_v0 = null][0] +EOF + ], + ]; + } +} diff --git a/tests/Node/Expression/Binary/SequenceDestructuringSetTest.php b/tests/Node/Expression/Binary/SequenceDestructuringSetTest.php new file mode 100644 index 000000000..cfc74c289 --- /dev/null +++ b/tests/Node/Expression/Binary/SequenceDestructuringSetTest.php @@ -0,0 +1,39 @@ +addElement(new AssignContextVariable('first', 1)); + $left->addElement(new EmptyExpression(1)); + $left->addElement(new AssignContextVariable('third', 1)); + $node = new SequenceDestructuringSetBinary($left, new ContextVariable('values', 1), 1); + + return [ + [$node, <<<'EOF' +// line 1 +[(($_v0 = ($context["values"] ?? null)) instanceof \Traversable ? CoreExtension::destructureSequence($context, [0 => "first", 1 => null, 2 => "third"], $_v0) : ([$context["first"], , $context["third"]] = array_pad($_v0, 3, null))), $_v0 = null][0] +EOF + ], + ]; + } +}