Fix optimizing non-public named closures

This commit is contained in:
Nicolas Grekas
2022-05-16 17:47:23 +02:00
parent 137b370376
commit dd34e9f5c9
2 changed files with 13 additions and 0 deletions
+5
View File
@@ -289,6 +289,7 @@ abstract class CallExpression extends AbstractExpression
return $this->reflector = [$r, $callable, $r->class.'::'.$r->name];
}
$checkVisibility = $callable instanceof \Closure;
$r = new \ReflectionFunction(\Closure::fromCallable($callable));
if (false !== strpos($r->name, '{closure}')) {
@@ -305,6 +306,10 @@ abstract class CallExpression extends AbstractExpression
$callable = $callableName = $r->name;
}
if ($checkVisibility && \is_array($callable) && method_exists(...$callable) && !(new \ReflectionMethod(...$callable))->isPublic()) {
$callable = $r->getClosure();
}
return $this->reflector = [$r, $callable, $callableName];
}
}
+8
View File
@@ -48,12 +48,17 @@ class FilterTest extends NodeTestCase
{
return [
new TwigFilter('foo', \Closure::fromCallable([$this, 'foo'])),
new TwigFilter('foobar', \Closure::fromCallable([$this, 'foobar'])),
];
}
public function foo()
{
}
protected function foobar()
{
}
};
$environment->addExtension($extension);
@@ -127,6 +132,9 @@ class FilterTest extends NodeTestCase
$node = $this->createFilter($string, 'foo');
$tests[] = [$node, sprintf('$this->extensions[\'%s\']->foo("abc")', \get_class($extension)), $environment];
$node = $this->createFilter($string, 'foobar');
$tests[] = [$node, '$this->env->getFilter(\'foobar\')->getCallable()("abc")', $environment];
return $tests;
}