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 very short method and function name rule. |
24
|
|
|
* |
25
|
|
|
* @covers PHPMD\Rule\Naming\ShortMethodName |
26
|
|
|
*/ |
27
|
|
|
class ShortMethodNameTest extends AbstractTest |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* testRuleAppliesToFunctionWithNameShorterThanThreshold |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function testRuleAppliesToFunctionWithNameShorterThanThreshold() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$rule = new ShortMethodName(); |
37
|
|
|
$rule->addProperty('minimum', 54); |
38
|
|
|
$rule->addProperty('exceptions', ''); |
39
|
|
|
$rule->setReport($this->getReportMock(1)); |
40
|
|
|
$rule->apply($this->getFunction()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* testRuleNotAppliesToFunctionWithNameEqualToThreshold |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function testRuleNotAppliesToFunctionWithNameEqualToThreshold() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$rule = new ShortMethodName(); |
51
|
|
|
$rule->addProperty('minimum', 52); |
52
|
|
|
$rule->addProperty('exceptions', ''); |
53
|
|
|
$rule->setReport($this->getReportMock(0)); |
54
|
|
|
$rule->apply($this->getFunction()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* testRuleNotAppliesToFunctionWithNameLongerThanThreshold |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function testRuleNotAppliesToFunctionWithNameLongerThanThreshold() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$rule = new ShortMethodName(); |
65
|
|
|
$rule->addProperty('minimum', 54); |
66
|
|
|
$rule->addProperty('exceptions', ''); |
67
|
|
|
$rule->setReport($this->getReportMock(0)); |
68
|
|
|
$rule->apply($this->getFunction()); |
69
|
|
|
} |
70
|
|
|
/** |
71
|
|
|
* testRuleAppliesToFunctionWithNameShorterThanThreshold |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function testRuleAppliesToMethodWithNameShorterThanThreshold() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$rule = new ShortMethodName(); |
78
|
|
|
$rule->addProperty('minimum', 52); |
79
|
|
|
$rule->addProperty('exceptions', ''); |
80
|
|
|
$rule->setReport($this->getReportMock(1)); |
81
|
|
|
$rule->apply($this->getMethod()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* testRuleNotAppliesToMethodWithNameEqualToThreshold |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function testRuleNotAppliesToMethodWithNameEqualToThreshold() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$rule = new ShortMethodName(); |
92
|
|
|
$rule->addProperty('minimum', 50); |
93
|
|
|
$rule->addProperty('exceptions', ''); |
94
|
|
|
$rule->setReport($this->getReportMock(0)); |
95
|
|
|
$rule->apply($this->getMethod()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* testRuleNotAppliesToMethodWithNameLongerThanThreshold |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function testRuleNotAppliesToMethodWithNameLongerThanThreshold() |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$rule = new ShortMethodName(); |
106
|
|
|
$rule->addProperty('minimum', 52); |
107
|
|
|
$rule->addProperty('exceptions', ''); |
108
|
|
|
$rule->setReport($this->getReportMock(0)); |
109
|
|
|
$rule->apply($this->getMethod()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* testRuleNotAppliesToMethodWithNameLongerThanThreshold |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function testRuleNotAppliesToMethodWithShortNameWhenException() |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$rule = new ShortMethodName(); |
120
|
|
|
$rule->addProperty('minimum', 100); |
121
|
|
|
$rule->addProperty('exceptions', 'testRuleNotAppliesToMethodWithShortNameWhenException,another'); |
122
|
|
|
$rule->setReport($this->getReportMock(0)); |
123
|
|
|
$rule->apply($this->getMethod()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* testRuleAlsoWorksWithoutExceptionListConfigured |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
* @since 2.2.2 |
131
|
|
|
* @link https://github.com/phpmd/phpmd/issues/80 |
132
|
|
|
* @link https://github.com/phpmd/phpmd/issues/270 |
133
|
|
|
*/ |
134
|
|
View Code Duplication |
public function testRuleAlsoWorksWithoutExceptionListConfigured() |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$rule = new ShortMethodName(); |
137
|
|
|
$rule->addProperty('minimum', 100); |
138
|
|
|
$rule->setReport($this->getReportMock(0)); |
139
|
|
|
$rule->apply($this->getMethodMock()); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.