Skip to content

Commit 59ae706

Browse files
committed
Fix array_filter callback return type
1 parent f8bc397 commit 59ae706

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/Reflection/ParametersAcceptorSelector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public static function selectFromArgs(
176176
$arrayFilterParameters ?? [
177177
new DummyParameter('item', $scope->getIterableValueType($scope->getType($args[0]->value)), false, PassedByReference::createNo(), false, null),
178178
],
179-
new MixedType(),
179+
new BooleanType(),
180180
false,
181181
),
182182
new NullType(),

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,13 +868,13 @@ 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)|null, Closure(string): true given.',
871+
'Parameter #2 $callback of function array_filter expects (callable(int): bool)|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)|null, Closure(int): true given.',
877+
'Parameter #2 $callback of function array_filter expects (callable(mixed): bool)|null, Closure(int): true given.',
878878
20,
879879
'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
];
@@ -1609,4 +1609,18 @@ public function testBug9697(): void
16091609
$this->analyse([__DIR__ . '/data/bug-9697.php'], []);
16101610
}
16111611

1612+
public function testDiscussion10454(): void
1613+
{
1614+
$this->analyse([__DIR__ . '/data/discussion-10454.php'], [
1615+
[
1616+
"Parameter #2 \$callback of function array_filter expects (callable('bar'|'baz'|'foo'|'quux'|'qux'): bool)|null, Closure(string): stdClass given.",
1617+
13,
1618+
],
1619+
[
1620+
"Parameter #2 \$callback of function array_filter expects (callable('bar'|'baz'|'foo'|'quux'|'qux'): bool)|null, Closure(string): stdClass given.",
1621+
23,
1622+
],
1623+
]);
1624+
}
1625+
16121626
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Discussion10454;
4+
5+
use stdClass;
6+
7+
function (): void {
8+
$arr = ['foo', 'bar', 'baz', 'qux', 'quux'];
9+
10+
print_r(
11+
array_filter(
12+
$arr,
13+
function (string $foo): stdClass
14+
{
15+
return new stdClass();
16+
}
17+
)
18+
);
19+
20+
print_r(
21+
array_filter(
22+
$arr,
23+
function (string $foo)
24+
{
25+
return new stdClass();
26+
}
27+
)
28+
);
29+
};

0 commit comments

Comments
 (0)