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