From 961e3e719ce82a10ecefa8aef94d7ac969d7f835 Mon Sep 17 00:00:00 2001 From: Samuel ROZE Date: Tue, 19 Dec 2017 11:02:38 +0000 Subject: [PATCH] Suggest to write an implementation if the interface cannot be autowired --- .../Compiler/AutowirePass.php | 4 ++++ .../Tests/Compiler/AutowirePassTest.php | 17 +++++++++++++++++ .../Fixtures/includes/autowiring_classes.php | 7 +++++++ 3 files changed, 28 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php index 1320ef1f2a..3cda29578c 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php @@ -457,6 +457,10 @@ class AutowirePass extends AbstractRecursivePass } else { $message = $this->container->has($type) ? 'this service is abstract' : 'no such service exists'; $message = sprintf('references %s "%s" but %s.%s', $r->isInterface() ? 'interface' : 'class', $type, $message, $this->createTypeAlternatives($reference)); + + if ($r->isInterface()) { + $message .= ' Did you create a class that implements this interface?'; + } } $message = sprintf('Cannot autowire service "%s": %s %s', $this->currentId, $label, $message); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php index e550bb2147..e821355518 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php @@ -703,6 +703,23 @@ class AutowirePassTest extends TestCase $pass->process($container); } + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException + * @expectedExceptionMessage Cannot autowire service "my_service": argument "$i" of method "Symfony\Component\DependencyInjection\Tests\Compiler\K::__construct()" references interface "Symfony\Component\DependencyInjection\Tests\Compiler\IInterface" but no such service exists. Did you create a class that implements this interface? + */ + public function testInterfaceWithNoImplementationSuggestToWriteOne() + { + $container = new ContainerBuilder(); + + $aDefinition = $container->register('my_service', K::class); + $aDefinition->setAutowired(true); + + (new AutowireRequiredMethodsPass())->process($container); + + $pass = new AutowirePass(); + $pass->process($container); + } + /** * @group legacy * @expectedDeprecation Autowiring services based on the types they implement is deprecated since Symfony 3.3 and won't be supported in version 4.0. You should rename (or alias) the "foo" service to "Symfony\Component\DependencyInjection\Tests\Compiler\Foo" instead. diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php index bf99eff6a2..990a873f34 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php @@ -90,6 +90,13 @@ class J } } +class K +{ + public function __construct(IInterface $i) + { + } +} + interface CollisionInterface { }