Skip to content

Commit

Permalink
Merge branch '3.x' into 4.x
Browse files Browse the repository at this point in the history
* 3.x:
  Fix the null coalescing operator when the test returns null
  Fix the Elvis operator when there is some space between ? and :
  Bump version
  Prepare the 3.17.0 release
  Update CHANGELOG
  • Loading branch information
fabpot committed Dec 12, 2024
2 parents 0c47b50 + 243f5f5 commit 63b86e3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ public function getOperators(): array
'+' => ['precedence' => 500, 'class' => PosUnary::class],
],
[
'? :' => ['precedence' => 5, 'class' => ElvisBinary::class, 'associativity' => ExpressionParser::OPERATOR_RIGHT],
'?:' => ['precedence' => 5, 'class' => ElvisBinary::class, 'associativity' => ExpressionParser::OPERATOR_RIGHT],
'??' => ['precedence' => 5, 'class' => NullCoalesceBinary::class, 'associativity' => ExpressionParser::OPERATOR_RIGHT],
'or' => ['precedence' => 10, 'class' => OrBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT],
Expand Down
3 changes: 1 addition & 2 deletions src/Node/Expression/Binary/NullCoalesceBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public function __construct(AbstractExpression $left, AbstractExpression $right,
parent::__construct($left, $right, $lineno);

if (!$left instanceof ContextVariable) {
$left = clone $left;
$test = new DefinedTest($left, new TwigTest('defined'), new EmptyNode(), $left->getTemplateLine());
$test = new DefinedTest(clone $left, new TwigTest('defined'), new EmptyNode(), $left->getTemplateLine());
// for "block()", we don't need the null test as the return value is always a string
if (!$left instanceof BlockReferenceExpression) {
$test = new AndBinary(
Expand Down
8 changes: 8 additions & 0 deletions tests/Fixtures/expressions/ternary_operator_nothen.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ Twig supports the ternary operator
--TEMPLATE--
{{ 'YES' ?: 'NO' }}
{{ 0 ?: 'NO' }}
{{ 'YES' ? : 'NO' }}
{{ 0 ? : 'NO' }}
{{ 'YES' ? : 'NO' }}
{{ 0 ? : 'NO' }}
--DATA--
return []
--EXPECT--
YES
NO
YES
NO
YES
NO
7 changes: 5 additions & 2 deletions tests/Fixtures/tests/null_coalesce.test
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ Twig supports the ?? operator
{{ 1 + (nope ?? (nada ?? 2)) }}
{{ 1 + (nope ?? 3) + (nada ?? 2) }}
{{ nope ?? nada ?? 2 }}
{{ obj.null() ?? 'OK' }}
{{ obj.empty() ?? 'KO' }}
--DATA--
return ['bar' => 'OK', 'foo' => ['bar' => 'OK']]
return ['bar' => 'OK', 'foo' => ['bar' => 'OK'], 'obj' => new Twig\Tests\TwigTestFoo()]
--EXPECT--
OK
OK
Expand All @@ -29,4 +31,5 @@ OK
OK
3
6
2
2
OK
10 changes: 10 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ public function getFoo()
return 'foo';
}

public function getEmpty()
{
return '';
}

public function getNull()
{
return null;
}

public function getSelf()
{
return $this;
Expand Down

0 comments on commit 63b86e3

Please sign in to comment.