-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathNodeAttributeReIndexer.php
More file actions
89 lines (72 loc) · 2.47 KB
/
NodeAttributeReIndexer.php
File metadata and controls
89 lines (72 loc) · 2.47 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
namespace Rector\Application;
use PhpParser\Node;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\Block;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\TryCatch;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class NodeAttributeReIndexer
{
public static function reIndexStmtKeyNodeAttributes(Node $node): ?Node
{
if (! $node instanceof StmtsAwareInterface && ! $node instanceof ClassLike && ! $node instanceof Declare_ && ! $node instanceof Block) {
return null;
}
if ($node->stmts === null) {
return null;
}
$node->stmts = array_values($node->stmts);
// re-index stmt key under current node
foreach ($node->stmts as $key => $childStmt) {
$childStmt->setAttribute(AttributeKey::STMT_KEY, $key);
}
return $node;
}
public static function reIndexNodeAttributes(Node $node, bool $reIndexStmtKey = true): ?Node
{
if ($reIndexStmtKey) {
self::reIndexStmtKeyNodeAttributes($node);
}
if ($node instanceof If_) {
$node->elseifs = array_values($node->elseifs);
return $node;
}
if ($node instanceof TryCatch) {
$node->catches = array_values($node->catches);
return $node;
}
if ($node instanceof FunctionLike) {
/** @var ClassMethod|Function_|Closure $node */
$node->params = array_values($node->params);
if ($node instanceof Closure) {
$node->uses = array_values($node->uses);
}
return $node;
}
if ($node instanceof CallLike) {
/** @var FuncCall|MethodCall|New_|NullsafeMethodCall|StaticCall $node */
$node->args = array_values($node->args);
return $node;
}
if ($node instanceof Switch_) {
$node->cases = array_values($node->cases);
return $node;
}
return null;
}
}