Skip to content

Commit 36a9a6a

Browse files
committed
[DI] Show the right class autowired when providing a non-existing class in constructor
1 parent e02da2a commit 36a9a6a

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,14 @@ private function createTypeNotFoundMessageCallback(TypedReference $reference, $l
379379
$container->setAliases($this->container->getAliases());
380380
$container->setDefinitions($this->container->getDefinitions());
381381
$container->setResourceTracking(false);
382+
$currentId = $this->currentId;
382383

383-
return function () use ($container, $reference, $label) {
384-
return $this->createTypeNotFoundMessage($container, $reference, $label);
384+
return function () use ($container, $reference, $label, $currentId) {
385+
return $this->createTypeNotFoundMessage($container, $reference, $label, $currentId);
385386
};
386387
}
387388

388-
private function createTypeNotFoundMessage(ContainerBuilder $container, TypedReference $reference, $label)
389+
private function createTypeNotFoundMessage(ContainerBuilder $container, TypedReference $reference, $label, string $currentId)
389390
{
390391
if (!$r = $container->getReflectionClass($type = $reference->getType(), false)) {
391392
// either $type does not exist or a parent class does not exist
@@ -409,7 +410,7 @@ private function createTypeNotFoundMessage(ContainerBuilder $container, TypedRef
409410
}
410411
}
411412

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

414415
if (null !== $this->lastFailure) {
415416
$message = $this->lastFailure."\n".$message;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ public function testProcess()
5050
$this->assertEquals(Foo::class, (string) $container->getDefinition('bar')->getArgument(0));
5151
}
5252

53+
/**
54+
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
55+
* @expectedExceptionMessage Cannot autowire service "Symfony\Component\DependencyInjection\Tests\CompilerEslaAction": argument "$notExisting" of method "Symfony\Component\DependencyInjection\Tests\Compiler\ElsaAction::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\NotExisting" but this class was not found.
56+
*/
57+
public function testProcessNotExisintActionParam()
58+
{
59+
$container = new ContainerBuilder();
60+
61+
$container->register(Foo::class);
62+
$barDefinition = $container->register(__NAMESPACE__.'EslaAction', __NAMESPACE__.'\ElsaAction');
63+
$barDefinition->setAutowired(true);
64+
65+
(new ResolveClassPass())->process($container);
66+
(new AutowirePass())->process($container);
67+
}
68+
5369
public function testProcessVariadic()
5470
{
5571
$container = new ContainerBuilder();

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,10 @@ public function __construct(LoggerInterface $logger, DecoratorInterface $decorat
419419
{
420420
}
421421
}
422+
423+
final class ElsaAction
424+
{
425+
public function __construct(NotExisting $notExisting)
426+
{
427+
}
428+
}

0 commit comments

Comments
 (0)