diff --git a/src/Reflection/BetterReflection/BetterReflectionProvider.php b/src/Reflection/BetterReflection/BetterReflectionProvider.php index 6236191b63..8ed0d04b86 100644 --- a/src/Reflection/BetterReflection/BetterReflectionProvider.php +++ b/src/Reflection/BetterReflection/BetterReflectionProvider.php @@ -53,7 +53,9 @@ use PHPStan\TrinaryLogic; use PHPStan\Type\FileTypeMapper; use PHPStan\Type\Generic\TemplateTypeMap; +use PHPStan\Type\IntegerRangeType; use PHPStan\Type\Type; +use PHPStan\Type\VerbosityLevel; use function array_key_exists; use function array_key_first; use function array_map; @@ -76,7 +78,7 @@ final class BetterReflectionProvider implements ReflectionProvider /** @var ClassReflection[] */ private static array $anonymousClasses = []; - /** @var array */ + /** @var array> */ private array $cachedConstants = []; /** @@ -372,8 +374,19 @@ public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAn throw new ConstantNotFoundException((string) $nameNode); } - if (array_key_exists($constantName, $this->cachedConstants)) { - return $this->cachedConstants[$constantName]; + $phpVersionType = null; + $versionKey = 'current_version'; + if ($namespaceAnswerer instanceof Scope) { + $phpVersionType = $namespaceAnswerer->getPhpVersion()->getType(); + $versionKey = $phpVersionType->describe(VerbosityLevel::cache()); + } + + if (!array_key_exists($versionKey, $this->cachedConstants)) { + $this->cachedConstants[$versionKey] = []; + } + + if (array_key_exists($constantName, $this->cachedConstants[$versionKey])) { + return $this->cachedConstants[$versionKey][$constantName]; } $constantReflection = $this->reflector->reflectConstant($constantName); @@ -399,20 +412,24 @@ public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAn $patch = $matches[3] ?? 0; $versionId = sprintf('%d%02d%02d', $major, $minor, $patch); - $isDeprecated = $this->phpVersion->getVersionId() >= $versionId; + if ($phpVersionType !== null) { + $isDeprecated = IntegerRangeType::fromInterval((int) $versionId, null)->isSuperTypeOf($phpVersionType)->yes(); + } else { + $isDeprecated = $this->phpVersion->getVersionId() >= $versionId; + } } else { // filter raw version number messages like in // https://github.com/JetBrains/phpstorm-stubs/blob/9608c953230b08f07b703ecfe459cc58d5421437/filter/filter.php#L478 $deprecatedDescription = $deprecatedMessage; } + } elseif (!$isDeprecated) { + $isDeprecated = $constantReflection->isDeprecated(); } - } - - if (!$isDeprecated) { + } elseif (!$isDeprecated) { $isDeprecated = $constantReflection->isDeprecated(); } - return $this->cachedConstants[$constantName] = new RuntimeConstantReflection( + return $this->cachedConstants[$versionKey][$constantName] = new RuntimeConstantReflection( $constantName, $constantValueType, $fileName,