Use ternary operator with PHP 5.4 when strict_variables is true
authorCharles <char101@ui.ac.id>
Thu, 8 Nov 2012 02:16:19 +0000 (09:16 +0700)
committerFabien Potencier <fabien.potencier@gmail.com>
Thu, 8 Nov 2012 06:21:48 +0000 (07:21 +0100)
lib/Twig/Node/Expression/Name.php
test/Twig/Tests/Node/Expression/NameTest.php

index 2e5321d..9d581b0 100644 (file)
@@ -44,15 +44,18 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression
             // remove the non-PHP 5.4 version when PHP 5.3 support is dropped
             // as the non-optimized version is just a workaround for slow ternary operator
             // when the context has a lot of variables
-            if (version_compare(phpversion(), '5.4.0RC1', '>=') && ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables())) {
+            if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
                 // PHP 5.4 ternary operator performance was optimized
                 $compiler
                     ->raw('(isset($context[')
                     ->string($name)
                     ->raw(']) ? $context[')
-                    ->string($name)
-                    ->raw('] : null)')
-                ;
+                    ->string($name);
+                               if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
+                    $compiler->raw('] : null)');
+                               } else {
+                    $compiler->raw('] : $this->getContext($context, ')->string($name)->raw('))');
+                               }
             } else {
                 $compiler
                     ->raw('$this->getContext($context, ')
index 945bd9f..76d109b 100644 (file)
@@ -40,7 +40,7 @@ class Twig_Tests_Node_Expression_NameTest extends Twig_Test_NodeTestCase
         $env1 = new Twig_Environment(null, array('strict_variables' => false));
 
         return array(
-            array($node, '$this->getContext($context, "foo")', $env),
+            version_compare(PHP_VERSION, '5.4.0') >= 0 ? array($node, '(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))', $env) : array($node, '$this->getContext($context, "foo")', $env),
             array($node, $this->getVariableGetter('foo'), $env1),
             array($self, '$this'),
             array($context, '$context'),