Skip to content

Commit a748096

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: 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 e3d08ee + 7a5e65d commit a748096

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

@@ -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

@@ -408,6 +429,22 @@ public function testParameterWithNullUnionIsSkipped()
408429
$this->assertNull($definition->getArgument(0));
409430
}
410431

432+
/**
433+
* @requires PHP 8.2
434+
*/
435+
public function testParameterWithNullableIntersectionIsSkipped()
436+
{
437+
$container = new ContainerBuilder();
438+
439+
$optDefinition = $container->register('opt', NullableIntersection::class);
440+
$optDefinition->setAutowired(true);
441+
442+
(new AutowirePass())->process($container);
443+
444+
$definition = $container->getDefinition('opt');
445+
$this->assertNull($definition->getArgument(0));
446+
}
447+
411448
public function testParameterWithNullUnionIsAutowired()
412449
{
413450
$container = new ContainerBuilder();

Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
if (\PHP_VERSION_ID >= 80100) {
1010
require __DIR__.'/intersectiontype_classes.php';
1111
}
12+
if (\PHP_VERSION_ID >= 80200) {
13+
require __DIR__.'/compositetype_classes.php';
14+
}
1215

1316
class Foo
1417
{
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)