From d6a7fbfbc3c988068754e519ae70ace62a7944a7 Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Fri, 20 Dec 2019 17:42:47 +0100 Subject: [PATCH] [DependencyInjection][CheckTypeDeclarationsPass] Handle \Closure for callable --- .../Compiler/CheckTypeDeclarationsPass.php | 2 +- .../Compiler/CheckTypeDeclarationsPassTest.php | 16 ++++++++++++++++ .../CheckTypeDeclarationsPass/BarMethodCall.php | 4 ++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php b/src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php index 0743cbbb5f..4bbcedbf12 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php @@ -166,7 +166,7 @@ final class CheckTypeDeclarationsPass extends AbstractRecursivePass return; } - if ('callable' === $type && method_exists($class, '__invoke')) { + if ('callable' === $type && (\Closure::class === $class || method_exists($class, '__invoke'))) { return; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php index 13c34e156c..22a29fa4d6 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php @@ -681,4 +681,20 @@ class CheckTypeDeclarationsPassTest extends TestCase $this->addToAssertionCount(1); } + + public function testProcessHandleClosureForCallable() + { + $closureDefinition = new Definition(\Closure::class); + $closureDefinition->setFactory([\Closure::class, 'fromCallable']); + $closureDefinition->setArguments(['strlen']); + + $container = new ContainerBuilder(); + $container + ->register('foobar', BarMethodCall::class) + ->addMethodCall('setCallable', [$closureDefinition]); + + (new CheckTypeDeclarationsPass(true))->process($container); + + $this->addToAssertionCount(1); + } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/CheckTypeDeclarationsPass/BarMethodCall.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/CheckTypeDeclarationsPass/BarMethodCall.php index c308ef9545..b705601609 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/CheckTypeDeclarationsPass/BarMethodCall.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/CheckTypeDeclarationsPass/BarMethodCall.php @@ -36,4 +36,8 @@ class BarMethodCall public function setIterable(iterable $iterable) { } + + public function setCallable(callable $callable): void + { + } }