[ExpressionParser] forbids true, false, null and none keywords for variables names.

This commit is contained in:
Hugo Hamon
2016-04-23 16:01:10 +02:00
parent babe6fca0e
commit 2e02f73bf7
2 changed files with 9 additions and 3 deletions
+4 -3
View File
@@ -539,10 +539,11 @@ class Twig_ExpressionParser
$targets = array();
while (true) {
$token = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, null, 'Only variables can be assigned to');
if (in_array($token->getValue(), array('true', 'false', 'none'))) {
throw new Twig_Error_Syntax(sprintf('You cannot assign a value to "%s".', $token->getValue()), $token->getLine(), $this->parser->getFilename());
$value = $token->getValue();
if (in_array(strtolower($value), array('true', 'false', 'none', 'null'))) {
throw new Twig_Error_Syntax(sprintf('You cannot assign a value to "%s"', $value), $token->getLine(), $this->parser->getFilename());
}
$targets[] = new Twig_Node_Expression_AssignName($token->getValue(), $token->getLine());
$targets[] = new Twig_Node_Expression_AssignName($value, $token->getLine());
if (!$this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
break;
+5
View File
@@ -27,8 +27,13 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase
{
return array(
array('{% set false = "foo" %}'),
array('{% set FALSE = "foo" %}'),
array('{% set true = "foo" %}'),
array('{% set TRUE = "foo" %}'),
array('{% set none = "foo" %}'),
array('{% set NONE = "foo" %}'),
array('{% set null = "foo" %}'),
array('{% set NULL = "foo" %}'),
array('{% set 3 = "foo" %}'),
array('{% set 1 + 2 = "foo" %}'),
array('{% set "bar" = "foo" %}'),