Skip to content

Commit 7a5e65d

Browse files
committed
Merge branch '4.4' into 5.4
* 4.4: 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
2 parents 2062f14 + 9cc5694 commit 7a5e65d

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
@@ -244,8 +244,6 @@ public function testTypeNotGuessableNoServicesFound()
244244
*/
245245
public function testTypeNotGuessableUnionType()
246246
{
247-
$this->expectException(AutowiringFailedException::class);
248-
$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.');
249247
$container = new ContainerBuilder();
250248

251249
$container->register(CollisionA::class);
@@ -255,6 +253,9 @@ public function testTypeNotGuessableUnionType()
255253
$aDefinition->setAutowired(true);
256254

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

@@ -294,7 +295,27 @@ public function testTypeNotGuessableIntersectionType()
294295
$pass = new AutowirePass();
295296

296297
$this->expectException(AutowiringFailedException::class);
297-
$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.');
298+
$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.');
299+
$pass->process($container);
300+
}
301+
302+
/**
303+
* @requires PHP 8.2
304+
*/
305+
public function testTypeNotGuessableCompositeType()
306+
{
307+
$container = new ContainerBuilder();
308+
309+
$container->register(CollisionInterface::class);
310+
$container->register(AnotherInterface::class);
311+
312+
$aDefinition = $container->register('a', CompositeTypeClasses::class);
313+
$aDefinition->setAutowired(true);
314+
315+
$pass = new AutowirePass();
316+
317+
$this->expectException(AutowiringFailedException::class);
318+
$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.');
298319
$pass->process($container);
299320
}
300321

@@ -418,6 +439,22 @@ public function testParameterWithNullUnionIsSkipped()
418439
$this->assertNull($definition->getArgument(0));
419440
}
420441

442+
/**
443+
* @requires PHP 8.2
444+
*/
445+
public function testParameterWithNullableIntersectionIsSkipped()
446+
{
447+
$container = new ContainerBuilder();
448+
449+
$optDefinition = $container->register('opt', NullableIntersection::class);
450+
$optDefinition->setAutowired(true);
451+
452+
(new AutowirePass())->process($container);
453+
454+
$definition = $container->getDefinition('opt');
455+
$this->assertNull($definition->getArgument(0));
456+
}
457+
421458
/**
422459
* @requires PHP 8
423460
*/

Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
if (\PHP_VERSION_ID >= 80100) {
1212
require __DIR__.'/intersectiontype_classes.php';
1313
}
14+
if (\PHP_VERSION_ID >= 80200) {
15+
require __DIR__.'/compositetype_classes.php';
16+
}
1417

1518
class Foo
1619
{
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)