Skip to content

Commit f11e3a0

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: 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 9aa3c0f + a748096 commit f11e3a0

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
@@ -243,8 +243,6 @@ public function testTypeNotGuessableNoServicesFound()
243243

244244
public function testTypeNotGuessableUnionType()
245245
{
246-
$this->expectException(AutowiringFailedException::class);
247-
$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.');
248246
$container = new ContainerBuilder();
249247

250248
$container->register(CollisionA::class);
@@ -254,6 +252,9 @@ public function testTypeNotGuessableUnionType()
254252
$aDefinition->setAutowired(true);
255253

256254
$pass = new AutowirePass();
255+
256+
$this->expectException(AutowiringFailedException::class);
257+
$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.');
257258
$pass->process($container);
258259
}
259260

@@ -287,7 +288,27 @@ public function testTypeNotGuessableIntersectionType()
287288
$pass = new AutowirePass();
288289

289290
$this->expectException(AutowiringFailedException::class);
290-
$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.');
291+
$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.');
292+
$pass->process($container);
293+
}
294+
295+
/**
296+
* @requires PHP 8.2
297+
*/
298+
public function testTypeNotGuessableCompositeType()
299+
{
300+
$container = new ContainerBuilder();
301+
302+
$container->register(CollisionInterface::class);
303+
$container->register(AnotherInterface::class);
304+
305+
$aDefinition = $container->register('a', CompositeTypeClasses::class);
306+
$aDefinition->setAutowired(true);
307+
308+
$pass = new AutowirePass();
309+
310+
$this->expectException(AutowiringFailedException::class);
311+
$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.');
291312
$pass->process($container);
292313
}
293314

@@ -405,6 +426,22 @@ public function testParameterWithNullUnionIsSkipped()
405426
$this->assertNull($definition->getArgument(0));
406427
}
407428

429+
/**
430+
* @requires PHP 8.2
431+
*/
432+
public function testParameterWithNullableIntersectionIsSkipped()
433+
{
434+
$container = new ContainerBuilder();
435+
436+
$optDefinition = $container->register('opt', NullableIntersection::class);
437+
$optDefinition->setAutowired(true);
438+
439+
(new AutowirePass())->process($container);
440+
441+
$definition = $container->getDefinition('opt');
442+
$this->assertNull($definition->getArgument(0));
443+
}
444+
408445
public function testParameterWithNullUnionIsAutowired()
409446
{
410447
$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)