8000 bug #25155 [DependencyInjection] Detect case mismatch in autowiring (… · symfony/symfony@ee71106 · GitHub
[go: up one dir, main page]

Skip to content

Commit ee71106

Browse files
bug #25155 [DependencyInjection] Detect case mismatch in autowiring (Simperfit, sroze)
This PR was merged into the 4.0 branch. Discussion ---------- [DependencyInjection] Detect case mismatch in autowiring | Q | A | ------------- | --- | Branch? | 4.0 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25122 | License | MIT | Doc PR | ø If there is a case-sensitive typo in the service injection, this will suggest the non-typoed version. Commits ------- 407f132 Test the suggestion of already registered services decaf23 [DependencyInjection] Add more information to the message when passing miss matching class.
2 parents ff87b4c + 407f132 commit ee71106

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,10 @@ private function createTypeAlternatives(TypedReference $reference)
337337
return ' '.$message;
338338
}
339339

340-
if (isset($this->ambiguousServiceTypes[$type])) {
340+
$servicesAndAliases = $this->container->getServiceIds();
341+
if (!$this->container->has($type) && false !== $key = array_search(strtolower($type), array_map('strtolower', $servicesAndAliases))) {
342+
return sprintf(' Did you mean "%s"?', $servicesAndAliases[$key]);
343+
} elseif (isset($this->ambiguousServiceTypes[$type])) {
341344
$message = sprintf('one of these existing services: "%s"', implode('", "', $this->ambiguousServiceTypes[$type]));
342345
} elseif (isset($this->types[$type])) {
343346
$message = sprintf('the existing "%s" service', $this->types[$type]);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,24 @@ public function provideNotWireableCalls()
685685
);
686686
}
687687

688+
/**
689+
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
690+
* @expectedExceptionMessage Cannot autowire service "foo": argument "$sam" of method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setNotAutowireableBecauseOfATypo()" references class "Symfony\Component\DependencyInjection\Tests\Compiler\lesTilleuls" but no such service exists. Did you mean "Symfony\Component\DependencyInjection\Tests\Compiler\LesTilleuls"?
691+
*/
692+
public function testSuggestRegisteredServicesWithSimilarCase()
693+
{
694+
$container = new ContainerBuilder();
695+
696+
$container->register(LesTilleuls::class, LesTilleuls::class);
697+
$container->register('foo', NotWireable::class)->setAutowired(true)
698+
->addMethodCall('setNotAutowireableBecauseOfATypo', array())
699+
;
700+
701+
(new ResolveClassPass())->process($container);
702+
(new AutowireRequiredMethodsPass())->process($container);
703+
(new AutowirePass())->process($container);
704+
}
705+
688706
/**
689707
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
690708
* @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.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ public function setNotAutowireable(NotARealClass $n)
299299
{
300300
}
301301

302+
public function setNotAutowireableBecauseOfATypo(lesTilleuls $sam)
303+
{
304+
}
305+
302306
public function setBar()
303307
{
304308
}

0 commit comments

Comments
 (0)
0