Browse code

changed context access to use the PHP 7 null coalescing operator when available

Fabien Potencier authored on 13/11/2016 03:26:22
Showing 4 changed files
... ...
@@ -1,5 +1,6 @@
1 1
 * 1.28.0 (2016-XX-XX)
2 2
 
3
+ * changed context access to use the PHP 7 null coalescing operator when available
3 4
  * added the "with" tag
4 5
  * added support for a custom template on the block() function
5 6
  * added "is defined" support for block() and constant()
... ...
@@ -43,10 +43,20 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression
43 43
                 ->raw(']')
44 44
             ;
45 45
         } else {
46
-            // remove the non-PHP 5.4 version when PHP 5.3 support is dropped
47
-            // as the non-optimized version is just a workaround for slow ternary operator
48
-            // when the context has a lot of variables
49
-            if (PHP_VERSION_ID >= 50400) {
46
+            if (PHP_VERSION_ID >= 70000) {
47
+                // use PHP 7 null coalescing operator
48
+                $compiler
49
+                    ->raw('($context[')
50
+                    ->string($name)
51
+                    ->raw('] ?? ')
52
+                ;
53
+
54
+                if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
55
+                    $compiler->raw('null)');
56
+                } else {
57
+                    $compiler->raw('$this->getContext($context, ')->string($name)->raw('))');
58
+                }
59
+            } elseif (PHP_VERSION_ID >= 50400) {
50 60
                 // PHP 5.4 ternary operator performance was optimized
51 61
                 $compiler
52 62
                     ->raw('(isset($context[')
... ...
@@ -46,6 +46,10 @@ abstract class Twig_Test_NodeTestCase extends PHPUnit_Framework_TestCase
46 46
     {
47 47
         $line = $line > 0 ? "// line {$line}\n" : '';
48 48
 
49
+        if (PHP_VERSION_ID >= 70000) {
50
+            return sprintf('%s($context["%s"] ?? null)', $line, $name, $name);
51
+        }
52
+
49 53
         if (PHP_VERSION_ID >= 50400) {
50 54
             return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name);
51 55
         }
... ...
@@ -26,8 +26,16 @@ class Twig_Tests_Node_Expression_NameTest extends Twig_Test_NodeTestCase
26 26
         $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('strict_variables' => true));
27 27
         $env1 = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('strict_variables' => false));
28 28
 
29
+        if (PHP_VERSION_ID >= 70000) {
30
+            $output = '($context["foo"] ?? $this->getContext($context, "foo"))';
31
+        } elseif (PHP_VERSION_ID >= 50400) {
32
+            $output = '(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))';
33
+        } else {
34
+            $output = '$this->getContext($context, "foo")';
35
+        }
36
+
29 37
         return array(
30
-            array($node, "// line 1\n".(PHP_VERSION_ID >= 50400 ? '(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))' : '$this->getContext($context, "foo")'), $env),
38
+            array($node, "// line 1\n".$output, $env),
31 39
             array($node, $this->getVariableGetter('foo', 1), $env1),
32 40
             array($context, "// line 1\n\$context"),
33 41
         );