1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHP Mess Detector. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Manuel Pichler <[email protected]>. |
6
|
|
|
* All rights reserved. |
7
|
|
|
* |
8
|
|
|
* Licensed under BSD License |
9
|
|
|
* For full copyright and license information, please see the LICENSE file. |
10
|
|
|
* Redistributions of files must retain the above copyright notice. |
11
|
|
|
* |
12
|
|
|
* @author Manuel Pichler <[email protected]> |
13
|
|
|
* @copyright Manuel Pichler. All rights reserved. |
14
|
|
|
* @license https://opensource.org/licenses/bsd-license.php BSD License |
15
|
|
|
* @link http://phpmd.org/ |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace PHPMD\Rule; |
19
|
|
|
|
20
|
|
|
use PHPMD\AbstractTest; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Test case for the unused formal parameter rule. |
24
|
|
|
* |
25
|
|
|
* @covers \PHPMD\Rule\UnusedFormalParameter |
26
|
|
|
* @covers \PHPMD\Rule\AbstractLocalVariable |
27
|
|
|
*/ |
28
|
|
|
class UnusedFormalParameterTest extends AbstractTest |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* testRuleAppliesToFunctionUnusedFormalParameter |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function testRuleAppliesToFunctionUnusedFormalParameter() |
36
|
|
|
{ |
37
|
|
|
$rule = new UnusedFormalParameter(); |
38
|
|
|
$rule->setReport($this->getReportMock(1)); |
39
|
|
|
$rule->apply($this->getFunction()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* testRuleAppliesToMultipleFunctionUnusedFormalParameter |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function testRuleAppliesToMultipleFunctionUnusedFormalParameter() |
48
|
|
|
{ |
49
|
|
|
$rule = new UnusedFormalParameter(); |
50
|
|
|
$rule->setReport($this->getReportMock(3)); |
51
|
|
|
$rule->apply($this->getFunction()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* testRuleAppliesToMethodUnusedFormalParameter |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function testRuleAppliesToMethodUnusedFormalParameter() |
60
|
|
|
{ |
61
|
|
|
$rule = new UnusedFormalParameter(); |
62
|
|
|
$rule->setReport($this->getReportMock(1)); |
63
|
|
|
$rule->apply($this->getMethod()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* testRuleAppliesToMultipleMethodUnusedFormalParameter |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function testRuleAppliesToMultipleMethodUnusedFormalParameter() |
72
|
|
|
{ |
73
|
|
|
$rule = new UnusedFormalParameter(); |
74
|
|
|
$rule->setReport($this->getReportMock(2)); |
75
|
|
|
$rule->apply($this->getMethod()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* testRuleAppliesToFormalParameterWhenSimilarStaticMemberIsAccessed |
80
|
|
|
* |
81
|
|
|
* <code> |
82
|
|
|
* class Foo { |
83
|
|
|
* public static $bar = null; |
84
|
|
|
* public function baz($bar) { |
85
|
|
|
* self::$bar = 'fooBar'; |
86
|
|
|
* } |
87
|
|
|
* } |
88
|
|
|
* </code> |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function testRuleAppliesToFormalParameterWhenSimilarStaticMemberIsAccessed() |
93
|
|
|
{ |
94
|
|
|
$rule = new UnusedFormalParameter(); |
95
|
|
|
$rule->setReport($this->getReportMock(1)); |
96
|
|
|
$rule->apply($this->getMethod()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* testRuleNotAppliesToFormalParameterUsedInPropertyCompoundVariable |
101
|
|
|
* |
102
|
|
|
* <code> |
103
|
|
|
* class Foo { |
104
|
|
|
* public function baz($bar) { |
105
|
|
|
* self::${$bar} = 'fooBar'; |
106
|
|
|
* } |
107
|
|
|
* } |
108
|
|
|
* </code> |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
public function testRuleNotAppliesToFormalParameterUsedInPropertyCompoundVariable() |
113
|
|
|
{ |
114
|
|
|
$rule = new UnusedFormalParameter(); |
115
|
|
|
$rule->setReport($this->getReportMock(0)); |
116
|
|
|
$rule->apply($this->getMethod()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* testRuleNotAppliesToFormalParameterUsedInMethodCompoundVariable |
121
|
|
|
* |
122
|
|
|
* <code> |
123
|
|
|
* class Foo { |
124
|
|
|
* public function baz($bar) { |
125
|
|
|
* self::${$bar}(); |
126
|
|
|
* } |
127
|
|
|
* } |
128
|
|
|
* </code> |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
public function testRuleNotAppliesToFormalParameterUsedInMethodCompoundVariable() |
133
|
|
|
{ |
134
|
|
|
$rule = new UnusedFormalParameter(); |
135
|
|
|
$rule->setReport($this->getReportMock(0)); |
136
|
|
|
$rule->apply($this->getMethod()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* testRuleDoesNotApplyToAbstractMethodFormalParameter |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function testRuleDoesNotApplyToAbstractMethodFormalParameter() |
145
|
|
|
{ |
146
|
|
|
$rule = new UnusedFormalParameter(); |
147
|
|
|
$rule->setReport($this->getReportMock(0)); |
148
|
|
|
$rule->apply($this->getMethod()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* testRuleDoesNotApplyToInterfaceMethodFormalParameter |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
public function testRuleDoesNotApplyToInterfaceMethodFormalParameter() |
157
|
|
|
{ |
158
|
|
|
$rule = new UnusedFormalParameter(); |
159
|
|
|
$rule->setReport($this->getReportMock(0)); |
160
|
|
|
$rule->apply($this->getMethod()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* testRuleDoesNotApplyToInnerFunctionDeclaration |
165
|
|
|
* |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
public function testRuleDoesNotApplyToInnerFunctionDeclaration() |
169
|
|
|
{ |
170
|
|
|
$rule = new UnusedFormalParameter(); |
171
|
|
|
$rule->setReport($this->getReportMock(0)); |
172
|
|
|
$rule->apply($this->getFunction()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* testRuleDoesNotApplyToFormalParameterUsedInCompoundExpression |
177
|
|
|
* |
178
|
|
|
* <code> |
179
|
|
|
* class Foo { |
180
|
|
|
* public static $bar; |
181
|
|
|
* public function baz($bar) { |
182
|
|
|
* self::${$bar} = 42; |
183
|
|
|
* } |
184
|
|
|
* } |
185
|
|
|
* </code> |
186
|
|
|
* |
187
|
|
|
* @return void |
188
|
|
|
*/ |
189
|
|
|
public function testRuleDoesNotApplyToFormalParameterUsedInCompoundExpression() |
190
|
|
|
{ |
191
|
|
|
$rule = new UnusedFormalParameter(); |
192
|
|
|
$rule->setReport($this->getReportMock(0)); |
193
|
|
|
$rule->apply($this->getMethod()); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* testRuleDoesNotApplyToMethodArgument |
198
|
|
|
* |
199
|
|
|
* <code> |
200
|
|
|
* class Foo { |
201
|
|
|
* function bar($baz) { |
202
|
|
|
* $this->foo($baz); |
203
|
|
|
* } |
204
|
|
|
* } |
205
|
|
|
* </code> |
206
|
|
|
* |
207
|
|
|
* @return void |
208
|
|
|
*/ |
209
|
|
|
public function testRuleDoesNotApplyToMethodArgument() |
210
|
|
|
{ |
211
|
|
|
$rule = new UnusedFormalParameter(); |
212
|
|
|
$rule->setReport($this->getReportMock(0)); |
213
|
|
|
$rule->apply($this->getMethod()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* testRuleDoesNotApplyToMethodArgumentUsedAsArrayIndex |
218
|
|
|
* |
219
|
|
|
* @return void |
220
|
|
|
*/ |
221
|
|
|
public function testRuleDoesNotApplyToMethodArgumentUsedAsArrayIndex() |
222
|
|
|
{ |
223
|
|
|
$rule = new UnusedFormalParameter(); |
224
|
|
|
$rule->setReport($this->getReportMock(0)); |
225
|
|
|
$rule->apply($this->getMethod()); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* testRuleDoesNotApplyToParameterUsedAsArrayIndex |
230
|
|
|
* |
231
|
|
|
* <code> |
232
|
|
|
* class Foo { |
233
|
|
|
* function bar($baz) { |
234
|
|
|
* self::$values[$baz]; |
235
|
|
|
* } |
236
|
|
|
* } |
237
|
|
|
* </code> |
238
|
|
|
* |
239
|
|
|
* @return void |
240
|
|
|
*/ |
241
|
|
|
public function testRuleDoesNotApplyToParameterUsedAsArrayIndex() |
242
|
|
|
{ |
243
|
|
|
$rule = new UnusedFormalParameter(); |
244
|
|
|
$rule->setReport($this->getReportMock(0)); |
245
|
|
|
$rule->apply($this->getMethod()); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* testRuleDoesNotApplyToParameterUsedAsStringIndex |
250
|
|
|
* |
251
|
|
|
* <code> |
252
|
|
|
* class Foo { |
253
|
|
|
* function bar($baz) { |
254
|
|
|
* self::$string{$baz}; |
255
|
|
|
* } |
256
|
|
|
* } |
257
|
|
|
* </code> |
258
|
|
|
* |
259
|
|
|
* @return void |
260
|
|
|
*/ |
261
|
|
|
public function testRuleDoesNotApplyToParameterUsedAsStringIndex() |
262
|
|
|
{ |
263
|
|
|
$rule = new UnusedFormalParameter(); |
264
|
|
|
$rule->setReport($this->getReportMock(0)); |
265
|
|
|
$rule->apply($this->getMethod()); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* testRuleDoesNotApplyToMethodWithFuncGetArgs |
270
|
|
|
* |
271
|
|
|
* If func_get_args() is called then all parameters are |
272
|
|
|
* automatically referenced without needing them to be referenced |
273
|
|
|
* explicitly |
274
|
|
|
* |
275
|
|
|
* <code> |
276
|
|
|
* class Foo { |
277
|
|
|
* function bar($baz) { |
278
|
|
|
* print_r(func_get_args()); |
279
|
|
|
* } |
280
|
|
|
* } |
281
|
|
|
* </code> |
282
|
|
|
* |
283
|
|
|
* @return void |
284
|
|
|
*/ |
285
|
|
|
public function testRuleDoesNotApplyToMethodWithFuncGetArgs() |
286
|
|
|
{ |
287
|
|
|
$rule = new UnusedFormalParameter(); |
288
|
|
|
$rule->setReport($this->getReportMock(0)); |
289
|
|
|
$rule->apply($this->getMethod()); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return void |
294
|
|
|
* @since 2.0.0 |
295
|
|
|
*/ |
296
|
|
|
public function testFuncGetArgsRuleWorksCaseInsensitive() |
297
|
|
|
{ |
298
|
|
|
$rule = new UnusedFormalParameter(); |
299
|
|
|
$rule->setReport($this->getReportMock(0)); |
300
|
|
|
$rule->apply($this->getMethod()); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* testRuleDoesNotApplyToInheritMethod |
305
|
|
|
* |
306
|
|
|
* @return void |
307
|
|
|
* @since 1.2.1 |
308
|
|
|
*/ |
309
|
|
|
public function testRuleDoesNotApplyToInheritMethod() |
310
|
|
|
{ |
311
|
|
|
$rule = new UnusedFormalParameter(); |
312
|
|
|
$rule->setReport($this->getReportMock(0)); |
313
|
|
|
$rule->apply($this->getMethod()); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* testRuleDoesNotApplyToImplementedAbstractMethod |
318
|
|
|
* |
319
|
|
|
* @return void |
320
|
|
|
* @since 1.2.1 |
321
|
|
|
*/ |
322
|
|
|
public function testRuleDoesNotApplyToImplementedAbstractMethod() |
323
|
|
|
{ |
324
|
|
|
$rule = new UnusedFormalParameter(); |
325
|
|
|
$rule->setReport($this->getReportMock(0)); |
326
|
|
|
$rule->apply($this->getMethod()); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* testRuleDoesNotApplyToImplementedInterfaceMethod |
331
|
|
|
* |
332
|
|
|
* @return void |
333
|
|
|
* @since 1.2.1 |
334
|
|
|
*/ |
335
|
|
|
public function testRuleDoesNotApplyToImplementedInterfaceMethod() |
336
|
|
|
{ |
337
|
|
|
$rule = new UnusedFormalParameter(); |
338
|
|
|
$rule->setReport($this->getReportMock(0)); |
339
|
|
|
$rule->apply($this->getMethod()); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* testRuleDoesNotApplyToMagicMethod |
344
|
|
|
* |
345
|
|
|
* @return void |
346
|
|
|
*/ |
347
|
|
|
public function testRuleDoesNotApplyToMagicMethod() |
348
|
|
|
{ |
349
|
|
|
$methods = array_filter($this->getClass()->getMethods(), function ($method) { |
350
|
|
|
return $method->getName() == '__call'; |
351
|
|
|
}); |
352
|
|
|
|
353
|
|
|
$rule = new UnusedFormalParameter(); |
354
|
|
|
$rule->setReport($this->getReportMock(0)); |
355
|
|
|
$rule->apply(reset($methods)); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* testRuleDoesNotApplyToMethodWithInheritdocAnnotation |
360
|
|
|
*/ |
361
|
|
|
public function testRuleDoesNotApplyToMethodWithInheritdocAnnotation() |
362
|
|
|
{ |
363
|
|
|
$rule = new UnusedFormalParameter(); |
364
|
|
|
$rule->setReport($this->getReportMock(0)); |
365
|
|
|
$rule->apply($this->getMethod()); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* testRuleDoesNotApplyToMethodWithInheritdocAnnotationCamelCase |
370
|
|
|
*/ |
371
|
|
|
public function testRuleDoesNotApplyToMethodWithInheritdocAnnotationCamelCase() |
372
|
|
|
{ |
373
|
|
|
$rule = new UnusedFormalParameter(); |
374
|
|
|
$rule->setReport($this->getReportMock(0)); |
375
|
|
|
$rule->apply($this->getMethod()); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* @return void |
380
|
|
|
* @since 2.0.0 |
381
|
|
|
*/ |
382
|
|
|
public function testCompactFunctionRuleDoesNotApply() |
383
|
|
|
{ |
384
|
|
|
$rule = new UnusedFormalParameter(); |
385
|
|
|
$rule->setReport($this->getReportMock(0)); |
386
|
|
|
$rule->apply($this->getMethod()); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @return void |
391
|
|
|
* @since 2.0.0 |
392
|
|
|
*/ |
393
|
|
|
public function testCompactFunctionRuleOnlyAppliesToUsedParameters() |
394
|
|
|
{ |
395
|
|
|
$rule = new UnusedFormalParameter(); |
396
|
|
|
$rule->setReport($this->getReportMock(2)); |
397
|
|
|
$rule->apply($this->getMethod()); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* @return void |
402
|
|
|
* @since 2.0.0 |
403
|
|
|
*/ |
404
|
|
|
public function testCompactFunctionRuleWorksCaseInsensitive() |
405
|
|
|
{ |
406
|
|
|
$rule = new UnusedFormalParameter(); |
407
|
|
|
$rule->setReport($this->getReportMock(0)); |
408
|
|
|
$rule->apply($this->getMethod()); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @return void |
413
|
|
|
* @since 2.0.1 |
414
|
|
|
*/ |
415
|
|
|
public function testNamespacedCompactFunctionRuleDoesNotApply() |
416
|
|
|
{ |
417
|
|
|
$rule = new UnusedFormalParameter(); |
418
|
|
|
$rule->setReport($this->getReportMock(0)); |
419
|
|
|
$rule->apply($this->getMethod()); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* @return void |
424
|
|
|
* @since 2.0.1 |
425
|
|
|
*/ |
426
|
|
|
public function testNamespacedCompactFunctionRuleOnlyAppliesToUsedParameters() |
427
|
|
|
{ |
428
|
|
|
$rule = new UnusedFormalParameter(); |
429
|
|
|
$rule->setReport($this->getReportMock(2)); |
430
|
|
|
$rule->apply($this->getMethod()); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* @return void |
435
|
|
|
* @since 2.0.1 |
436
|
|
|
*/ |
437
|
|
|
public function testNamespacedCompactFunctionRuleWorksCaseInsensitive() |
438
|
|
|
{ |
439
|
|
|
$rule = new UnusedFormalParameter(); |
440
|
|
|
$rule->setReport($this->getReportMock(0)); |
441
|
|
|
$rule->apply($this->getMethod()); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* testRuleDoesNotApplyToFormalParameterUsedInStringCompoundVariable |
446
|
|
|
* |
447
|
|
|
* <code> |
448
|
|
|
* class Foo { |
449
|
|
|
* public function foo($bar) { |
450
|
|
|
* return "me_${bar}"; |
451
|
|
|
* } |
452
|
|
|
* } |
453
|
|
|
* </code> |
454
|
|
|
* |
455
|
|
|
* @return void |
456
|
|
|
*/ |
457
|
|
|
public function testRuleDoesNotApplyToFormalParameterUsedInStringCompoundVariable() |
458
|
|
|
{ |
459
|
|
|
$rule = new UnusedFormalParameter(); |
460
|
|
|
$rule->setReport($this->getReportMock(0)); |
461
|
|
|
$rule->apply($this->getMethod()); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* testRuleDoesNotApplyToFormalParameterUsedAsParameterInStringCompoundVariable |
466
|
|
|
* |
467
|
|
|
* <code> |
468
|
|
|
* class Foo { |
469
|
|
|
* public function foo($bar) { |
470
|
|
|
* return $this->baz("${bar}"); |
471
|
|
|
* } |
472
|
|
|
* |
473
|
|
|
* private function baz($bar) { |
474
|
|
|
* return "who ${bar}?"; |
475
|
|
|
* } |
476
|
|
|
* } |
477
|
|
|
* </code> |
478
|
|
|
* |
479
|
|
|
* @return void |
480
|
|
|
*/ |
481
|
|
|
public function testRuleDoesNotApplyToFormalParameterUsedAsParameterInStringCompoundVariable() |
482
|
|
|
{ |
483
|
|
|
$rule = new UnusedFormalParameter(); |
484
|
|
|
$rule->setReport($this->getReportMock(0)); |
485
|
|
|
$rule->apply($this->getMethod()); |
486
|
|
|
} |
487
|
|
|
} |
488
|
|
|
|