mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-17 04:46:49 +00:00
Release destructuring temporaries after assignment
This commit is contained in:
@@ -54,7 +54,7 @@ class ObjectDestructuringSetBinary extends AbstractBinary
|
|||||||
{
|
{
|
||||||
$compiler->addDebugInfo($this);
|
$compiler->addDebugInfo($this);
|
||||||
$var = '$'.$compiler->getVarName();
|
$var = '$'.$compiler->getVarName();
|
||||||
$compiler->raw('[');
|
$compiler->raw('[[');
|
||||||
foreach ($this->mappings as $i => $mapping) {
|
foreach ($this->mappings as $i => $mapping) {
|
||||||
if ($i) {
|
if ($i) {
|
||||||
$compiler->raw(', ');
|
$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(', ')->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
|
public function operator(Compiler $compiler): Compiler
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ class SequenceDestructuringSetBinary extends AbstractBinary
|
|||||||
$compiler->addDebugInfo($this);
|
$compiler->addDebugInfo($this);
|
||||||
$var = '$'.$compiler->getVarName();
|
$var = '$'.$compiler->getVarName();
|
||||||
$compiler
|
$compiler
|
||||||
->raw('(('.$var.' = ')
|
->raw('[(('.$var.' = ')
|
||||||
->subcompile($this->getNode('right'))
|
->subcompile($this->getNode('right'))
|
||||||
->raw(') instanceof \Traversable ? CoreExtension::destructureSequence($context, ')
|
->raw(') instanceof \Traversable ? CoreExtension::destructureSequence($context, ')
|
||||||
->repr($this->variables)
|
->repr($this->variables)
|
||||||
@@ -67,7 +67,7 @@ class SequenceDestructuringSetBinary extends AbstractBinary
|
|||||||
$compiler
|
$compiler
|
||||||
->raw('] = array_pad('.$var.', ')
|
->raw('] = array_pad('.$var.', ')
|
||||||
->repr(\count($this->variables))
|
->repr(\count($this->variables))
|
||||||
->raw(', null)))')
|
->raw(', null))), '.$var.' = null][0]')
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Twig.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Twig\Tests\Node\Expression\Binary;
|
||||||
|
|
||||||
|
use Twig\Node\Expression\ArrayExpression;
|
||||||
|
use Twig\Node\Expression\Binary\ObjectDestructuringSetBinary;
|
||||||
|
use Twig\Node\Expression\ConstantExpression;
|
||||||
|
use Twig\Node\Expression\Variable\AssignContextVariable;
|
||||||
|
use Twig\Node\Expression\Variable\ContextVariable;
|
||||||
|
use Twig\Test\NodeTestCase;
|
||||||
|
|
||||||
|
class ObjectDestructuringSetTest extends NodeTestCase
|
||||||
|
{
|
||||||
|
public static function provideTests(): iterable
|
||||||
|
{
|
||||||
|
$left = new ArrayExpression([], 1);
|
||||||
|
$left->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
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Twig.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Twig\Tests\Node\Expression\Binary;
|
||||||
|
|
||||||
|
use Twig\Node\Expression\ArrayExpression;
|
||||||
|
use Twig\Node\Expression\Binary\SequenceDestructuringSetBinary;
|
||||||
|
use Twig\Node\Expression\EmptyExpression;
|
||||||
|
use Twig\Node\Expression\Variable\AssignContextVariable;
|
||||||
|
use Twig\Node\Expression\Variable\ContextVariable;
|
||||||
|
use Twig\Test\NodeTestCase;
|
||||||
|
|
||||||
|
class SequenceDestructuringSetTest extends NodeTestCase
|
||||||
|
{
|
||||||
|
public static function provideTests(): iterable
|
||||||
|
{
|
||||||
|
$left = new ArrayExpression([], 1);
|
||||||
|
$left->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
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user