8000 bug #26595 [DI] Do not suggest writing an implementation when multipl… · symfony/symfony@99df7cb · GitHub
[go: up one dir, main page]

Skip to content

Commit 99df7cb

Browse files
bug #26595 [DI] Do not suggest writing an implementation when multiple exist (chalasr)
This PR was merged into the 3.4 branch. Discussion ---------- [DI] Do not suggest writing an implementation when multiple exist | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Before: > Cannot autowire service "App\Decorator2": argument "$inner" of method "__construct()" references interface "App\SomeInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "App\Decorator1", "App\Decorator2", "App\Original". Did you create a class that implements this interface? After: > Cannot autowire service "App\Decorator2": argument "$inner" of method "__construct()" references interface "App\SomeInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "App\Decorator1", "App\Decorator2", "App\Original". Commits ------- 1ffdb50 [DI] Do not suggest writing an implementation when multiple exist
2 parents 3d5f04c + 1ffdb50 commit 99df7cb

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,11 @@ private function createTypeNotFoundMessage(TypedReference $reference, $label)
462462

463463
$message = sprintf('has type "%s" but this class %s.', $type, $parentMsg ? sprintf('is missing a parent class (%s)', $parentMsg) : 'was not found');
464464
} else {
465+
$alternatives = $this->createTypeAlternatives($reference);
465466
$message = $this->container->has($type) ? 'this service is abstract' : 'no such service exists';
466-
$message = sprintf('references %s "%s" but %s.%s', $r->isInterface() ? 'interface' : 'class', $type, $message, $this->createTypeAlternatives($reference));
467+
$message = sprintf('references %s "%s" but %s.%s', $r->isInterface() ? 'interface' : 'class', $type, $message, $alternatives);
467468

468-
if ($r->isInterface()) {
469+
if ($r->isInterface() && !$alternatives) {
469470
$message .= ' Did you create a class that implements this interface?';
470471
}
471472
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
1818
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
1919
use Symfony\Component\DependencyInjection\ContainerBuilder;
20+
use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;
2021
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2122
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2223
use Symfony\Component\DependencyInjection\Reference;
@@ -684,10 +685,6 @@ public function testIgnoreServiceWithClassNotExisting()
684685
$this->assertTrue($container->hasDefinition('bar'));
685686
}
686687

687-
/**
688-
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
689-
* @expectedExceptionMessage Cannot autowire service "setter_injection_collision": argument "$collision" of method "Symfony\Component\DependencyInjection\Tests\Compiler\SetterInjectionCollision::setMultipleInstancesForOneArg()" references interface "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "c1", "c2".
690-
*/
691688
public function testSetterInjectionCollisionThrowsException()
692689
{
693690
$container = new ContainerBuilder();
@@ -700,7 +697,14 @@ public function testSetterInjectionCollisionThrowsException()
700697
(new AutowireRequiredMethodsPass())->process($container);
701698

702699
$pass = new AutowirePass();
703-
$pass->process($container);
700+
701+
try {
702+
$pass->process($container);
703+
} catch (AutowiringFailedException $e) {
704+
}
705+
706+
$this->assertNotNull($e);
707+
$this->assertSame('Cannot autowire service "setter_injection_collision": argument "$collision" of method "Symfony\Component\DependencyInjection\Tests\Compiler\SetterInjectionCollision::setMultipleInstancesForOneArg()" references interface "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "c1", "c2".', $e->getMessage());
704708
}
705709

706710
/**

0 commit comments

Comments
 (0)
0