-
Notifications
You must be signed in to change notification settings - Fork 574
Fix phpstan/phpstan#11339: filter_input with FILTER_FORCE_ARRAY has incorrect return type
#5315
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
Closed
phpstan-bot
wants to merge
5
commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-fvwtg37
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1eecbdb
Fix phpstan/phpstan#11339: filter_input with FILTER_FORCE_ARRAY has i…
github-actions[bot] 6eaa2ba
Improve precision of filter_var/filter_input return types for typed a…
phpstan-bot 9d9f614
Simplify nested array handling using TypeTraverser::map
phpstan-bot 6568982
Use TypeTraverser::map for nested array type in maybe-array input paths
phpstan-bot da9a05f
Unify TypeTraverser::map usages into single filterTypeComponents method
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,62 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug11339; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| // assume query string to be ?a[a][b][]=a&a[a][b][]=b | ||
|
|
||
| $f = filter_input(INPUT_GET, 'a', FILTER_DEFAULT, FILTER_FORCE_ARRAY); | ||
| assertType('array<array|string|false>|null', $f); | ||
|
|
||
| $g = filter_input(INPUT_GET, 'a', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); | ||
| assertType('array<array|string|false>|false|null', $g); | ||
|
|
||
| $h = filter_input(INPUT_GET, 'a', FILTER_VALIDATE_INT, FILTER_FORCE_ARRAY); | ||
| assertType('array<array|int|false>|null', $h); | ||
|
|
||
| $i = filter_input(INPUT_GET, 'a', FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY); | ||
| assertType('array<array|int|false>|false|null', $i); | ||
|
|
||
| // filter_var with known scalar should not include array in value type | ||
| $j = filter_var('foo', FILTER_DEFAULT, FILTER_FORCE_ARRAY); | ||
| assertType("array<'foo'>", $j); | ||
|
|
||
| $k = filter_var('foo', FILTER_VALIDATE_INT, FILTER_FORCE_ARRAY); | ||
| assertType('array<false>', $k); | ||
|
|
||
| /** | ||
| * @param array<int> $arrayOfInt | ||
| * @param array<array<int>> $arrayOfArrayOfInt | ||
| * @param array<int|array<int>> $arrayOfIntOrArrayOfInt | ||
| */ | ||
| function typedArrayInputs(array $arrayOfInt, array $arrayOfArrayOfInt, array $arrayOfIntOrArrayOfInt): void | ||
| { | ||
| // array<int> - values are ints, not arrays, so no array in value type | ||
| $l = filter_var($arrayOfInt, FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY]); | ||
| assertType('array<int>', $l); | ||
|
|
||
| $m = filter_var($arrayOfInt, FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY|FILTER_NULL_ON_FAILURE]); | ||
| assertType('array<int>', $m); | ||
|
|
||
| // array<array<int>> - values are arrays, recursively filtered | ||
| $n = filter_var($arrayOfArrayOfInt, FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY]); | ||
| assertType('array<array<int>>', $n); | ||
|
|
||
| $o = filter_var($arrayOfArrayOfInt, FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY|FILTER_NULL_ON_FAILURE]); | ||
| assertType('array<array<int>>', $o); | ||
|
|
||
| // array<int|array<int>> - mixed values, both scalar and array possible | ||
| $p = filter_var($arrayOfIntOrArrayOfInt, FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY]); | ||
| assertType('array<array<int>|int>', $p); | ||
|
|
||
| $q = filter_var($arrayOfIntOrArrayOfInt, FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY|FILTER_NULL_ON_FAILURE]); | ||
| assertType('array<array<int>|int>', $q); | ||
|
|
||
| // FORCE_ARRAY with typed arrays | ||
| $r = filter_var($arrayOfInt, FILTER_VALIDATE_INT, ['flags' => FILTER_FORCE_ARRAY]); | ||
| assertType('array<int>', $r); | ||
|
|
||
| $s = filter_var($arrayOfArrayOfInt, FILTER_VALIDATE_INT, ['flags' => FILTER_FORCE_ARRAY]); | ||
| assertType('array<array<int>>', $s); | ||
| } |
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
Oops, something went wrong.
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.
This might be simpler to implement with the
TypeTraverser::mapmethod.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.
Both background tasks confirmed complete — tests (11,677 passed) and phpstan (0 errors). The commit addressing the review is already pushed.