From 75af86632bea5ed26596fa329bdeca7db9126910 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Tue, 14 Feb 2023 21:51:46 +0100 Subject: [PATCH] [DependencyInjection] Fix autowire attribute with nullable parameters --- .../DependencyInjection/Compiler/AutowirePass.php | 10 ++-------- .../Tests/Compiler/AutowirePassTest.php | 14 ++++++++------ .../Fixtures/includes/autowiring_classes_80.php | 2 ++ 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php index f593ecb181c0b..96e1169e81997 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php @@ -276,23 +276,17 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a continue; } - $type = ProxyHelper::exportType($parameter, true); - if ($checkAttributes) { foreach ($parameter->getAttributes() as $attribute) { if (\in_array($attribute->getName(), [TaggedIterator::class, TaggedLocator::class, Autowire::class, MapDecorated::class], true)) { $arguments[$index] = $this->processAttribute($attribute->newInstance(), $parameter->allowsNull()); - break; + continue 2; } } - - if ('' !== ($arguments[$index] ?? '')) { - continue; - } } - if (!$type) { + if (!$type = ProxyHelper::exportType($parameter, true)) { if (isset($arguments[$index])) { continue; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php index 171169ae7c34a..fbe6adb25ee71 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php @@ -1185,21 +1185,23 @@ public function testAutowireAttribute() $container->register('some.id', \stdClass::class); $container->setParameter('some.parameter', 'foo'); + $container->setParameter('null.parameter', null); (new ResolveClassPass())->process($container); (new AutowirePass())->process($container); $definition = $container->getDefinition(AutowireAttribute::class); - $this->assertCount(8, $definition->getArguments()); + $this->assertCount(9, $definition->getArguments()); $this->assertEquals(new Reference('some.id'), $definition->getArgument(0)); $this->assertEquals(new Expression("parameter('some.parameter')"), $definition->getArgument(1)); $this->assertSame('foo/bar', $definition->getArgument(2)); - $this->assertEquals(new Reference('some.id'), $definition->getArgument(3)); - $this->assertEquals(new Expression("parameter('some.parameter')"), $definition->getArgument(4)); - $this->assertSame('bar', $definition->getArgument(5)); - $this->assertSame('@bar', $definition->getArgument(6)); - $this->assertEquals(new Reference('invalid.id', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(7)); + $this->assertNull($definition->getArgument(3)); + $this->assertEquals(new Reference('some.id'), $definition->getArgument(4)); + $this->assertEquals(new Expression("parameter('some.parameter')"), $definition->getArgument(5)); + $this->assertSame('bar', $definition->getArgument(6)); + $this->assertSame('@bar', $definition->getArgument(7)); + $this->assertEquals(new Reference('invalid.id', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(8)); $container->compile(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php index c1c772b684a48..30a575ff3d901 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php @@ -40,6 +40,8 @@ public function __construct( public string $expression, #[Autowire(value: '%some.parameter%/bar')] public string $value, + #[Autowire(value: '%null.parameter%')] + public ?string $nullableValue, #[Autowire('@some.id')] public \stdClass $serviceAsValue, #[Autowire("@=parameter('some.parameter')")]