-
Notifications
You must be signed in to change notification settings - Fork 544
count(non-empty-array, COUNT_RECURSIVE) is int<1, max>
#4515
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
staabm
wants to merge
11
commits into
phpstan:2.1.x
Choose a base branch
from
staabm:recurs
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.
+220
−9
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d67dca5
`count(non-empty-array, COUNT_RECURSIVE)` is `int<1, max>`
staabm aae8b4a
Update count-recursive.php
staabm 85d4ff2
cs
staabm 5277274
test union
staabm 6ba7f92
add tests
staabm 8b630f0
more tests
staabm 15d7656
more tests
staabm 214cee1
take HasOffset*Type into account
staabm 6a1160b
cs
staabm 17c48e8
test optional keys
staabm 9d8c80e
more tests
staabm 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| <?php | ||
|
|
||
| namespace CountRecursive; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| /** | ||
| * @param array<array<mixed>> $muliDimArr | ||
| * @return void | ||
| */ | ||
| public function countMultiDim(array $muliDimArr, $mixed): void | ||
| { | ||
| if (count($muliDimArr, $mixed) > 2) { | ||
| assertType('int<1, max>', count($muliDimArr)); | ||
| assertType('int<3, max>', count($muliDimArr, $mixed)); | ||
| assertType('int<1, max>', count($muliDimArr, COUNT_NORMAL)); | ||
| assertType('int<1, max>', count($muliDimArr, COUNT_RECURSIVE)); | ||
| } | ||
| } | ||
|
|
||
| public function countUnknownArray(array $arr): void | ||
| { | ||
| assertType('array', $arr); | ||
| assertType('int<0, max>', count($arr)); | ||
| assertType('int<0, max>', count($arr, COUNT_NORMAL)); | ||
| assertType('int<0, max>', count($arr, COUNT_RECURSIVE)); | ||
| } | ||
|
|
||
| public function countEmptyArray(array $arr): void | ||
| { | ||
| if (count($arr) == 0) { | ||
| assertType('array{}', $arr); | ||
| assertType('0', count($arr)); | ||
| assertType('0', count($arr, COUNT_NORMAL)); | ||
| assertType('0', count($arr, COUNT_RECURSIVE)); | ||
| } | ||
| } | ||
|
|
||
| public function countArray(array $arr): void | ||
| { | ||
| if (count($arr) > 2) { | ||
| assertType('non-empty-array', $arr); | ||
| assertType('int<3, max>', count($arr)); | ||
| assertType('int<1, max>', count($arr, COUNT_NORMAL)); // could be int<3, max> | ||
| assertType('int<1, max>', count($arr, COUNT_RECURSIVE)); | ||
| } | ||
| } | ||
|
|
||
| public function countArrayNormal(array $arr): void | ||
| { | ||
| if (count($arr, COUNT_NORMAL) > 2) { | ||
| assertType('non-empty-array', $arr); | ||
| assertType('int<1, max>', count($arr)); // could be int<3, max> | ||
| assertType('int<3, max>', count($arr, COUNT_NORMAL)); | ||
| assertType('int<1, max>', count($arr, COUNT_RECURSIVE)); | ||
| } | ||
| } | ||
|
|
||
| public function countArrayRecursive(array $arr): void | ||
| { | ||
| if (count($arr, COUNT_RECURSIVE) > 2) { | ||
| assertType('non-empty-array', $arr); | ||
| assertType('int<1, max>', count($arr)); | ||
| assertType('int<1, max>', count($arr, COUNT_NORMAL)); | ||
| assertType('int<3, max>', count($arr, COUNT_RECURSIVE)); | ||
| } | ||
| } | ||
|
|
||
| public function countArrayUnionMode(array $arr): void | ||
| { | ||
| $mode = rand(0,1) ? COUNT_NORMAL : COUNT_RECURSIVE; | ||
| if (count($arr, $mode) > 2) { | ||
| assertType('non-empty-array', $arr); | ||
| assertType('int<3, max>', count($arr, $mode)); | ||
| assertType('int<1, max>', count($arr, COUNT_NORMAL)); | ||
| assertType('int<1, max>', count($arr, COUNT_RECURSIVE)); | ||
| } | ||
| } | ||
|
|
||
| /** @param list<int> $list */ | ||
| public function countList($list): void | ||
| { | ||
| if (count($list) > 2) { | ||
| assertType('int<3, max>', count($list)); | ||
| assertType('int<1, max>', count($list, COUNT_NORMAL)); | ||
| assertType('int<1, max>', count($list, COUNT_RECURSIVE)); | ||
| } | ||
| } | ||
|
|
||
| /** @param list<int> $list */ | ||
| public function countListNormal($list): void | ||
| { | ||
| if (count($list, COUNT_NORMAL) > 2) { | ||
| assertType('int<1, max>', count($list)); | ||
| assertType('int<3, max>', count($list, COUNT_NORMAL)); | ||
| assertType('int<1, max>', count($list, COUNT_RECURSIVE)); | ||
| } | ||
| } | ||
|
|
||
| public function countImplicitNormal($mode): void | ||
| { | ||
| $arr = [1, 2, 3]; | ||
| if (count($arr, $mode) > 2) { | ||
| assertType('3', count($arr)); | ||
| assertType('3', count($arr, $mode)); | ||
| assertType('3', count($arr, COUNT_NORMAL)); | ||
| assertType('3', count($arr, COUNT_RECURSIVE)); | ||
| } | ||
| } | ||
|
|
||
| public function countMixed($arr, $mode): void | ||
| { | ||
| if (count($arr, $mode) > 2) { | ||
| assertType('int<0, max>', count($arr)); | ||
| assertType('int<3, max>', count($arr, $mode)); | ||
| assertType('int<0, max>', count($arr, COUNT_NORMAL)); | ||
| assertType('int<0, max>', count($arr, COUNT_RECURSIVE)); | ||
| } | ||
| } | ||
|
|
||
| /** @param list<int> $list */ | ||
| public function countListRecursive($list): void | ||
| { | ||
| if (count($list, COUNT_RECURSIVE) > 2) { | ||
| assertType('int<1, max>', count($list)); | ||
| assertType('int<1, max>', count($list, COUNT_NORMAL)); | ||
| assertType('int<3, max>', count($list, COUNT_RECURSIVE)); | ||
| } | ||
| } | ||
|
|
||
| public function countConstantArray(array $anotherArray): void { | ||
| $arr = [1, 2, 3, [4, 5]]; | ||
| assertType('4', count($arr)); | ||
| assertType('4', count($arr, COUNT_NORMAL)); | ||
| assertType('int<1, max>', count($arr, COUNT_RECURSIVE)); | ||
|
|
||
| $arr = [1, 2, 3, $anotherArray]; | ||
| assertType('array{1, 2, 3, array}', $arr); | ||
| assertType('4', count($arr)); | ||
| assertType('4', count($arr, COUNT_NORMAL)); | ||
| assertType('int<1, max>', count($arr, COUNT_RECURSIVE)); // could be int<4, max> | ||
|
|
||
| if (rand(0,1)) { | ||
| $arr[] = 10; | ||
| } | ||
| assertType('array{0: 1, 1: 2, 2: 3, 3: array, 4?: 10}', $arr); | ||
| assertType('int<4, 5>', count($arr)); | ||
| assertType('int<4, 5>', count($arr, COUNT_NORMAL)); | ||
| assertType('int<1, max>', count($arr, COUNT_RECURSIVE)); // could be int<4, max> | ||
|
|
||
| $arr = [1, 2, 3] + $anotherArray; | ||
| assertType('non-empty-array&hasOffsetValue(0, 1)&hasOffsetValue(1, 2)&hasOffsetValue(2, 3)', $arr); | ||
| assertType('int<3, max>', count($arr)); | ||
| assertType('int<3, max>', count($arr, COUNT_NORMAL)); | ||
| assertType('int<1, max>', count($arr, COUNT_RECURSIVE)); // could be int<3, max> | ||
| } | ||
|
|
||
| public function countAfterKeyExists(array $array, int $i): void { | ||
| if (array_key_exists(5, $array)) { | ||
| assertType('non-empty-array&hasOffset(5)', $array); | ||
| assertType('int<1, max>', count($array)); | ||
| } | ||
|
|
||
| if ($array !== []) { | ||
| assertType('non-empty-array', $array); | ||
| assertType('int<1, max>', count($array)); | ||
| if (array_key_exists(5, $array)) { | ||
| assertType('non-empty-array&hasOffset(5)', $array); | ||
| assertType('int<1, max>', count($array)); | ||
|
|
||
| if (array_key_exists(15, $array)) { | ||
| assertType('non-empty-array&hasOffset(15)&hasOffset(5)', $array); | ||
| assertType('int<2, max>', count($array)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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.
maybe put the COUNT_MODE onto
Type->getArraySize()as a first parameter