Skip to content

Improve array filter return type on php8 #4123

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
merged 1 commit into from
Jul 21, 2025
Merged
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
10 changes: 9 additions & 1 deletion src/Type/Php/ArrayFilterFunctionReturnTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
Expand Down Expand Up @@ -47,7 +48,10 @@ final class ArrayFilterFunctionReturnTypeHelper
private const USE_KEY = 2;
private const USE_ITEM = 3;

public function __construct(private ReflectionProvider $reflectionProvider)
public function __construct(
private ReflectionProvider $reflectionProvider,
private PhpVersion $phpVersion,
)
{
}

Expand All @@ -67,6 +71,10 @@ public function getType(Scope $scope, ?Expr $arrayArg, ?Expr $callbackArg, ?Expr
}

if ($arrayArgType instanceof MixedType) {
if ($this->phpVersion->throwsValueErrorForInternalFunctions()) {
return new ArrayType(new MixedType(), new MixedType());
}

return new BenevolentUnionType([
new ArrayType(new MixedType(), new MixedType()),
new NullType(),
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4854,7 +4854,7 @@ public static function dataArrayFunctions(): array
'array_filter($withPossiblyFalsey)',
],
[
'(array|null)',
PHP_VERSION_ID < 80000 ? '(array|null)' : 'array',
'array_filter($mixed)',
],
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
<?php // lint < 8.0

namespace ArrayFilter;
namespace ArrayFilterPHP7;

use function PHPStan\Testing\assertType;

Expand Down
42 changes: 42 additions & 0 deletions tests/PHPStan/Analyser/nsrt/array-filter-php8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php // lint >= 8.0

namespace ArrayFilterPHP8;

use function PHPStan\Testing\assertType;

function withoutAnyArgs(): void
{
$filtered1 = array_filter();
assertType('array', $filtered1);
}

/**
* @param mixed $var1
*/
function withMixedInsteadOfArray($var1): void
{
$filtered1 = array_filter($var1);
assertType('array', $filtered1);
}

/**
* @param array<string, bool|float|int|string> $map1
* @param array<string, bool|float|int|string> $map2
* @param array<string, bool|float|int|string> $map3
*/
function withoutCallback(array $map1, array $map2, array $map3): void
{
$filtered1 = array_filter($map1);
assertType('array<string, float|int<min, -1>|int<1, max>|non-falsy-string|true>', $filtered1);

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

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

function invalidCallableName(array $arr) {
assertType('*ERROR*', array_filter($arr, ''));
assertType('*ERROR*', array_filter($arr, '\\'));
}
Loading