ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
NullCoalesce.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
12 {
13  public function __construct(Twig_NodeInterface $left, Twig_NodeInterface $right, $lineno)
14  {
16  new Twig_Node_Expression_Test_Defined(clone $left, 'defined', new Twig_Node(), $left->getTemplateLine()),
18  $left->getTemplateLine()
19  );
20 
21  parent::__construct($test, $left, $right, $lineno);
22  }
23 
24  public function compile(Twig_Compiler $compiler)
25  {
26  /*
27  * This optimizes only one case. PHP 7 also supports more complex expressions
28  * that can return null. So, for instance, if log is defined, log("foo") ?? "..." works,
29  * but log($a["foo"]) ?? "..." does not if $a["foo"] is not defined. More advanced
30  * cases might be implemented as an optimizer node visitor, but has not been done
31  * as benefits are probably not worth the added complexity.
32  */
33  if (PHP_VERSION_ID >= 70000 && $this->getNode('expr2') instanceof Twig_Node_Expression_Name) {
34  $this->getNode('expr2')->setAttribute('always_defined', true);
35  $compiler
36  ->raw('((')
37  ->subcompile($this->getNode('expr2'))
38  ->raw(') ?? (')
39  ->subcompile($this->getNode('expr3'))
40  ->raw('))')
41  ;
42  } else {
43  parent::compile($compiler);
44  }
45  }
46 }
47 
48 class_alias('Twig_Node_Expression_NullCoalesce', 'Twig\Node\Expression\NullCoalesceExpression', false);
$lineno
Definition: Node.php:22
raw($string)
Adds a raw string to the compiled code.
Definition: Compiler.php:112
Represents a node in the AST.
Represents a node in the AST.
Definition: Node.php:18
Checks that a variable is null.
Definition: Null.php:21
getTemplateLine()
Definition: Node.php:121
getNode($name)
Definition: Node.php:186
Checks if a variable is defined in the current context.
Definition: Defined.php:24
compile(Twig_Compiler $compiler)
Compiles the node to PHP.
$test
Definition: Utf8Test.php:84
__construct(Twig_NodeInterface $left, Twig_NodeInterface $right, $lineno)