Skip to content

Commit

Permalink
feature #3044 Allow string operators as variables names in assignment…
Browse files Browse the repository at this point in the history
…s (fabpot)

This PR was merged into the 1.x branch.

Discussion
----------

Allow string operators as variables names in assignments

closes #3041

Commits
-------

5835c23 allowed string operators as variables names in assignments
  • Loading branch information
fabpot committed Jun 3, 2019
2 parents a42e02d + 5835c23 commit 51f33f7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG
@@ -1,6 +1,6 @@
* 1.42.1 (2019-XX-XX)

* n/a
* allowed string operators as variables names in assignments

* 1.42.0 (2019-05-31)

Expand Down
8 changes: 7 additions & 1 deletion src/ExpressionParser.php
Expand Up @@ -649,7 +649,13 @@ public function parseAssignmentExpression()
$stream = $this->parser->getStream();
$targets = [];
while (true) {
$token = $stream->expect(Token::NAME_TYPE, null, 'Only variables can be assigned to');
$token = $this->parser->getCurrentToken();
if ($stream->test(Token::OPERATOR_TYPE) && preg_match(Lexer::REGEX_NAME, $token->getValue())) {
// in this context, string operators are variable names
$this->parser->getStream()->next();
} else {
$stream->expect(Token::NAME_TYPE, null, 'Only variables can be assigned to');
}
$value = $token->getValue();
if (\in_array(strtolower($value), ['true', 'false', 'none', 'null'])) {
throw new SyntaxError(sprintf('You cannot assign a value to "%s".', $value), $token->getLine(), $stream->getSourceContext());
Expand Down
@@ -0,0 +1,18 @@
--TEST--
Twig supports the string operators as variable names in assignments
--TEMPLATE--
{% for matches in [1, 2] %}
{{- matches }}
{% endfor %}

{% set matches = [1, 2] %}

OK
--DATA--
return []
--EXPECT--
1
2


OK

0 comments on commit 51f33f7

Please sign in to comment.