Repository navigation
Allow ARRAY_FILTER_USE_VALUE as the mode of array_filter() and narrow its result - #6681
Conversation
| @@ -1,2 +1,2 @@ | |||
| parameters: | |||
| phpVersion: 80500 | |||
| phpVersion: 80600 | |||
There was a problem hiding this comment.
why do we need to adjust pre-existing tests? we shouldn't do that
There was a problem hiding this comment.
The map test resolves every constant of constantToFunctionParameterMap.php through reflection for this phpVersion, and ARRAY_FILTER_USE_VALUE only exists as of PHP 8.6, so with 80500 it fails with Constant ARRAY_FILTER_USE_VALUE (used in array_filter($mode)) not found in reflection. With 80600 every 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?
There was a problem hiding this comment.
🟢 Approval recommended
The implementation is localized, consistent with existing mode handling, and covered by targeted regression tests.
0 open findings
What changed in this PR
Adds PHP 8.6 support for ARRAY_FILTER_USE_VALUE in array_filter() validation and return-type narrowing.
Changes:
- Allows
ARRAY_FILTER_USE_VALUEas the$mode. - Treats mode
0as value filtering for type narrowing. - Adds rule and type-inference regression coverage.
| File | Description |
|---|---|
resources/constantToFunctionParameterMap.php |
Registers the new allowed mode. |
src/Type/Php/ArrayFilterFunctionReturnTypeHelper.php |
Maps mode 0 to value filtering. |
tests/PHPStan/Analyser/nsrt/bug-15372.php |
Verifies narrowed result types. |
tests/PHPStan/Reflection/constantToFunctionParameterMap.neon |
Tests constants against PHP 8.6. |
tests/PHPStan/Rules/Functions/CallToFunctionParametersRulePhp86Test.php |
Adds PHP 8.6 rule coverage. |
tests/PHPStan/Rules/Functions/data/bug-15372.php |
Covers valid and invalid modes. |
tests/PHPStan/Rules/Functions/data/call-to-function-php86.neon |
Configures PHP 8.6 for the rule test. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
467ee83 to
594e241
Compare
|
thank you! |
Closes phpstan/phpstan#15372
PHP 8.6 adds
ARRAY_FILTER_USE_VALUE(0) as an explicit$modeforarray_filter(). It is now part of the allowed constants for that parameter. The parameter takes a single value, so combining it with another mode is still reported asargument.bitmaskNotAllowed.While testing it, I noticed the result was not narrowed with this mode:
ArrayFilterFunctionReturnTypeHelper::determineMode()only recognised the values ofARRAY_FILTER_USE_KEYandARRAY_FILTER_USE_BOTH, soARRAY_FILTER_USE_VALUE(and a literal0) gavearray<int, int|null>where omitting the mode givesarray<int, int>. A value of0now maps to the default mode.The rule test runs with
phpVersion: 80600. The type inference test needs the constant at runtime, so it is gated with// lint >= 8.6.