Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ??? Empty Coalesce Operator #2787

Open
wants to merge 6 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/Twig/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public function getOperators()
'is not' => array('precedence' => 100, 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
'**' => array('precedence' => 200, 'class' => 'Twig_Node_Expression_Binary_Power', 'associativity' => Twig_ExpressionParser::OPERATOR_RIGHT),
'??' => array('precedence' => 300, 'class' => 'Twig_Node_Expression_NullCoalesce', 'associativity' => Twig_ExpressionParser::OPERATOR_RIGHT),
'???' => array('precedence' => 300, 'class' => 'Twig_Node_Expression_EmptyCoalesce', 'associativity' => Twig_ExpressionParser::OPERATOR_RIGHT),
),
);
}
Expand Down
44 changes: 44 additions & 0 deletions lib/Twig/Node/Expression/EmptyCoalesce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class Twig_Node_Expression_EmptyCoalesce extends \Twig_Node_Expression
{

public function __construct(Twig_Node $left, Twig_Node $right, $lineno)
{
$left->setAttribute('ignore_strict_check', true);
$left->setAttribute('is_defined_test', false);
$right->setAttribute('ignore_strict_check', true);
$right->setAttribute('is_defined_test', false);
parent::__construct(
['left' => $left, 'right' => $right],
['ignore_strict_check' => true, 'is_defined_test' => false],
$lineno
);
}

public function compile(\Twig_Compiler $compiler)
{
$compiler
->raw('((empty(')
khalwat marked this conversation as resolved.
Show resolved Hide resolved
->subcompile($this->getNode('left'))
->raw(') ? null : ')
->subcompile($this->getNode('left'))
->raw(') ?? (empty(')
->subcompile($this->getNode('right'))
->raw(') ? null : ')
->subcompile($this->getNode('right'))
->raw('))')
;
}
}

class_alias('Twig_Node_Expression_EmptyCoalesce', 'Twig\Node\Expression\EmptyCoalesceExpression', false);
11 changes: 11 additions & 0 deletions src/Node/Expression/EmptyCoalesceExpression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Twig\Node\Expression;

class_exists('Twig_Node_Expression_EmptyCoalesce');

if (\false) {
class EmptyCoalesceExpression extends \Twig_Node_Expression_EmptyCoalesce
{
}
}
22 changes: 22 additions & 0 deletions test/Twig/Tests/Node/Expression/EmptyCoalesceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class Twig_Tests_Node_Expression_EmptyCoalesceTest extends Twig_Test_NodeTestCase
{
public function getTests()
{
$left = new Twig_Node_Expression_Name('foo', 1);
$right = new Twig_Node_Expression_Constant(2, 1);
$node = new Twig_Node_Expression_EmptyCoalesce($left, $right, 1);

return array(array($node, "((empty(// line 1\n(\$context[\"foo\"] ?? null)) ? null : (\$context[\"foo\"] ?? null)) ?? (empty(2) ? null : 2))"));
}
}