Merge branch '3.x' into 4.x

* 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
This commit is contained in:
Fabien Potencier
2024-12-12 10:28:23 +01:00
5 changed files with 25 additions and 4 deletions
+1
View File
@@ -324,6 +324,7 @@ final class CoreExtension extends AbstractExtension
'+' => ['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],
@@ -29,8 +29,7 @@ final class NullCoalesceBinary extends AbstractBinary implements OperatorEscapeI
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(
@@ -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
+5 -2
View File
@@ -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
@@ -29,4 +31,5 @@ OK
OK
3
6
2
2
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;