From ac3ac747be8a57876ad01e9c1006248f64e79f25 Mon Sep 17 00:00:00 2001 From: Steven Renaux Date: Thu, 24 Apr 2025 23:21:16 +0200 Subject: [PATCH] Fix ServiceSubscriberTrait and ServiceMethodsSubscriberTrait for nullable service --- .../Service/ServiceMethodsSubscriberTrait.php | 2 +- .../Contracts/Service/ServiceSubscriberTrait.php | 2 +- .../Contracts/Tests/Service/LegacyTestService.php | 12 +++++++++++- .../Service/ServiceMethodsSubscriberTraitTest.php | 15 +++++++++++++-- .../Tests/Service/ServiceSubscriberTraitTest.php | 5 +++-- 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Contracts/Service/ServiceMethodsSubscriberTrait.php b/src/Symfony/Contracts/Service/ServiceMethodsSubscriberTrait.php index 2c4c274f725d9..844be8907744b 100644 --- a/src/Symfony/Contracts/Service/ServiceMethodsSubscriberTrait.php +++ b/src/Symfony/Contracts/Service/ServiceMethodsSubscriberTrait.php @@ -53,7 +53,7 @@ public static function getSubscribedServices(): array $attribute = $attribute->newInstance(); $attribute->key ??= self::class.'::'.$method->name; $attribute->type ??= $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType; - $attribute->nullable = $returnType->allowsNull(); + $attribute->nullable = $attribute->nullable ?: $returnType->allowsNull(); if ($attribute->attributes) { $services[] = $attribute; diff --git a/src/Symfony/Contracts/Service/ServiceSubscriberTrait.php b/src/Symfony/Contracts/Service/ServiceSubscriberTrait.php index f22a303b163d5..ed4cec044a831 100644 --- a/src/Symfony/Contracts/Service/ServiceSubscriberTrait.php +++ b/src/Symfony/Contracts/Service/ServiceSubscriberTrait.php @@ -57,7 +57,7 @@ public static function getSubscribedServices(): array $attribute = $attribute->newInstance(); $attribute->key ??= self::class.'::'.$method->name; $attribute->type ??= $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType; - $attribute->nullable = $returnType->allowsNull(); + $attribute->nullable = $attribute->nullable ?: $returnType->allowsNull(); if ($attribute->attributes) { $services[] = $attribute; diff --git a/src/Symfony/Contracts/Tests/Service/LegacyTestService.php b/src/Symfony/Contracts/Tests/Service/LegacyTestService.php index 760c8efec849f..a6e2d486c7954 100644 --- a/src/Symfony/Contracts/Tests/Service/LegacyTestService.php +++ b/src/Symfony/Contracts/Tests/Service/LegacyTestService.php @@ -41,8 +41,18 @@ public function aService(): Service2 return $this->container->get(__METHOD__); } + #[SubscribedService(nullable: true)] + public function nullableInAttribute(): Service2 + { + if (!$this->container->has(__METHOD__) ) { + throw new \LogicException(); + } + + return $this->container->get(__METHOD__); + } + #[SubscribedService] - public function nullableService(): ?Service2 + public function nullableReturnType(): ?Service2 { return $this->container->get(__METHOD__); } diff --git a/src/Symfony/Contracts/Tests/Service/ServiceMethodsSubscriberTraitTest.php b/src/Symfony/Contracts/Tests/Service/ServiceMethodsSubscriberTraitTest.php index 246cb6194bcec..0cd70296fb3f5 100644 --- a/src/Symfony/Contracts/Tests/Service/ServiceMethodsSubscriberTraitTest.php +++ b/src/Symfony/Contracts/Tests/Service/ServiceMethodsSubscriberTraitTest.php @@ -25,7 +25,8 @@ public function testMethodsOnParentsAndChildrenAreIgnoredInGetSubscribedServices { $expected = [ TestService::class.'::aService' => Service2::class, - TestService::class.'::nullableService' => '?'.Service2::class, + TestService::class.'::nullableInAttribute' => '?'.Service2::class, + TestService::class.'::nullableReturnType' => '?'.Service2::class, new SubscribedService(TestService::class.'::withAttribute', Service2::class, true, new Required()), ]; @@ -104,8 +105,18 @@ public function aService(): Service2 return $this->container->get(__METHOD__); } + #[SubscribedService(nullable: true)] + public function nullableInAttribute(): Service2 + { + if (!$this->container->has(__METHOD__) ) { + throw new \LogicException(); + } + + return $this->container->get(__METHOD__); + } + #[SubscribedService] - public function nullableService(): ?Service2 + public function nullableReturnType(): ?Service2 { return $this->container->get(__METHOD__); } diff --git a/src/Symfony/Contracts/Tests/Service/ServiceSubscriberTraitTest.php b/src/Symfony/Contracts/Tests/Service/ServiceSubscriberTraitTest.php index 739d693562d14..bf0db2c1e158a 100644 --- a/src/Symfony/Contracts/Tests/Service/ServiceSubscriberTraitTest.php +++ b/src/Symfony/Contracts/Tests/Service/ServiceSubscriberTraitTest.php @@ -33,7 +33,8 @@ public function testMethodsOnParentsAndChildrenAreIgnoredInGetSubscribedServices { $expected = [ LegacyTestService::class.'::aService' => Service2::class, - LegacyTestService::class.'::nullableService' => '?'.Service2::class, + LegacyTestService::class.'::nullableInAttribute' => '?'.Service2::class, + LegacyTestService::class.'::nullableReturnType' => '?'.Service2::class, new SubscribedService(LegacyTestService::class.'::withAttribute', Service2::class, true, new Required()), ]; @@ -54,7 +55,7 @@ public function testParentNotCalledIfHasMagicCall() $container = new class([]) implements ContainerInterface { use ServiceLocatorTrait; }; - $service = new class extends ParentWithMagicCall { + $service = new class extends LegacyParentWithMagicCall { use ServiceSubscriberTrait; private $container;