Skip to content

Commit

Permalink
minor #2238 added support for PHP 7 null coalescing operator (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.x branch.

Discussion
----------

added support for PHP 7 null coalescing operator

fixes #1979

Commits
-------

6effc9a changed context access to use the PHP 7 null coalescing operator when available
  • Loading branch information
fabpot committed Nov 13, 2016
2 parents 4419ff3 + 6effc9a commit 827d8e7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,5 +1,6 @@
* 1.28.0 (2016-XX-XX)

* changed context access to use the PHP 7 null coalescing operator when available
* added the "with" tag
* added support for a custom template on the block() function
* added "is defined" support for block() and constant()
Expand Down
18 changes: 14 additions & 4 deletions lib/Twig/Node/Expression/Name.php
Expand Up @@ -43,10 +43,20 @@ public function compile(Twig_Compiler $compiler)
->raw(']')
;
} else {
// 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 (PHP_VERSION_ID >= 50400) {
if (PHP_VERSION_ID >= 70000) {
// use PHP 7 null coalescing operator
$compiler
->raw('($context[')
->string($name)
->raw('] ?? ')
;

if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
$compiler->raw('null)');
} else {
$compiler->raw('$this->getContext($context, ')->string($name)->raw('))');
}
} elseif (PHP_VERSION_ID >= 50400) {
// PHP 5.4 ternary operator performance was optimized
$compiler
->raw('(isset($context[')
Expand Down
4 changes: 4 additions & 0 deletions lib/Twig/Test/NodeTestCase.php
Expand Up @@ -46,6 +46,10 @@ protected function getVariableGetter($name, $line = false)
{
$line = $line > 0 ? "// line {$line}\n" : '';

if (PHP_VERSION_ID >= 70000) {
return sprintf('%s($context["%s"] ?? null)', $line, $name, $name);
}

if (PHP_VERSION_ID >= 50400) {
return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name);
}
Expand Down
10 changes: 9 additions & 1 deletion test/Twig/Tests/Node/Expression/NameTest.php
Expand Up @@ -26,8 +26,16 @@ public function getTests()
$env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('strict_variables' => true));
$env1 = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('strict_variables' => false));

if (PHP_VERSION_ID >= 70000) {
$output = '($context["foo"] ?? $this->getContext($context, "foo"))';
} elseif (PHP_VERSION_ID >= 50400) {
$output = '(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))';
} else {
$output = '$this->getContext($context, "foo")';
}

return array(
array($node, "// line 1\n".(PHP_VERSION_ID >= 50400 ? '(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))' : '$this->getContext($context, "foo")'), $env),
array($node, "// line 1\n".$output, $env),
array($node, $this->getVariableGetter('foo', 1), $env1),
array($context, "// line 1\n\$context"),
);
Expand Down

0 comments on commit 827d8e7

Please sign in to comment.