Passed
Push — master ( 902a34...c826a7 )
by Kyle
53s queued 11s
created

test/php/PHPMD/Rule/Naming/ShortVariableTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
     * testRuleAppliesToTryCatchBlocks
46
     *
47
     * @return void
48
     */
49 View Code Duplication
    public function testRuleNotAppliesToTryCatchBlocksInsideForeach()
50
    {
51
        $rule = new ShortVariable();
52
        $rule->addProperty('minimum', 3);
53
        $rule->addProperty('exceptions', '');
54
        $rule->setReport($this->getReportWithNoViolation());
55
        $rule->apply($this->getFunction());
56
    }
57
58
    /**
59
     * testRuleNotAppliesToLocalVariableInFunctionWithNameLongerThanThreshold
60
     *
61
     * @return void
62
     */
63 View Code Duplication
    public function testRuleNotAppliesToLocalVariableInFunctionWithNameLongerThanThreshold()
64
    {
65
        $rule = new ShortVariable();
66
        $rule->addProperty('minimum', 2);
67
        $rule->addProperty('exceptions', '');
68
        $rule->setReport($this->getReportWithNoViolation());
69
        $rule->apply($this->getFunction());
70
    }
71
72
    /**
73
     * testRuleNotAppliesToLocalVariableInFunctionWithNameEqualToThreshold
74
     *
75
     * @return void
76
     */
77 View Code Duplication
    public function testRuleNotAppliesToLocalVariableInFunctionWithNameEqualToThreshold()
78
    {
79
        $rule = new ShortVariable();
80
        $rule->addProperty('minimum', 3);
81
        $rule->addProperty('exceptions', '');
82
        $rule->setReport($this->getReportWithNoViolation());
83
        $rule->apply($this->getFunction());
84
    }
85
86
    /**
87
     * testRuleAppliesToFunctionParameterWithNameShorterThanThreshold
88
     *
89
     * @return void
90
     */
91
    public function testRuleAppliesToFunctionParameterWithNameShorterThanThreshold()
92
    {
93
        $rule = new ShortVariable();
94
        $rule->addProperty('minimum', 3);
95
        $rule->addProperty('exceptions', '');
96
        $rule->setReport($this->getReportWithOneViolation());
97
        $rule->apply($this->getFunction());
98
    }
99
100
    /**
101
     * testRuleNotAppliesToFunctionParameterWithNameLongerThanThreshold
102
     *
103
     * @return void
104
     */
105 View Code Duplication
    public function testRuleNotAppliesToFunctionParameterWithNameLongerThanThreshold()
106
    {
107
        $rule = new ShortVariable();
108
        $rule->addProperty('minimum', 3);
109
        $rule->addProperty('exceptions', '');
110
        $rule->setReport($this->getReportWithNoViolation());
111
        $rule->apply($this->getFunction());
112
    }
113
114
    /**
115
     * testRuleAppliesToLocalVariableInMethodWithNameShorterThanThreshold
116
     *
117
     * @return void
118
     */
119
    public function testRuleAppliesToLocalVariableInMethodWithNameShorterThanThreshold()
120
    {
121
        $rule = new ShortVariable();
122
        $rule->addProperty('minimum', 3);
123
        $rule->addProperty('exceptions', '');
124
        $rule->setReport($this->getReportWithOneViolation());
125
126
        $class = $this->getClass();
127
        $rule->apply($class);
128
129
        foreach ($class->getMethods() as $method) {
130
            $rule->apply($method);
131
        }
132
    }
133
134
    /**
135
     * testRuleNotAppliesToLocalVariableInMethodWithNameEqualToThreshold
136
     *
137
     * @return void
138
     */
139
    public function testRuleNotAppliesToLocalVariableInMethodWithNameEqualToThreshold()
140
    {
141
        $rule = new ShortVariable();
142
        $rule->addProperty('minimum', 3);
143
        $rule->addProperty('exceptions', '');
144
        $rule->setReport($this->getReportWithNoViolation());
145
        $rule->apply($this->getClass());
146
    }
147
148
    /**
149
     * testRuleNotAppliesToLocalVariableInMethodWithNameLongerThanThreshold
150
     *
151
     * @return void
152
     */
