-
Notifications
You must be signed in to change notification settings - Fork 578
An optional non-list key makes isList Maybe, not No #6025
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
zonuexe
wants to merge
5
commits into
phpstan:2.2.x
Choose a base branch
from
zonuexe:fix-islist-optional-keys
base: 2.2.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
5 commits
Select commit
Hold shift + click to select a range
830f4db
An optional non-list key makes isList Maybe, not No
zonuexe 9fa048d
fixup! An optional non-list key makes isList Maybe, not No
zonuexe 7f213ca
fixup! An optional non-list key makes isList Maybe, not No
zonuexe af8a2ef
fixup! An optional non-list key makes isList Maybe, not No
zonuexe 702067f
fixup! An optional non-list key makes isList Maybe, not No
zonuexe 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
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,86 @@ | ||
| <?php | ||
|
|
||
| namespace Bug14938; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| /** | ||
| * @param array{a?: string} $optStr | ||
| * @param array{0: int, a?: string} $listPlusOptStr | ||
| * @param array{-1?: string} $optNeg | ||
| * @param array{0: int, 5?: string} $listPlusGap | ||
| * @param array{a: string} $reqStr | ||
| * @param array{1: string} $gapReq | ||
| * @param array{0?: string} $optZero | ||
| */ | ||
| public function doFoo( | ||
| array $optStr, | ||
| array $listPlusOptStr, | ||
| array $optNeg, | ||
| array $listPlusGap, | ||
| array $reqStr, | ||
| array $gapReq, | ||
| array $optZero, | ||
| ): void | ||
| { | ||
| // An optional non-list key might be absent, so the array can still be | ||
| // a list ([] / the list prefix) — array_is_list() is not decidable. | ||
| assertType('bool', array_is_list($optStr)); | ||
| assertType('bool', array_is_list($listPlusOptStr)); | ||
| assertType('bool', array_is_list($optNeg)); | ||
| assertType('bool', array_is_list($listPlusGap)); | ||
|
|
||
| // A required non-list key is always present, so it is never a list. | ||
| assertType('false', array_is_list($reqStr)); | ||
| assertType('false', array_is_list($gapReq)); | ||
|
|
||
| // Only an optional key 0 keeps it a guaranteed list ([] and [v]). | ||
| assertType('true', array_is_list($optZero)); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{a?: string} $optStr | ||
| */ | ||
| public function narrowing(array $optStr): void | ||
| { | ||
| if (array_is_list($optStr)) { | ||
| assertType('list{}&list', $optStr); | ||
| } else { | ||
| // array_is_list()'s false branch is not narrowed, so `a` stays optional | ||
| // here rather than being refined to array{a: string}. | ||
| assertType('array{a?: string}', $optStr); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| } | ||
| } | ||
|
|
||
| public function mergedShapes(): void | ||
| { | ||
| // Merging a list with a shape that adds a string key: the empty-of-the-extra | ||
| // realization is still a list, so array_is_list() is not decidable. | ||
| $a = [0 => 'z']; | ||
| if (rand(0, 1)) { | ||
| $a['y'] = 1; | ||
| } | ||
| assertType("array{0: 'z', y?: 1}", $a); | ||
| assertType('bool', array_is_list($a)); | ||
|
|
||
| // Two pure lists merge into a list (optional keys stay a suffix). | ||
| $b = [0 => 'z']; | ||
| if (rand(0, 1)) { | ||
| $b[1] = 'w'; | ||
| } | ||
| assertType("array{0: 'z', 1?: 'w'}", $b); | ||
| assertType('true', array_is_list($b)); | ||
|
|
||
| // Shapes disjoint except for the empty array still admit the empty list. | ||
| $c = []; | ||
| if (rand(0, 1)) { | ||
| $c['a'] = 1; | ||
| } | ||
| assertType('array{}|array{a: 1}', $c); | ||
| assertType('bool', array_is_list($c)); | ||
| } | ||
|
|
||
| } | ||
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
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.
Before this fix
array{a?: string}hadisList = No, so this branch was*NEVER*(unreachable). WithisListnowMaybe, the empty-array realisation survives theis listintersection, so the branch is reachable and narrows to the empty list.