Skip to content

Commit bd60051

Browse files
committed
Add test cases that produce errors
1 parent 46cd1a6 commit bd60051

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/Reflection/ParametersAcceptorSelector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public static function selectFromArgs(
8080
&& count($parametersAcceptors) > 0
8181
) {
8282
$arrayMapArgs = $args[0]->value->getAttribute(ArrayMapArgVisitor::ATTRIBUTE_NAME);
83+
if ($arrayMapArgs === null && isset($args[1])) {
84+
// callback argument of array_map() may be the second one (named argument)
85+
$arrayMapArgs = $args[1]->value->getAttribute(ArrayMapArgVisitor::ATTRIBUTE_NAME);
86+
}
8387
if ($arrayMapArgs !== null) {
8488
$acceptor = $parametersAcceptors[0];
8589
$parameters = $acceptor->getParameters();

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,16 @@ public function testBug12317(): void
21902190

21912191
$this->checkExplicitMixed = true;
21922192
$this->checkImplicitMixed = true;
2193-
$this->analyse([__DIR__ . '/data/bug-12317.php'], []);
2193+
$this->analyse([__DIR__ . '/data/bug-12317.php'], [
2194+
[
2195+
'Parameter #1 $callback of function array_map expects (callable(Bug12317\Uuid): mixed)|null, Closure(string): string given.',
2196+
28,
2197+
],
2198+
[
2199+
'Parameter $callback of function array_map expects (callable(Bug12317\Uuid): mixed)|null, Closure(string): string given.',
2200+
29,
2201+
],
2202+
]);
21942203
}
21952204

21962205
}

tests/PHPStan/Rules/Functions/data/bug-12317.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ public function sayHello(array $arr): void
1616
{
1717
$callback = static fn(Uuid $uuid): string => (string) $uuid;
1818

19+
// ok
1920
array_map(array: $arr, callback: $callback);
2021
array_map(callback: $callback, array: $arr);
2122
array_map($callback, $arr);
2223
array_map($callback, array: $arr);
2324
array_map(static fn (Uuid $u1, Uuid $u2): string => (string) $u1, $arr, $arr);
25+
26+
// should be reported
27+
$invalidCallback = static fn(string $uuid): string => $uuid;
28+
array_map($invalidCallback, $arr);
29+
array_map(array: $arr, callback: $invalidCallback);
2430
}
2531
}

0 commit comments

Comments
 (0)