8000 bug #25547 [DX][DependencyInjection] Suggest to write an implementati… · symfony/symfony@0422471 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0422471

Browse files
bug #25547 [DX][DependencyInjection] Suggest to write an implementation if the interface cannot be autowired (sroze)
This PR was merged into the 3.4 branch. Discussion ---------- [DX][DependencyInjection] Suggest to write an implementation if the interface cannot be autowired | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ø | License | MIT | Doc PR | ø This would add a hint for the developers when the interface cannot be wired. This suggests creating the implementation of the interface. **Note:** this is 3.4 because I believe DX should be treated as bugs. **Note 2:** fabbot issue is false positive Commits ------- 961e3e7 Suggest to write an implementation if the interface cannot be autowired
2 parents 3b8ca1a + 961e3e7 commit 0422471

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 create a class that implements 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 create a class that implements 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/Fixtures/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