8000 Suggest to write an implementation if the interface cannot be autowired · symfony/symfony@50da469 · GitHub
[go: up one dir, main page]

Skip to content

Commit 50da469

Browse files
committed
Suggest to write an implementation if the interface cannot be autowired
1 parent 77104a1 commit 50da469

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ private function createTypeNotFoundMessage(TypedReference $reference, $label)
457457
} else {
458458
$message = $this->container->has($type) ? 'this service is abstract' : 'no such service exists';
459459
$message = sprintf('references %s "%s" but %s.%s', $r->isInterface() ? 'interface' : 'class', $type, $message, $this->createTypeAlternatives($reference));
460+
461+
if ($r->isInterface()) {
462+
$message .= ' Did you write an implementation of this interface?';
463+
}
460464
}
461465

462466
$message = sprintf('Cannot autowire service "%s": %s %s', $this->currentId, $label, $message);

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,23 @@ public function testSetterInjectionCollisionThrowsException()
703703
$pass->process($container);
704704
}
705705

706+
/**
707+
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
708+
* @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 write an implementation of this interface?
709+
*/
710+
public function testInterfaceWithNoImplementationSuggestToWriteOne()
711+
{
712+
$container = new ContainerBuilder();
713+
714+
$aDefinition = $container->register('my_service', K::class);
715+
$aDefinition->setAutowired(true);
716+
717+
(new AutowireRequiredMethodsPass())->process($container);
718+
719+
$pass = new AutowirePass();
720+
$pass->process($container);
721+
}
722+
706723
/**
707724
* @group legacy
708725
* @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.

src/Symfony/Component/DependencyInjection/Tests/Fixtu A5AC res/includes/autowiring_classes.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ public function __construct(I $i)
9090
}
9191
}
9292

93+
class K
94+
{
95+
public function __construct(IInterface $i)
96+
{
97+
}
98+
}
99+
93100
interface CollisionInterface
94101
{
95102
}

0 commit comments

Comments
 (0)
0