88use PHPStan \Reflection \ParametersAcceptorSelector ;
99use PHPStan \ShouldNotHappenException ;
1010use PHPStan \Symfony \Configuration ;
11+ use PHPStan \Symfony \ParameterMap ;
12+ use PHPStan \Symfony \ServiceDefinition ;
1113use PHPStan \Symfony \ServiceMap ;
1214use PHPStan \Type \Constant \ConstantBooleanType ;
1315use PHPStan \Type \DynamicMethodReturnTypeExtension ;
1416use PHPStan \Type \ObjectType ;
1517use PHPStan \Type \Type ;
18+ use Symfony \Component \DependencyInjection \ParameterBag \ParameterBag ;
1619use function in_array ;
1720
1821final class ServiceDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
@@ -27,11 +30,20 @@ final class ServiceDynamicReturnTypeExtension implements DynamicMethodReturnType
2730 /** @var ServiceMap */
2831 private $ serviceMap ;
2932
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+ )
3142 {
3243 $ this ->className = $ className ;
3344 $ this ->constantHassers = $ configuration ->hasConstantHassers ();
3445 $ this ->serviceMap = $ symfonyServiceMap ;
46+ $ this ->parameterBag = $ this ->createParameterBag ($ symfonyParameterMap );
3547 }
3648
3749 public function getClass (): string
@@ -70,7 +82,7 @@ private function getGetTypeFromMethodCall(
7082 if ($ serviceId !== null ) {
7183 $ service = $ this ->serviceMap ->getService ($ serviceId );
7284 if ($ service !== null && (!$ service ->isSynthetic () || $ service ->getClass () !== null )) {
73- return new ObjectType ($ service -> getClass ( ) ?? $ serviceId );
85+ return new ObjectType ($ this -> determineServiceClass ( $ service ) ?? $ serviceId );
7486 }
7587 }
7688
@@ -97,4 +109,20 @@ private function getHasTypeFromMethodCall(
97109 return $ returnType ;
98110 }
99111
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+
100128}
0 commit comments