-
Notifications
You must be signed in to change notification settings - Fork 574
Resolve method reflection for dynamic static calls ($var::method()) to enable purity and side-effect checking
#5572
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
Merged
VincentLanglet
merged 5 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-nw3wc3z
May 3, 2026
+247
−7
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef0678d
Resolve method reflection for dynamic static calls (`$var::method()`)…
phpstan-bot e0e28b9
Simplify
VincentLanglet dea8766
Fix test
VincentLanglet 24e0653
Add non-regression test for dynamic static call with by-reference par…
phpstan-bot 998d101
Add test for dynamic static call on object instance (MyEnum $enum)
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
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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| namespace Bug5020; | ||
|
|
||
| interface ITransformer | ||
| { | ||
| public static function Transform(string $theInput, bool &$theErrorEncountered): string; | ||
| } | ||
|
|
||
| class Transformer implements ITransformer | ||
| { | ||
| public static function Transform(string $theInput, bool &$theErrorEncountered): string | ||
| { | ||
| if ($theInput === 'invalid') { | ||
| $theErrorEncountered = true; | ||
| return ''; | ||
| } | ||
| return strtoupper(trim($theInput)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string<Transformer> $transformer | ||
| */ | ||
| function foo(string $transformer): void | ||
| { | ||
| $input = ' asdasda asdasd '; | ||
| $error = false; | ||
| $output = $transformer::Transform($input, $error); | ||
| if ($error) { | ||
|
|
||
| } | ||
| } |
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
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,55 @@ | ||
| <?php // lint >= 8.1 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug14557Function; | ||
|
|
||
| enum MyEnum: string | ||
| { | ||
| case Foo = 'foo'; | ||
| case Bar = 'bar'; | ||
| } | ||
|
|
||
| /** | ||
| * @param enum-string<MyEnum> $enum | ||
| * @phpstan-pure | ||
| */ | ||
| function fromEnumString(string $enum): MyEnum | ||
| { | ||
| return $enum::from('foo'); | ||
| } | ||
|
|
||
| /** | ||
| * @param enum-string<MyEnum> $enum | ||
| * @phpstan-pure | ||
| */ | ||
| function tryFromEnumString(string $enum): ?MyEnum | ||
| { | ||
| return $enum::tryFrom('foo'); | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string<MyEnum> $enum | ||
| * @phpstan-pure | ||
| */ | ||
| function fromClassString(string $enum): MyEnum | ||
| { | ||
| return $enum::from('foo'); | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string<MyEnum> $enum | ||
| * @phpstan-pure | ||
| */ | ||
| function tryFromClassString(string $enum): ?MyEnum | ||
| { | ||
| return $enum::tryFrom('foo'); | ||
| } | ||
|
|
||
| /** | ||
| * @phpstan-pure | ||
| */ | ||
| function fromEnum(MyEnum $enum): MyEnum | ||
| { | ||
| return $enum::from('foo'); | ||
| } | ||
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,96 @@ | ||
| <?php // lint >= 8.1 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug14557; | ||
|
|
||
| enum MyEnum: string | ||
| { | ||
| case Foo = 'foo'; | ||
| case Bar = 'bar'; | ||
| } | ||
|
|
||
| class SomeClass | ||
| { | ||
|
|
||
| /** @phpstan-pure */ | ||
| public static function pureStaticMethod(): int | ||
| { | ||
| return 1; | ||
| } | ||
|
|
||
| /** @phpstan-impure */ | ||
| public static function impureStaticMethod(): int | ||
| { | ||
| echo 'hello'; | ||
| return 1; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| /** | ||
| * @param enum-string<MyEnum> $enum | ||
| * @phpstan-pure | ||
| */ | ||
| public function doFoo(string $enum): MyEnum | ||
| { | ||
| return $enum::from('foo'); | ||
| } | ||
|
|
||
| /** | ||
| * @param enum-string<MyEnum> $enum | ||
| * @phpstan-pure | ||
| */ | ||
| public function doBar(string $enum): ?MyEnum | ||
| { | ||
| return $enum::tryFrom('foo'); | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string<MyEnum> $enum | ||
| * @phpstan-pure | ||
| */ | ||
| public function doBaz(string $enum): MyEnum | ||
| { | ||
| return $enum::from('foo'); | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string<MyEnum> $enum | ||
| * @phpstan-pure | ||
| */ | ||
| public function doLorem(string $enum): ?MyEnum | ||
| { | ||
| return $enum::tryFrom('foo'); | ||
| } | ||
|
|
||
| /** | ||
| * @phpstan-pure | ||
| */ | ||
| public function fromEnum(MyEnum $enum): MyEnum | ||
| { | ||
| return $enum::from('foo'); | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string<SomeClass> $class | ||
| * @phpstan-pure | ||
| */ | ||
| public function pureViaClassString(string $class): int | ||
| { | ||
| return $class::pureStaticMethod(); | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string<SomeClass> $class | ||
| * @phpstan-pure | ||
| */ | ||
| public function impureViaClassString(string $class): int | ||
| { | ||
| return $class::impureStaticMethod(); // error | ||
| } | ||
|
|
||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
add another test for
which unexpectedly errors before this PR
https://phpstan.org/r/1dccccac-c269-4df9-9cb1-d7715a501d5b
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.
Done. Added the test case staabm requested — a pure function/method calling
$enum::from('foo')where$enumis typed asMyEnum(an object instance, not a class-string or enum-string). Added to both the function-level and method-level test files, updated the error line expectation, and all tests +make phpstanpass.