8000 Fix ServiceMethodsSubscriberTrait for nullable service · symfony/symfony@1a9c3a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a9c3a2

Browse files
committed
Fix ServiceMethodsSubscriberTrait for nullable service
1 parent ff0fc69 commit 1a9c3a2

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/Symfony/Contracts/Service/ServiceMethodsSubscriberTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function getSubscribedServices(): array
5353
$attribute = $attribute->newInstance();
5454
$attribute->key ??= self::class.'::'.$method->name;
5555
$attribute->type ??= $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType;
56-
$attribute->nullable = $returnType->allowsNull();
56+
$attribute->nullable = $attribute->nullable ?: $returnType->allowsNull();
5757

5858
if ($attribute->attributes) {
5959
$services[] = $attribute;

src/Symfony/Contracts/Tests/Service/ServiceMethodsSubscriberTraitTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public function testMethodsOnParentsAndChildrenAreIgnoredInGetSubscribedServices
2525
{
2626
$expected = [
2727
TestService::class.'::aService' => Service2::class,
28-
TestService::class.'::nullableService' => '?'.Service2::class,
28+
TestService::class.'::nullableInAttribute' => '?'.Service2::class,
29+
TestService::class.'::nullableReturnType' => '?'.Service2::class,
2930
new SubscribedService(TestService::class.'::withAttribute', Service2::class, true, new Required()),
3031
];
3132

@@ -104,8 +105,18 @@ public function aService(): Service2
104105
return $this->container->get(__METHOD__);
105106
}
106107

108+
#[SubscribedService(nullable: true)]
109+
public function nullableInAttribute(): Service2
110+
{
111+
if (!$this->container->has(__METHOD__) ) {
112+
throw new \LogicException();
113+
}
114+
115+
return $this->container->get(__METHOD__);
116+
}
117+
107118
#[SubscribedService]
108-
public function nullableService(): ?Service2
119+
public function nullableReturnType(): ?Service2
109120
{
110121
return $this->container->get(__METHOD__);
111122
}

0 commit comments

Comments
 (0)
0