mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-05 06:56:51 +00:00
Fix the null coalescing operator when the test returns null
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -77,6 +77,16 @@ class TwigTestFoo implements \Iterator
|
||||
return 'foo';
|
||||
}
|
||||
|
||||
public function getEmpty()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function getNull()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getSelf()
|
||||
{
|
||||
return $this;
|
||||
|
||||
Reference in New Issue
Block a user