Skip to content

Commit c896ac4

Browse files
Improve array filter return type on php8
1 parent 5878035 commit c896ac4

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

src/Type/Php/ArrayFilterFunctionReturnTypeHelper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\Analyser\MutatingScope;
1717
use PHPStan\Analyser\Scope;
1818
use PHPStan\DependencyInjection\AutowiredService;
19+
use PHPStan\Php\PhpVersion;
1920
use PHPStan\Reflection\ReflectionProvider;
2021
use PHPStan\ShouldNotHappenException;
2122
use PHPStan\TrinaryLogic;
@@ -47,7 +48,10 @@ final class ArrayFilterFunctionReturnTypeHelper
4748
private const USE_KEY = 2;
4849
private const USE_ITEM = 3;
4950

50-
public function __construct(private ReflectionProvider $reflectionProvider)
51+
public function __construct(
52+
private ReflectionProvider $reflectionProvider,
53+
private PhpVersion $phpVersion,
54+
)
5155
{
5256
}
5357

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

6973
if ($arrayArgType instanceof MixedType) {
74+
if ($this->phpVersion->throwsValueErrorForInternalFunctions()) {
75+
return new ArrayType(new MixedType(), new MixedType());
76+
}
77+
7078
return new BenevolentUnionType([
7179
new ArrayType(new MixedType(), new MixedType()),
7280
new NullType(),

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4854,7 +4854,7 @@ public static function dataArrayFunctions(): array
48544854
'array_filter($withPossiblyFalsey)',
48554855
],
48564856
[
4857-
'(array|null)',
4857+
PHP_VERSION_ID < 80000 ? '(array|null)' : 'array',
48584858
'array_filter($mixed)',
48594859
],
48604860
[

tests/PHPStan/Analyser/nsrt/array-filter.php renamed to tests/PHPStan/Analyser/nsrt/array-filter-php7.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<?php
1+
<?php // lint < 8.0
22

3-
namespace ArrayFilter;
3+
namespace ArrayFilterPHP7;
44

55
use function PHPStan\Testing\assertType;
66

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php // lint >= 8.0
2+
3+
namespace ArrayFilterPHP8;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function withoutAnyArgs(): void
8+
{
9+
$filtered1 = array_filter();
10+
assertType('array', $filtered1);
11+
}
12+
13+
/**
14+
* @param mixed $var1
15+
*/
16+
function withMixedInsteadOfArray($var1): void
17+
{
18+
$filtered1 = array_filter($var1);
19+
assertType('array', $filtered1);
20+
}
21+
22+
/**
23+
* @param array<string, bool|float|int|string> $map1
24+
* @param array<string, bool|float|int|string> $map2
25+
* @param array<string, bool|float|int|string> $map3
26+
*/
27+
function withoutCallback(array $map1, array $map2, array $map3): void
28+
{
29+
$filtered1 = array_filter($map1);
30+
assertType('array<string, float|int<min, -1>|int<1, max>|non-falsy-string|true>', $filtered1);
31+
32+
$filtered2 = array_filter($map2, null, ARRAY_FILTER_USE_KEY);
33+
assertType('array<string, float|int<min, -1>|int<1, max>|non-falsy-string|true>', $filtered2);
34+
35+
$filtered3 = array_filter($map3, null, ARRAY_FILTER_USE_BOTH);
36+
assertType('array<string, float|int<min, -1>|int<1, max>|non-falsy-string|true>', $filtered3);
37+
}
38+
39+
function invalidCallableName(array $arr) {
40+
assertType('*ERROR*', array_filter($arr, ''));
41+
assertType('*ERROR*', array_filter($arr, '\\'));
42+
}

0 commit comments

Comments
 (0)