8000 bug #43922 [DependencyInjection] only allow `ReflectionNamedType` for… · symfony/symfony@4f1c67a · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f1c67a

Browse files
bug #43922 [DependencyInjection] only allow ReflectionNamedType for ServiceSubscriberTrait (kbond)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [DependencyInjection] only allow `ReflectionNamedType` for `ServiceSubscriberTrait` | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #43913 | License | MIT | Doc PR | n/a I'll follow this up with a PR on 5.4 to allow union/intersections when using the `SubscribedService` attribute (once `ServiceSubscriberInterface` [supports this](#43913 (comment))). Commits ------- b616042 [DependencyInjection] only allow `ReflectionNamedType` for `ServiceSubscriberTrait`
2 parents 1cb6934 + b616042 commit 4f1c67a

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/Symfony/Contracts/Service/ServiceSubscriberTrait.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,19 @@ public static function getSubscribedServices(): array
4242
continue;
4343
}
4444

45-
if (self::class === $method->getDeclaringClass()->name && ($returnType = $method->getReturnType()) && !$returnType->isBuiltin()) {
46-
$services[self::class.'::'.$method->name] = '?'.($returnType instanceof \ReflectionNamedType ? $returnType->getName() : $returnType);
45+
if (self::class !== $method->getDeclaringClass()->name) {
46+
continue;
47+
}
48+
49+
if (!($returnType = $method->getReturnType()) instanceof \ReflectionNamedType) {
50+
continue;
4751
}
52+
53+
if ($returnType->isBuiltin()) {
54+
continue;
55+
}
56+
57+
$services[self::class.'::'.$method->name] = '?'.$returnType->getName();
4858
}
4959

5060
return $services;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Symfony\Contracts\Tests\Fixtures;
4+
5+
use Symfony\Contracts\Service\ServiceSubscriberTrait;
6+
7+
class TestServiceSubscriberUnion
8+
{
9+
use ServiceSubscriberTrait;
10+
11+
private function method1(): Service1
12+
{
13+
return $this->container->get(__METHOD__);
14+
}
15+
16+
private function method2(): Service1|Service2
17+
{
18+
return $this->container->get(__METHOD__);
19+
}
20+
21+
private function method3(): Service1|Service2|null
22+
{
23+
return $this->container->get(__METHOD__);
24+
}
25+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Contracts\Service\ServiceLocatorTrait;
1717
use Symfony\Contracts\Service\ServiceSubscriberInterface;
1818
use Symfony\Contracts\Service\ServiceSubscriberTrait;
19+
use Symfony\Contracts\Tests\Fixtures\TestServiceSubscriberUnion;
1920

2021
class ServiceSubscriberTraitTest extends TestCase
2122
{
@@ -34,6 +35,16 @@ public function testSetContainerIsCalledOnParent()
3435

3536
$this->assertSame($container, (new TestService())->setContainer($container));
3637
}
38+
39+
/**
40+
* @requires PHP 8
41+
*/
42+
public function testMethodsWithUnionReturnTypesAreIgnored()
43+
{
44+
$expected = [TestServiceSubscriberUnion::class.'::method1' => '?Symfony\Contracts\Tests\Fixtures\Service1'];
45+
46+
$this->assertEquals($expected, TestServiceSubscriberUnion::getSubscribedServices());
47+
}
3748
}
3849

3950
class ParentTestService

0 commit comments

Comments
 (0)
0