66use PhpParser \Node ;
77use PhpParser \NodeVisitorAbstract ;
88use PHPStan \DependencyInjection \AutowiredService ;
9+ use function array_slice ;
910use function count ;
1011
1112#[AutowiredService]
@@ -17,60 +18,30 @@ final class ArrayMapArgVisitor extends NodeVisitorAbstract
1718 #[Override]
1819 public function enterNode (Node $ node ): ?Node
1920 {
20- if (!$ this ->isArrayMapCall ($ node )) {
21- return null ;
22- }
23-
24- $ args = $ node ->getArgs ();
25- if (count ($ args ) < 2 ) {
26- return null ;
27- }
28-
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 ;
21+ if ($ node instanceof Node \Expr \FuncCall && $ node ->name instanceof Node \Name && !$ node ->isFirstClassCallable ()) {
22+ $ functionName = $ node ->name ->toLowerString ();
23+ if ($ functionName === 'array_map ' ) {
24+ $ args = $ node ->getArgs ();
25+ $ arrayArgs = [];
26+ foreach ($ args as $ i => $ arg ) {
27+ if ($ arg ->name === null && $ i === 0 ) {
28+ continue ;
29+ }
30+ if ($ arg ->name !== null && $ arg ->name ->toString () === 'callback ' ) {
31+ continue ;
32+ }
33+
34+ $ arrayArgs [] = $ arg ;
3635 }
37- if ($ arg ->name !== null && $ arg ->name ->toString () === 'callback ' ) {
38- $ callbackArg = $ arg ;
39- continue ;
36+ if (isset ($ args [0 ])) {
37+ $ slicedArgs = array_slice ($ args , 1 );
38+ if (count ($ slicedArgs ) > 0 ) {
39+ $ args [0 ]->value ->setAttribute (self ::ATTRIBUTE_NAME , $ arrayArgs );
40+ }
4041 }
4142 }
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- );
5343 }
54-
5544 return null ;
5645 }
5746
58- /**
59- * @phpstan-assert-if-true Node\Expr\FuncCall $node
60- */
61- private function isArrayMapCall (Node $ node ): bool
62- {
63- if (!$ node instanceof Node \Expr \FuncCall) {
64- return false ;
65- }
66- if (!$ node ->name instanceof Node \Name) {
67- return false ;
68- }
69- if ($ node ->isFirstClassCallable ()) {
70- return false ;
71- }
72-
73- return $ node ->name ->toLowerString () === 'array_map ' ;
74- }
75-
7647}
0 commit comments