Skip to content

Commit 05c1149

Browse files
committed
Merge branch '6.1' into 6.2
* 6.1: Fix CS Fix CS quote address names if they contain parentheses [FrameworkBundle] Fail gracefully when forms use disabled CSRF [Mime] Fix inline parts when added via attachPart() Fail gracefully when attempting to autowire composite types [VarDumper] Add a test case for nesting intersection and union types Fix #46592 - Ignore getter with required parameters [Serializer] Fix inconsistent behaviour of nullable objects in key/value arrays
2 parents 28c3300 + f11e3a0 commit 05c1149

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

LazyProxy/ProxyHelper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public static function getTypeHint(\ReflectionFunctionAbstract $r, \ReflectionPa
3232
return null;
3333
}
3434

35+
return self::getTypeHintForType($type, $r, $noBuiltin);
36+
}
37+
38+
private static function getTypeHintForType(\ReflectionType $type, \ReflectionFunctionAbstract $r, bool $noBuiltin): ?string
39+
{
3540
$types = [];
3641
$glue = '|';
3742
if ($type instanceof \ReflectionUnionType) {
@@ -46,6 +51,17 @@ public static function getTypeHint(\ReflectionFunctionAbstract $r, \ReflectionPa
4651
}
4752

4853
foreach ($reflectionTypes as $type) {
54+
if ($type instanceof \ReflectionIntersectionType) {
55+
$typeHint = self::getTypeHintForType($type, $r, $noBuiltin);
56+
if (null === $typeHint) {
57+
return null;
58+
}
59+
60+
$types[] = sprintf('(%s)', $typeHint);
61+
62+
continue;
63+
}
64+
4965
if ($type->isBuiltin()) {
5066
if (!$noBuiltin) {
5167
$types[] = $type->getName();

Tests/Compiler/AutowirePassTest.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,6 @@ public function testTypeNotGuessableNoServicesFound()
240240

241241
public function testTypeNotGuessableUnionType()
242242
{
243-
$this->expectException(AutowiringFailedException::class);
244-
$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.');
245243
$container = new ContainerBuilder();
246244

247245
$container->register(CollisionA::class);
@@ -251,6 +249,9 @@ public function testTypeNotGuessableUnionType()
251249
$aDefinition->setAutowired(true);
252250

253251
$pass = new AutowirePass();
252+
253+
$this->expectException(AutowiringFailedException::class);
254+
$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.');
254255
$pass->process($container);
255256
}
256257

@@ -284,7 +285,27 @@ public function testTypeNotGuessableIntersectionType()
284285
$pass = new AutowirePass();
285286

286287
$this->expectException(AutowiringFailedException::class);
287-
$this->expectExceptionMessage('Cannot autowire service "a": argument "$collision" of method "Symfony\Component\DependencyInjection\Tests\Compiler\IntersectionClasses::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\AnotherInterface&Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface" but this class was not found.');
288+
$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.');
289+
$pass->process($container);
290+
}
291+
292+
/**
293+
* @requires PHP 8.2
294+
*/
295+
public function testTypeNotGuessableCompositeType()
296+
{
297+
$container = new ContainerBuilder();
298+
299+
$container->register(CollisionInterface::class);
300+
$container->register(AnotherInterface::class);
301+
302+
$aDefinition = $container->register('a', CompositeTypeClasses::class);
303+
$aDefinition->setAutowired(true);
304+
305+
$pass = new AutowirePass();
306+
307+
$this->expectException(AutowiringFailedException::class);
308+
$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.');
288309
$pass->process($container);
289310
}
290311

@@ -402,6 +423,22 @@ public function testParameterWithNullUnionIsSkipped()
402423
$this->assertNull($definition->getArgument(0));
403424
}
404425

426+
/**
427+
* @requires PHP 8.2
428+
*/
429+
public function testParameterWithNullableIntersectionIsSkipped()
430+
{
431+
$container = new ContainerBuilder();
432+
433+
$optDefinition = $container->register('opt', NullableIntersection::class);
434+
$optDefinition->setAutowired(true);
435+
436+
(new AutowirePass())->process($container);
437+
438+
$definition = $container->getDefinition('opt');
439+
$this->assertNull($definition->getArgument(0));
440+
}
441+
405442
public function testParameterWithNullUnionIsAutowired()
406443
{
407444
$container = new ContainerBuilder();

Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
require __DIR__.'/uniontype_classes.php';
88
require __DIR__.'/autowiring_classes_80.php';
99
require __DIR__.'/intersectiontype_classes.php';
10+
if (\PHP_VERSION_ID >= 80200) {
11+
require __DIR__.'/compositetype_classes.php';
12+
}
1013

1114
class Foo
1215
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
4+
5+
interface YetAnotherInterface
6+
{
7+
}
8+
9+
class CompositeTypeClasses
10+
{
11+
public function __construct((CollisionInterface&AnotherInterface)|YetAnotherInterface $collision)
12+
{
13+
}
14+
}
15+
16+
class NullableIntersection
17+
{
18+
public function __construct((CollisionInterface&AnotherInterface)|null $a)
19+
{
20+
}
21+
}

0 commit comments

Comments
 (0)