153
    public function testRuleNotAppliesToLocalVariableInMethodWithNameLongerThanThreshold()
154
    {
155
        $rule = new ShortVariable();
156
        $rule->addProperty('minimum', 2);
157
        $rule->addProperty('exceptions', '');
158
        $rule->setReport($this->getReportWithNoViolation());
159
        $rule->apply($this->getClass());
160
    }
161
162
    /**
163
     * testRuleAppliesToMethodParameterWithNameShorterThanThreshold
164
     *
165
     * @return void
166
     */
167
    public function testRuleAppliesToMethodParameterWithNameShorterThanThreshold()
168
    {
169
        $rule = new ShortVariable();
170
        $rule->addProperty('minimum', 3);
171
        $rule->addProperty('exceptions', '');
172
        $rule->setReport($this->getReportWithOneViolation());
173
174
        $class = $this->getClass();
175
        $rule->apply($class);
176
177
        foreach ($class->getMethods() as $method) {
178
            $rule->apply($method);
179
        }
180
    }
181
182
    /**
183
     * testRuleNotAppliesToMethodParameterWithNameLongerThanThreshold
184
     *
185
     * @return void
186
     */
187
    public function testRuleNotAppliesToMethodParameterWithNameLongerThanThreshold()
188
    {
189
        $rule = new ShortVariable();
190
        $rule->addProperty('minimum', 2);
191
        $rule->addProperty('exceptions', '');
192
        $rule->setReport($this->getReportWithNoViolation());
193
        $rule->apply($this->getClass());
194
    }
195
196
    /**
197
     * testRuleAppliesToFieldWithNameShorterThanThreshold
198
     *
199
     * @return void
200
     */
201
    public function testRuleAppliesToFieldWithNameShorterThanThreshold()
202
    {
203
        $rule = new ShortVariable();
204
        $rule->addProperty('minimum', 3);
205
        $rule->addProperty('exceptions', '');
206
        $rule->setReport($this->getReportWithOneViolation());
207
        $rule->apply($this->getClass());
208
    }
209
210
    /**
211
     * testRuleNotAppliesToFieldWithNameEqualToThreshold
212
     *
213
     * @return void
214
     */
215
    public function testRuleNotAppliesToFieldWithNameEqualToThreshold()
216
    {
217
        $rule = new ShortVariable();
218
        $rule->addProperty('minimum', 3);
219
        $rule->addProperty('exceptions', '');
220
        $rule->setReport($this->getReportWithNoViolation());
221
        $rule->apply($this->getClass());
222
    }
223
224
    /**
225
     * testRuleNotAppliesToFieldWithNameGreaterThanThreshold
226
     *
227
     * @return void
228
     */
229
    public function testRuleNotAppliesToFieldWithNameGreaterThanThreshold()
230
    {
231
        $rule = new ShortVariable();
232
        $rule->addProperty('minimum', 2);
233
        $rule->addProperty('exceptions', '');
234
        $rule->setReport($this->getReportWithNoViolation());
235
        $rule->apply($this->getClass());
236
    }
237
238
    /**
239
     * testRuleAppliesToFieldAndParameterWithNameShorterThanThreshold
240
     *
241
     * @return void
242
     */
243 View Code Duplication
    public function testRuleAppliesToFieldAndParameterWithNameShorterThanThreshold()
244
    {
245
        $rule = new ShortVariable();
246
        $rule->addProperty('minimum', 3);
247
        $rule->addProperty('exceptions', '');
248
        $rule->setReport($this->getReportMock(2));
249
250
        $class = $this->getClass();
251
        $rule->apply($class);
252
253
        foreach ($class->getMethods() as $method) {
254
            $rule->apply($method);
255
        }
256
    }
257
258
    /**
259
     * testRuleNotAppliesToShortVariableNameAsForLoopIndex
260
     *
261
     * @return void
262
     */
263 View Code Duplication
    public function testRuleNotAppliesToShortVariableNameAsForLoopIndex()
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
     * testRuleNotAppliesToShortVariableNameAsForeachLoopIndex
274
     *
275
     * @return void
276
     */
277 View Code Duplication
    public function testRuleNotAppliesToShortVariableNameAsForeachLoopIndex()
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
     * testRuleNotAppliesToShortVariableNameInCatchStatement
