From b93eb3ce5830ba64bcaeb0eaca02244d67d2e663 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 30 Sep 2024 14:05:03 +0200 Subject: [PATCH] Refactor code --- src/ExpressionParser.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index 23b24aee26a..ed61dcc2afb 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -93,9 +93,19 @@ public function parseExpression($precedence = 0, $allowArrow = false) $token = $this->parser->getCurrentToken(); } - if ( - ($expr instanceof AddBinary || $expr instanceof SubBinary) - ) { + $this->triggerPrecedenceDeprecations($expr, $token); + + if (0 === $precedence) { + return $this->parseConditionalExpression($expr); + } + + return $expr; + } + + private function triggerPrecedenceDeprecations(AbstractExpression $expr, Token $token): void + { + // Precedence of the ~ operator will be lower than + and - in Twig 4.0 + if ($expr instanceof AddBinary || $expr instanceof SubBinary) { /** @var AbstractExpression $left */ $left = $expr->getNode('left'); /** @var AbstractExpression $right */ @@ -108,12 +118,6 @@ public function parseExpression($precedence = 0, $allowArrow = false) trigger_deprecation('twig/twig', '3.15', \sprintf('As "+" / "-" will have a higher precedence than "~" in Twig 4.0, please add parentheses to keep the current behavior in "%s" at line %d.', $this->parser->getStream()->getSourceContext()->getName(), $token->getLine())); } } - - if (0 === $precedence) { - return $this->parseConditionalExpression($expr); - } - - return $expr; } /**