1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padawan\Parser\Walker; |
4
|
|
|
|
5
|
|
|
use Padawan\Parser\UseParser; |
6
|
|
|
use Padawan\Parser\CommentParser; |
7
|
|
|
use Padawan\Parser\ParamParser; |
8
|
|
|
use Padawan\Framework\Complete\Resolver\NodeTypeResolver; |
9
|
|
|
use Padawan\Domain\Project\FQCN; |
10
|
|
|
use Padawan\Domain\Project\Index; |
11
|
|
|
use Padawan\Domain\Project\Node\Uses; |
12
|
|
|
use Padawan\Domain\Project\Node\Variable; |
13
|
|
|
use Padawan\Domain\Scope; |
14
|
|
|
use Padawan\Domain\Scope\FileScope; |
15
|
|
|
use Padawan\Domain\Scope\FunctionScope; |
16
|
|
|
use Padawan\Domain\Scope\MethodScope; |
17
|
|
|
use Padawan\Domain\Scope\ClassScope; |
18
|
|
|
use Padawan\Domain\Scope\ClosureScope; |
19
|
|
|
use PhpParser\NodeTraverserInterface; |
20
|
|
|
use PhpParser\NodeVisitorAbstract; |
21
|
|
|
use PhpParser\Node; |
22
|
|
|
use PhpParser\Node\Expr\Variable as NodeVar; |
23
|
|
|
use PhpParser\Node\Expr\Assign; |
24
|
|
|
use PhpParser\Node\Stmt\Use_; |
25
|
|
|
use PhpParser\Node\Stmt\Class_; |
26
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
27
|
|
|
use PhpParser\Node\Expr\Closure; |
28
|
|
|
|
29
|
|
|
class ScopeWalker extends NodeVisitorAbstract implements WalkerInterface |
30
|
|
|
{ |
31
|
|
|
public function __construct( |
32
|
|
|
UseParser $useParser, |
33
|
|
|
NodeTypeResolver $typeResolver, |
34
|
|
|
CommentParser $commentParser, |
35
|
|
|
ParamParser $paramParser |
36
|
|
|
) { |
37
|
|
|
$this->useParser = $useParser; |
38
|
|
|
$this->typeResolver = $typeResolver; |
39
|
|
|
$this->commentParser = $commentParser; |
40
|
|
|
$this->paramParser = $paramParser; |
41
|
|
|
} |
42
|
|
|
public function setLine($line) |
43
|
|
|
{ |
44
|
|
|
$this->line = $line; |
45
|
|
|
} |
46
|
|
|
public function enterNode(Node $node) |
47
|
|
|
{ |
48
|
|
|
list($startLine, $endLine) = $this->getNodeLines($node); |
|
|
|
|
49
|
|
|
if (!$this->isIn($node, $this->line)) { |
50
|
|
|
return NodeTraverserInterface::DONT_TRAVERSE_CHILDREN; |
51
|
|
|
} |
52
|
|
|
if ($node instanceof Class_) { |
53
|
|
|
$this->createScopeFromClass($node); |
54
|
|
|
} elseif ($node instanceof ClassMethod) { |
55
|
|
|
$this->createScopeFromMethod($node); |
56
|
|
|
} elseif ($node instanceof Closure) { |
57
|
|
|
$this->createScopeFromClosure($node); |
58
|
|
|
} elseif ($node instanceof Assign) { |
59
|
|
|
$this->addVarToScope($node); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
public function leaveNode(Node $node) |
63
|
|
|
{ |
64
|
|
|
if (!$this->isIn($node, $this->line)) { |
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
public function updateFileInfo(Uses $uses, $file) |
68
|
|
|
{ |
69
|
|
|
$this->scope = new FileScope($uses->getFQCN(), $uses); |
|
|
|
|
70
|
|
|
$this->fileScope = $this->scope; |
71
|
|
|
} |
72
|
|
|
public function getResultScope() |
73
|
|
|
{ |
74
|
|
|
return $this->scope; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Node $node |
79
|
|
|
*/ |
80
|
|
|
public function isIn($node, $line) |
81
|
|
|
{ |
82
|
|
|
list($startLine, $endLine) = $this->getNodeLines($node); |
83
|
|
|
if ($node instanceof ClassMethod |
84
|
|
|
|| $node instanceof Closure |
85
|
|
|
|| $node instanceof Class_ |
86
|
|
|
) { |
87
|
|
|
return $line >= $startLine && $line <= $endLine; |
88
|
|
|
} |
89
|
|
|
return $line >= $startLine; |
90
|
|
|
} |
91
|
|
|
public function getNodeLines($node) |
92
|
|
|
{ |
93
|
|
|
$startLine = $endLine = -1; |
94
|
|
|
if ($node->hasAttribute('startLine')) { |
95
|
|
|
$startLine = $node->getAttribute('startLine'); |
96
|
|
|
} |
97
|
|
|
if ($node->hasAttribute('endLine')) { |
98
|
|
|
$endLine = $node->getAttribute('endLine'); |
99
|
|
|
} |
100
|
|
|
return [$startLine, $endLine]; |
101
|
|
|
} |
102
|
|
|
protected function createScopeFromClass(Class_ $node) |
103
|
|
|
{ |
104
|
|
|
$scope = $this->scope; |
105
|
|
|
$index = $this->getIndex(); |
106
|
|
|
if (empty($index)) { |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
$fqcn = new FQCN( |
110
|
|
|
$node->name, |
111
|
|
|
$this->scope->getNamespace() |
|
|
|
|
112
|
|
|
); |
113
|
|
|
$classData = $index->findClassByFQCN($fqcn); |
114
|
|
|
if (empty($classData)) { |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
$this->scope = new ClassScope($scope, $classData); |
118
|
|
|
} |
119
|
|
|
public function createScopeFromClosure(Closure $node) |
120
|
|
|
{ |
121
|
|
|
$scope = $this->scope; |
122
|
|
|
$this->scope = new ClosureScope($scope); |
123
|
|
|
foreach ($node->params as $param) { |
124
|
|
|
$this->scope->addVar( |
125
|
|
|
$this->paramParser->parse($param) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
foreach ($node->uses as $closureUse) { |
129
|
|
|
$var = $this->scope->getParent()->getVar($closureUse->var); |
130
|
|
|
if ($var instanceof Variable) { |
131
|
|
|
$this->scope->addVar( |
132
|
|
|
$var |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
public function createScopeFromMethod(ClassMethod $node) |
138
|
|
|
{ |
139
|
|
|
$classScope = $this->scope; |
140
|
|
|
$index = $this->getIndex(); |
141
|
|
|
if (empty($index)) { |
142
|
|
|
return; |
143
|
|
|
} |
144
|
|
|
$fqcn = $classScope->getFQCN(); |
|
|
|
|
145
|
|
|
$classData = $index->findClassByFQCN($fqcn); |
146
|
|
|
if (empty($classData)) { |
147
|
|
|
return; |
148
|
|
|
} |
149
|
|
|
$method = $classData->methods->get($node->name); |
|
|
|
|
150
|
|
|
if (empty($method)) { |
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
$this->scope = new MethodScope($classScope, $method); |
154
|
|
|
} |
155
|
|
|
public function addVarToScope(Assign $node) |
156
|
|
|
{ |
157
|
|
|
if (!$node->var instanceof NodeVar) { |
158
|
|
|
return; |
159
|
|
|
} |
160
|
|
|
$var = new Variable($node->var->name); |
161
|
|
|
$comment = $this->commentParser->parse($node->getAttribute('comments')); |
162
|
|
|
if ($comment->getVar($var->getName())) { |
163
|
|
|
$type = $comment->getVar($var->getName())->getType(); |
164
|
|
|
} else { |
165
|
|
|
$type = $this->typeResolver->getType( |
166
|
|
|
$node->expr, |
167
|
|
|
$this->getIndex(), |
168
|
|
|
$this->scope |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
$var->setType($type); |
172
|
|
|
$this->scope->addVar($var); |
173
|
|
|
} |
174
|
|
|
public function parseUse(Use_ $node, $fqcn, $file) |
175
|
|
|
{ |
176
|
|
|
$this->useParser->parse($node, $fqcn, $file); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return FQCN |
181
|
|
|
*/ |
182
|
|
|
public function parseFQCN($fqcn) |
183
|
|
|
{ |
184
|
|
|
return $this->useParser->parseFQCN($fqcn); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return Index |
189
|
|
|
*/ |
190
|
|
|
public function getIndex() |
191
|
|
|
{ |
192
|
|
|
return $this->index; |
193
|
|
|
} |
194
|
|
|
public function setIndex(Index $index) |
195
|
|
|
{ |
196
|
|
|
$this->index = $index; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** @var FileScope */ |
200
|
|
|
private $fileScope; |
201
|
|
|
private $line; |
202
|
|
|
/** @var UseParser */ |
203
|
|
|
private $useParser; |
204
|
|
|
private $index; |
205
|
|
|
/** @property Scope */ |
206
|
|
|
private $scope; |
207
|
|
|
private $typeResolver; |
208
|
|
|
private $commentParser; |
209
|
|
|
/** @property ParamParser */ |
210
|
|
|
private $paramParser; |
211
|
|
|
} |
212
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.