made it possible to use named operators as variables
authorFabien Potencier <fabien.potencier@gmail.com>
Tue, 15 Oct 2013 16:27:17 +0000 (18:27 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Tue, 15 Oct 2013 16:35:22 +0000 (18:35 +0200)
CHANGELOG
lib/Twig/ExpressionParser.php
test/Twig/Tests/Fixtures/expressions/operators_as_variables.test [new file with mode: 0644]
test/Twig/Tests/Fixtures/expressions/two_word_operators_as_variables.test [new file with mode: 0644]
test/Twig/Tests/Fixtures/regression/matches_as_variable.test [deleted file]

index e3c92c2..cb52fa6 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,6 @@
 * 1.14.1 (2013-XX-XX)
 
+ * made it possible to use named operators as variables
  * fixed the possibility to have a variable named 'matches'
  * added support for PHP 5.5 DateTimeInterface
 
index 67d3337..96f9224 100644 (file)
@@ -162,9 +162,10 @@ class Twig_ExpressionParser
                 break;
 
             case Twig_Token::OPERATOR_TYPE:
-                if ('matches' == $token->getValue()) {
+                if (preg_match(Twig_Lexer::REGEX_NAME, $token->getValue(), $matches) && $matches[0] == $token->getValue()) {
+                    // in this context, string operators are variable names
                     $this->parser->getStream()->next();
-                    $node = new Twig_Node_Expression_Name('matches', $token->getLine());
+                    $node = new Twig_Node_Expression_Name($token->getValue(), $token->getLine());
                     break;
                 }
 
diff --git a/test/Twig/Tests/Fixtures/expressions/operators_as_variables.test b/test/Twig/Tests/Fixtures/expressions/operators_as_variables.test
new file mode 100644 (file)
index 0000000..fe29d08
--- /dev/null
@@ -0,0 +1,16 @@
+--TEST--
+Twig allows to use named operators as variable names
+--TEMPLATE--
+{% for match in matches %}
+    {{- match }}
+{% endfor %}
+{{ in }}
+{{ is }}
+--DATA--
+return array('matches' => array(1, 2, 3), 'in' => 'in', 'is' => 'is')
+--EXPECT--
+1
+2
+3
+in
+is
diff --git a/test/Twig/Tests/Fixtures/expressions/two_word_operators_as_variables.test b/test/Twig/Tests/Fixtures/expressions/two_word_operators_as_variables.test
new file mode 100644 (file)
index 0000000..47f37e4
--- /dev/null
@@ -0,0 +1,8 @@
+--TEST--
+Twig does not allow to use two-word named operators as variable names
+--TEMPLATE--
+{{ starts with }}
+--DATA--
+return array()
+--EXCEPTION--
+Twig_Error_Syntax: Unexpected token "operator" of value "starts with" in "index.twig" at line 2
diff --git a/test/Twig/Tests/Fixtures/regression/matches_as_variable.test b/test/Twig/Tests/Fixtures/regression/matches_as_variable.test
deleted file mode 100644 (file)
index 00ca200..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
---TEST--
-Twig allows to use 'matches' for a variable name
---TEMPLATE--
-{% for match in matches %}
-    {{- match }}
-{% endfor %}
---DATA--
-return array('matches' => array(1, 2, 3))
---EXPECT--
-1
-2
-3