-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathChangedNodeScopeRefresher.php
More file actions
158 lines (129 loc) · 4.63 KB
/
ChangedNodeScopeRefresher.php
File metadata and controls
158 lines (129 loc) · 4.63 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
declare(strict_types=1);
namespace Rector\Application;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\ClosureUse;
use PhpParser\Node\DeclareItem;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Param;
use PhpParser\Node\PropertyItem;
use PhpParser\Node\StaticVar;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Static_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\UseItem;
use PHPStan\Analyser\MutatingScope;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeAnalyzer\ScopeAnalyzer;
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
/**
* In case of changed node, we need to re-traverse the PHPStan Scope to make all the new nodes aware of what is going on.
*/
final readonly class ChangedNodeScopeRefresher
{
public function __construct(
private PHPStanNodeScopeResolver $phpStanNodeScopeResolver,
private ScopeAnalyzer $scopeAnalyzer,
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser
) {
}
public function refresh(Node $node, string $filePath, ?MutatingScope $mutatingScope): void
{
// nothing to refresh
if (! $this->scopeAnalyzer->isRefreshable($node)) {
return;
}
if (! $mutatingScope instanceof MutatingScope) {
$errorMessage = sprintf('Node "%s" with is missing scope required for scope refresh', $node::class);
throw new ShouldNotHappenException($errorMessage);
}
$stmts = $this->resolveStmts($node);
$this->phpStanNodeScopeResolver->processNodes($stmts, $filePath, $mutatingScope);
}
/**
* @return Stmt[]
*/
private function resolveStmts(Node $node): array
{
if ($node instanceof Stmt) {
return [$node];
}
if ($node instanceof Expr) {
return [new Expression($node)];
}
// moved from Expr/Stmt to directly under Node on PHPParser 5
if ($node instanceof ArrayItem) {
return [new Expression(new Array_([$node]))];
}
if ($node instanceof ClosureUse) {
$closure = new Closure();
$closure->uses[] = $node;
return [new Expression($closure)];
}
if ($node instanceof DeclareItem) {
return [new Declare_([$node])];
}
if ($node instanceof PropertyItem) {
return [new Property(Modifiers::PUBLIC, [$node])];
}
if ($node instanceof StaticVar) {
return [new Static_([$node])];
}
if ($node instanceof UseItem) {
return [new Use_([$node])];
}
if ($node instanceof Param) {
$closure = new Closure();
$closure->params[] = $node;
return [new Expression($closure)];
}
if ($node instanceof AttributeGroup) {
$class = new Class_(null);
$class->attrGroups[] = $node;
$this->setLineAttributesOnClass($class, $node);
return [$class];
}
if ($node instanceof Attribute) {
$class = new Class_(null);
$class->attrGroups[] = new AttributeGroup([$node]);
$this->setLineAttributesOnClass($class, $node);
return [$class];
}
if ($node instanceof Arg) {
$class = new Class_(null, [], [
'startLine' => $node->getStartLine(),
'endLine' => $node->getEndLine(),
]);
$new = new New_($class, [$node]);
return [new Expression($new)];
}
$errorMessage = sprintf('Complete parent node of "%s" be a stmt.', $node::class);
throw new ShouldNotHappenException($errorMessage);
}
private function setLineAttributesOnClass(Class_ $class, Attribute|AttributeGroup $node): void
{
$this->simpleCallableNodeTraverser->traverseNodesWithCallable([$class], function (Node $subNode) use (
$node
): Node {
if ($subNode->getStartLine() >= 0 && $subNode->getEndLine() >= 0) {
return $subNode;
}
$subNode->setAttribute('startLine', $node->getStartLine());
$subNode->setAttribute('endLine', $node->getEndLine());
return $subNode;
});
}
}