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\Naming; |
19
|
|
|
|
20
|
|
|
use PHPMD\AbstractTest; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Test case for the really short variable, parameter and property name rule. |
24
|
|
|
* |
25
|
|
|
* @covers PHPMD\Rule\Naming\ShortVariable |
26
|
|
|
*/ |
27
|
|
|
class ShortVariableTest extends AbstractTest |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* testRuleAppliesToLocalVariableInFunctionWithNameShorterThanThreshold |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function testRuleAppliesToLocalVariableInFunctionWithNameShorterThanThreshold() |
36
|
|
|
{ |
37
|
|
|
$rule = new ShortVariable(); |
38
|
|
|
$rule->addProperty('minimum', 3); |
39
|
|
|
$rule->addProperty('exceptions', ''); |
40
|
|
|
$rule->setReport($this->getReportWithOneViolation()); |
41
|
|
|
$rule->apply($this->getFunction()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* testRuleNotAppliesToLocalVariableInFunctionWithNameLongerThanThreshold |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function testRuleNotAppliesToLocalVariableInFunctionWithNameLongerThanThreshold() |
50
|
|
|
{ |
51
|
|
|
$rule = new ShortVariable(); |
52
|
|
|
$rule->addProperty('minimum', 2); |
53
|
|
|
$rule->addProperty('exceptions', ''); |
54
|
|
|
$rule->setReport($this->getReportWithNoViolation()); |
55
|
|
|
$rule->apply($this->getFunction()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* testRuleNotAppliesToLocalVariableInFunctionWithNameEqualToThreshold |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function testRuleNotAppliesToLocalVariableInFunctionWithNameEqualToThreshold() |
64
|
|
|
{ |
65
|
|
|
$rule = new ShortVariable(); |
66
|
|
|
$rule->addProperty('minimum', 3); |
67
|
|
|
$rule->addProperty('exceptions', ''); |
68
|
|
|
$rule->setReport($this->getReportWithNoViolation()); |
69
|
|
|
$rule->apply($this->getFunction()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* testRuleAppliesToFunctionParameterWithNameShorterThanThreshold |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function testRuleAppliesToFunctionParameterWithNameShorterThanThreshold() |
78
|
|
|
{ |
79
|
|
|
$rule = new ShortVariable(); |
80
|
|
|
$rule->addProperty('minimum', 3); |
81
|
|
|
$rule->addProperty('exceptions', ''); |
82
|
|
|
$rule->setReport($this->getReportWithOneViolation()); |
83
|
|
|
$rule->apply($this->getFunction()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* testRuleNotAppliesToFunctionParameterWithNameLongerThanThreshold |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function testRuleNotAppliesToFunctionParameterWithNameLongerThanThreshold() |
92
|
|
|
{ |
93
|
|
|
$rule = new ShortVariable(); |
94
|
|
|
$rule->addProperty('minimum', 3); |
95
|
|
|
$rule->addProperty('exceptions', ''); |
96
|
|
|
$rule->setReport($this->getReportWithNoViolation()); |
97
|
|
|
$rule->apply($this->getFunction()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* testRuleAppliesToLocalVariableInMethodWithNameShorterThanThreshold |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function testRuleAppliesToLocalVariableInMethodWithNameShorterThanThreshold() |
106
|
|
|
{ |
107
|
|
|
$rule = new ShortVariable(); |
108
|
|
|
$rule->addProperty('minimum', 3); |
109
|
|
|
$rule->addProperty('exceptions', ''); |
110
|
|
|
$rule->setReport($this->getReportWithOneViolation()); |
111
|
|
|
|
112
|
|
|
$class = $this->getClass(); |
113
|
|
|
$rule->apply($class); |
114
|
|
|
|
115
|
|
|
foreach ($class->getMethods() as $method) { |
116
|
|
|
$rule->apply($method); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* testRuleNotAppliesToLocalVariableInMethodWithNameEqualToThreshold |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function testRuleNotAppliesToLocalVariableInMethodWithNameEqualToThreshold() |
126
|
|
|
{ |
127
|
|
|
$rule = new ShortVariable(); |
128
|
|
|
$rule->addProperty('minimum', 3); |
129
|
|
|
$rule->addProperty('exceptions', ''); |
130
|
|
|
$rule->setReport($this->getReportWithNoViolation()); |
131
|
|
|
$rule->apply($this->getClass()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* testRuleNotAppliesToLocalVariableInMethodWithNameLongerThanThreshold |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function testRuleNotAppliesToLocalVariableInMethodWithNameLongerThanThreshold() |
140
|
|
|
{ |
141
|
|
|
$rule = new ShortVariable(); |
142
|
|
|
$rule->addProperty('minimum', 2); |
143
|
|
|
$rule->addProperty('exceptions', ''); |
144
|
|
|
$rule->setReport($this->getReportWithNoViolation()); |
145
|
|
|
$rule->apply($this->getClass()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* testRuleAppliesToMethodParameterWithNameShorterThanThreshold |
150
|
|
|
* |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
public function testRuleAppliesToMethodParameterWithNameShorterThanThreshold() |
154
|
|
|
{ |
155
|
|
|
$rule = new ShortVariable(); |
156
|
|
|
$rule->addProperty('minimum', 3); |
157
|
|
|
$rule->addProperty('exceptions', ''); |
158
|
|
|
$rule->setReport($this->getReportWithOneViolation()); |
159
|
|
|
|
160
|
|
|
$class = $this->getClass(); |
161
|
|
|
$rule->apply($class); |
162
|
|
|
|
163
|
|
|
foreach ($class->getMethods() as $method) { |
164
|
|
|
$rule->apply($method); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* testRuleNotAppliesToMethodParameterWithNameLongerThanThreshold |
170
|
|
|
* |
171
|
|
|
* @return void |
172
|
|
|
*/ |
173
|
|
|
public function testRuleNotAppliesToMethodParameterWithNameLongerThanThreshold() |
174
|
|
|
{ |
175
|
|
|
$rule = new ShortVariable(); |
176
|
|
|
$rule->addProperty('minimum', 2); |
177
|
|
|
$rule->addProperty('exceptions', ''); |
178
|
|
|
$rule->setReport($this->getReportWithNoViolation()); |
179
|
|
|
$rule->apply($this->getClass()); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* testRuleAppliesToFieldWithNameShorterThanThreshold |
184
|
|
|
* |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
|
|
public function testRuleAppliesToFieldWithNameShorterThanThreshold() |
188
|
|
|
{ |
189
|
|
|
$rule = new ShortVariable(); |
190
|
|
|
$rule->addProperty('minimum', 3); |
191
|
|
|
$rule->addProperty('exceptions', ''); |
192
|
|
|
$rule->setReport($this->getReportWithOneViolation()); |
193
|
|
|
$rule->apply($this->getClass()); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* testRuleNotAppliesToFieldWithNameEqualToThreshold |
198
|
|
|
* |
199
|
|
|
* @return void |
200
|
|
|
*/ |
201
|
|
|
public function testRuleNotAppliesToFieldWithNameEqualToThreshold() |
202
|
|
|
{ |
203
|
|
|
$rule = new ShortVariable(); |
204
|
|
|
$rule->addProperty('minimum', 3); |
205
|
|
|
$rule->addProperty('exceptions', ''); |
206
|
|
|
$rule->setReport($this->getReportWithNoViolation()); |
207
|
|
|
$rule->apply($this->getClass()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* testRuleNotAppliesToFieldWithNameGreaterThanThreshold |
212
|
|
|
* |
213
|
|
|
* @return void |
214
|
|
|
*/ |
215
|
|
|
public function testRuleNotAppliesToFieldWithNameGreaterThanThreshold() |
216
|
|
|
{ |
217
|
|
|
$rule = new ShortVariable(); |
218
|
|
|
$rule->addProperty('minimum', 2); |
219
|
|
|
$rule->addProperty('exceptions', ''); |
220
|
|
|
$rule->setReport($this->getReportWithNoViolation()); |
221
|
|
|
$rule->apply($this->getClass()); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* testRuleAppliesToFieldAndParameterWithNameShorterThanThreshold |
226
|
|
|
* |
227
|
|
|
* @return void |
228
|
|
|
*/ |
229
|
|
View Code Duplication |
public function testRuleAppliesToFieldAndParameterWithNameShorterThanThreshold() |
230
|
|
|
{ |
231
|
|
|
$rule = new ShortVariable(); |
232
|
|
|
$rule->addProperty('minimum', 3); |
233
|
|
|
$rule->addProperty('exceptions', ''); |
234
|
|
|
$rule->setReport($this->getReportMock(2)); |
|
|
|
|
235
|
|
|
|
236
|
|
|
$class = $this->getClass(); |
237
|
|
|
$rule->apply($class); |
238
|
|
|
|
239
|
|
|
foreach ($class->getMethods() as $method) { |
240
|
|
|
$rule->apply($method); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* testRuleNotAppliesToShortVariableNameAsForLoopIndex |
246
|
|
|
* |
247
|
|
|
* @return void |
248
|
|
|
*/ |
249
|
|
View Code Duplication |
public function testRuleNotAppliesToShortVariableNameAsForLoopIndex() |
250
|
|
|
{ |
251
|
|
|
$rule = new ShortVariable(); |
252
|
|
|
$rule->addProperty('minimum', 3); |
253
|
|
|
$rule->addProperty('exceptions', ''); |
254
|
|
|
$rule->setReport($this->getReportWithNoViolation()); |
255
|
|
|
$rule->apply($this->getFunction()); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* testRuleNotAppliesToShortVariableNameAsForeachLoopIndex |
260
|
|
|
* |
261
|
|
|
* @return void |
262
|
|
|
*/ |
263
|
|
View Code Duplication |
public function testRuleNotAppliesToShortVariableNameAsForeachLoopIndex() |
264
|
|
|
{ |
265
|
|
|
$rule = new ShortVariable(); |
266
|
|
|
$rule->addProperty('minimum', 3); |
267
|
|
|
$rule->addProperty('exceptions', ''); |
268
|
|
|
$rule->setReport($this->getReportWithNoViolation()); |
269
|
|
|
$rule->apply($this->getFunction()); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* testRuleNotAppliesToShortVariableNameInCatchStatement |
274
|
|
|
* |
275
|
|
|
* @return void |
276
|
|
|
*/ |
277
|
|
View Code Duplication |
public function testRuleNotAppliesToShortVariableNameInCatchStatement() |
278
|
|
|
{ |
279
|
|
|
$rule = new ShortVariable(); |
280
|
|
|
$rule->addProperty('minimum', 3); |
281
|
|
|
$rule->addProperty('exceptions', ''); |
282
|
|
|
$rule->setReport($this->getReportWithNoViolation()); |
283
|
|
|
$rule->apply($this->getFunction()); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* testRuleNotAppliesToStaticMembersAccessedInMethod |
288
|
|
|
* |
289
|
|
|
* @return void |
290
|
|
|
*/ |
291
|
|
|
public function testRuleNotAppliesToStaticMembersAccessedInMethod() |
292
|
|
|
{ |
293
|
|
|
$rule = new ShortVariable(); |
294
|
|
|
$rule->addProperty('minimum', 3); |
295
|
|
|
$rule->addProperty('exceptions', ''); |
296
|
|
|
$rule->setReport($this->getReportWithNoViolation()); |
297
|
|
|
$rule->apply($this->getMethod()); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* testRuleAppliesToIdenticalVariableOnlyOneTime |
302
|
|
|
* |
303
|
|
|
* @return void |
304
|
|
|
*/ |
305
|
|
View Code Duplication |
public function testRuleAppliesToIdenticalVariableOnlyOneTime() |
306
|
|
|
{ |
307
|
|
|
$rule = new ShortVariable(); |
308
|
|
|
$rule->addProperty('minimum', 3); |
309
|
|
|
$rule->addProperty('exceptions', ''); |
310
|
|
|
$rule->setReport($this->getReportMock(2)); |
|
|
|
|
311
|
|
|
$rule->apply($this->getMethod()); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* testRuleAppliesToIdenticalVariablesInDifferentContextsSeveralTimes |
316
|
|
|
* |
317
|
|
|
* @return void |
318
|
|
|
*/ |
319
|
|
View Code Duplication |
public function testRuleAppliesToIdenticalVariablesInDifferentContextsSeveralTimes() |
320
|
|
|
{ |
321
|
|
|
$rule = new ShortVariable(); |
322
|
|
|
$rule->addProperty('minimum', 3); |
323
|
|
|
$rule->addProperty('exceptions', ''); |
324
|
|
|
$rule->setReport($this->getReportMock(2)); |
|
|
|
|
325
|
|
|
|
326
|
|
|
$class = $this->getClass(); |
327
|
|
|
$rule->apply($class); |
328
|
|
|
|
329
|
|
|
foreach ($class->getMethods() as $method) { |
330
|
|
|
$rule->apply($method); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* testRuleNotAppliesToVariablesFromExceptionsList |
336
|
|
|
* |
337
|
|
|
* @return void |
338
|
|
|
*/ |
339
|
|
|
public function testRuleNotAppliesToVariablesFromExceptionsList() |
340
|
|
|
{ |
341
|
|
|
$rule = new ShortVariable(); |
342
|
|
|
$rule->addProperty('minimum', 3); |
343
|
|
|
$rule->addProperty('exceptions', 'id'); |
344
|
|
|
$rule->setReport($this->getReportWithNoViolation()); |
345
|
|
|
|
346
|
|
|
$rule->apply($this->getClass()); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* testRuleAppliesToVariablesWithinForeach |
351
|
|
|
* |
352
|
|
|
* @dataProvider provideClassWithShortForeachVariables |
353
|
|
|
* @return void |
354
|
|
|
*/ |
355
|
|
|
public function testRuleAppliesToVariablesWithinForeach($allowShortVarInLoop, $expectedErrorsCount) |
356
|
|
|
{ |
357
|
|
|
$rule = new ShortVariable(); |
358
|
|
|
$rule->addProperty('minimum', 3); |
359
|
|
|
$rule->addProperty('exceptions', ''); |
360
|
|
|
$rule->addProperty('allow-short-variables-in-loop', $allowShortVarInLoop); |
361
|
|
|
$rule->setReport($this->getReportMock($expectedErrorsCount)); |
|
|
|
|
362
|
|
|
|
363
|
|
|
$class = $this->getClass(); |
364
|
|
|
$rule->apply($class); |
365
|
|
|
|
366
|
|
|
foreach ($class->getMethods() as $method) { |
367
|
|
|
$rule->apply($method); |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function provideClassWithShortForeachVariables() |
372
|
|
|
{ |
373
|
|
|
return array( |
374
|
|
|
array(true, 2), |
375
|
|
|
array(false, 5), |
376
|
|
|
); |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.