Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e5bb8fa

Browse files
committed
minor #22648 Improving autowire exception when you type-hint a class & alias is available (weaverryan)
This PR was merged into the 3.3-dev branch. Discussion ---------- Improving autowire exception when you type-hint a class & alias is available | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | n/a Suppose you type-hint a class instead of the correct interface (which is aliased): ```php public function __construct(Logger $logger) ``` Current error: > Cannot autowire service "AppBundle\Security\PostVoter": argument "$logger" of method "__construct()" references class "Symfony\Bridge\Monolog\Logger" but no such service exists. You should maybe alias this class to one of these existing services: "monolog.logger", "monolog.logger.request", "monolog.logger.console", "monolog.logger.cache", "monolog.logger.templating", "monolog.logger.translation", "monolog.logger.profiler", "monolog.logger.php", "monolog.logger.event", "monolog.logger.router", "monolog.logger.security", "monolog.logger.doctrine"; or type-hint against interface "Psr\Log\LoggerInterface" instead. New error: > Cannot autowire service "AppBundle\Security\PostVoter": argument "$logger" of method "__construct()" references class "Symfony\Bridge\Monolog\Logger" but no such service exists. Try changing the type-hint to "Psr\Log\LoggerInterface" instead. The correct "suggestion" was always there (at the end). I think if there is already a definitive alias available for your type-hint, we can say that they are simply using the wrong type-hint and suggest *only* that. Commits ------- 5a3c156 Improving autowire exception when you type-hint a class and there is an interface alias available
2 parents fb02140 + 5a3c156 commit e5bb8fa

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -445,35 +445,38 @@ private function createTypeNotFoundMessage(TypedReference $reference, $label)
445445

446446
private function createTypeAlternatives(TypedReference $reference)
447447
{
448-
if (isset($this->ambiguousServiceTypes[$type = $reference->getType()])) {
449-
$message = sprintf('one of these existing services: "%s"', implode('", "', $this->ambiguousServiceTypes[$type]));
450-
} elseif (isset($this->types[$type])) {
451-
$message = sprintf('the existing "%s" service', $this->types[$type]);
452-
} elseif ($reference->getRequiringClass() && !$reference->canBeAutoregistered()) {
453-
return ' It cannot be auto-registered because it is from a different root namespace.';
454-
} else {
455-
return;
456-
}
457-
$message = sprintf(' You should maybe alias this %s to %s', class_exists($type, false) ? 'class' : 'interface', $message);
448+
$type = $reference->getType();
458449
$aliases = array();
459-
460450
foreach (class_parents($type) + class_implements($type) as $parent) {
461451
if ($this->container->has($parent) && !$this->container->findDefinition($parent)->isAbstract()) {
462452
$aliases[] = $parent;
463453
}
464454
}
465455

466456
if (1 < $len = count($aliases)) {
467-
$message .= '; or type-hint against one of its parents: ';
457+
$message = ' Try changing the type-hint to one of its parents: ';
468458
for ($i = 0, --$len; $i < $len; ++$i) {
469459
$message .= sprintf('%s "%s", ', class_exists($aliases[$i], false) ? 'class' : 'interface', $aliases[$i]);
470460
}
471-
$message .= sprintf('or %s "%s"', class_exists($aliases[$i], false) ? 'class' : 'interface', $aliases[$i]);
472-
} elseif ($aliases) {
473-
$message .= sprintf('; or type-hint against %s "%s" instead', class_exists($aliases[0], false) ? 'class' : 'interface', $aliases[0]);
461+
$message .= sprintf('or %s "%s".', class_exists($aliases[$i], false) ? 'class' : 'interface', $aliases[$i]);
462+
463+
return $message;
464+
}
465+
if ($aliases) {
466+
return sprintf(' Try changing the type-hint to "%s" instead.', $aliases[0]);
467+
}
468+
469+
if (isset($this->ambiguousServiceTypes[$type])) {
470+
$message = sprintf('one of these existing services: "%s"', implode('", "', $this->ambiguousServiceTypes[$type]));
471+
} elseif (isset($this->types[$type])) {
472+
$message = sprintf('the existing "%s" service', $this->types[$type]);
473+
} elseif ($reference->getRequiringClass() && !$reference->canBeAutoregistered()) {
474+
return ' It cannot be auto-registered because it is from a different root namespace.';
475+
} else {
476+
return;
474477
}
475478

476-
return $message.'.';
479+
return sprintf(' You should maybe alias this %s to %s.', class_exists($type, false) ? 'class' : 'interface', $message);
477480
}
478481

479482
/**

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ public function provideNotWireableCalls()
703703
* @group legacy
704704
* @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 "i" service to "Symfony\Component\DependencyInjection\Tests\Compiler\I" instead.
705705
* @expectedExceptionInSymfony4 \Symfony\Component\DependencyInjection\Exception\RuntimeException
706-
* @expectedExceptionMessageInSymfony4 Cannot autowire service "j": argument "$i" of method "Symfony\Component\DependencyInjection\Tests\Compiler\J::__construct()" references class "Symfony\Component\DependencyInjection\Tests\Compiler\I" but no such service exists. You should maybe alias this class to the existing "i" service; or type-hint against interface "Symfony\Component\DependencyInjection\Tests\Compiler\IInterface" instead.
706+
* @expectedExceptionMessageInSymfony4 Cannot autowire service "j": argument "$i" of method "Symfony\Component\DependencyInjection\Tests\Compiler\J::__construct()" references class "Symfony\Component\DependencyInjection\Tests\Compiler\I" but no such service exists. Try changing the type-hint to "Symfony\Component\DependencyInjection\Tests\Compiler\IInterface" instead.
707707
*/
708708
public function testByIdAlternative()
709709
{
@@ -717,6 +717,45 @@ public function testByIdAlternative()
717717
$pass = new AutowirePass();
718718
$pass->process($container);
719719
}
720+
721+
/**
722+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
723+
* @expectedExceptionMessage Cannot autowire service "j": argument "$i" of method "Symfony\Component\DependencyInjection\Tests\Compiler\J::__construct()" references class "Symfony\Component\DependencyInjection\Tests\Compiler\I" but no such service exists. Try changing the type-hint to "Symfony\Component\DependencyInjection\Tests\Compiler\IInterface" instead.
724+
*/
725+
public function testExceptionWhenAliasExists()
726+
{
727+
$container = new ContainerBuilder();
728+
729+
// multiple I services... but there *is* IInterface available
730+
$container->setAlias(IInterface::class, 'i');
731+
$container->register('i', I::class);
732+
$container->register('i2', I::class);
733+
// J type-hints against I concretely
734+
$container->register('j', J::class)
735+
->setAutowired(true);
736+
737+
$pass = new AutowirePass();
738+
$pass->process($container);
739+
}
740+
741+
/**
742+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
743+
* @expectedExceptionMessage Cannot autowire service "j": argument "$i" of method "Symfony\Component\DependencyInjection\Tests\Compiler\J::__construct()" references class "Symfony\Component\DependencyInjection\Tests\Compiler\I" but no such service exists. You should maybe alias this class to one of these existing services: "i", "i2".
744+
*/
745+
public function testExceptionWhenAliasDoesNotExist()
746+
{
747+
$container = new ContainerBuilder();
748+
749+
// multiple I instances... but no IInterface alias
750+
$container->register('i', I::class);
751+
$container->register('i2', I::class);
752+
// J type-hints against I concretely
753+
$container->register('j', J::class)
754+
->setAutowired(true);
755+
756+
$pass = new AutowirePass();
757+
$pass->process($container);
758+
}
720759
}
721760

722761
class Foo

0 commit comments

Comments
 (0)