twigphp / Twig

Twig, the flexible, fast, and secure template language for PHP
https://twig.symfony.com/
BSD 3-Clause "New" or "Revised" License
8.17k stars 1.25k forks source link

Wrong precedence of NOT operator #3642

Closed mhcwebdesign closed 1 month ago

mhcwebdesign commented 2 years ago

In most programming languages (such as C++, Java, or PHP) the boolean unary NOT operator has a higher precedence than e.g. the multiplication, division, remainder, or relational operators. It usually has a precedence same as the unary '+' or unary '-'.

But why does Twig deviate from this practice and puts the unary 'NOT' operator precedence between the ones below:

.... '+' (binary add) '-' (binary minus) '~' (binary concat) 'NOT' (unary not) '*' (binary multiply) '/' (binary division) ....

Not even Django (Python) has such a strange precedence placement.

There are several other bad habits in the Twig expression syntax, but I think above mentioned issue could be fixed, and if need be, add a runtime switch to ensure backward compatibility with older Twig versions, but have future Twig releases use a proper operator precedence.

lacatoire commented 8 months ago

This is a good point, however I don't think it will ever be changed, it would be too sensitive for users to move. This can have serious consequences on calculations etc.