mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-15 11:56:50 +00:00
Fixed using of unary operators with default parameters
This commit is contained in:
@@ -164,6 +164,21 @@ class Twig_ExpressionParser
|
|||||||
$this->parser->getStream()->next();
|
$this->parser->getStream()->next();
|
||||||
$node = new Twig_Node_Expression_Name($token->getValue(), $token->getLine());
|
$node = new Twig_Node_Expression_Name($token->getValue(), $token->getLine());
|
||||||
break;
|
break;
|
||||||
|
} elseif (isset($this->unaryOperators[$token->getValue()])) {
|
||||||
|
$class = $this->unaryOperators[$token->getValue()]['class'];
|
||||||
|
|
||||||
|
$ref = new ReflectionClass($class);
|
||||||
|
$negClass = 'Twig_Node_Expression_Unary_Neg';
|
||||||
|
$posClass = 'Twig_Node_Expression_Unary_Pos';
|
||||||
|
if (!(in_array($ref->getName(), array($negClass, $posClass)) || $ref->isSubclassOf($negClass) || $ref->isSubclassOf($posClass))) {
|
||||||
|
throw new Twig_Error_Syntax(sprintf('Unexpected unary operator "%s"', $token->getValue()), $token->getLine(), $this->parser->getFilename());
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->parser->getStream()->next();
|
||||||
|
$expr = $this->parsePrimaryExpression();
|
||||||
|
|
||||||
|
$node = new $class($expr, $token->getLine());
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -583,7 +598,9 @@ class Twig_ExpressionParser
|
|||||||
// checks that the node only contains "constant" elements
|
// checks that the node only contains "constant" elements
|
||||||
protected function checkConstantExpression(Twig_NodeInterface $node)
|
protected function checkConstantExpression(Twig_NodeInterface $node)
|
||||||
{
|
{
|
||||||
if (!($node instanceof Twig_Node_Expression_Constant || $node instanceof Twig_Node_Expression_Array)) {
|
if (!($node instanceof Twig_Node_Expression_Constant || $node instanceof Twig_Node_Expression_Array
|
||||||
|
|| $node instanceof Twig_Node_Expression_Unary_Neg || $node instanceof Twig_Node_Expression_Unary_Pos
|
||||||
|
)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,12 +18,9 @@ abstract class Twig_Node_Expression_Unary extends Twig_Node_Expression
|
|||||||
|
|
||||||
public function compile(Twig_Compiler $compiler)
|
public function compile(Twig_Compiler $compiler)
|
||||||
{
|
{
|
||||||
$compiler->raw('(');
|
$compiler->raw(' ');
|
||||||
$this->operator($compiler);
|
$this->operator($compiler);
|
||||||
$compiler
|
$compiler->subcompile($this->getNode('node'));
|
||||||
->subcompile($this->getNode('node'))
|
|
||||||
->raw(')')
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract public function operator(Twig_Compiler $compiler);
|
abstract public function operator(Twig_Compiler $compiler);
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
--TEST--
|
||||||
|
Twig manages negative numbers as default parameters
|
||||||
|
--TEMPLATE--
|
||||||
|
{% import _self as macros %}
|
||||||
|
{{ macros.negative_number1() }}
|
||||||
|
{{ macros.negative_number2() }}
|
||||||
|
{{ macros.negative_number3() }}
|
||||||
|
{{ macros.positive_number1() }}
|
||||||
|
{{ macros.positive_number2() }}
|
||||||
|
{% macro negative_number1(nb=-1) %}{{ nb }}{% endmacro %}
|
||||||
|
{% macro negative_number2(nb = --1) %}{{ nb }}{% endmacro %}
|
||||||
|
{% macro negative_number3(nb = - 1) %}{{ nb }}{% endmacro %}
|
||||||
|
{% macro positive_number1(nb = +1) %}{{ nb }}{% endmacro %}
|
||||||
|
{% macro positive_number2(nb = ++1) %}{{ nb }}{% endmacro %}
|
||||||
|
{{ -1 }}
|
||||||
|
{{ - 1 }}
|
||||||
|
{{ 5 - 1 }}
|
||||||
|
{{ 5-1 }}
|
||||||
|
--DATA--
|
||||||
|
return array()
|
||||||
|
--EXPECT--
|
||||||
|
-1
|
||||||
|
1
|
||||||
|
-1
|
||||||
|
1
|
||||||
|
1
|
||||||
|
-1
|
||||||
|
-1
|
||||||
|
4
|
||||||
|
4
|
||||||
@@ -38,7 +38,8 @@ class Twig_Tests_Node_Expression_Unary_NegTest extends Twig_Test_NodeTestCase
|
|||||||
$node = new Twig_Node_Expression_Unary_Neg($node, 1);
|
$node = new Twig_Node_Expression_Unary_Neg($node, 1);
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
array($node, '(-1)'),
|
array($node, '-1'),
|
||||||
|
array(new Twig_Node_Expression_Unary_Neg($node, 1), '- -1'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class Twig_Tests_Node_Expression_Unary_NotTest extends Twig_Test_NodeTestCase
|
|||||||
$node = new Twig_Node_Expression_Unary_Not($node, 1);
|
$node = new Twig_Node_Expression_Unary_Not($node, 1);
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
array($node, '(!1)'),
|
array($node, '!1'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class Twig_Tests_Node_Expression_Unary_PosTest extends Twig_Test_NodeTestCase
|
|||||||
$node = new Twig_Node_Expression_Unary_Pos($node, 1);
|
$node = new Twig_Node_Expression_Unary_Pos($node, 1);
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
array($node, '(+1)'),
|
array($node, '+1'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user