You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bug #46973 [DependencyInjection] Fail gracefully when attempting to autowire composite types (derrabus)
This PR was merged into the 4.4 branch.
Discussion
----------
[DependencyInjection] Fail gracefully when attempting to autowire composite types
| Q | A
| ------------- | ---
| Branch? | 4.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | Part of #44282
| License | MIT
| Doc PR | N/A
Symfony 4.4 does not support autowiring union types. Unfortunately, we run into a fatal error when autowiring is attempted for a parameter with an intersection type nested into a union (`(A&B)|C`). Ironically, the error occurs while we try to generate a nice exception message.
This PR fixes this, so the developer gets a nice exception message about the parameter that cannot be autowired.
For nullable unions however, `null` is injected. This already was the case for `A|null` and it works out of the box for `(A&B)|null`. I've added a test case that covers this case.
Commits
-------
2543091 Fail gracefully when attempting to autowire composite types
$this->expectExceptionMessage('Cannot autowire service "a": argument "$collision" of method "Symfony\Component\DependencyInjection\Tests\Compiler\UnionClasses::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionA|Symfony\Component\DependencyInjection\Tests\Compiler\CollisionB" but this class was not found.');
246
244
$container = newContainerBuilder();
247
245
248
246
$container->register(CollisionA::class);
@@ -252,6 +250,9 @@ public function testTypeNotGuessableUnionType()
$this->expectExceptionMessage('Cannot autowire service "a": argument "$collision" of method "Symfony\Component\DependencyInjection\Tests\Compiler\UnionClasses::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionA|Symfony\Component\DependencyInjection\Tests\Compiler\CollisionB" but this class was not found.');
255
256
$pass->process($container);
256
257
}
257
258
@@ -260,17 +261,38 @@ public function testTypeNotGuessableUnionType()
$this->expectExceptionMessage('Cannot autowire service "a": argument "$collision" of method "Symfony\Component\DependencyInjection\Tests\Compiler\IntersectionClasses::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface&Symfony\Component\DependencyInjection\Tests\Compiler\AnotherInterface" but this class was not found.');
$this->expectExceptionMessage('Cannot autowire service "a": argument "$collision" of method "Symfony\Component\DependencyInjection\Tests\Compiler\CompositeTypeClasses::__construct()" has type "(Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface&Symfony\Component\DependencyInjection\Tests\Compiler\AnotherInterface)|Symfony\Component\DependencyInjection\Tests\Compiler\YetAnotherInterface" but this class was not found.');
274
296
$pass->process($container);
275
297
}
276
298
@@ -373,6 +395,22 @@ public function testParameterWithNullUnionIsSkipped()
0 commit comments