Skip to content

Commit

Permalink
fixed the defined test when used on a constant, a map, or a sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 25, 2016
1 parent 7dc9fb2 commit 62e8ee3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,5 +1,6 @@
* 1.23.4 (2016-XX-XX)

* fixed the defined test when used on a constant, a map, or a sequence
* undeprecated _self (should only be used to get the template name, not the template instance)
* fixed parsing on PHP7

Expand Down
6 changes: 4 additions & 2 deletions lib/Twig/Node/Expression/Test/Defined.php
Expand Up @@ -25,17 +25,19 @@ class Twig_Node_Expression_Test_Defined extends Twig_Node_Expression_Test
{
public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno)
{
parent::__construct($node, $name, $arguments, $lineno);

if ($node instanceof Twig_Node_Expression_Name) {
$node->setAttribute('is_defined_test', true);
} elseif ($node instanceof Twig_Node_Expression_GetAttr) {
$node->setAttribute('is_defined_test', true);

$this->changeIgnoreStrictCheck($node);
} elseif ($node instanceof Twig_Node_Expression_Constant || $node instanceof Twig_Node_Expression_Array) {
$node = new Twig_Node_Expression_Constant(true, $node->getLine());
} else {
throw new Twig_Error_Syntax('The "defined" test only works with simple variables.', $this->getLine());
}

parent::__construct($node, $name, $arguments, $lineno);
}

protected function changeIgnoreStrictCheck(Twig_Node_Expression_GetAttr $node)
Expand Down
21 changes: 21 additions & 0 deletions test/Twig/Tests/Fixtures/tests/defined.test
Expand Up @@ -26,6 +26,13 @@
{{ object.self.foo is defined ? 'ok' : 'ko' }}
{{ object.self.undefinedMethod is defined ? 'ko' : 'ok' }}
{{ object.undefinedMethod.self is defined ? 'ko' : 'ok' }}
{{ 0 is defined ? 'ok' : 'ko' }}
{{ "foo" is defined ? 'ok' : 'ko' }}
{{ true is defined ? 'ok' : 'ko' }}
{{ false is defined ? 'ok' : 'ko' }}
{{ null is defined ? 'ok' : 'ko' }}
{{ [1, 2] is defined ? 'ok' : 'ko' }}
{{ { foo: "bar" } is defined ? 'ok' : 'ko' }}
--DATA--
return array(
'definedVar' => 'defined',
Expand Down Expand Up @@ -65,6 +72,13 @@ ok
ok
ok
ok
ok
ok
ok
ok
ok
ok
ok
--DATA--
return array(
'definedVar' => 'defined',
Expand Down Expand Up @@ -106,3 +120,10 @@ ok
ok
ok
ok
ok
ok
ok
ok
ok
ok
ok

0 comments on commit 62e8ee3

Please sign in to comment.