File tree Expand file tree Collapse file tree 3 files changed +48
-2
lines changed Expand file tree Collapse file tree 3 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -42,9 +42,19 @@ public static function getSubscribedServices(): array
42
42
continue ;
43
43
}
44
44
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 ;
47
51
}
52
+
53
+ if ($ returnType ->isBuiltin ()) {
54
+ continue ;
55
+ }
56
+
57
+ $ services [self ::class.':: ' .$ method ->name ] = '? ' .$ returnType ->getName ();
48
58
}
49
59
50
60
return $ services ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 16
16
use Symfony \Contracts \Service \ServiceLocatorTrait ;
17
17
use Symfony \Contracts \Service \ServiceSubscriberInterface ;
18
18
use Symfony \Contracts \Service \ServiceSubscriberTrait ;
19
+ use Symfony \Contracts \Tests \Fixtures \TestServiceSubscriberUnion ;
19
20
20
21
class ServiceSubscriberTraitTest extends TestCase
21
22
{
@@ -34,6 +35,16 @@ public function testSetContainerIsCalledOnParent()
34
35
35
36
$ this ->assertSame ($ container , (new TestService ())->setContainer ($ container ));
36
37
}
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
+ }
37
48
}
38
49
39
50
class ParentTestService
You can’t perform that action at this time.
0 commit comments