8000 bug #24041 [ExpressionLanguage] throws an exception on calling uncall… · symfony/symfony@a819038 · GitHub
[go: up one dir, main page]

Skip to content

Commit a819038

Browse files
committed
bug #24041 [ExpressionLanguage] throws an exception on calling uncallable method (fmata)
This PR was merged into the 2.7 branch. Discussion ---------- [ExpressionLanguage] throws an exception on calling uncallable method | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a When we evaluate an expression, if a callable is incorrect (not exists or not accessible) a warning is printed. This PR handles this case and throws a \RuntimeException when `is_callable()` returns `false` : ```php $el = new ExpressionLanguage(); $el->evaluate('foo.myfunction()', array('foo' => new \stdClass())); ``` **Before:** `Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'stdClass' does not have a method 'myfunction' in /home/.../src/Symfony/Component/ExpressionLanguage/Node/GetAttrNode.php on line 84` **After:** `Fatal error: Uncaught RuntimeException: Unable to call method "myfunction" of object "stdClass". in /home/.../src/Symfony/Component/ExpressionLanguage/Node/GetAttrNode.php:81` Commits ------- c8b65ae [ExpressionLanguage] throws an exception on calling uncallable method
2 parents fd568ac + c8b65ae commit a819038

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Symfony/Component/ExpressionLanguage/Node/GetAttrNode.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ public function evaluate($functions, $values)
7777
if (!is_object($obj)) {
7878
throw new \RuntimeException('Unable to get a property on a non-object.');
7979
}
80+
if (!is_callable($toCall = array($obj, $this->nodes['attribute']->attributes['value']))) {
81+
throw new \RuntimeException(sprintf('Unable to call method "%s" of object "%s".', $this->nodes['attribute']->attributes['value'], get_class($obj)));
82+
}
8083

81-
return call_user_func_array(array($obj, $this->nodes['attribute']->attributes['value']), $this->nodes['arguments']->evaluate($functions, $values));
84+
return call_user_func_array($toCall, $this->nodes['arguments']->evaluate($functions, $values));
8285

8386
case self::ARRAY_CALL:
8487
$array = $this->nodes['node']->evaluate($functions, $values);

src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ public function testRegisterAfterEval($registerCallback)
163163
$registerCallback($el);
164164
}
165165

166+
/**
167+
* @expectedException \RuntimeException
168+
* @expectedExceptionMessageRegExp /Unable to call method "\w+" of object "\w+"./
169+
*/
170+
public function testCallBadCallable()
171+
{
172+
$el = new ExpressionLanguage();
173+
$el->evaluate('foo.myfunction()', array('foo' => new \stdClass()));
174+
}
175+
166176
/**
167177
* @dataProvider getRegisterCallbacks
168178
* @expectedException \LogicException

0 commit comments

Comments
 (0)
0