1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ComposerRequireCheckerTest\NodeVisitor; |
4
|
|
|
|
5
|
|
|
use ComposerRequireChecker\NodeVisitor\UsedSymbolCollector; |
6
|
|
|
use PhpParser\Node\Expr\ClassConstFetch; |
7
|
|
|
use PhpParser\Node\Expr\ConstFetch; |
8
|
|
|
use PhpParser\Node\Expr\FuncCall; |
9
|
|
|
use PhpParser\Node\Expr\Instanceof_; |
10
|
|
|
use PhpParser\Node\Expr\New_; |
11
|
|
|
use PhpParser\Node\Expr\StaticCall; |
12
|
|
|
use PhpParser\Node\Expr\StaticPropertyFetch; |
13
|
|
|
use PhpParser\Node\Expr\Variable; |
14
|
|
|
use PhpParser\Node\Name; |
15
|
|
|
use PhpParser\Node\Param; |
16
|
|
|
use PhpParser\Node\Stmt\Catch_; |
17
|
|
|
use PhpParser\Node\Stmt\Class_; |
18
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
19
|
|
|
use PhpParser\Node\Stmt\Function_; |
20
|
|
|
use PhpParser\Node\Stmt\Interface_; |
21
|
|
|
use PhpParser\Node\Stmt\TraitUse; |
22
|
|
|
use PHPUnit\Framework\TestCase; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @covers \ComposerRequireChecker\NodeVisitor\UsedSymbolCollector |
26
|
|
|
*/ |
27
|
|
|
class UsedSymbolCollectorTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** @var UsedSymbolCollector */ |
30
|
|
|
private $visitor; |
31
|
|
|
|
32
|
|
|
protected function setUp() |
33
|
|
|
{ |
34
|
|
|
parent::setUp(); |
35
|
|
|
|
36
|
|
|
$this->visitor = new UsedSymbolCollector(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testExtendingClass() |
40
|
|
|
{ |
41
|
|
|
$node = new Class_('Foo'); |
42
|
|
|
$node->extends = new Name('Bar'); |
43
|
|
|
|
44
|
|
|
$this->visitor->enterNode($node); |
45
|
|
|
|
46
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
47
|
|
|
$this->assertCount(1, $symbols); |
48
|
|
|
$this->assertContains('Bar', $symbols); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function testExtendingInterface() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$node = new Interface_('Foo'); |
54
|
|
|
$node->extends = [new Name('Bar'), new Name('Baz')]; |
55
|
|
|
|
56
|
|
|
$this->visitor->enterNode($node); |
57
|
|
|
|
58
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
59
|
|
|
$this->assertCount(2, $symbols); |
60
|
|
|
$this->assertContains('Bar', $symbols); |
61
|
|
|
$this->assertContains('Baz', $symbols); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
public function testImplements() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$node = new Class_('Foo'); |
67
|
|
|
$node->implements = [new Name('Bar'), new Name('Baz')]; |
68
|
|
|
|
69
|
|
|
$this->visitor->enterNode($node); |
70
|
|
|
|
71
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
72
|
|
|
$this->assertCount(2, $symbols); |
73
|
|
|
$this->assertContains('Bar', $symbols); |
74
|
|
|
$this->assertContains('Baz', $symbols); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function testStaticCall() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$class = new Name('Foo'); |
80
|
|
|
$node = new StaticCall($class, 'foo'); |
81
|
|
|
$node->class = $class; |
82
|
|
|
|
83
|
|
|
$this->visitor->enterNode($node); |
84
|
|
|
|
85
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
86
|
|
|
$this->assertCount(1, $symbols); |
87
|
|
|
$this->assertContains('Foo', $symbols); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
public function testStaticPropertyFetch() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$class = new Name('Foo'); |
93
|
|
|
$node = new StaticPropertyFetch($class, 'foo'); |
94
|
|
|
$node->class = $class; |
95
|
|
|
|
96
|
|
|
$this->visitor->enterNode($node); |
97
|
|
|
|
98
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
99
|
|
|
$this->assertCount(1, $symbols); |
100
|
|
|
$this->assertContains('Foo', $symbols); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
View Code Duplication |
public function testClassConstantFetch() |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$class = new Name('Foo'); |
106
|
|
|
$node = new ClassConstFetch($class, 'FOO'); |
107
|
|
|
$node->class = $class; |
108
|
|
|
|
109
|
|
|
$this->visitor->enterNode($node); |
110
|
|
|
|
111
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
112
|
|
|
$this->assertCount(1, $symbols); |
113
|
|
|
$this->assertContains('Foo', $symbols); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function testNew() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$class = new Name('Foo'); |
119
|
|
|
$node = new New_($class); |
120
|
|
|
$node->class = $class; |
121
|
|
|
|
122
|
|
|
$this->visitor->enterNode($node); |
123
|
|
|
|
124
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
125
|
|
|
$this->assertCount(1, $symbols); |
126
|
|
|
$this->assertContains('Foo', $symbols); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
View Code Duplication |
public function testInstanceof() |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$class = new Name('Foo'); |
132
|
|
|
$node = new Instanceof_(new Variable('foo'), $class); |
133
|
|
|
$node->class = $class; |
134
|
|
|
|
135
|
|
|
$this->visitor->enterNode($node); |
136
|
|
|
|
137
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
138
|
|
|
$this->assertCount(1, $symbols); |
139
|
|
|
$this->assertContains('Foo', $symbols); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testCatch() |
143
|
|
|
{ |
144
|
|
|
$class = new Name('Foo'); |
145
|
|
|
$node = new Catch_([$class], 'e'); |
146
|
|
|
|
147
|
|
|
$this->visitor->enterNode($node); |
148
|
|
|
|
149
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
150
|
|
|
$this->assertCount(1, $symbols); |
151
|
|
|
$this->assertContains('Foo', $symbols); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
View Code Duplication |
public function testFunctionCallUsage() |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$functionName = new Name('foo'); |
157
|
|
|
$node = new FuncCall($functionName); |
158
|
|
|
$node->name = $functionName; |
159
|
|
|
|
160
|
|
|
$this->visitor->enterNode($node); |
161
|
|
|
|
162
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
163
|
|
|
$this->assertCount(1, $symbols); |
164
|
|
|
$this->assertContains('foo', $symbols); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
View Code Duplication |
public function testFunctionParameterType() |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$functionName = new Name('foo'); |
170
|
|
|
$node = new Function_($functionName); |
171
|
|
|
$node->name = $functionName; |
|
|
|
|
172
|
|
|
$param = new Param('bar'); |
173
|
|
|
$param->type = new Name('Baz'); |
174
|
|
|
$node->params = [$param]; |
175
|
|
|
|
176
|
|
|
$this->visitor->enterNode($node); |
177
|
|
|
|
178
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
179
|
|
|
$this->assertCount(1, $symbols); |
180
|
|
|
$this->assertContains('Baz', $symbols); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
View Code Duplication |
public function testFunctionParameterTypeAsString() |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
$functionName = new Name('foo'); |
186
|
|
|
$node = new Function_($functionName); |
187
|
|
|
$node->name = $functionName; |
|
|
|
|
188
|
|
|
$param = new Param('bar'); |
189
|
|
|
$param->type = 'Baz'; |
190
|
|
|
$node->params = [$param]; |
191
|
|
|
|
192
|
|
|
$this->visitor->enterNode($node); |
193
|
|
|
|
194
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
195
|
|
|
$this->assertCount(1, $symbols); |
196
|
|
|
$this->assertContains('Baz', $symbols); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
View Code Duplication |
public function testMethodParameterType() |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
$functionName = new Name('foo'); |
202
|
|
|
$node = new ClassMethod($functionName); |
203
|
|
|
$node->name = $functionName; |
|
|
|
|
204
|
|
|
$param = new Param('bar'); |
205
|
|
|
$param->type = new Name('Baz'); |
206
|
|
|
$node->params = [$param]; |
207
|
|
|
|
208
|
|
|
$this->visitor->enterNode($node); |
209
|
|
|
|
210
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
211
|
|
|
$this->assertCount(1, $symbols); |
212
|
|
|
$this->assertContains('Baz', $symbols); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
View Code Duplication |
public function testMethodParameterTypeAsString() |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
$functionName = new Name('foo'); |
218
|
|
|
$node = new ClassMethod($functionName); |
219
|
|
|
$node->name = $functionName; |
|
|
|
|
220
|
|
|
$param = new Param('bar'); |
221
|
|
|
$param->type = 'Baz'; |
222
|
|
|
$node->params = [$param]; |
223
|
|
|
|
224
|
|
|
$this->visitor->enterNode($node); |
225
|
|
|
|
226
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
227
|
|
|
$this->assertCount(1, $symbols); |
228
|
|
|
$this->assertContains('Baz', $symbols); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
View Code Duplication |
public function testFunctionReturnType() |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
$functionName = new Name('foo'); |
234
|
|
|
$node = new Function_($functionName); |
235
|
|
|
$node->returnType = new Name('Bar'); |
236
|
|
|
|
237
|
|
|
$this->visitor->enterNode($node); |
238
|
|
|
|
239
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
240
|
|
|
$this->assertCount(1, $symbols); |
241
|
|
|
$this->assertContains('Bar', $symbols); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
View Code Duplication |
public function testFunctionReturnTypeAsString() |
|
|
|
|
245
|
|
|
{ |
246
|
|
|
$functionName = new Name('foo'); |
247
|
|
|
$node = new Function_($functionName); |
248
|
|
|
$node->returnType = 'Bar'; |
249
|
|
|
|
250
|
|
|
$this->visitor->enterNode($node); |
251
|
|
|
|
252
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
253
|
|
|
$this->assertCount(1, $symbols); |
254
|
|
|
$this->assertContains('Bar', $symbols); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
View Code Duplication |
public function testMethodReturnType() |
|
|
|
|
258
|
|
|
{ |
259
|
|
|
$functionName = new Name('foo'); |
260
|
|
|
$node = new ClassMethod($functionName); |
261
|
|
|
$node->returnType = new Name('Bar'); |
262
|
|
|
|
263
|
|
|
$this->visitor->enterNode($node); |
264
|
|
|
|
265
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
266
|
|
|
$this->assertCount(1, $symbols); |
267
|
|
|
$this->assertContains('Bar', $symbols); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
View Code Duplication |
public function testMethodReturnTypeAsString() |
|
|
|
|
271
|
|
|
{ |
272
|
|
|
$functionName = new Name('foo'); |
273
|
|
|
$node = new ClassMethod($functionName); |
274
|
|
|
$node->returnType = 'Bar'; |
275
|
|
|
|
276
|
|
|
$this->visitor->enterNode($node); |
277
|
|
|
|
278
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
279
|
|
|
$this->assertCount(1, $symbols); |
280
|
|
|
$this->assertContains('Bar', $symbols); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
View Code Duplication |
public function testConstantFetch() |
|
|
|
|
284
|
|
|
{ |
285
|
|
|
$exceptionClass = new Name('FooException'); |
286
|
|
|
$node = new ConstFetch($exceptionClass); |
287
|
|
|
$node->name = $exceptionClass; |
288
|
|
|
|
289
|
|
|
$this->visitor->enterNode($node); |
290
|
|
|
|
291
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
292
|
|
|
$this->assertCount(1, $symbols); |
293
|
|
|
$this->assertContains('FooException', $symbols); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
View Code Duplication |
public function testTraits() |
|
|
|
|
297
|
|
|
{ |
298
|
|
|
$node = new TraitUse([new Name('Foo')]); |
299
|
|
|
|
300
|
|
|
$this->visitor->enterNode($node); |
301
|
|
|
|
302
|
|
|
$symbols = $this->visitor->getCollectedSymbols(); |
303
|
|
|
$this->assertCount(1, $symbols); |
304
|
|
|
$this->assertContains('Foo', $symbols); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
View Code Duplication |
public function testBeforeTraverseResetsRecordedSymbols() |
|
|
|
|
308
|
|
|
{ |
309
|
|
|
$node = new Class_('Foo'); |
310
|
|
|
$node->extends = new Name('Bar'); |
311
|
|
|
$this->visitor->enterNode($node); |
312
|
|
|
|
313
|
|
|
$this->visitor->beforeTraverse([]); |
314
|
|
|
|
315
|
|
|
$this->assertCount(0, $this->visitor->getCollectedSymbols()); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.