Skip to content

Commit 8d460ac

Browse files
authored
Update array_filter signature to allow null as callback (#2740)
1 parent 55b53e8 commit 8d460ac

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

src/Reflection/ParametersAcceptorSelector.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,16 @@ public static function selectFromArgs(
171171
$parameters[1] = new NativeParameterReflection(
172172
$parameters[1]->getName(),
173173
$parameters[1]->isOptional(),
174-
new CallableType(
175-
$arrayFilterParameters ?? [
176-
new DummyParameter('item', $scope->getIterableValueType($scope->getType($args[0]->value)), false, PassedByReference::createNo(), false, null),
177-
],
178-
new MixedType(),
179-
false,
180-
),
174+
new UnionType([
175+
new CallableType(
176+
$arrayFilterParameters ?? [
177+
new DummyParameter('item', $scope->getIterableValueType($scope->getType($args[0]->value)), false, PassedByReference::createNo(), false, null),
178+
],
179+
new MixedType(),
180+
false,
181+
),
182+
new NullType(),
183+
]),
181184
$parameters[1]->passedByReference(),
182185
$parameters[1]->isVariadic(),
183186
$parameters[1]->getDefaultValue(),

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ public function testBug9994(): void
12391239
$errors = $this->runAnalyse(__DIR__ . '/data/bug-9994.php');
12401240
$this->assertCount(2, $errors);
12411241
$this->assertSame('Negated boolean expression is always false.', $errors[0]->getMessage());
1242-
$this->assertSame('Parameter #2 $callback of function array_filter expects callable(1|2|3|null): mixed, false given.', $errors[1]->getMessage());
1242+
$this->assertSame('Parameter #2 $callback of function array_filter expects (callable(1|2|3|null): mixed)|null, false given.', $errors[1]->getMessage());
12431243
}
12441244

12451245
public function testBug10049(): void

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -868,15 +868,15 @@ public function testArrayFilterCallback(bool $checkExplicitMixed): void
868868
$this->checkExplicitMixed = $checkExplicitMixed;
869869
$errors = [
870870
[
871-
'Parameter #2 $callback of function array_filter expects callable(int): mixed, Closure(string): true given.',
871+
'Parameter #2 $callback of function array_filter expects (callable(int): mixed)|null, Closure(string): true given.',
872872
17,
873873
],
874874
];
875875
if ($checkExplicitMixed) {
876876
$errors[] = [
877-
'Parameter #2 $callback of function array_filter expects callable(mixed): mixed, Closure(int): true given.',
877+
'Parameter #2 $callback of function array_filter expects (callable(mixed): mixed)|null, Closure(int): true given.',
878878
20,
879-
'Type int of parameter #1 $i of passed callable needs to be same or wider than parameter type mixed of accepting callable.',
879+
'Type #1 from the union: Type int of parameter #1 $i of passed callable needs to be same or wider than parameter type mixed of accepting callable.',
880880
];
881881
}
882882
$this->analyse([__DIR__ . '/data/array_filter_callback.php'], $errors);
@@ -1549,4 +1549,13 @@ public function testBug9793(): void
15491549
$this->analyse([__DIR__ . '/data/bug-9793.php'], $errors);
15501550
}
15511551

1552+
public function testCallToArrayFilterWithNullCallback(): void
1553+
{
1554+
if (PHP_VERSION_ID < 80000) {
1555+
$this->markTestSkipped('Test requires PHP 8.0');
1556+
}
1557+
1558+
$this->analyse([__DIR__ . '/data/array_filter_null_callback.php'], []);
1559+
}
1560+
15521561
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
array_filter([], null);

0 commit comments

Comments
 (0)