288
     *
289
     * @return void
290
     */
291 View Code Duplication
    public function testRuleNotAppliesToShortVariableNameInCatchStatement()
292
    {
293
        $rule = new ShortVariable();
294
        $rule->addProperty('minimum', 3);
295
        $rule->addProperty('exceptions', '');
296
        $rule->setReport($this->getReportWithNoViolation());
297
        $rule->apply($this->getFunction());
298
    }
299
300
    /**
301
     * testRuleNotAppliesToStaticMembersAccessedInMethod
302
     *
303
     * @return void
304
     */
305
    public function testRuleNotAppliesToStaticMembersAccessedInMethod()
306
    {
307
        $rule = new ShortVariable();
308
        $rule->addProperty('minimum', 3);
309
        $rule->addProperty('exceptions', '');
310
        $rule->setReport($this->getReportWithNoViolation());
311
        $rule->apply($this->getMethod());
312
    }
313
314
    /**
315
     * testRuleAppliesToIdenticalVariableOnlyOneTime
316
     *
317
     * @return void
318
     */
319 View Code Duplication
    public function testRuleAppliesToIdenticalVariableOnlyOneTime()
320
    {
321
        $rule = new ShortVariable();
322
        $rule->addProperty('minimum', 3);
323
        $rule->addProperty('exceptions', '');
324
        $rule->setReport($this->getReportMock(2));
325
        $rule->apply($this->getMethod());
326
    }
327
328
    /**
329
     * testRuleAppliesToIdenticalVariablesInDifferentContextsSeveralTimes
330
     *
331
     * @return void
332
     */
333 View Code Duplication
    public function testRuleAppliesToIdenticalVariablesInDifferentContextsSeveralTimes()
334
    {
335
        $rule = new ShortVariable();
336
        $rule->addProperty('minimum', 3);
337
        $rule->addProperty('exceptions', '');
338
        $rule->setReport($this->getReportMock(2));
0 ignored issues
show
It seems like $this->getReportMock(2) targeting PHPMD\AbstractTest::getReportMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PHPMD\AbstractRule::setReport() does only seem to accept object<PHPMD\Report>, maybe add an additional type check?

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.

Loading history...
339
340
        $class = $this->getClass();
341
        $rule->apply($class);
342
343
        foreach ($class->getMethods() as $method) {
344
            $rule->apply($method);
345
        }
346
    }
347
348
    /**
349
     * testRuleNotAppliesToVariablesFromExceptionsList
350
     *
351
     * @return void
352
     */
353
    public function testRuleNotAppliesToVariablesFromExceptionsList()
354
    {
355
        $rule = new ShortVariable();
356
        $rule->addProperty('minimum', 3);
357
        $rule->addProperty('exceptions', 'id');
358
        $rule->setReport($this->getReportWithNoViolation());
359
360
        $rule->apply($this->getClass());
361
    }
362
363
    /**
364
     * testRuleAppliesToVariablesWithinForeach
365
     *
366
     * @dataProvider provideClassWithShortForeachVariables
367
     * @return void
368
     */
369
    public function testRuleAppliesToVariablesWithinForeach($allowShortVarInLoop, $expectedErrorsCount)
370
    {
371
        $rule = new ShortVariable();
372
        $rule->addProperty('minimum', 3);
373
        $rule->addProperty('exceptions', '');
374
        $rule->addProperty('allow-short-variables-in-loop', $allowShortVarInLoop);
375
        $rule->setReport($this->getReportMock($expectedErrorsCount));
0 ignored issues
show
It seems like $this->getReportMock($expectedErrorsCount) targeting PHPMD\AbstractTest::getReportMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PHPMD\AbstractRule::setReport() does only seem to accept object<PHPMD\Report>, maybe add an additional type check?

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.

Loading history...
376
377
        $class = $this->getClass();
378
        $rule->apply($class);
379
380
        foreach ($class->getMethods() as $method) {
381
            $rule->apply($method);
382
        }
383
    }
384
385
    public function provideClassWithShortForeachVariables()
386
    {
387
        return array(
388
            array(true, 2),
389
            array(false, 5),
390
        );
391
    }
392
}
393