Fixes CallExpression::reflectCallable() throwing TypeError

This commit is contained in:
ju1ius
2022-05-20 23:32:48 +02:00
committed by Fabien Potencier
parent fcc900cf80
commit e333ccc9f6
2 changed files with 15 additions and 1 deletions
+6 -1
View File
@@ -290,7 +290,12 @@ abstract class CallExpression extends AbstractExpression
}
$checkVisibility = $callable instanceof \Closure;
$r = new \ReflectionFunction(\Closure::fromCallable($callable));
try {
$closure = \Closure::fromCallable($callable);
} catch (\TypeError $e) {
throw new \LogicException(sprintf('Callback for %s "%s" is not callable in the current scope.', $this->getAttribute('type'), $this->getAttribute('name')), 0, $e);
}
$r = new \ReflectionFunction($closure);
if (false !== strpos($r->name, '{closure}')) {
return $this->reflector = [$r, $callable, 'Closure'];
+9
View File
@@ -94,6 +94,15 @@ class CallTest extends TestCase
$this->getArguments($node, [[$this, 'customFunctionWithArbitraryArguments'], []]);
}
public function testGetArgumentsWithInvalidCallable()
{
// see https://github.com/twigphp/Twig/issues/3708
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Callback for function "foo" is not callable in the current scope.');
$node = new Node_Expression_Call([], ['type' => 'function', 'name' => 'foo', 'is_variadic' => true]);
$this->getArguments($node, ['<not-a-callable>', []]);
}
public static function customStaticFunction($arg1, $arg2 = 'default', $arg3 = [])
{
}