8
8
use PHPStan \Reflection \ParametersAcceptorSelector ;
9
9
use PHPStan \ShouldNotHappenException ;
10
10
use PHPStan \Symfony \Configuration ;
11
+ use PHPStan \Symfony \ParameterMap ;
12
+ use PHPStan \Symfony \ServiceDefinition ;
11
13
use PHPStan \Symfony \ServiceMap ;
12
14
use PHPStan \Type \Constant \ConstantBooleanType ;
13
15
use PHPStan \Type \DynamicMethodReturnTypeExtension ;
14
16
use PHPStan \Type \ObjectType ;
15
17
use PHPStan \Type \Type ;
18
+ use Symfony \Component \DependencyInjection \ParameterBag \ParameterBag ;
16
19
use function in_array ;
17
20
18
21
final class ServiceDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
@@ -27,11 +30,20 @@ final class ServiceDynamicReturnTypeExtension implements DynamicMethodReturnType
27
30
/** @var ServiceMap */
28
31
private $ serviceMap ;
29
32
30
- public function __construct (string $ className , Configuration $ configuration , ServiceMap $ symfonyServiceMap )
33
+ /** @var ParameterBag */
34
+ private $ parameterBag ;
35
+
36
+ public function __construct (
37
+ string $ className ,
38
+ Configuration $ configuration ,
39
+ ServiceMap $ symfonyServiceMap ,
40
+ ParameterMap $ symfonyParameterMap
41
+ )
31
42
{
32
43
$ this ->className = $ className ;
33
44
$ this ->constantHassers = $ configuration ->hasConstantHassers ();
34
45
$ this ->serviceMap = $ symfonyServiceMap ;
46
+ $ this ->parameterBag = $ this ->createParameterBag ($ symfonyParameterMap );
35
47
}
36
48
37
49
public function getClass (): string
@@ -70,7 +82,7 @@ private function getGetTypeFromMethodCall(
70
82
if ($ serviceId !== null ) {
71
83
$ service = $ this ->serviceMap ->getService ($ serviceId );
72
84
if ($ service !== null && (!$ service ->isSynthetic () || $ service ->getClass () !== null )) {
73
- return new ObjectType ($ service -> getClass ( ) ?? $ serviceId );
85
+ return new ObjectType ($ this -> determineServiceClass ( $ service ) ?? $ serviceId );
74
86
}
75
87
}
76
88
@@ -97,4 +109,20 @@ private function getHasTypeFromMethodCall(
97
109
return $ returnType ;
98
110
}
99
111
112
+ private function determineServiceClass (ServiceDefinition $ service ): ?string
113
+ {
114
+ return $ this ->parameterBag ->resolveValue ($ service ->getClass ());
115
+ }
116
+
117
+ private function createParameterBag (ParameterMap $ symfonyParameterMap ): ParameterBag
118
+ {
119
+ $ parameters = [];
120
+
121
+ foreach ($ symfonyParameterMap ->getParameters () as $ parameterDefinition ) {
122
+ $ parameters [$ parameterDefinition ->getKey ()] = $ parameterDefinition ->getValue ();
123
+ }
124
+
125
+ return new ParameterBag ($ parameters );
126
+ }
127
+
100
128
}
0 commit comments