Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 27 | class CamelCaseMethodNameTest extends AbstractTest |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Tests that the rule does not apply for a valid method name. |
||
| 31 | * |
||
| 32 | * @return void |
||
| 33 | */ |
||
| 34 | public function testRuleDoesNotApplyForValidMethodName() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Tests that the rule does apply for an method name |
||
| 49 | * starting with a capital. |
||
| 50 | * |
||
| 51 | * @return void |
||
| 52 | */ |
||
| 53 | View Code Duplication | public function testRuleDoesApplyForMethodNameWithCapital() |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Tests that the rule does apply for a method name |
||
| 70 | * with underscores. |
||
| 71 | * |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function testRuleDoesApplyForMethodNameWithUnderscores() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Tests that the rule does apply for a valid method name |
||
| 91 | * with an underscore at the beginning when it is allowed. |
||
| 92 | * |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | View Code Duplication | public function testRuleDoesApplyForValidMethodNameWithUnderscoreWhenNotAllowed() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Tests that the rule does not apply for a valid method name |
||
| 111 | * with an underscore at the beginning when it is not allowed. |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | View Code Duplication | public function testRuleDoesNotApplyForValidMethodNameWithUnderscoreWhenAllowed() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Tests that the rule does apply for a valid test method name |
||
| 131 | * with an underscore. |
||
| 132 | * |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | View Code Duplication | public function testRuleDoesApplyForTestMethodWithUnderscoreWhenNotAllowed() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Tests that the rule does not apply for a valid test method name |
||
| 151 | * with an underscore when an single underscore is allowed. |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function testRuleDoesNotApplyForTestMethodWithUnderscoreWhenAllowed() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Tests that the rule does apply for a test method name |
||
| 170 | * with multiple underscores even when one is allowed. |
||
| 171 | * |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function testRuleAppliesToTestMethodWithTwoUnderscoresEvenWhenOneIsAllowed() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Tests that the rule does apply to for test method names that |
||
| 189 | * have a capital after their single allowed underscore. |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | View Code Duplication | public function testRuleAppliesToTestMethodWithUnderscoreFollowedByCapital() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Tests that the rule does apply to for test method names that |
||
| 208 | * have multiple underscore in the declaration of test |
||
| 209 | * |
||
| 210 | * @return void |
||
| 211 | */ |
||
| 212 | View Code Duplication | public function testRuleAppliesToTestMethodWithMultipleUnderscores() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Tests that the rule does apply to for test method names that |
||
| 227 | * have multiple underscore in the declaration of test |
||
| 228 | * |
||
| 229 | * @return void |
||
| 230 | */ |
||
| 231 | View Code Duplication | public function testRuleDoesNotApplyForTestMethodWithMultipleUnderscoreWhenAllowed() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Returns the first method found in a source file related to the calling |
||
| 246 | * test method. |
||
| 247 | * |
||
| 248 | * @return \PHPMD\Node\MethodNode |
||
| 249 | */ |
||
| 250 | protected function getMethod() |
||
| 255 | } |
||
| 256 |
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.