Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite4f854d

Browse files
committed
minor#22451 [DI] Enhance auto-registration failure message (nicolas-grekas)
This PR was merged into the 3.3-dev branch.Discussion----------[DI] Enhance auto-registration failure message| Q | A| ------------- | ---| Branch? | 3.3| Bug fix? | no| New feature? | no| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets |#22306 (comment)| License | MIT| Doc PR | -Commits-------b23d5da [DI] Enhance auto-registration failure message
2 parents9296a10 +b23d5da commite4f854d

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(newTypedReference($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 =newTypedReference($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
returnnewTypedReference($argumentId,$type);
425425
}
426426

427-
privatefunctioncreateTypeNotFoundMessage($type,$label)
427+
privatefunctioncreateTypeNotFoundMessage(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-
privatefunctioncreateTypeAlternatives($type)
446+
privatefunctioncreateTypeAlternatives(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
returnarray(
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+
publicfunctionsetDifferentNamespace(\stdClass$n)
1024+
{
1025+
}
1026+
10221027
publicfunctionsetOptionalNoTypeHint($foo =null)
10231028
{
10241029
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp