Skip to content

[Contracts] Fix ServiceMethodsSubscriberTrait for nullable service #60267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Contracts/Service/ServiceSubscriberTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Contracts/Tests/Service/LegacyTestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
];

Expand Down Expand Up @@ -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__);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
];

Expand All @@ -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;
Expand Down