Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 30, 2024
1 parent 9f8c52d commit b93eb3c
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit b93eb3c

Please sign in to comment.