From 1af334be8923f184714902848d5fec5190e6a5f1 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 11 Dec 2024 10:38:21 +0100 Subject: [PATCH] Fix the Elvis operator when there is some space between ? and : --- CHANGELOG | 2 +- src/Extension/CoreExtension.php | 1 + tests/Fixtures/expressions/ternary_operator_nothen.test | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 481866a8887..d6bf5cbd45a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ # 3.17.1 (2024-XX-XX) - * n/a + * Fix the Elvis operator when used as '? :' instead of '?:' # 3.17.0 (2024-12-10) diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 567c7e6771f..b60944c4a4e 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -321,6 +321,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' => 300, 'precedence_change' => new OperatorPrecedenceChange('twig/twig', '3.15', 5), 'class' => NullCoalesceBinary::class, 'associativity' => ExpressionParser::OPERATOR_RIGHT], 'or' => ['precedence' => 10, 'class' => OrBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT], diff --git a/tests/Fixtures/expressions/ternary_operator_nothen.test b/tests/Fixtures/expressions/ternary_operator_nothen.test index ecd6b754656..53f8c0b3caf 100644 --- a/tests/Fixtures/expressions/ternary_operator_nothen.test +++ b/tests/Fixtures/expressions/ternary_operator_nothen.test @@ -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