Skip to content

Fix array_filter narrowing with explicit null callback #1085

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use function array_map;
use function count;
use function is_string;
use function strtolower;

class ArrayFilterFunctionReturnTypeReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
Expand Down Expand Up @@ -63,7 +62,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
]);
}

if ($callbackArg === null || ($callbackArg instanceof ConstFetch && strtolower($callbackArg->name->parts[0]) === 'null')) {
if ($callbackArg === null) {
return TypeCombinator::union(
...array_map([$this, 'removeFalsey'], TypeUtils::getArrays($arrayArgType)),
);
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/data/array-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ function withoutCallback(array $map1, array $map2, array $map3): void
assertType('array<string, float|int<min, -1>|int<1, max>|non-empty-string|true>', $filtered1);

$filtered2 = array_filter($map2, null, ARRAY_FILTER_USE_KEY);
assertType('array<string, float|int<min, -1>|int<1, max>|non-empty-string|true>', $filtered2);
assertType('array<string, bool|float|int|string>', $filtered2);

$filtered3 = array_filter($map3, null, ARRAY_FILTER_USE_BOTH);
assertType('array<string, float|int<min, -1>|int<1, max>|non-empty-string|true>', $filtered3);
assertType('array<string, bool|float|int|string>', $filtered3);
}