16
16
use PHPStan \Type \ObjectType ;
17
17
use PHPStan \Type \Type ;
18
18
use Symfony \Component \DependencyInjection \ParameterBag \ParameterBag ;
19
+ use function class_exists ;
19
20
use function in_array ;
20
21
21
22
final class ServiceDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
@@ -30,7 +31,10 @@ final class ServiceDynamicReturnTypeExtension implements DynamicMethodReturnType
30
31
/** @var ServiceMap */
31
32
private $ serviceMap ;
32
33
33
- /** @var ParameterBag */
34
+ /** @var ParameterMap */
35
+ private $ parameterMap ;
36
+
37
+ /** @var ParameterBag|null */
34
38
private $ parameterBag ;
35
39
36
40
public function __construct (
@@ -43,7 +47,7 @@ public function __construct(
43
47
$ this ->className = $ className ;
44
48
$ this ->constantHassers = $ configuration ->hasConstantHassers ();
45
49
$ this ->serviceMap = $ symfonyServiceMap ;
46
- $ this ->parameterBag = $ this -> createParameterBag ( $ symfonyParameterMap) ;
50
+ $ this ->parameterMap = $ symfonyParameterMap ;
47
51
}
48
52
49
53
public function getClass (): string
@@ -78,17 +82,46 @@ private function getGetTypeFromMethodCall(
78
82
return $ returnType ;
79
83
}
80
84
85
+ $ parameterBag = $ this ->tryGetParameterBag ();
86
+ if ($ parameterBag === null ) {
87
+ return $ returnType ;
88
+ }
89
+
81
90
$ serviceId = $ this ->serviceMap ::getServiceIdFromNode ($ methodCall ->getArgs ()[0 ]->value , $ scope );
82
91
if ($ serviceId !== null ) {
83
92
$ service = $ this ->serviceMap ->getService ($ serviceId );
84
93
if ($ service !== null && (!$ service ->isSynthetic () || $ service ->getClass () !== null )) {
85
- return new ObjectType ($ this ->determineServiceClass ($ service ) ?? $ serviceId );
94
+ return new ObjectType ($ this ->determineServiceClass ($ parameterBag , $ service ) ?? $ serviceId );
86
95
}
87
96
}
88
97
89
98
return $ returnType ;
90
99
}
91
100
101
+ private function tryGetParameterBag (): ?ParameterBag
102
+ {
103
+ if ($ this ->parameterBag !== null ) {
104
+ return $ this ->parameterBag ;
105
+ }
106
+
107
+ return $ this ->parameterBag = $ this ->tryCreateParameterBag ();
108
+ }
109
+
110
+ private function tryCreateParameterBag (): ?ParameterBag
111
+ {
112
+ if (!class_exists (ParameterBag::class)) {
113
+ return null ;
114
+ }
115
+
116
+ $ parameters = [];
117
+
118
+ foreach ($ this ->parameterMap ->getParameters () as $ parameterDefinition ) {
119
+ $ parameters [$ parameterDefinition ->getKey ()] = $ parameterDefinition ->getValue ();
120
+ }
121
+
122
+ return new ParameterBag ($ parameters );
123
+ }
124
+
92
125
private function getHasTypeFromMethodCall (
93
126
MethodReflection $ methodReflection ,
94
127
MethodCall $ methodCall ,
@@ -109,20 +142,9 @@ private function getHasTypeFromMethodCall(
109
142
return $ returnType ;
110
143
}
111
144
112
- private function determineServiceClass (ServiceDefinition $ service ): ?string
113
- {
114
- return $ this ->parameterBag ->resolveValue ($ service ->getClass ());
115
- }
116
-
117
- private function createParameterBag (ParameterMap $ symfonyParameterMap ): ParameterBag
145
+ private function determineServiceClass (ParameterBag $ parameterBag , ServiceDefinition $ service ): ?string
118
146
{
119
- $ parameters = [];
120
-
121
- foreach ($ symfonyParameterMap ->getParameters () as $ parameterDefinition ) {
122
- $ parameters [$ parameterDefinition ->getKey ()] = $ parameterDefinition ->getValue ();
123
- }
124
-
125
- return new ParameterBag ($ parameters );
147
+ return $ parameterBag ->resolveValue ($ service ->getClass ());
126
148
}
127
149
128
150
}
0 commit comments