function ExpressionParser::triggerPrecedenceDeprecations
2 calls to ExpressionParser::triggerPrecedenceDeprecations()
- ExpressionParser::getPrimary in vendor/
twig/ twig/ src/ ExpressionParser.php - ExpressionParser::parseExpression in vendor/
twig/ twig/ src/ ExpressionParser.php
File
-
vendor/
twig/ twig/ src/ ExpressionParser.php, line 140
Class
- ExpressionParser
- Parses expressions.
Namespace
TwigCode
private function triggerPrecedenceDeprecations(AbstractExpression $expr) : void {
// Check that the all nodes that are between the 2 precedences have explicit parentheses
if (!$expr->hasAttribute('operator') || !isset($this->precedenceChanges[$expr->getAttribute('operator')])) {
return;
}
if (str_starts_with($unaryOp = $expr->getAttribute('operator'), 'unary')) {
if ($expr->hasExplicitParentheses()) {
return;
}
$target = explode('_', $unaryOp)[1];
$change = $this->unaryOperators[$target]['precedence_change'];
/** @var AbstractExpression $node */
$node = $expr->getNode('node');
foreach ($this->precedenceChanges as $operatorName => $changes) {
if (!in_array($unaryOp, $changes)) {
continue;
}
if ($node->hasAttribute('operator') && $operatorName === $node->getAttribute('operator')) {
trigger_deprecation($change->getPackage(), $change->getVersion(), \sprintf('Add explicit parentheses around the "%s" unary operator to avoid behavior change in the next major version as its precedence will change in "%s" at line %d.', $target, $this->parser
->getStream()
->getSourceContext()
->getName(), $node->getTemplateLine()));
}
}
}
else {
foreach ($this->precedenceChanges[$expr->getAttribute('operator')] as $operatorName) {
foreach ($expr as $node) {
/** @var AbstractExpression $node */
if ($node->hasAttribute('operator') && $operatorName === $node->getAttribute('operator') && !$node->hasExplicitParentheses()) {
$op = explode('_', $operatorName)[1];
$change = $this->binaryOperators[$op]['precedence_change'];
trigger_deprecation($change->getPackage(), $change->getVersion(), \sprintf('Add explicit parentheses around the "%s" binary operator to avoid behavior change in the next major version as its precedence will change in "%s" at line %d.', $op, $this->parser
->getStream()
->getSourceContext()
->getName(), $node->getTemplateLine()));
}
}
}
}
}