-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathNodeAttributeReIndexer.php
More file actions
81 lines (65 loc) · 2.09 KB
/
NodeAttributeReIndexer.php
File metadata and controls
81 lines (65 loc) · 2.09 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
<?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\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\TryCatch;
use Rector\PhpParser\Enum\NodeGroup;
use Webmozart\Assert\Assert;
final class NodeAttributeReIndexer
{
public static function reIndexNodeAttributes(Node $node): ?Node
{
self::reIndexStmtsKeys($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;
}
private static function reIndexStmtsKeys(Node $node): ?Node
{
if (! NodeGroup::isStmtAwareNode($node) && ! $node instanceof ClassLike) {
return null;
}
Assert::propertyExists($node, 'stmts');
if ($node->stmts === null) {
return null;
}
$node->stmts = array_values($node->stmts);
return $node;
}
}