-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathArrayMapArgVisitor.php
54 lines (43 loc) · 1.06 KB
/
ArrayMapArgVisitor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php declare(strict_types = 1);
namespace PHPStan\Parser;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use function array_splice;
use function count;
final class ArrayMapArgVisitor extends NodeVisitorAbstract
{
public const ATTRIBUTE_NAME = 'arrayMapArgs';
public function enterNode(Node $node): ?Node
{
if (!$this->isArrayMapCall($node)) {
return null;
}
$args = $node->getArgs();
if (count($args) < 2) {
return null;
}
$callbackPos = 0;
if ($args[1]->name !== null && $args[1]->name->name === 'callback') {
$callbackPos = 1;
}
[$callback] = array_splice($args, $callbackPos, 1);
$callback->value->setAttribute(self::ATTRIBUTE_NAME, $args);
return null;
}
/**
* @phpstan-assert-if-true Node\Expr\FuncCall $node
*/
private function isArrayMapCall(Node $node): bool
{
if (!$node instanceof Node\Expr\FuncCall) {
return false;
}
if (!$node->name instanceof Node\Name) {
return false;
}
if ($node->isFirstClassCallable()) {
return false;
}
return $node->name->toLowerString() === 'array_map';
}
}