Repository navigation
Narrow getName() and $name instead of invariant self<T&BackedEnum> in the PHP 8.4+ ReflectionEnum::isBacked() stub
#7109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
phpstan-bot
wants to merge
4
commits into
phpstan:2.3.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-09th3ho
base: 2.3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a4103e2
Narrow `getName()` and `$name` instead of invariant `self<T&BackedEnu…
VincentLanglet 7f73829
Narrow `getCases()` after `isBacked()` in the PHP 8.4+ ReflectionEnum…
phpstan-bot b73adad
Expect no PHPDoc tip for ReflectionEnum::isBacked() impossible check …
phpstan-bot 4168f51
Narrow ReflectionEnum::getCase() to ReflectionEnumBackedCase after is…
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\Php; | ||
|
|
||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Identifier; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Reflection\MethodReflection; | ||
| use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
| use PHPStan\Type\ObjectType; | ||
| use PHPStan\Type\Type; | ||
|
|
||
| /** | ||
| * Narrows ReflectionEnum::getCase() to ReflectionEnumBackedCase once getBackingType() | ||
| * is known to be non-null (e.g. after isBacked()), which the conditional return type | ||
| * cannot express when the template type of ReflectionEnum is invariant. | ||
| */ | ||
| #[AutowiredService] | ||
| final class ReflectionEnumGetCaseDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
| { | ||
|
|
||
| public function getClass(): string | ||
| { | ||
| return 'ReflectionEnum'; | ||
|
Check failure on line 25 in src/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php
|
||
| } | ||
|
|
||
| public function isMethodSupported(MethodReflection $methodReflection): bool | ||
| { | ||
| return $methodReflection->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'); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| <?php // lint < 8.4 | ||
| <?php // lint >= 8.1 | ||
|
|
||
| namespace EnumReflection; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php // lint >= 8.4 | ||
|
|
||
| namespace Bug15165Types; | ||
|
|
||
| use ReflectionEnum; | ||
| use UnitEnum; | ||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** @param class-string<UnitEnum> $class */ | ||
| function testNarrowGetNameTypeAfterIsBacked(string $class): void | ||
| { | ||
| $r = new ReflectionEnum($class); | ||
| assertType('class-string<UnitEnum>', $r->getName()); | ||
| assertType('class-string<UnitEnum>', $r->name); | ||
| assertType('ReflectionNamedType|null', $r->getBackingType()); | ||
| assertType('array<ReflectionEnumUnitCase>', $r->getCases()); | ||
| assertType('ReflectionEnumUnitCase', $r->getCase('A')); | ||
| if ($r->isBacked()) { | ||
| assertType('array<ReflectionEnumBackedCase>', $r->getCases()); | ||
| assertType('ReflectionEnumBackedCase', $r->getCase('A')); | ||
| assertType('class-string<BackedEnum>', $r->getName()); | ||
| assertType('class-string<BackedEnum>', $r->name); | ||
| assertType('ReflectionNamedType', $r->getBackingType()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @template T of UnitEnum | ||
| * @param ReflectionEnum<T> $enum | ||
| */ | ||
| function testTemplate(ReflectionEnum $enum): void | ||
| { | ||
| assertType('ReflectionEnumUnitCase', $enum->getCase('A')); | ||
| if ($enum->isBacked()) { | ||
| assertType('ReflectionEnum<T of UnitEnum (function Bug15165Types\testTemplate(), argument)>', $enum); | ||
| assertType('ReflectionNamedType', $enum->getBackingType()); | ||
| assertType('array<ReflectionEnumBackedCase>', $enum->getCases()); | ||
| assertType('ReflectionEnumBackedCase', $enum->getCase('A')); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <?php // lint >= 8.1 | ||
|
|
||
| namespace Bug15165; | ||
|
|
||
| use BackedEnum; | ||
| use ReflectionEnum; | ||
| use UnitEnum; | ||
|
|
||
| /** | ||
| * @template T of UnitEnum | ||
| * @param ReflectionEnum<T> $enum | ||
| * @return void | ||
| */ | ||
| function testUnit(ReflectionEnum $enum): void | ||
| { | ||
| if ($enum->isBacked()) { | ||
| echo "Backed!"; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @template T of BackedEnum | ||
| * @param ReflectionEnum<T> $enum | ||
| * @return void | ||
| */ | ||
| function testBacked(ReflectionEnum $enum): void | ||
| { | ||
| if (!$enum->isBacked()) { | ||
| echo "Unit!"; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @template T of BackedEnum|UnitEnum | ||
| * @param ReflectionEnum<T> $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!"; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should in addition assert the type for
$r->getCase(), before and after$r->isBacked()There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the
getCase()assertions to the test, before and afterisBacked(). To make the "after" case come out right, I also added a small extension sogetCase()now actually narrows. Both are in one local commit, not pushed.make testsandmake phpstanboth pass.Tests (
tests/PHPStan/Analyser/nsrt/bug-15165.php):ReflectionEnum<UnitEnum>:getCase('A')isReflectionEnumUnitCasebeforeisBacked()andReflectionEnumBackedCaseinsideif ($r->isBacked()).ReflectionEnum<T of UnitEnum>: the same two types, before and after.The extension: before this,
getCase()stayedReflectionEnumUnitCaseafterisBacked(), so the "after" check would only have recorded a wrong result. A docblock assertion can't fix this, because it doesn't apply togetCase($name)calls that pass an argument. So I addedsrc/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php. When the current scope already knows thatgetBackingType()on the same enum isn't null (which is whatisBacked()returning true tells it),getCase()returnsReflectionEnumBackedCase. Otherwise it changes nothing. The new "afterisBacked()" checks fail without the extension (they getReflectionEnumUnitCase) 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.