-
Notifications
You must be signed in to change notification settings - Fork 574
Intersect cached count()/strlen() expression types with current argument type constraints
#5702
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
2
commits into
phpstan:2.1.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-wf2a367
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.
+145
−0
Open
Changes from 1 commit
Commits
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,84 @@ | ||
| <?php | ||
|
|
||
| namespace Bug13750; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| /** @param array<mixed> $arr */ | ||
| public function forgetCount(array $arr): void | ||
| { | ||
| if (count($arr) > 2) { | ||
| assertType('non-empty-array<mixed>', $arr); | ||
| assertType('int<3, max>', count($arr)); | ||
| } | ||
| assertType('array<mixed>', $arr); | ||
| assertType('int<0, max>', count($arr)); | ||
| if (count($arr, COUNT_RECURSIVE) > 2) { | ||
| assertType('non-empty-array<mixed>', $arr); | ||
| assertType('int<1, max>', count($arr)); | ||
| } | ||
| } | ||
|
|
||
| /** @param array<mixed> $arr */ | ||
| public function sizeofAfterCount(array $arr): void | ||
| { | ||
| if (sizeof($arr) > 2) { | ||
| assertType('int<3, max>', sizeof($arr)); | ||
| } | ||
| assertType('int<0, max>', sizeof($arr)); | ||
| if (sizeof($arr, COUNT_RECURSIVE) > 2) { | ||
| assertType('non-empty-array<mixed>', $arr); | ||
| assertType('int<1, max>', sizeof($arr)); | ||
| } | ||
| } | ||
|
|
||
| /** @param array<mixed> $arr */ | ||
| public function countAfterNonEmptyNarrowing(array $arr): void | ||
| { | ||
| if (count($arr) > 2) { | ||
| assertType('int<3, max>', count($arr)); | ||
| } | ||
| assertType('int<0, max>', count($arr)); | ||
| if ($arr !== []) { | ||
| assertType('non-empty-array<mixed>', $arr); | ||
| assertType('int<1, max>', count($arr)); | ||
| } | ||
| } | ||
|
|
||
| /** @param array<mixed> $arr */ | ||
| public function nestedCountPreservation(array $arr): void | ||
| { | ||
| if (count($arr) > 2) { | ||
| assertType('int<3, max>', count($arr)); | ||
| if (count($arr, COUNT_RECURSIVE) > 10) { | ||
| assertType('int<3, max>', count($arr)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function strlenAfterNonEmptyNarrowing(string $str): void | ||
| { | ||
| if (strlen($str) > 5) { | ||
| assertType('int<6, max>', strlen($str)); | ||
| } | ||
| assertType('int<0, max>', strlen($str)); | ||
| if ($str !== '') { | ||
| assertType('non-empty-string', $str); | ||
| assertType('int<1, max>', strlen($str)); | ||
| } | ||
| } | ||
|
|
||
| public function mbStrlenAfterNonEmptyNarrowing(string $str): void | ||
| { | ||
| if (mb_strlen($str) > 5) { | ||
| assertType('int<6, max>', mb_strlen($str)); | ||
| } | ||
| assertType('int<0, max>', mb_strlen($str)); | ||
| if ($str !== '') { | ||
| assertType('non-empty-string', $str); | ||
| assertType('int<1, max>', mb_strlen($str)); | ||
| } | ||
| } | ||
| } |
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.
why do we need to patch the types after the fact, instead of remembering the proper types in the first place?
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 what changed:
The reviewer's concern: Why patch cached types on retrieval instead of storing the correct types from the start?
The fix: Moved the refinement logic from
resolveType()(retrieval time) tospecifyExpressionType()(storage time). When a variable's type is narrowed viaspecifyExpressionType, the newrefineDependentFuncCallTypesmethod scansexpressionTypesforcount()/sizeof()/strlen()/mb_strlen()FuncCall entries whose first argument matches the narrowed variable, and intersects their cached types with the constraint derived from the new variable type. This ensures cached expression types are always correct when retrieved, rather than being patched after the fact.The
resolveType()method is back to simply returning the cached type directly.make phpstan— no errorsmake tests— all 12,105 tests pass