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 070869c

Browse filesBrowse files
committed
bug #60267 [Contracts] Fix ServiceMethodsSubscriberTrait for nullable service (StevenRenaux)
This PR was merged into the 7.2 branch. Discussion ---------- [Contracts] Fix `ServiceMethodsSubscriberTrait` for nullable service | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT Used in a bundle context. If you use the following example, you will get an exception: `ServiceNotFoundException: The service "twig" in the container provided to "App\Service\Dependency" has a dependency on a non-existent service "Twig\Environment".` The `nullable` argument of the `SubscribedService` attribute is ignored. ```php // src/Service/TwigAware.php namespace App\Service; use Twig\Environment; use Symfony\Contracts\Service\Attribute\SubscribedService; trait TwigAware { #[SubscribedService('twig', nullable: true)] private function twig(): Environment { if (!$this->container->has('twig') ) { throw new \LogicException(\sprintf('Twig is required to use "%s" method. Try to run "composer require symfony/twig-bundle".', __METHOD__)); } return $environment; } } // src/Service/MyService.php namespace App\Service; use Symfony\Contracts\Service\ServiceSubscriberInterface; use Symfony\Contracts\Service\ServiceSubscriberTrait; class MyService implements ServiceSubscriberInterface { use ServiceSubscriberTrait, TwigAware; public function doWithTwig(): void { // $this->twig() ... } } ``` Related to #60265 Commits ------- e67db9a Fix ServiceMethodsSubscriberTrait for nullable service
2 parents 7804692 + e67db9a commit 070869c
Copy full SHA for 070869c

File tree

4 files changed

+28
-6
lines changed
Filter options

4 files changed

+28
-6
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Contracts/Service/ServiceMethodsSubscriberTrait.php
+1-1Lines 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/LegacyTestService.php

Copy file name to clipboardExpand all lines: src/Symfony/Contracts/Tests/Service/LegacyTestService.php
+11-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Contracts/Tests/Service/ServiceMethodsSubscriberTraitTest.php
+13-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Contracts/Tests/Service/ServiceSubscriberTraitTest.php
+3-2Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.