Fix the compilation of Twig comparison operators

The new compilation implemented in Twig 3 for PHP 7 (to match the
comparison semantic of PHP 8) was missing the surrounding braces (which
are part of the AsbtractBinary compilation). This caused issues when the
full expression would involve other operators (like the not operator),
as things would then rely on having the correct precedence in PHP (which
would not be the case).
This commit is contained in:
Christophe Coevoet
2020-06-05 15:54:17 +02:00
parent 3aa40a42b3
commit bf24f136b9
7 changed files with 26 additions and 12 deletions
+2 -2
View File
@@ -24,11 +24,11 @@ class EqualBinary extends AbstractBinary
}
$compiler
->raw('0 === twig_compare(')
->raw('(0 === twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
->raw('))')
;
}
+2 -2
View File
@@ -24,11 +24,11 @@ class GreaterBinary extends AbstractBinary
}
$compiler
->raw('1 === twig_compare(')
->raw('(1 === twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
->raw('))')
;
}
@@ -24,11 +24,11 @@ class GreaterEqualBinary extends AbstractBinary
}
$compiler
->raw('0 <= twig_compare(')
->raw('(0 <= twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
->raw('))')
;
}
+2 -2
View File
@@ -24,11 +24,11 @@ class LessBinary extends AbstractBinary
}
$compiler
->raw('-1 === twig_compare(')
->raw('(-1 === twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
->raw('))')
;
}
@@ -24,11 +24,11 @@ class LessEqualBinary extends AbstractBinary
}
$compiler
->raw('0 >= twig_compare(')
->raw('(0 >= twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
->raw('))')
;
}
@@ -24,11 +24,11 @@ class NotEqualBinary extends AbstractBinary
}
$compiler
->raw('0 !== twig_compare(')
->raw('(0 !== twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
->raw('))')
;
}
@@ -0,0 +1,14 @@
--TEST--
Twig comparison operators precendence
--TEMPLATE--
{{ not(1 > 2) }}/{{ not(1 > 1) }}/{{ not(1 >= 2) }}/{{ not(1 >= 1) }}
{{ not(1 < 2) }}/{{ not(1 < 1) }}/{{ not(1 <= 2) }}/{{ not(1 <= 1) }}
{{ not(1 == 1) }}/{{ not(1 == 2) }}
{{ not(1 != 1) }}/{{ not(1 != 2) }}
--DATA--
return []
--EXPECT--
1/1/1/
/1//
/1
1/