mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 12:26:30 +00:00
optimized nested array calls
This commit is contained in:
@@ -29,13 +29,16 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression
|
|||||||
&& !$this->getAttribute('is_defined_test')
|
&& !$this->getAttribute('is_defined_test')
|
||||||
&& Twig_Template::ARRAY_CALL === $this->getAttribute('type')
|
&& Twig_Template::ARRAY_CALL === $this->getAttribute('type')
|
||||||
) {
|
) {
|
||||||
|
$var = '$'.$compiler->getVarName();
|
||||||
$compiler
|
$compiler
|
||||||
->raw('(is_array(')
|
->raw('(('.$var.' = ')
|
||||||
->subcompile($this->getNode('node'))
|
->subcompile($this->getNode('node'))
|
||||||
|
->raw(') && is_array(')
|
||||||
|
->raw($var)
|
||||||
->raw(') || ')
|
->raw(') || ')
|
||||||
->subcompile($this->getNode('node'))
|
->raw($var)
|
||||||
->raw(' instanceof ArrayAccess ? (')
|
->raw(' instanceof ArrayAccess ? (')
|
||||||
->subcompile($this->getNode('node'))
|
->raw($var)
|
||||||
->raw('[')
|
->raw('[')
|
||||||
->subcompile($this->getNode('attribute'))
|
->subcompile($this->getNode('attribute'))
|
||||||
->raw('] ?? null) : null)')
|
->raw('] ?? null) : null)')
|
||||||
|
|||||||
@@ -37,7 +37,8 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Test_NodeTestCase
|
|||||||
$tests[] = array($node, sprintf('%s%s, "bar", array())', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1)));
|
$tests[] = array($node, sprintf('%s%s, "bar", array())', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1)));
|
||||||
|
|
||||||
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Template::ARRAY_CALL, 1);
|
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Template::ARRAY_CALL, 1);
|
||||||
$tests[] = array($node, sprintf('(is_array(%s) || ($context["foo"] ?? null) instanceof ArrayAccess ? (($context["foo"] ?? null)["bar"] ?? null) : null)', $this->getVariableGetter('foo', 1), $this->getVariableGetter('foo', 1)));
|
$tests[] = array($node, '(($__internal_%s = // line 1'."\n".
|
||||||
|
'($context["foo"] ?? null)) && is_array($__internal_%s) || $__internal_%s instanceof ArrayAccess ? ($__internal_%s["bar"] ?? null) : null)', null, true);
|
||||||
|
|
||||||
$args = new Twig_Node_Expression_Array(array(), 1);
|
$args = new Twig_Node_Expression_Array(array(), 1);
|
||||||
$args->addElement(new Twig_Node_Expression_Name('foo', 1));
|
$args->addElement(new Twig_Node_Expression_Name('foo', 1));
|
||||||
|
|||||||
Reference in New Issue
Block a user