Skip to content

Commit 98f8d8a

Browse files
committed
Merge branch '3.x' into 4.x
* 3.x: Fix AssignNameExpression to forbid using names that won't work Do not allow : as macro definition separator
2 parents 10cb4be + 950ad5a commit 98f8d8a

File tree

9 files changed

+73
-9
lines changed

9 files changed

+73
-9
lines changed

src/ExpressionParser.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ public function parseArguments($namedArguments = true, $definition = false)
673673
}
674674

675675
$name = null;
676-
if (($token = $stream->nextIf(Token::OPERATOR_TYPE, '=')) || ($token = $stream->nextIf(Token::PUNCTUATION_TYPE, ':'))) {
676+
if (($token = $stream->nextIf(Token::OPERATOR_TYPE, '=')) || (!$definition && $token = $stream->nextIf(Token::PUNCTUATION_TYPE, ':'))) {
677677
if (!$value instanceof NameExpression) {
678678
throw new SyntaxError(\sprintf('A parameter name must be a string, "%s" given.', $value::class), $token->getLine(), $stream->getSourceContext());
679679
}
@@ -722,11 +722,7 @@ public function parseAssignmentExpression()
722722
} else {
723723
$stream->expect(Token::NAME_TYPE, null, 'Only variables can be assigned to');
724724
}
725-
$value = $token->getValue();
726-
if (\in_array(strtolower($value), ['true', 'false', 'none', 'null'])) {
727-
throw new SyntaxError(\sprintf('You cannot assign a value to "%s".', $value), $token->getLine(), $stream->getSourceContext());
728-
}
729-
$targets[] = new AssignNameExpression($value, $token->getLine());
725+
$targets[] = new AssignNameExpression($token->getValue(), $token->getLine());
730726

731727
if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) {
732728
break;

src/Node/Expression/AssignNameExpression.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@
1313
namespace Twig\Node\Expression;
1414

1515
use Twig\Compiler;
16+
use Twig\Error\SyntaxError;
1617

1718
class AssignNameExpression extends NameExpression
1819
{
20+
public function __construct(string $name, int $lineno)
21+
{
22+
// All names supported by ExpressionParser::parsePrimaryExpression() should be excluded
23+
if (\in_array(strtolower($name), ['true', 'false', 'none', 'null'])) {
24+
throw new SyntaxError(\sprintf('You cannot assign a value to "%s".', $name), $lineno);
25+
}
26+
27+
parent::__construct($name, $lineno);
28+
}
29+
1930
public function compile(Compiler $compiler): void
2031
{
2132
$compiler

src/TokenParser/FromTokenParser.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ public function parse(Token $token): Node
3535
while (true) {
3636
$name = $stream->expect(Token::NAME_TYPE)->getValue();
3737

38-
$alias = $name;
3938
if ($stream->nextIf('as')) {
40-
$alias = $stream->expect(Token::NAME_TYPE)->getValue();
39+
$alias = new AssignNameExpression($stream->expect(Token::NAME_TYPE)->getValue(), $token->getLine());
40+
} else {
41+
$alias = new AssignNameExpression($name, $token->getLine());
4142
}
4243

4344
$targets[$name] = $alias;
@@ -53,7 +54,7 @@ public function parse(Token $token): Node
5354
$node = new ImportNode($macro, $var, $token->getLine(), $this->parser->isMainScope());
5455

5556
foreach ($targets as $name => $alias) {
56-
$this->parser->addImportedSymbol('function', $alias, 'macro_'.$name, $var);
57+
$this->parser->addImportedSymbol('function', $alias->getAttribute('name'), 'macro_'.$name, $var);
5758
}
5859

5960
return $node;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
"map" filter
3+
--TEMPLATE--
4+
{{ [1, 2]|map(true => true * 2)|join(', ') }}
5+
--DATA--
6+
return []
7+
--EXCEPTION--
8+
Twig\Error\SyntaxError: You cannot assign a value to "true" in "index.twig" at line 2.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
"for" tag
3+
--TEMPLATE--
4+
{% for true in [1, 2] %}
5+
{% endfor %}
6+
--DATA--
7+
return []
8+
--EXCEPTION--
9+
Twig\Error\SyntaxError: You cannot assign a value to "true" in "index.twig" at line 2.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
"macro" tag does not support : as a separator in definition, only = is supported
3+
--TEMPLATE--
4+
{% macro test(foo: "foo") -%}
5+
{{ foo }}
6+
{%- endmacro %}
7+
--DATA--
8+
return []
9+
--EXCEPTION--
10+
Twig\Error\SyntaxError: Arguments must be separated by a comma. Unexpected token "punctuation" of value ":" ("punctuation" expected with value ",") in "index.twig" at line 2.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
"from" tag
3+
--TEMPLATE--
4+
{% from _self import input as true %}
5+
6+
{{ true('username') }}
7+
8+
{% macro input(name) -%}
9+
{% endmacro %}
10+
--DATA--
11+
return []
12+
--EXCEPTION--
13+
Twig\Error\SyntaxError: You cannot assign a value to "true" in "index.twig" at line 2.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
"import" tag
3+
--TEMPLATE--
4+
{% import _self as true %}
5+
--DATA--
6+
return []
7+
--EXCEPTION--
8+
Twig\Error\SyntaxError: You cannot assign a value to "true" in "index.twig" at line 2.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
"set" tag
3+
--TEMPLATE--
4+
{% set true = 'foo' %}
5+
--DATA--
6+
return []
7+
--EXCEPTION--
8+
Twig\Error\SyntaxError: You cannot assign a value to "true" in "index.twig" at line 2.

0 commit comments

Comments
 (0)