From 0fbf12c2dae6e5e327e407f352c87a1c915d747b Mon Sep 17 00:00:00 2001 From: VincentLanglet <9052536+VincentLanglet@users.noreply.github.com> Date: Thu, 19 Nov 2026 10:05:14 +0000 Subject: [PATCH 1/7] Narrow `getName()` and `$name` instead of invariant `self` in the PHP 8.4+ `ReflectionEnum::isBacked()` stub - On PHP 8.4+ `ReflectionEnumWithLazyObjects.stub` declares `T` invariant (it has to, because the lazy-object `ReflectionClass` parent is invariant). The `@phpstan-assert-if-true self $this` assertion intersected `ReflectionEnum` with `ReflectionEnum`. Under invariance that is always `never`, so every `isBacked()` call on a non-concrete enum was reported as always false, and `$this` became `*NEVER*` in the truthy branch. - Replace it with `class-string` assertions on `$this->name` and `$this->getName()`. These narrow the class name without depending on the variance of `T`. Calls on a concrete unit enum (`ReflectionEnum`) are still reported as always false. - Enable `enum-reflection-backed.php` on PHP 8.4+ again. It was limited to < 8.4 because of this problem. - Checked the other stubs: none use a `self<...> $this` assertion on an invariant template. --- stubs/ReflectionEnumWithLazyObjects.stub | 3 +- .../Analyser/NodeScopeResolverTest.php | 2 +- .../Analyser/data/enum-reflection-backed.php | 2 +- tests/PHPStan/Analyser/nsrt/bug-15165.php | 33 ++++++++++ .../ImpossibleCheckTypeMethodCallRuleTest.php | 13 ++++ .../Rules/Comparison/data/bug-15165.php | 66 +++++++++++++++++++ 6 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 tests/PHPStan/Analyser/nsrt/bug-15165.php create mode 100644 tests/PHPStan/Rules/Comparison/data/bug-15165.php diff --git a/stubs/ReflectionEnumWithLazyObjects.stub b/stubs/ReflectionEnumWithLazyObjects.stub index 92ec6c660b6..cbfc471dc52 100644 --- a/stubs/ReflectionEnumWithLazyObjects.stub +++ b/stubs/ReflectionEnumWithLazyObjects.stub @@ -19,7 +19,8 @@ class ReflectionEnum extends ReflectionClass public function getCase(string $name): ReflectionEnumUnitCase {} /** - * @phpstan-assert-if-true self $this + * @phpstan-assert-if-true class-string $this->name + * @phpstan-assert-if-true class-string $this->getName() * @phpstan-assert-if-true !null $this->getBackingType() */ public function isBacked(): bool {} diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index fd6133805c5..71ee0a86c8f 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -52,7 +52,7 @@ private static function findTestFiles(): iterable yield __DIR__ . '/data/enum-reflection-php81.php'; } - if (PHP_VERSION_ID >= 80100 && PHP_VERSION_ID < 80400) { + if (PHP_VERSION_ID >= 80100) { yield __DIR__ . '/data/enum-reflection-backed.php'; } diff --git a/tests/PHPStan/Analyser/data/enum-reflection-backed.php b/tests/PHPStan/Analyser/data/enum-reflection-backed.php index 00f1b9634fd..54d3f2ea433 100644 --- a/tests/PHPStan/Analyser/data/enum-reflection-backed.php +++ b/tests/PHPStan/Analyser/data/enum-reflection-backed.php @@ -1,4 +1,4 @@ -= 8.1 namespace EnumReflection; diff --git a/tests/PHPStan/Analyser/nsrt/bug-15165.php b/tests/PHPStan/Analyser/nsrt/bug-15165.php new file mode 100644 index 00000000000..bf9b39d37f9 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-15165.php @@ -0,0 +1,33 @@ += 8.4 + +namespace Bug15165Types; + +use ReflectionEnum; +use UnitEnum; +use function PHPStan\Testing\assertType; + +/** @param class-string $class */ +function testNarrowGetNameTypeAfterIsBacked(string $class): void +{ + $r = new ReflectionEnum($class); + assertType('class-string', $r->getName()); + assertType('class-string', $r->name); + assertType('ReflectionNamedType|null', $r->getBackingType()); + if ($r->isBacked()) { + assertType('class-string', $r->getName()); + assertType('class-string', $r->name); + assertType('ReflectionNamedType', $r->getBackingType()); + } +} + +/** + * @template T of UnitEnum + * @param ReflectionEnum $enum + */ +function testTemplate(ReflectionEnum $enum): void +{ + if ($enum->isBacked()) { + assertType('ReflectionEnum', $enum); + assertType('ReflectionNamedType', $enum->getBackingType()); + } +} diff --git a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php index 076a291bb79..41870f07e99 100644 --- a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php @@ -346,6 +346,19 @@ public function testBug14705(): void ]); } + #[RequiresPhp('>= 8.1.0')] + public function testBug15165(): void + { + $this->treatPhpDocTypesAsCertain = true; + $this->analyse([__DIR__ . '/data/bug-15165.php'], [ + [ + 'Call to method ReflectionEnum::isBacked() will always evaluate to false.', + 58, + 'Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your %configurationFile%.', + ], + ]); + } + public function testInTrait(): void { $this->treatPhpDocTypesAsCertain = true; diff --git a/tests/PHPStan/Rules/Comparison/data/bug-15165.php b/tests/PHPStan/Rules/Comparison/data/bug-15165.php new file mode 100644 index 00000000000..5ee1e8fb635 --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-15165.php @@ -0,0 +1,66 @@ += 8.1 + +namespace Bug15165; + +use BackedEnum; +use ReflectionEnum; +use UnitEnum; + +/** + * @template T of UnitEnum + * @param ReflectionEnum $enum + * @return void + */ +function testUnit(ReflectionEnum $enum): void +{ + if ($enum->isBacked()) { + echo "Backed!"; + } +} + +/** + * @template T of BackedEnum + * @param ReflectionEnum $enum + * @return void + */ +function testBacked(ReflectionEnum $enum): void +{ + if (!$enum->isBacked()) { + echo "Unit!"; + } +} + +/** + * @template T of BackedEnum|UnitEnum + * @param ReflectionEnum $enum + * @return void + */ +function testAny(ReflectionEnum $enum): void +{ + if ($enum->isBacked()) { + echo "Backed!"; + } +} + +enum UnitFoo +{ + case A; +} + +enum BackedFoo: int +{ + case A = 1; +} + +function testConcrete(): void +{ + $unit = new ReflectionEnum(UnitFoo::class); + if ($unit->isBacked()) { + echo "Backed!"; + } + + $backed = new ReflectionEnum(BackedFoo::class); + if ($backed->isBacked()) { + echo "Backed!"; + } +} From 2db04926f4f425d95834180042128e48a9c9a8e0 Mon Sep 17 00:00:00 2001 From: phpstan-bot Date: Thu, 19 Nov 2026 10:20:20 +0000 Subject: [PATCH 2/7] Narrow `getCases()` after `isBacked()` in the PHP 8.4+ ReflectionEnum stub Co-Authored-By: Claude Opus 5.5 --- stubs/ReflectionEnumWithLazyObjects.stub | 1 + tests/PHPStan/Analyser/nsrt/bug-15165.php | 3 +++ 2 files changed, 4 insertions(+) diff --git a/stubs/ReflectionEnumWithLazyObjects.stub b/stubs/ReflectionEnumWithLazyObjects.stub index cbfc471dc52..811f785cc22 100644 --- a/stubs/ReflectionEnumWithLazyObjects.stub +++ b/stubs/ReflectionEnumWithLazyObjects.stub @@ -21,6 +21,7 @@ class ReflectionEnum extends ReflectionClass /** * @phpstan-assert-if-true class-string $this->name * @phpstan-assert-if-true class-string $this->getName() + * @phpstan-assert-if-true array $this->getCases() * @phpstan-assert-if-true !null $this->getBackingType() */ public function isBacked(): bool {} diff --git a/tests/PHPStan/Analyser/nsrt/bug-15165.php b/tests/PHPStan/Analyser/nsrt/bug-15165.php index bf9b39d37f9..4c5fa00f45a 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-15165.php +++ b/tests/PHPStan/Analyser/nsrt/bug-15165.php @@ -13,7 +13,9 @@ function testNarrowGetNameTypeAfterIsBacked(string $class): void assertType('class-string', $r->getName()); assertType('class-string', $r->name); assertType('ReflectionNamedType|null', $r->getBackingType()); + assertType('array', $r->getCases()); if ($r->isBacked()) { + assertType('array', $r->getCases()); assertType('class-string', $r->getName()); assertType('class-string', $r->name); assertType('ReflectionNamedType', $r->getBackingType()); @@ -29,5 +31,6 @@ function testTemplate(ReflectionEnum $enum): void if ($enum->isBacked()) { assertType('ReflectionEnum', $enum); assertType('ReflectionNamedType', $enum->getBackingType()); + assertType('array', $enum->getCases()); } } From 5c4cf4cd7e7e2959345eaa6ddcb1c26b62852a80 Mon Sep 17 00:00:00 2001 From: phpstan-bot Date: Thu, 19 Nov 2026 10:50:05 +0000 Subject: [PATCH 3/7] Expect no PHPDoc tip for ReflectionEnum::isBacked() impossible check on PHP < 8.4 Co-Authored-By: Claude Opus 5.5 --- .../Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php index 41870f07e99..0f07c7e1ac1 100644 --- a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php @@ -7,6 +7,7 @@ use PHPStan\Testing\RuleTestCase; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\RequiresPhp; +use const PHP_VERSION_ID; /** * @extends RuleTestCase @@ -354,7 +355,7 @@ public function testBug15165(): void [ 'Call to method ReflectionEnum::isBacked() will always evaluate to false.', 58, - 'Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your %configurationFile%.', + PHP_VERSION_ID >= 80400 ? 'Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your %configurationFile%.' : null, ], ]); } From 2d4f92bb7186c79f1f859bfa682e3f0cd99be872 Mon Sep 17 00:00:00 2001 From: phpstan-bot Date: Thu, 19 Nov 2026 12:14:30 +0000 Subject: [PATCH 4/7] Narrow ReflectionEnum::getCase() to ReflectionEnumBackedCase after isBacked() Co-Authored-By: Claude Opus 5.5 --- ...nEnumGetCaseDynamicReturnTypeExtension.php | 43 +++++++++++++++++++ tests/PHPStan/Analyser/nsrt/bug-15165.php | 4 ++ 2 files changed, 47 insertions(+) create mode 100644 src/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php diff --git a/src/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php b/src/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php new file mode 100644 index 00000000000..5cc7f9b2943 --- /dev/null +++ b/src/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php @@ -0,0 +1,43 @@ +getName() === 'getCase'; + } + + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type + { + $backingType = $scope->getType(new MethodCall($methodCall->var, new Identifier('getBackingType'))); + if (!$backingType->isNull()->no()) { + return null; + } + + return new ObjectType('ReflectionEnumBackedCase'); + } + +} diff --git a/tests/PHPStan/Analyser/nsrt/bug-15165.php b/tests/PHPStan/Analyser/nsrt/bug-15165.php index 4c5fa00f45a..59fe159006d 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-15165.php +++ b/tests/PHPStan/Analyser/nsrt/bug-15165.php @@ -14,8 +14,10 @@ function testNarrowGetNameTypeAfterIsBacked(string $class): void assertType('class-string', $r->name); assertType('ReflectionNamedType|null', $r->getBackingType()); assertType('array', $r->getCases()); + assertType('ReflectionEnumUnitCase', $r->getCase('A')); if ($r->isBacked()) { assertType('array', $r->getCases()); + assertType('ReflectionEnumBackedCase', $r->getCase('A')); assertType('class-string', $r->getName()); assertType('class-string', $r->name); assertType('ReflectionNamedType', $r->getBackingType()); @@ -28,9 +30,11 @@ function testNarrowGetNameTypeAfterIsBacked(string $class): void */ function testTemplate(ReflectionEnum $enum): void { + assertType('ReflectionEnumUnitCase', $enum->getCase('A')); if ($enum->isBacked()) { assertType('ReflectionEnum', $enum); assertType('ReflectionNamedType', $enum->getBackingType()); assertType('array', $enum->getCases()); + assertType('ReflectionEnumBackedCase', $enum->getCase('A')); } } From f25090481cb372635a4190f50732ddec243fc7c7 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 20 Nov 2026 13:08:59 +0100 Subject: [PATCH 5/7] Update baseline-7.4.neon --- build/baseline-7.4.neon | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/build/baseline-7.4.neon b/build/baseline-7.4.neon index 7dee4eae344..86558d7d9ee 100644 --- a/build/baseline-7.4.neon +++ b/build/baseline-7.4.neon @@ -77,3 +77,8 @@ parameters: message: "#^Class PHPStan\\\\Reflection\\\\BetterReflection\\\\SourceLocator\\\\CachingVisitor has an uninitialized property \\$constantNodes\\. Give it default value or assign it in the constructor\\.$#" count: 1 path: ../src/Reflection/BetterReflection/SourceLocator/CachingVisitor.php + + - + '#^Method PHPStan\\Type\\Php\\ReflectionEnumGetCaseDynamicReturnTypeExtension\:\:getClass\(\) should return class\-string but returns string\.$#' + count: 1 + path: ../src/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php From f631090ceb955d3f3af0542cb9bdf8d5877dd3f9 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 20 Nov 2026 13:39:34 +0100 Subject: [PATCH 6/7] Update baseline-7.4.neon --- build/baseline-7.4.neon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/baseline-7.4.neon b/build/baseline-7.4.neon index 86558d7d9ee..790724e0d0a 100644 --- a/build/baseline-7.4.neon +++ b/build/baseline-7.4.neon @@ -79,6 +79,6 @@ parameters: path: ../src/Reflection/BetterReflection/SourceLocator/CachingVisitor.php - - '#^Method PHPStan\\Type\\Php\\ReflectionEnumGetCaseDynamicReturnTypeExtension\:\:getClass\(\) should return class\-string but returns string\.$#' + message: '#^Method PHPStan\\Type\\Php\\ReflectionEnumGetCaseDynamicReturnTypeExtension\:\:getClass\(\) should return class\-string but returns string\.$#' count: 1 path: ../src/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php From bc2b5d002c4f18c55e64b1d2ae97f5ada474a4bb Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 20 Nov 2026 13:44:49 +0100 Subject: [PATCH 7/7] fix --- build/baseline-7.4.neon | 5 ----- build/baseline-pre-8.0.neon | 5 +++++ 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/build/baseline-7.4.neon b/build/baseline-7.4.neon index 790724e0d0a..7dee4eae344 100644 --- a/build/baseline-7.4.neon +++ b/build/baseline-7.4.neon @@ -77,8 +77,3 @@ parameters: message: "#^Class PHPStan\\\\Reflection\\\\BetterReflection\\\\SourceLocator\\\\CachingVisitor has an uninitialized property \\$constantNodes\\. Give it default value or assign it in the constructor\\.$#" count: 1 path: ../src/Reflection/BetterReflection/SourceLocator/CachingVisitor.php - - - - message: '#^Method PHPStan\\Type\\Php\\ReflectionEnumGetCaseDynamicReturnTypeExtension\:\:getClass\(\) should return class\-string but returns string\.$#' - count: 1 - path: ../src/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php diff --git a/build/baseline-pre-8.0.neon b/build/baseline-pre-8.0.neon index a59e5c583a4..03a81c8a7c1 100644 --- a/build/baseline-pre-8.0.neon +++ b/build/baseline-pre-8.0.neon @@ -271,3 +271,8 @@ parameters: identifier: catch.neverThrown count: 1 path: ../src/Type/Php/RangeFunctionArgumentsHelper.php + + - + message: '#^Method PHPStan\\Type\\Php\\ReflectionEnumGetCaseDynamicReturnTypeExtension\:\:getClass\(\) should return class\-string but returns string\.$#' + count: 1 + path: ../src/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php