33namespace PHPStan \Analyser ;
44
55use PhpParser \Node \Name ;
6+ use PHPStan \DependencyInjection \Container ;
67use PHPStan \Php \PhpVersion ;
8+ use PHPStan \PhpDoc \TypeStringResolver ;
79use PHPStan \Reflection \NamespaceAnswerer ;
810use PHPStan \Reflection \ReflectionProvider ;
911use PHPStan \Reflection \ReflectionProvider \ReflectionProviderProvider ;
@@ -41,6 +43,7 @@ public function __construct(
4143 private array $ dynamicConstantNames ,
4244 private ?PhpVersion $ composerMinPhpVersion ,
4345 private ?PhpVersion $ composerMaxPhpVersion ,
46+ private ?Container $ container ,
4447 )
4548 {
4649 }
@@ -353,8 +356,17 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
353356
354357 public function resolveConstantType (string $ constantName , Type $ constantType ): Type
355358 {
356- if ($ constantType ->isConstantValue ()->yes () && in_array ($ constantName , $ this ->dynamicConstantNames , true )) {
357- return $ constantType ->generalize (GeneralizePrecision::lessSpecific ());
359+ if ($ constantType ->isConstantValue ()->yes ()) {
360+ if (array_key_exists ($ constantName , $ this ->dynamicConstantNames )) {
361+ $ phpdocTypes = $ this ->dynamicConstantNames [$ constantName ];
362+ $ typeStringResolver = $ this ->container ?->getByType(TypeStringResolver::class);
363+ if ($ typeStringResolver !== null ) {
364+ return $ typeStringResolver ->resolve ($ phpdocTypes , new NameScope (null , [], null ));
365+ }
366+ }
367+ if (in_array ($ constantName , $ this ->dynamicConstantNames , true )) {
368+ return $ constantType ->generalize (GeneralizePrecision::lessSpecific ());
369+ }
358370 }
359371
360372 return $ constantType ;
@@ -363,6 +375,20 @@ public function resolveConstantType(string $constantName, Type $constantType): T
363375 public function resolveClassConstantType (string $ className , string $ constantName , Type $ constantType , ?Type $ nativeType ): Type
364376 {
365377 $ lookupConstantName = sprintf ('%s::%s ' , $ className , $ constantName );
378+ if (array_key_exists ($ lookupConstantName , $ this ->dynamicConstantNames )) {
379+ if ($ nativeType !== null ) {
380+ return $ nativeType ;
381+ }
382+
383+ if ($ constantType ->isConstantValue ()->yes ()) {
384+ $ phpdocTypes = $ this ->dynamicConstantNames [$ lookupConstantName ];
385+ $ typeStringResolver = $ this ->container ?->getByType(TypeStringResolver::class);
386+ if ($ typeStringResolver !== null ) {
387+ return $ typeStringResolver ->resolve ($ phpdocTypes , new NameScope (null , [], $ className ));
388+ }
389+ }
390+ }
391+
366392 if (in_array ($ lookupConstantName , $ this ->dynamicConstantNames , true )) {
367393 if ($ nativeType !== null ) {
368394 return $ nativeType ;
0 commit comments