Skip to content

Commit

Permalink
Fix array_filter callback return type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 19, 2024
1 parent f8bc397 commit 59ae706
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static function selectFromArgs(
$arrayFilterParameters ?? [
new DummyParameter('item', $scope->getIterableValueType($scope->getType($args[0]->value)), false, PassedByReference::createNo(), false, null),
],
new MixedType(),
new BooleanType(),
false,
),
new NullType(),
Expand Down
18 changes: 16 additions & 2 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,13 +868,13 @@ public function testArrayFilterCallback(bool $checkExplicitMixed): void
$this->checkExplicitMixed = $checkExplicitMixed;
$errors = [
[
'Parameter #2 $callback of function array_filter expects (callable(int): mixed)|null, Closure(string): true given.',
'Parameter #2 $callback of function array_filter expects (callable(int): bool)|null, Closure(string): true given.',
17,
],
];
if ($checkExplicitMixed) {
$errors[] = [
'Parameter #2 $callback of function array_filter expects (callable(mixed): mixed)|null, Closure(int): true given.',
'Parameter #2 $callback of function array_filter expects (callable(mixed): bool)|null, Closure(int): true given.',
20,
'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.',
];
Expand Down Expand Up @@ -1609,4 +1609,18 @@ public function testBug9697(): void
$this->analyse([__DIR__ . '/data/bug-9697.php'], []);
}

public function testDiscussion10454(): void
{
$this->analyse([__DIR__ . '/data/discussion-10454.php'], [
[
"Parameter #2 \$callback of function array_filter expects (callable('bar'|'baz'|'foo'|'quux'|'qux'): bool)|null, Closure(string): stdClass given.",
13,
],
[
"Parameter #2 \$callback of function array_filter expects (callable('bar'|'baz'|'foo'|'quux'|'qux'): bool)|null, Closure(string): stdClass given.",
23,
],
]);
}

}
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Functions/data/discussion-10454.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Discussion10454;

use stdClass;

function (): void {
$arr = ['foo', 'bar', 'baz', 'qux', 'quux'];

print_r(
array_filter(
$arr,
function (string $foo): stdClass
{
return new stdClass();
}
)
);

print_r(
array_filter(
$arr,
function (string $foo)
{
return new stdClass();
}
)
);
};

0 comments on commit 59ae706

Please sign in to comment.