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

Skip to content

Commit e67db9a

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

5 files changed

+29
-7
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/Service/ServiceSubscriberTrait.php

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

6262
if ($attribute->attributes) {
6363
$services[] = $attribute;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,18 @@ public function aService(): Service2
4141
return $this->container->get(__METHOD__);
4242
}
4343

44+
#[SubscribedService(nullable: true)]
45+
public function nullableInAttribute(): Service2
46+
{
47+
if (!$this->container->has(__METHOD__)) {
48+
throw new \LogicException();
49+
}
50+
51+
return $this->container->get(__METHOD__);
52+
}
53+
4454
#[SubscribedService]
45-
public function nullableService(): ?Service2
55+
public function nullableReturnType(): ?Service2
4656
{
4757
return $this->container->get(__METHOD__);
4858
}

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
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public function testMethodsOnParentsAndChildrenAreIgnoredInGetSubscribedServices
3333
{
3434
$expected = [
3535
LegacyTestService::class.'::aService' => Service2::class,
36-
LegacyTestService::class.'::nullableService' => '?'.Service2::class,
36+
LegacyTestService::class.'::nullableInAttribute' => '?'.Service2::class,
37+
LegacyTestService::class.'::nullableReturnType' => '?'.Service2::class,
3738
new SubscribedService(LegacyTestService::class.'::withAttribute', Service2::class, true, new Required()),
3839
];
3940

@@ -54,7 +55,7 @@ public function testParentNotCalledIfHasMagicCall()
5455
$container = new class([]) implements ContainerInterface {
5556
use ServiceLocatorTrait;
5657
};
57-
$service = new class extends ParentWithMagicCall {
58+
$service = new class extends LegacyParentWithMagicCall {
5859
use ServiceSubscriberTrait;
5960

6061
private $container;

0 commit comments

Comments
 (0)
0