Skip to content
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

Update array_filter signature in PHP 8.0 to allow null as callback #2740

Merged
merged 6 commits into from
Nov 17, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);