From ee3b8604e3f801910606cc1533ac4c180daf9cf0 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 12 Dec 2025 14:14:23 +0100 Subject: [PATCH 1/4] Constants isDeprecation() depends on Scope->getPhpVersion() --- .../BetterReflectionProvider.php | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Reflection/BetterReflection/BetterReflectionProvider.php b/src/Reflection/BetterReflection/BetterReflectionProvider.php index 6236191b63..3e25ceb428 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; @@ -372,8 +374,15 @@ 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; + $cacheKey = $constantName; + if ($namespaceAnswerer instanceof Scope) { + $phpVersionType = $namespaceAnswerer->getPhpVersion()->getType(); + $cacheKey = $constantName . '-' . $phpVersionType->describe(VerbosityLevel::cache()); + } + + if (array_key_exists($cacheKey, $this->cachedConstants)) { + return $this->cachedConstants[$cacheKey]; } $constantReflection = $this->reflector->reflectConstant($constantName); @@ -399,7 +408,11 @@ 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 @@ -412,7 +425,7 @@ public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAn $isDeprecated = $constantReflection->isDeprecated(); } - return $this->cachedConstants[$constantName] = new RuntimeConstantReflection( + return $this->cachedConstants[$cacheKey] = new RuntimeConstantReflection( $constantName, $constantValueType, $fileName, From 87b63142520cdbad4f504cc708f93edd0a2aee16 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 12 Dec 2025 14:30:24 +0100 Subject: [PATCH 2/4] Update BetterReflectionProvider.php --- src/Reflection/BetterReflection/BetterReflectionProvider.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Reflection/BetterReflection/BetterReflectionProvider.php b/src/Reflection/BetterReflection/BetterReflectionProvider.php index 3e25ceb428..b6ce2ccb07 100644 --- a/src/Reflection/BetterReflection/BetterReflectionProvider.php +++ b/src/Reflection/BetterReflection/BetterReflectionProvider.php @@ -419,9 +419,7 @@ public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAn $deprecatedDescription = $deprecatedMessage; } } - } - - if (!$isDeprecated) { + } elseif (!$isDeprecated) { $isDeprecated = $constantReflection->isDeprecated(); } From fe00d41bab84c6ace7025fdfe3f2459fc97b3c63 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 12 Dec 2025 14:35:47 +0100 Subject: [PATCH 3/4] Update BetterReflectionProvider.php --- src/Reflection/BetterReflection/BetterReflectionProvider.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Reflection/BetterReflection/BetterReflectionProvider.php b/src/Reflection/BetterReflection/BetterReflectionProvider.php index b6ce2ccb07..253f8cf0ff 100644 --- a/src/Reflection/BetterReflection/BetterReflectionProvider.php +++ b/src/Reflection/BetterReflection/BetterReflectionProvider.php @@ -418,6 +418,8 @@ public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAn // https://github.com/JetBrains/phpstorm-stubs/blob/9608c953230b08f07b703ecfe459cc58d5421437/filter/filter.php#L478 $deprecatedDescription = $deprecatedMessage; } + } elseif (!$isDeprecated) { + $isDeprecated = $constantReflection->isDeprecated(); } } elseif (!$isDeprecated) { $isDeprecated = $constantReflection->isDeprecated(); From fe2ca032bb09b2b4d818082930109863794997f5 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 12 Dec 2025 16:26:14 +0100 Subject: [PATCH 4/4] cleanup caching --- .../BetterReflectionProvider.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Reflection/BetterReflection/BetterReflectionProvider.php b/src/Reflection/BetterReflection/BetterReflectionProvider.php index 253f8cf0ff..8ed0d04b86 100644 --- a/src/Reflection/BetterReflection/BetterReflectionProvider.php +++ b/src/Reflection/BetterReflection/BetterReflectionProvider.php @@ -78,7 +78,7 @@ final class BetterReflectionProvider implements ReflectionProvider /** @var ClassReflection[] */ private static array $anonymousClasses = []; - /** @var array */ + /** @var array> */ private array $cachedConstants = []; /** @@ -375,14 +375,18 @@ public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAn } $phpVersionType = null; - $cacheKey = $constantName; + $versionKey = 'current_version'; if ($namespaceAnswerer instanceof Scope) { $phpVersionType = $namespaceAnswerer->getPhpVersion()->getType(); - $cacheKey = $constantName . '-' . $phpVersionType->describe(VerbosityLevel::cache()); + $versionKey = $phpVersionType->describe(VerbosityLevel::cache()); } - if (array_key_exists($cacheKey, $this->cachedConstants)) { - return $this->cachedConstants[$cacheKey]; + 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); @@ -425,7 +429,7 @@ public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAn $isDeprecated = $constantReflection->isDeprecated(); } - return $this->cachedConstants[$cacheKey] = new RuntimeConstantReflection( + return $this->cachedConstants[$versionKey][$constantName] = new RuntimeConstantReflection( $constantName, $constantValueType, $fileName,