Skip to content

Commit 32edfb3

Browse files
committed
Normalize array_map args in ArrayMapArgVisitor
1 parent c5586bc commit 32edfb3

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/Parser/ArrayMapArgVisitor.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PhpParser\Node;
77
use PhpParser\NodeVisitorAbstract;
88
use PHPStan\DependencyInjection\AutowiredService;
9-
use function array_splice;
109
use function count;
1110

1211
#[AutowiredService]
@@ -27,14 +26,31 @@ public function enterNode(Node $node): ?Node
2726
return null;
2827
}
2928

30-
$callbackPos = 0;
31-
if ($args[1]->name !== null && $args[1]->name->name === 'callback') {
32-
$callbackPos = 1;
29+
$callbackArg = null;
30+
$arrayArgs = [];
31+
foreach ($args as $i => $arg) {
32+
if ($callbackArg === null) {
33+
if ($arg->name === null && $i === 0) {
34+
$callbackArg = $arg;
35+
continue;
36+
}
37+
if ($arg->name !== null && $arg->name->toString() === 'callback') {
38+
$callbackArg = $arg;
39+
continue;
40+
}
41+
}
42+
43+
$arrayArgs[] = $arg;
44+
}
45+
46+
if ($callbackArg !== null) {
47+
$callbackArg->value->setAttribute(self::ATTRIBUTE_NAME, $arrayArgs);
48+
return new Node\Expr\FuncCall(
49+
$node->name,
50+
[$callbackArg, ...$arrayArgs],
51+
$node->getAttributes(),
52+
);
3353
}
34-
$callbackArg = $args[$callbackPos];
35-
$arrayArgs = $args;
36-
array_splice($arrayArgs, $callbackPos, 1);
37-
$callbackArg->value->setAttribute(self::ATTRIBUTE_NAME, $arrayArgs);
3854

3955
return null;
4056
}

src/Reflection/ParametersAcceptorSelector.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ 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-
}
8783
if ($arrayMapArgs !== null) {
8884
$acceptor = $parametersAcceptors[0];
8985
$parameters = $acceptor->getParameters();

0 commit comments

Comments
 (0)