5353use PHPStan \TrinaryLogic ;
5454use PHPStan \Type \FileTypeMapper ;
5555use PHPStan \Type \Generic \TemplateTypeMap ;
56+ use PHPStan \Type \IntegerRangeType ;
5657use PHPStan \Type \Type ;
58+ use PHPStan \Type \VerbosityLevel ;
5759use function array_key_exists ;
5860use function array_key_first ;
5961use function array_map ;
@@ -76,7 +78,7 @@ final class BetterReflectionProvider implements ReflectionProvider
7678 /** @var ClassReflection[] */
7779 private static array $ anonymousClasses = [];
7880
79- /** @var array<string, ConstantReflection> */
81+ /** @var array<string, array<string, ConstantReflection> > */
8082 private array $ cachedConstants = [];
8183
8284 /**
@@ -377,8 +379,19 @@ public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAn
377379 throw new ConstantNotFoundException ((string ) $ nameNode );
378380 }
379381
380- if (array_key_exists ($ constantName , $ this ->cachedConstants )) {
381- return $ this ->cachedConstants [$ constantName ];
382+ $ phpVersionType = null ;
383+ $ versionKey = 'current_version ' ;
384+ if ($ namespaceAnswerer instanceof Scope) {
385+ $ phpVersionType = $ namespaceAnswerer ->getPhpVersion ()->getType ();
386+ $ versionKey = $ phpVersionType ->describe (VerbosityLevel::cache ());
387+ }
388+
389+ if (!array_key_exists ($ versionKey , $ this ->cachedConstants )) {
390+ $ this ->cachedConstants [$ versionKey ] = [];
391+ }
392+
393+ if (array_key_exists ($ constantName , $ this ->cachedConstants [$ versionKey ])) {
394+ return $ this ->cachedConstants [$ versionKey ][$ constantName ];
382395 }
383396
384397 $ constantReflection = $ this ->reflector ->reflectConstant ($ constantName );
@@ -404,20 +417,24 @@ public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAn
404417 $ patch = $ matches [3 ] ?? 0 ;
405418 $ versionId = sprintf ('%d%02d%02d ' , $ major , $ minor , $ patch );
406419
407- $ isDeprecated = $ this ->phpVersion ->getVersionId () >= $ versionId ;
420+ if ($ phpVersionType !== null ) {
421+ $ isDeprecated = IntegerRangeType::fromInterval ((int ) $ versionId , null )->isSuperTypeOf ($ phpVersionType )->yes ();
422+ } else {
423+ $ isDeprecated = $ this ->phpVersion ->getVersionId () >= $ versionId ;
424+ }
408425 } else {
409426 // filter raw version number messages like in
410427 // https://github.com/JetBrains/phpstorm-stubs/blob/9608c953230b08f07b703ecfe459cc58d5421437/filter/filter.php#L478
411428 $ deprecatedDescription = $ deprecatedMessage ;
412429 }
430+ } elseif (!$ isDeprecated ) {
431+ $ isDeprecated = $ constantReflection ->isDeprecated ();
413432 }
414- }
415-
416- if (!$ isDeprecated ) {
433+ } elseif (!$ isDeprecated ) {
417434 $ isDeprecated = $ constantReflection ->isDeprecated ();
418435 }
419436
420- return $ this ->cachedConstants [$ constantName ] = new RuntimeConstantReflection (
437+ return $ this ->cachedConstants [$ versionKey ][ $ constantName ] = new RuntimeConstantReflection (
421438 $ constantName ,
422439 $ constantValueType ,
423440 $ fileName ,
0 commit comments