Repository navigation
Allow ARRAY_FILTER_USE_VALUE as the mode of array_filter() and narrow its result #6681
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
Merged
staabm
merged 2 commits into
phpstan:2.3.x
from
Amoifr:feature-15372-array-filter-use-value
Oct 9, 2026
Merged
Changes from all commits
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
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,17 @@ | ||
| <?php // lint >= 8.6 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug15372; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @param list<int|null> $list | ||
| */ | ||
| function doFoo(array $list): void | ||
| { | ||
| assertType('array<int<0, max>, int>', array_filter($list, static fn ($item): bool => $item !== null, ARRAY_FILTER_USE_VALUE)); | ||
| assertType('array<int<0, max>, int>', array_filter($list, static fn ($item): bool => $item !== null, mode: ARRAY_FILTER_USE_VALUE)); | ||
| assertType('array<int<0, max>, int>', array_filter($list, static fn ($item): bool => $item !== null, 0)); | ||
| } |
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 |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| parameters: | ||
| phpVersion: 80500 | ||
| phpVersion: 80600 | ||
64 changes: 64 additions & 0 deletions
64
tests/PHPStan/Rules/Functions/CallToFunctionParametersRulePhp86Test.php
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,64 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Functions; | ||
|
|
||
| use PHPStan\Rules\FunctionCallParametersCheck; | ||
| use PHPStan\Rules\NullsafeCheck; | ||
| use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper; | ||
| use PHPStan\Rules\Properties\PropertyReflectionFinder; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleLevelHelper; | ||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<CallToFunctionParametersRule> | ||
| */ | ||
| class CallToFunctionParametersRulePhp86Test extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| $broker = self::createReflectionProvider(); | ||
| return new CallToFunctionParametersRule( | ||
| $broker, | ||
| new FunctionCallParametersCheck( | ||
| new RuleLevelHelper( | ||
| $broker, | ||
| checkNullables: true, | ||
| checkThisOnly: false, | ||
| checkUnionTypes: true, | ||
| checkExplicitMixed: true, | ||
| checkImplicitMixed: true, | ||
| checkBenevolentUnionTypes: false, | ||
| discoveringSymbolsTip: true, | ||
| ), | ||
| new NullsafeCheck(), | ||
| new UnresolvableTypeHelper(), | ||
| new PropertyReflectionFinder(), | ||
| $broker, | ||
| checkArgumentTypes: true, | ||
| checkArgumentsPassedByReference: true, | ||
| checkExtraArguments: true, | ||
| checkMissingTypehints: true, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| public function testBug15372(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/bug-15372.php'], [ | ||
| [ | ||
| 'Constant SORT_REGULAR is not allowed for parameter #3 $mode of function array_filter.', | ||
| 12, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| public static function getAdditionalConfigFiles(): array | ||
| { | ||
| return [ | ||
| __DIR__ . '/data/call-to-function-php86.neon', | ||
| ]; | ||
| } | ||
|
|
||
| } |
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,13 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug15372; | ||
|
|
||
| function (array $a): void { | ||
| array_filter($a, fn ($v) => $v > 1, ARRAY_FILTER_USE_VALUE); | ||
| array_filter($a, fn ($v) => $v > 1, mode: ARRAY_FILTER_USE_VALUE); | ||
| array_filter($a, fn ($k) => $k > 1, ARRAY_FILTER_USE_KEY); | ||
| array_filter($a, fn ($v, $k) => $v > $k, ARRAY_FILTER_USE_BOTH); | ||
| array_filter($a, fn ($v) => $v > 1, SORT_REGULAR); | ||
| }; |
2 changes: 2 additions & 0 deletions
2
tests/PHPStan/Rules/Functions/data/call-to-function-php86.neon
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,2 @@ | ||
| parameters: | ||
| phpVersion: 80600 |
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.
why do we need to adjust pre-existing tests? we shouldn't do that
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.
The map test resolves every constant of
constantToFunctionParameterMap.phpthrough reflection for thisphpVersion, andARRAY_FILTER_USE_VALUEonly exists as of PHP 8.6, so with80500it fails withConstant ARRAY_FILTER_USE_VALUE (used in array_filter($mode)) not found in reflection.With80600every existing entry is still checked and passes.If you'd rather keep this file untouched, I could make the test skip constants that don't exist for the configured version instead. Which do you prefer?