function NullCoalesceExpression::compile
Overrides ConditionalExpression::compile
File
-
vendor/
twig/ twig/ src/ Node/ Expression/ NullCoalesceExpression.php, line 51
Class
Namespace
Twig\Node\ExpressionCode
public function compile(Compiler $compiler) : void {
/*
* This optimizes only one case. PHP 7 also supports more complex expressions
* that can return null. So, for instance, if log is defined, log("foo") ?? "..." works,
* but log($a["foo"]) ?? "..." does not if $a["foo"] is not defined. More advanced
* cases might be implemented as an optimizer node visitor, but has not been done
* as benefits are probably not worth the added complexity.
*/
if ($this->getNode('expr2') instanceof NameExpression) {
$this->getNode('expr2')
->setAttribute('always_defined', true);
$compiler->raw('((')
->subcompile($this->getNode('expr2'))
->raw(') ?? (')
->subcompile($this->getNode('expr3'))
->raw('))');
}
else {
parent::compile($compiler);
}
}