Skip to content

Commit 6a5496c

Browse files
committed
Add test cases that produce errors
1 parent 0b072cf commit 6a5496c

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
@@ -1944,7 +1944,16 @@ public function testBug12317(): void
19441944

19451945
$this->checkExplicitMixed = true;
19461946
$this->checkImplicitMixed = true;
1947-
$this->analyse([__DIR__ . '/data/bug-12317.php'], []);
1947+
$this->analyse([__DIR__ . '/data/bug-12317.php'], [
1948+
[
1949+
'Parameter #1 $callback of function array_map expects (callable(Bug12317\Uuid): mixed)|null, Closure(string): string given.',
1950+
28,
1951+
],
1952+
[
1953+
'Parameter $callback of function array_map expects (callable(Bug12317\Uuid): mixed)|null, Closure(string): string given.',
1954+
29,
1955+
],
1956+
]);
19481957
}
19491958

19501959
}

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)