Repository navigation
Narrow getName() and $name instead of invariant self<T&BackedEnum> in the PHP 8.4+ ReflectionEnum::isBacked() stub - #7109
Conversation
…m>` 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<T>` parent is invariant). The `@phpstan-assert-if-true self<T&BackedEnum> $this` assertion intersected `ReflectionEnum<T>` with `ReflectionEnum<T&BackedEnum>`. 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<T&BackedEnum>` 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<UnitFoo>`) 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.
VincentLanglet
left a comment
There was a problem hiding this comment.
Does a check on isBacked still narrow getCases and getCase ?
… stub Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
What I changed: I added What still doesn't work:
I'd leave either for a follow-up unless you want it in this PR. The PHP < 8.4 stub is unchanged; it narrows |
VincentLanglet
left a comment
There was a problem hiding this comment.
Tests fails between 8.1 and 8.3
…on PHP < 8.4 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I've fixed the test that failed on PHP 8.1–8.3. Only What went wrong: on PHP < 8.4 PHPStan uses the older Fix: the test now expects the tip only on PHP 8.4+ and no tip on older versions ( Testing:
|
VincentLanglet
left a comment
There was a problem hiding this comment.
Why the tips is only added on PHP 8.4+ ?
|
The tip shows up only on PHP 8.4+ because the two versions use different stubs. On PHP 8.4+ the call is "always false" only because of PHPDoc types. On PHP < 8.4 a quirk in how assertions are resolved makes the native-types check give the same verdict. I didn't change any code because nothing needed fixing, so there are no commits for this review. How the tip is decided. In In PHP 8.4+ (
PHP < 8.4 (
On both versions the only evidence that |
| assertType('ReflectionNamedType|null', $r->getBackingType()); | ||
| assertType('array<ReflectionEnumUnitCase>', $r->getCases()); | ||
| if ($r->isBacked()) { | ||
| assertType('array<ReflectionEnumBackedCase>', $r->getCases()); |
There was a problem hiding this comment.
we should in addition assert the type for $r->getCase(), before and after $r->isBacked()
There was a problem hiding this comment.
I added the getCase() assertions to the test, before and after isBacked(). To make the "after" case come out right, I also added a small extension so getCase() now actually narrows. Both are in one local commit, not pushed. make tests and make phpstan both pass.
Tests (tests/PHPStan/Analyser/nsrt/bug-15165.php):
ReflectionEnum<UnitEnum>:getCase('A')isReflectionEnumUnitCasebeforeisBacked()andReflectionEnumBackedCaseinsideif ($r->isBacked()).- Templated
ReflectionEnum<T of UnitEnum>: the same two types, before and after.
The extension: before this, getCase() stayed ReflectionEnumUnitCase after isBacked(), so the "after" check would only have recorded a wrong result. A docblock assertion can't fix this, because it doesn't apply to getCase($name) calls that pass an argument. So I added src/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php. When the current scope already knows that getBackingType() on the same enum isn't null (which is what isBacked() returning true tells it), getCase() returns ReflectionEnumBackedCase. Otherwise it changes nothing. The new "after isBacked()" checks fail without the extension (they get ReflectionEnumUnitCase) and pass with it.
The extension also runs on PHP < 8.4. There the older stub already narrows getCase(), and the extension gives the same answer, so nothing should change. I only ran the tests on PHP 8.4, though, and the new test file only runs on 8.4+, so CI will be the first check on 8.1–8.3.
If you'd rather keep the extension out of this PR, I can drop it and only assert the current type, ReflectionEnumUnitCase.
…Backed() Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
On PHP 8.4+,
ReflectionEnum<T of UnitEnum>::isBacked()was reported as "will always evaluate to false", and$enumbecame*NEVER*insideif ($enum->isBacked()). This change replaces the PHP 8.4+ stub'sself<T&BackedEnum> $thisassertion with assertions on the enum's class name, which work even thoughTis invariant.Changes
stubs/ReflectionEnumWithLazyObjects.stub: replaced@phpstan-assert-if-true self<T&BackedEnum> $thiswith@phpstan-assert-if-true class-string<T&BackedEnum> $this->nameand... $this->getName(). The!null $this->getBackingType()assertion is unchanged.tests/PHPStan/Analyser/NodeScopeResolverTest.php,tests/PHPStan/Analyser/data/enum-reflection-backed.php: this test, which checks thatgetName()narrows toclass-string<BackedEnum>, now also runs on PHP 8.4+. It had been limited to PHP < 8.4 because of this problem.self<...> $thisassertion on an invariant template. The PHP < 8.4 stub (ReflectionEnum.stub) keepsself<T&BackedEnum>because itsTis@template-covariant, so the intersection works there.Root cause
On PHP 8.4+ the lazy-objects
ReflectionClassstub needs an invariantT, soReflectionEnumWithLazyObjects.stubis invariant too. Narrowing$thisfromReflectionEnum<T>toReflectionEnum<T&BackedEnum>is an intersection of two invariant generic types with different type arguments. That intersection isnever, so the impossible-check rule reported the call as always false. Asserting onclass-string<T&BackedEnum>for$this->nameand$this->getName()keeps the useful narrowing without depending on the variance ofT. For a concrete unit enum (ReflectionEnum<UnitFoo>) it still resolves tonever, so that case is still correctly reported as always false.Test
tests/PHPStan/Rules/Comparison/data/bug-15165.php+ImpossibleCheckTypeMethodCallRuleTest::testBug15165: the reproducer from the issue (no errors expected for the template cases), plus concrete enums.ReflectionEnum<UnitFoo>::isBacked()is still reported and the backed one is not.tests/PHPStan/Analyser/nsrt/bug-15165.php(PHP 8.4+):getName(),->nameandgetBackingType()narrow afterisBacked(), and a templatedReflectionEnum<T>stays as it is instead of becomingnever.Fixes phpstan/phpstan#15165