Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 95b0f9b

Browse filesBrowse files
committed
Fix ServiceMethodsSubscriberTrait for nullable service
1 parent 368bfb7 commit 95b0f9b
Copy full SHA for 95b0f9b

File tree

2 files changed

+14
-3
lines changed
Filter options

2 files changed

+14
-3
lines changed

‎src/Symfony/Contracts/Service/ServiceSubscriberTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Contracts/Service/ServiceSubscriberTrait.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static function getSubscribedServices(): array
5151
$attribute = $attribute->newInstance();
5252
$attribute->key ??= self::class.'::'.$method->name;
5353
$attribute->type ??= $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType;
54-
$attribute->nullable = $returnType->allowsNull();
54+
$attribute->nullable = $attribute->nullable ?: $returnType->allowsNull();
5555

5656
if ($attribute->attributes) {
5757
$services[] = $attribute;

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

Copy file name to clipboardExpand all lines: src/Symfony/Contracts/Tests/Service/ServiceSubscriberTraitTest.php
+13-2Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public function testMethodsOnParentsAndChildrenAreIgnoredInGetSubscribedServices
2727
{
2828
$expected = [
2929
TestService::class.'::aService' => Service2::class,
30-
TestService::class.'::nullableService' => '?'.Service2::class,
30+
TestService::class.'::nullableInAttribute' => '?'.Service2::class,
31+
TestService::class.'::nullableReturnType' => '?'.Service2::class,
3132
new SubscribedService(TestService::class.'::withAttribute', Service2::class, true, new Required()),
3233
];
3334

@@ -103,8 +104,18 @@ public function aService(): Service2
103104
{
104105
}
105106

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.