Fix the null coalescing operator when the test returns null

This commit is contained in:
Fabien Potencier
2024-12-12 08:34:15 +01:00
parent 47f527ef84
commit 4c5467f56a
4 changed files with 16 additions and 3 deletions
+1
View File
@@ -1,5 +1,6 @@
# 3.17.1 (2024-XX-XX)
* Fix the null coalescing operator when the test returns null
* Fix the Elvis operator when used as '? :' instead of '?:'
# 3.17.0 (2024-12-10)
@@ -29,8 +29,7 @@ final class NullCoalesceBinary extends AbstractBinary implements OperatorEscapeI
parent::__construct($left, $right, $lineno);
if (!$left instanceof NameExpression) {
$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(
+4 -1
View File
@@ -13,8 +13,10 @@ Twig supports the ?? operator
{{ nope ?? (nada ?? 'OK') }}
{{ 1 + (nope ?? (nada ?? 2)) }}
{{ 1 + (nope ?? 3) + (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
@@ -28,3 +30,4 @@ OK
OK
3
6
OK
+10
View File
@@ -77,6 +77,16 @@ class TwigTestFoo implements \Iterator
return 'foo';
}
public function getEmpty()
{
return '';
}
public function getNull()
{
return null;
}
public function getSelf()
{
return $this;