Skip to content

Commit 376ee1c

Browse files
committed
[DI] Do not suggest writing an implementation when multiple exist
1 parent ae16d77 commit 376ee1c

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,11 @@ private function createTypeNotFoundMessage(TypedReference $reference, $label)
455455

456456
$message = sprintf('has type "%s" but this class %s.', $type, $parentMsg ? sprintf('is missing a parent class (%s)', $parentMsg) : 'was not found');
457457
} else {
458+
$alternatives = $this->createTypeAlternatives($reference);
458459
$message = $this->container->has($type) ? 'this service is abstract' : 'no such service exists';
459460
$message = sprintf('references %s "%s" but %s.%s', $r->isInterface() ? 'interface' : 'class', $type, $message, $this->createTypeAlternatives($reference));
460461

461-
if ($r->isInterface()) {
462+
if ($r->isInterface() && !$alternatives) {
462463
$message .= ' Did you create a class that implements this interface?';
463464
}
464465
}

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)