-
Notifications
You must be signed in to change notification settings - Fork 574
Invalidate maybe-impure function return values after impure method/static calls #5667
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
11
commits into
phpstan:2.1.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-vf3uivq
base: 2.1.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.
+240
−80
Open
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e9ba032
Invalidate maybe-impure function return values after impure method/st…
VincentLanglet 923d2b1
Prevent type narrowing of expressions containing impure calls
phpstan-bot e731e88
Remove redundant early returns subsumed by expressionContainsNonPureCall
phpstan-bot 452a3c8
Return TrinaryLogic from expressionContainsNonPureCall
phpstan-bot 7687df3
Add tests for impure closures and arrow functions
phpstan-bot 1fd7cb8
Revert "Return TrinaryLogic from expressionContainsNonPureCall"
staabm 968e01a
Update TypeSpecifier.php
staabm aadf81a
Do not treat unknown functions as non-pure in sub-expression purity c…
phpstan-bot 9d80f7d
Add non-regression test for unknown function sub-expression handling
phpstan-bot 5ef8c3d
Replace unit test with NSRT integration test for FAUX_FUNCTION handling
phpstan-bot 159e29a
Restore unit test for unknown function sub-expression handling
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug13416; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class MyRecord | ||
| { | ||
| /** @var list<self> */ | ||
| private static array $storage = []; | ||
|
|
||
| /** | ||
| * @return list<self> | ||
| * @phpstan-impure | ||
| */ | ||
| public static function find(): array | ||
| { | ||
| return self::$storage; | ||
| } | ||
|
|
||
| /** @phpstan-impure */ | ||
| public function insert(): void | ||
| { | ||
| self::$storage[] = $this; | ||
| } | ||
|
|
||
| /** | ||
| * @return non-empty-string | ||
| * @phpstan-impure | ||
| */ | ||
| public function getName(): string | ||
| { | ||
| return 'test'; | ||
| } | ||
| } | ||
|
|
||
| class Repository | ||
| { | ||
| /** | ||
| * @return list<MyRecord> | ||
| * @phpstan-impure | ||
| */ | ||
| public function findAll(): array | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| /** @phpstan-impure */ | ||
| public function save(MyRecord $record): void | ||
| { | ||
| } | ||
| } | ||
|
|
||
| function testImpureStaticCallNotNarrowedByCount(): void | ||
| { | ||
| assert(count(MyRecord::find()) === 1); | ||
| // Impure call result should not be narrowed | ||
| assertType('int<0, max>', count(MyRecord::find())); | ||
| } | ||
|
|
||
| function testImpureMethodCallNotNarrowedByCount(): void | ||
| { | ||
| $repo = new Repository(); | ||
|
|
||
| assert(count($repo->findAll()) === 1); | ||
| // Impure call result should not be narrowed | ||
| assertType('int<0, max>', count($repo->findAll())); | ||
| } | ||
|
|
||
| function testStrlenOfImpureCallNotNarrowed(): void | ||
| { | ||
| $record = new MyRecord(); | ||
|
|
||
| assert(strlen($record->getName()) === 3); | ||
| // strlen wrapping an impure call should not be narrowed | ||
| assertType('int<1, max>', strlen($record->getName())); | ||
| } | ||
|
|
||
| function testPureFunctionStaysNarrowed(): void | ||
| { | ||
| /** @var list<int> $arr */ | ||
| $arr = [1]; | ||
| assert(count($arr) === 1); | ||
| assertType('1', count($arr)); | ||
|
|
||
| $x = rand(0, 10); | ||
|
|
||
| // Pure expressions stay narrowed | ||
| assertType('1', count($arr)); | ||
| } |
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.
do we have the same bug on impure
ArrowFunctions and impureClosures?if so, add tests and expand the callable to cover it
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.
Pushed. Here's a summary of the changes:
Investigation: The reviewer asked whether impure
ArrowFunctions andClosures are handled byexpressionContainsNonPureCall. The answer is yes, the current code handles them correctly through two mechanisms:(fn() => MyRecord::find())())ClosureType::isPure()viagetCallableParametersAcceptors(), which catches impurity from non-call sources likeechoTests added (7 new test cases):
strlenwrapping impure arrow function IIFEecho+ impure calls in bodyAll 12071 tests pass and PHPStan self-analysis reports no errors.