fixed regression

This commit is contained in:
Fabien Potencier
2018-04-02 10:59:29 +02:00
parent 8de61cef41
commit 0c2cdf2f46
5 changed files with 28 additions and 4 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
* 2.4.8 (2018-XX-XX)
* n/a
* fixed a regression when using the "default" filter or the "defined" test on non-existing arrays
* 2.4.7 (2018-03-20)
+3 -2
View File
@@ -18,7 +18,7 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression
$nodes['arguments'] = $arguments;
}
parent::__construct($nodes, array('type' => $type, 'is_defined_test' => false, 'ignore_strict_check' => false), $lineno);
parent::__construct($nodes, array('type' => $type, 'is_defined_test' => false, 'ignore_strict_check' => false, 'optimizable' => true), $lineno);
}
public function compile(Twig_Compiler $compiler)
@@ -27,7 +27,8 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression
// optimize array calls
if (
(!$env->isStrictVariables() || $this->getAttribute('ignore_strict_check'))
$this->getAttribute('optimizable')
&& (!$env->isStrictVariables() || $this->getAttribute('ignore_strict_check'))
&& !$this->getAttribute('is_defined_test')
&& Twig_Template::ARRAY_CALL === $this->getAttribute('type')
) {
@@ -45,6 +45,7 @@ class Twig_Node_Expression_Test_Defined extends Twig_Node_Expression_Test
private function changeIgnoreStrictCheck(Twig_Node_Expression_GetAttr $node)
{
$node->setAttribute('optimizable', false);
$node->setAttribute('ignore_strict_check', true);
if ($node->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
@@ -40,8 +40,13 @@ Twig supports array notation
{# ArrayAccess #}
{{ array_access['a'] }}
{# array that does not exist #}
{{ does_not_exist[0]|default('ok') }}
{{ does_not_exist[0].does_not_exist_either|default('ok') }}
{{ does_not_exist[0]['does_not_exist_either']|default('ok') }}
--DATA--
return array('bar' => 'bar', 'foo' => array('bar' => 'bar'), 'array_access' => new ArrayObject(array('a' => 'b')))
return array('bar' => 'bar', 'foo' => array('bar' => 'bar'), 'array_access' => new ArrayObject(array('a' => 'b')))
--EXPECT--
1,2
foo,bar
@@ -64,6 +69,10 @@ FOO,BAR,
a,b,c,d
b
ok
ok
ok
--DATA--
return array('bar' => 'bar', 'foo' => array('bar' => 'bar'), 'array_access' => new ArrayObject(array('a' => 'b')))
--CONFIG--
@@ -90,3 +99,7 @@ FOO,BAR,
a,b,c,d
b
ok
ok
ok
@@ -15,6 +15,7 @@ Array access:
{{ nested.nullVar |default('default') is same as('default') ? 'ok' : 'ko' }}
{{ nested.undefinedVar |default('default') is same as('default') ? 'ok' : 'ko' }}
{{ nested['undefinedVar'] |default('default') is same as('default') ? 'ok' : 'ko' }}
{{ undefined['undefined'] |default('default') is same as('default') ? 'ok' : 'ko' }}
{{ undefinedVar.foo |default('default') is same as('default') ? 'ok' : 'ko' }}
Plain values:
{{ 'defined' |default('default') is same as('default') ? 'ko' : 'ok' }}
@@ -35,6 +36,8 @@ Deep nested:
{{ nested.undefinedVar.foo.bar |default('default') is same as('default') ? 'ok' : 'ko' }}
{{ nested.definedArray.0 |default('default') is same as('default') ? 'ko' : 'ok' }}
{{ nested['definedArray'][0] |default('default') is same as('default') ? 'ko' : 'ok' }}
{{ nested['undefinedVar'][0] |default('default') is same as('default') ? 'ok' : 'ko' }}
{{ undefined['undefined'][0] |default('default') is same as('default') ? 'ok' : 'ko' }}
{{ object.self.foo |default('default') is same as('default') ? 'ko' : 'ok' }}
{{ object.self.undefinedMethod |default('default') is same as('default') ? 'ok' : 'ko' }}
{{ object.undefinedMethod.self |default('default') is same as('default') ? 'ok' : 'ko' }}
@@ -71,6 +74,7 @@ ok
ok
ok
ok
ok
Plain values:
ok
ok
@@ -93,6 +97,8 @@ ok
ok
ok
ok
ok
ok
--DATA--
return array(
'definedVar' => 'defined',
@@ -126,6 +132,7 @@ ok
ok
ok
ok
ok
Plain values:
ok
ok
@@ -148,3 +155,5 @@ ok
ok
ok
ok
ok
ok