merged branch nikic/allowIsDefinedOnMethods (PR #490)

Commits
-------

930330f Allow defined test and default filter on methods

Discussion
----------

Allow defined test and default filter on methods

Patch for #487.

---------------------------------------------------------------------------

by fabpot at 2011/10/30 03:45:49 -0700

Problem is when some of the arguments reference variables that do not exist (`foo.bar(bar.foobar)`). That's why I've added the check on arguments.

---------------------------------------------------------------------------

by nikic at 2011/10/30 03:58:58 -0700

@fabpot I'm not exactly sure what is the expected behavior in that case. When writing `foo.defined(undefined)|default()` I think an exception is the expected behavior (because there is an undefined variable without default, even if it is in the method arguments). With `foo.undefined(undefined)|default()` on the other hand I'm not sure. One could argue that as the method does not exist the arguments never need to be evaluated.

---------------------------------------------------------------------------

by fabpot at 2011/10/30 05:49:46 -0700

The same code is used for the `defined` test: what would be the expected behavior for `foo.defined(underfined) is defined`?
This commit is contained in:
Fabien Potencier
2011-10-30 14:46:53 +01:00
3 changed files with 24 additions and 4 deletions
-4
View File
@@ -35,10 +35,6 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression
if ($node->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
$this->changeIgnoreStrictCheck($node->getNode('node'));
}
if (count($node->getNode('arguments'))) {
throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine());
}
}
public function compile(Twig_Compiler $compiler)
@@ -27,6 +27,10 @@ Precedence:
Object methods:
{{ object.foo |default('default') is sameas('default') ? 'ko' : 'ok' }}
{{ object.undefinedMethod |default('default') is sameas('default') ? 'ok' : 'ko' }}
{{ object.getFoo() |default('default') is sameas('default') ? 'ko' : 'ok' }}
{{ object.getFoo('a') |default('default') is sameas('default') ? 'ko' : 'ok' }}
{{ object.undefinedMethod() |default('default') is sameas('default') ? 'ok' : 'ko' }}
{{ object.undefinedMethod('a') |default('default') is sameas('default') ? 'ok' : 'ko' }}
Deep nested:
{{ nested.undefinedVar.foo.bar |default('default') is sameas('default') ? 'ok' : 'ko' }}
{{ nested.definedArray.0 |default('default') is sameas('default') ? 'ko' : 'ok' }}
@@ -78,6 +82,10 @@ ok
Object methods:
ok
ok
ok
ok
ok
ok
Deep nested:
ok
ok
@@ -129,6 +137,10 @@ ok
Object methods:
ok
ok
ok
ok
ok
ok
Deep nested:
ok
ok
@@ -19,6 +19,10 @@
{{ nested['definedArray'][0] is defined ? 'ok' : 'ko' }}
{{ object.foo is defined ? 'ok' : 'ko' }}
{{ object.undefinedMethod is defined ? 'ko' : 'ok' }}
{{ object.getFoo() is defined ? 'ok' : 'ko' }}
{{ object.getFoo('a') is defined ? 'ok' : 'ko' }}
{{ object.undefinedMethod() is defined ? 'ko' : 'ok' }}
{{ object.undefinedMethod('a') is defined ? 'ko' : 'ok' }}
{{ object.self.foo is defined ? 'ok' : 'ko' }}
{{ object.self.undefinedMethod is defined ? 'ko' : 'ok' }}
{{ object.undefinedMethod.self is defined ? 'ko' : 'ok' }}
@@ -57,6 +61,10 @@ ok
ok
ok
ok
ok
ok
ok
ok
--DATA--
return array(
'definedVar' => 'defined',
@@ -94,3 +102,7 @@ ok
ok
ok
ok
ok
ok
ok
ok