Repository navigation
Expand file tree
/
Copy pathUnusedVariableRuleTest.php
More file actions
519 lines (479 loc) · 19.1 KB
/
Copy pathUnusedVariableRuleTest.php
File metadata and controls
519 lines (479 loc) · 19.1 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
<?php declare(strict_types = 1);
namespace PHPStan\Rules\DeadCode;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
/**
* @extends RuleTestCase<UnusedVariableRule>
*/
class UnusedVariableRuleTest extends RuleTestCase
{
protected function getRule(): Rule
{
return new UnusedVariableRule(self::getContainer()->getByType(ExprPrinter::class));
}
public function testBug15394(): void
{
$this->analyse([__DIR__ . '/data/bug-15394.php'], [
[
'Value assigned to variable $a is never read before being overwritten.',
24,
],
[
'Variable $a is never read.',
35,
],
]);
}
public function testBug15411(): void
{
$this->analyse([__DIR__ . '/data/bug-15411.php'], []);
}
public function testThrowableCatchAfterDocumentedException(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-throwable-catch.php'], []);
}
public function testOverridingThrowsReachesCatch(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-overriding-throws.php'], [
[
'Value assigned to variable $before is never read.',
13,
],
[
'Value assigned to variable $before is never read.',
24,
],
[
'Value assigned to variable $before is never read before being overwritten.',
32,
],
[
'Value assigned to variable $before is never read before being overwritten.',
43,
],
[
'Value assigned to variable $before is never read.',
82,
],
[
'Variable $before is never read.',
93,
],
[
'Variable $before is never read.',
95,
],
]);
}
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/unused-variable.php'], [
['Variable $a is never read.', 27],
['Value assigned to variable $a is never read before being overwritten.', 32],
['Variable $a is never read.', 40],
['Value assigned to variable $x is never read before being overwritten.', 46],
['Variable $a is never read.', 70],
['Variable $a is never read.', 76],
['Variable $a is never read.', 93],
['Variable $a is never read.', 95],
['Variable $a is never read.', 101],
['Variable $a is never read.', 113],
['Foreach key variable $k is never read.', 119],
['Foreach value variable $v is never read.', 133],
['Variable $a is never read.', 148],
['Value of variable $i after ++ is never read.', 157],
['Variable $x is never read.', 223],
['Value assigned to $x[] is never read.', 250],
['Value assigned to $x[\'k\'] is never read.', 251],
['Value assigned to variable $s is never read.', 264],
['Variable $a is never read.', 269],
['Variable $a is never read.', 276],
['Variable $f is never read.', 283],
['Variable $a is never read.', 303],
['Variable $x is never read.', 337],
['Value assigned to variable $title is never read.', 422],
['Variable $b is never read.', 614],
['Variable $a is never read.', 632],
['Variable $a is never read.', 637],
['Value assigned to variable $a is never read.', 703],
['Value assigned to variable $b only flows into values that are never used.', 709],
['Value assigned to variable $b only flows into values that are never used.', 719],
['Variable $a is never read.', 739],
['Value assigned to variable $a is never read before being overwritten.', 744],
['Value assigned to variable $tags is never read before being overwritten.', 840],
['Value of variable $i after -- is never read.', 864],
['Value assigned to variable $x is never read.', 870],
['Foreach value variable $v is never read.', 877],
['Value of variable $i after ++ is never read.', 885],
['Value of variable $i after -- is never read.', 892],
['Foreach value $a[\'x\'] is never read.', 915],
]);
}
#[RequiresPhp('>= 8.0.0')]
public function testPhp8(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-php8.php'], [
['Value assigned to variable $nightsFrom is never read before being overwritten.', 98],
['Variable $b is never read.', 108],
['Variable $b is never read.', 116],
]);
}
public function testBug12789(): void
{
$this->analyse([__DIR__ . '/data/bug-12789.php'], [
[
'Variable $RetVal is never read.',
12,
],
]);
}
public function testBug13472(): void
{
$this->analyse([__DIR__ . '/data/bug-13472.php'], [
[
'Value assigned to variable $v is never read before being overwritten.',
14,
],
[
'Foreach value variable $item is never read.',
41,
],
]);
}
public function testBug14258(): void
{
$this->analyse([__DIR__ . '/data/bug-14258.php'], [
[
'Variable $cutsomerId is never read.',
15,
],
]);
}
public function testBug12012(): void
{
$this->analyse([__DIR__ . '/data/bug-12012.php'], [
['Value assigned to variable $s1 is never read before being overwritten.', 10],
['Value assigned to variable $s1 is never read.', 12],
]);
}
public function testBug11483(): void
{
$this->analyse([__DIR__ . '/data/bug-11483.php'], [
[
'Value assigned to variable $hello is never read before being overwritten.',
9,
],
]);
}
public function testBug10202(): void
{
$this->analyse([__DIR__ . '/data/bug-10202.php'], [
[
'Variable $x is never read.',
9,
],
[
'Variable $x is never read.',
12,
],
[
'Variable $x is never read.',
14,
],
]);
}
public function testCatchVariableNotReported(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-catch.php'], []);
}
public function testRedundantAssignment(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-redundant.php'], [
['Variable $x is assigned value true but it already has that value.', 26],
['Value assigned to variable $x is never read before being overwritten.', 42],
['Variable $x is assigned value 1 but it already has that value.', 43],
['Variable $x is assigned value null but it already has that value.', 51],
['Variable $s is assigned value \'a\' but it already has that value.', 60],
['Variable $a is assigned value array{k: 1} but it already has that value.', 69],
['Value assigned to variable $x is never read before being overwritten.', 95],
['Variable $x is assigned value 1 but it already has that value.', 118],
]);
}
public function testByRefReturn(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-by-ref-return.php'], [
[
'Value assigned to variable $x is never read.',
26,
],
[
'Variable $unused is never read.',
32,
],
[
'Value assigned to variable $x is never read.',
68,
],
]);
}
public function testDeadBranchWrites(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-dead-branch.php'], [
[
'Value assigned to variable $x is never read before being overwritten.',
79,
],
]);
}
public function testResultFlow(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-result-flow.php'], [
['Value assigned to variable $shadowed is never read.', 15],
['Value assigned to variable $value is never read.', 16],
['Value assigned to variable $value is never read before being overwritten.', 73],
['Value assigned to variable $value is never read before being overwritten.', 98],
['Value assigned to variable $value is never read.', 128],
['Variable $value is never read.', 136],
]);
}
public function testUsageFlow(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-usage-flow.php'], []);
}
public function testNestedOffsetWrites(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-nested-offset-writes.php'], []);
}
public function testArrowReferenceParameters(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-arrow-reference.php'], [
['Value assigned to variable $value is never read.', 14],
]);
}
public function testBroadCatchesIncludeImplicitThrows(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-broad-catch.php'], [
['Value assigned to variable $file is never read before being overwritten.', 71],
]);
}
public function testValueFlow(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-value-flow.php'], [
['Value assigned to variable $a is never read.', 28],
['Value assigned to variable $s is never read.', 42],
['Value of variable $i after -- is never read.', 58],
['Value of variable $i after ++ is never read.', 71],
['Value of variable $i after ++ is never read.', 77],
['Variable $j is never read.', 77],
['Value assigned to variable $n only flows into values that are never used.', 98],
['Value assigned to variable $n only flows into values that are never used.', 100],
['Variable $ok is never read.', 118],
['Variable $b is never read.', 131],
['Variable $b is never read.', 144],
['Variable $b is never read.', 150],
['Variable $arr is never read.', 156],
['Variable $b is never read.', 169],
['Variable $c is never read.', 170],
['Variable $b is never read.', 176],
['Variable $c is never read.', 177],
['Variable $d is never read.', 178],
['Variable $b is never read.', 184],
['Variable $b is never read.', 190],
['Variable $b is never read.', 196],
['Variable $b is never read.', 202],
['Variable $b is never read.', 209],
['Variable $d is never read.', 210],
['Variable $b is never read.', 216],
['Variable $b is never read.', 222],
['Variable $b is never read.', 228],
['Variable $b is never read.', 234],
['Variable $f is never read.', 240],
['Variable $f is never read.', 248],
['Variable $b is never read.', 254],
['Variable $b is never read.', 260],
['Variable $b is never read.', 266],
['Variable $b is never read.', 272],
['Variable $b is never read.', 278],
['Variable $b is never read.', 284],
['Variable $b is never read.', 289],
['Variable $a is never read.', 295],
['Variable $b is never read.', 302],
['Variable $b is never read.', 309],
['Variable $a is never read.', 309],
['Variable $b is never read.', 315],
['Variable $b is never read.', 322],
['Value assigned to $a[\'x\'] is never read.', 331],
['Variable $b is never read.', 345],
['Variable $b is never read.', 359],
['Variable $a is never read.', 364],
['Variable $d is never read.', 372],
['Value assigned to variable $s only flows into values that are never used.', 395],
['Foreach value variable $v only flows into values that are never used.', 396],
['Value assigned to variable $s only flows into values that are never used.', 397],
['Value assigned to variable $a is never read.', 404],
['Value assigned to variable $s is never read.', 477],
['Value assigned to variable $s is never read.', 484],
['Variable $x is never read.', 484],
]);
}
public function testOffsets(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-offsets.php'], [
['Offset \'x\' of array assigned to variable $a is never read.', 27],
['Variable $a is never read.', 39],
['Offset \'x\' of array assigned to variable $a is never read.', 44],
['Offset 1 of array assigned to variable $a is never read.', 50],
['Offset 2 of array assigned to variable $a is never read.', 50],
['Offset \'y\' of array assigned to variable $a is never read.', 58],
['Offset \'x\' of array assigned to variable $a is never read before being overwritten.', 67],
['Offset \'y\' of array assigned to variable $a is never read.', 95],
['Offset \'y\' of array assigned to variable $a is never read.', 103],
['Offset \'z\' of array assigned to variable $a is never read.', 117],
['Offset \'x\' of array assigned to variable $a is never read.', 124],
['Offset 2 of array assigned to variable $a is never read.', 136],
['Offset 6 of array assigned to variable $a is never read.', 142],
['Offset \'x\' of array assigned to variable $a is never read.', 149],
['Offset \'x\' of array assigned to variable $a is never read.', 157],
['Offset \'y\' of array assigned to variable $a is never read.', 163],
['Offset \'y\' of array assigned to variable $a is never read.', 169],
['Offset \'y\' of array assigned to variable $a is never read.', 175],
['Value assigned to $a[\'x\'] is never read.', 191],
['Value assigned to $a[\'x\'] is never read before being overwritten.', 211],
['Value assigned to $a[\'x\'] is never read.', 219],
['Value assigned to $a[$i] is never read.', 234],
['Value assigned to $a[] is never read.', 247],
['Value assigned to $a[\'x\'][\'y\'] is never read.', 260],
['Offset \'x\' of array assigned to variable $a is never read before being overwritten.', 280],
['Value assigned to $a[\'x\'] is never read.', 295],
['Value of $a[\'n\'] after ++ is never read.', 315],
['Value assigned to $s[0] is never read.', 328],
['Value assigned to $a[\'x\'] is never read.', 356],
['Value assigned to $a[\'x\'] is never read.', 380],
['Value assigned to $a[\'y\'] is never read.', 380],
['Foreach value $a[\'x\'] is never read.', 393],
['Value assigned to $p[\'x\'] is never read.', 399],
['Value assigned to $a[\'x\'] is never read before being overwritten.', 412],
['Value assigned to $a[\'x\'] is never read.', 430],
['Variable $a is never read.', 444],
['Value assigned to variable $a is never read.', 450],
['Offset \'x\' of array assigned to variable $a is never read.', 474],
['Value assigned to $a[\'x\'] is never read.', 482],
['Offset \'y\' of array assigned to variable $a is never read.', 510],
['Offset \'y\' of array assigned to variable $a is never read.', 539],
['Value assigned to $cache[\'x\'] is never read.', 565],
['Variable $v is never read.', 565],
['Variable $b is never read.', 571],
]);
}
public function testRedundantOffsets(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-redundant-offsets.php'], [
['Offset 0 of array assigned to variable $a is never read before being overwritten.', 7],
['Offset 1 of array assigned to variable $a is never read.', 7],
['Offset $a[0] is assigned value 1 but it already has that value.', 8],
['Offset 0 of array assigned to variable $a is never read before being overwritten.', 13],
['Offset $a[0] is assigned value 1 but it already has that value.', 14],
['Offset $a[\'x\'][\'y\'] is assigned value 1 but it already has that value.', 21],
['Offset 0 of array assigned to variable $a is never read before being overwritten.', 27],
['Offset $a[\'0\'] is assigned value 1 but it already has that value.', 28],
['Offset 0 of array assigned to variable $a is never read before being overwritten.', 34],
]);
}
public function testDynamicOffsetOverwritten(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-offset-overwrite.php'], [
['Value assigned to $a[$i] is never read.', 8],
]);
}
public function testOverwritten(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-overwritten.php'], [
['Value assigned to variable $a is never read before being overwritten.', 24],
['Value assigned to variable $a is never read before being overwritten.', 31],
['Value assigned to variable $a is never read.', 41],
['Value assigned to variable $a is never read.', 47],
['Value assigned to variable $a is never read.', 48],
['Value assigned to variable $a is never read.', 56],
['Value assigned to variable $a is never read before being overwritten.', 63],
['Value of variable $i after ++ is never read before being overwritten.', 72],
['Foreach value variable $v is never read before being overwritten.', 79],
['Foreach key variable $k is never read before being overwritten.', 87],
['Value assigned to $a[\'x\'] is never read before being overwritten.', 96],
['Value assigned to $a[\'x\'] is never read before being overwritten.', 104],
['Value assigned to $a[\'x\'] is never read.', 112],
]);
}
public function testBug15263(): void
{
$this->analyse([__DIR__ . '/data/bug-15263.php'], []);
}
public function testInferredBranchesStayPossible(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-inferred-branches.php'], []);
}
public function testScopePollution(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-scope-pollution.php'], []);
}
#[RequiresPhp('>= 8.0.0')]
public function testBug15262(): void
{
$this->analyse([__DIR__ . '/data/bug-15262.php'], [
['Offset \'success\' of array assigned to variable $result is never read before being overwritten.', 22],
]);
}
public function testUnsetCallsDestructor(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-destructor.php'], [
['Variable $unused is never read.', 33],
['Variable $object is never read.', 75],
]);
}
public function testBug15225(): void
{
$this->analyse([__DIR__ . '/data/bug-15225.php'], []);
}
public function testFlowMessages(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-flow-messages.php'], [
['Value assigned to variable $a only flows into values that are never used.', 18],
['Value assigned to variable $a only flows into values that are never used.', 20],
['Value assigned to variable $s only flows into values that are never used.', 26],
['Value assigned to variable $s only flows into values that are never used.', 28],
['Offset \'k\' of array assigned to variable $a only flows into values that are never used.', 34],
['Value assigned to $a[\'k\'] only flows into values that are never used.', 36],
['Value assigned to variable $a only flows into values that are never used.', 42],
['Value assigned to variable $a only flows into values that are never used.', 44],
['Value assigned to variable $i only flows into values that are never used.', 50],
['Value of variable $i after ++ only flows into values that are never used.', 52],
['Value assigned to variable $i only flows into values that are never used.', 58],
['Value of variable $i after ++ only flows into values that are never used.', 60],
['Value assigned to variable $i only flows into values that are never used.', 66],
['Value of variable $i after -- only flows into values that are never used.', 68],
['Value assigned to variable $i only flows into values that are never used.', 74],
['Value of variable $i after -- only flows into values that are never used.', 76],
['Foreach key variable $k only flows into values that are never used.', 83],
['Foreach value variable $v only flows into values that are never used.', 83],
['Value assigned to variable $v only flows into values that are never used.', 85],
['Value assigned to variable $k only flows into values that are never used.', 86],
['Offset \'x\' of array assigned to variable $a only flows into values that are never used.', 93],
['Value assigned to $a[\'x\'] only flows into values that are never used.', 95],
['Variable $b is never read.', 103],
['Value assigned to variable $a is never read.', 110],
['Variable $a is never read.', 116],
['Variable $b is never read.', 116],
['Offset \'x\' of array assigned to variable $a is never read.', 122],
]);
}
#[RequiresPhp('>= 8.0.0')]
public function testFlowMessagesCatch(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-flow-messages-catch.php'], [
['Catch variable $e only flows into values that are never used.', 15],
['Value assigned to variable $e only flows into values that are never used.', 17],
['Variable $copy is never read.', 27],
]);
}
}