From: Charles Date: Thu, 8 Nov 2012 02:16:19 +0000 (+0700) Subject: Use ternary operator with PHP 5.4 when strict_variables is true X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=f397a8f79b732da9901edbffaa233c3e5b6680a8;p=web%2Fkonrad%2Ftwig.git Use ternary operator with PHP 5.4 when strict_variables is true --- diff --git a/lib/Twig/Node/Expression/Name.php b/lib/Twig/Node/Expression/Name.php index 2e5321d..9d581b0 100644 --- a/lib/Twig/Node/Expression/Name.php +++ b/lib/Twig/Node/Expression/Name.php @@ -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, ') diff --git a/test/Twig/Tests/Node/Expression/NameTest.php b/test/Twig/Tests/Node/Expression/NameTest.php index 945bd9f..76d109b 100644 --- a/test/Twig/Tests/Node/Expression/NameTest.php +++ b/test/Twig/Tests/Node/Expression/NameTest.php @@ -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'),