8000 [DI] Enhance auto-registration failure message · symfony/symfony@b23d5da · GitHub
[go: up one dir, main page]

Skip to content

Commit b23d5da

Browse files
[DI] Enhance auto-registration failure message
1 parent ac5cfee commit b23d5da

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected function processValue($value, $isRoot = false)
8080
if ($ref = $this->getAutowiredReference($value)) {
8181
return $ref;
8282
}
83-
$this->container->log($this, $this->createTypeNotFoundMessage($value->getType(), 'it'));
83+
$this->container->log($this, $this->createTypeNotFoundMessage($value, 'it'));
8484
}
8585
$value = parent::processValue($value, $isRoot);
8686

@@ -240,8 +240,8 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
240240
continue;
241241
}
242242

243-
if (!$value = $this->getAutowiredReference(new TypedReference($type, $type, !$parameter->isOptional() ? $class : ''))) {
244-
$failureMessage = $this->createTypeNotFoundMessage($type, sprintf('argument "$%s" of method "%s()"', $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));
243+
if (!$value = $this->getAutowiredReference($ref = new TypedReference($type, $type, !$parameter->isOptional() ? $class : ''))) {
244+
$failureMessage = $this->createTypeNotFoundMessage($ref, sprintf('argument "$%s" of method "%s()"', $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));
245245

246246
if ($parameter->isDefaultValueAvailable()) {
247247
$value = $parameter->getDefaultValue();
@@ -424,13 +424,13 @@ private function createAutowiredDefinition($type)
424424
return new TypedReference($argumentId, $type);
425425
}
426426

427-
private function createTypeNotFoundMessage($type, $label)
427+
private function createTypeNotFoundMessage(TypedReference $reference, $label)
428428
{
429-
if (!$r = $this->container->getReflectionClass($type, true)) {
429+
if (!$r = $this->container->getReflectionClass($type = $reference->getType(), true)) {
430430
$message = sprintf('has type "%s" but this class does not exist.', $type);
431431
} else {
432432
$message = $this->container->has($type) ? 'this service is abstract' : 'no such service exists';
433-
$message = sprintf('references %s "%s" but %s.%s', $r->isInterface() ? 'interface' : 'class', $type, $message, $this->createTypeAlternatives($type));
433+
$message = sprintf('references %s "%s" but %s.%s', $r->isInterface() ? 'interface' : 'class', $type, $message, $this->createTypeAlternatives($reference));
434434
}
435435

436436
$message = sprintf('Cannot autowire service "%s": %s %s', $this->currentId, $label, $message);
@@ -443,12 +443,14 @@ private function createTypeNotFoundMessage($type, $label)
443443
return $message;
444444
}
445445

446-
private function createTypeAlternatives($type)
446+
private function createTypeAlternatives(TypedReference $reference)
447447
{
448-
if (isset($this->ambiguousServiceTypes[$type])) {
448+
if (isset($this->ambiguousServiceTypes[$type = $reference->getType()])) {
449449
$message = sprintf('one of these existing services: "%s"', implode('", "', $this->ambiguousServiceTypes[$type]));
450450
} elseif (isset($this->types[$type])) {
451451
$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.';
452454
} else {
453455
return;
454456
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ public function provideNotWireableCalls()
694694
{
695695
return array(
696696
array('setNotAutowireable', 'Cannot autowire service "foo": argument "$n" of method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setNotAutowireable()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass" but this class does not exist.'),
697+
array('setDifferentNamespace', 'Cannot autowire service "foo": argument "$n" of method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setDifferentNamespace()" references class "stdClass" but no such service exists. It cannot be auto-registered because it is from a different root namespace.'),
697698
array(null, 'Cannot autowire service "foo": method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setProtectedMethod()" must be public.'),
698699
);
699700
}
@@ -1019,6 +1020,10 @@ public function setOptionalNotAutowireable(NotARealClass $n = null)
10191020
{
10201021
}
10211022

1023+
public function setDifferentNamespace(\stdClass $n)
1024+
{
1025+
}
1026+
10221027
public function setOptionalNoTypeHint($foo = null)
10231028
{
10241029
}

0 commit comments

Comments
 (0)
0