Skip to content

Commit

Permalink
Update array_filter signature to allow null as callback (#2740)
Browse files Browse the repository at this point in the history
  • Loading branch information
andyexeter committed Nov 17, 2023
1 parent 55b53e8 commit 8d460ac
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
17 changes: 10 additions & 7 deletions src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,16 @@ public static function selectFromArgs(
$parameters[1] = new NativeParameterReflection(
$parameters[1]->getName(),
$parameters[1]->isOptional(),
new CallableType(
$arrayFilterParameters ?? [
new DummyParameter('item', $scope->getIterableValueType($scope->getType($args[0]->value)), false, PassedByReference::createNo(), false, null),
],
new MixedType(),
false,
),
new UnionType([
new CallableType(
$arrayFilterParameters ?? [
new DummyParameter('item', $scope->getIterableValueType($scope->getType($args[0]->value)), false, PassedByReference::createNo(), false, null),
],
new MixedType(),
false,
),
new NullType(),
]),
$parameters[1]->passedByReference(),
$parameters[1]->isVariadic(),
$parameters[1]->getDefaultValue(),
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ public function testBug9994(): void
$errors = $this->runAnalyse(__DIR__ . '/data/bug-9994.php');
$this->assertCount(2, $errors);
$this->assertSame('Negated boolean expression is always false.', $errors[0]->getMessage());
$this->assertSame('Parameter #2 $callback of function array_filter expects callable(1|2|3|null): mixed, false given.', $errors[1]->getMessage());
$this->assertSame('Parameter #2 $callback of function array_filter expects (callable(1|2|3|null): mixed)|null, false given.', $errors[1]->getMessage());
}

public function testBug10049(): void
Expand Down
15 changes: 12 additions & 3 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,15 +868,15 @@ public function testArrayFilterCallback(bool $checkExplicitMixed): void
$this->checkExplicitMixed = $checkExplicitMixed;
$errors = [
[
'Parameter #2 $callback of function array_filter expects callable(int): mixed, Closure(string): true given.',
'Parameter #2 $callback of function array_filter expects (callable(int): mixed)|null, Closure(string): true given.',
17,
],
];
if ($checkExplicitMixed) {
$errors[] = [
'Parameter #2 $callback of function array_filter expects callable(mixed): mixed, Closure(int): true given.',
'Parameter #2 $callback of function array_filter expects (callable(mixed): mixed)|null, Closure(int): true given.',
20,
'Type int of parameter #1 $i of passed callable needs to be same or wider than parameter type mixed of accepting callable.',
'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.',
];
}
$this->analyse([__DIR__ . '/data/array_filter_callback.php'], $errors);
Expand Down Expand Up @@ -1549,4 +1549,13 @@ public function testBug9793(): void
$this->analyse([__DIR__ . '/data/bug-9793.php'], $errors);
}

public function testCallToArrayFilterWithNullCallback(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0');
}

$this->analyse([__DIR__ . '/data/array_filter_null_callback.php'], []);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

array_filter([], null);

0 comments on commit 8d460ac

Please sign in to comment.