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 |
||
28 | class PHPMDTest extends AbstractTest |
||
29 | { |
||
30 | /** |
||
31 | * Tests the main PHPMD interface with default settings an a xml-renderer. |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | View Code Duplication | public function testRunWithDefaultSettingsAndXmlRenderer() |
|
55 | |||
56 | /** |
||
57 | * testRunWithDefaultSettingsAndXmlRendererAgainstSingleFile |
||
58 | * |
||
59 | * @return void |
||
60 | */ |
||
61 | View Code Duplication | public function testRunWithDefaultSettingsAndXmlRendererAgainstDirectory() |
|
80 | |||
81 | /** |
||
82 | * testRunWithDefaultSettingsAndXmlRendererAgainstSingleFile |
||
83 | * |
||
84 | * @return void |
||
85 | */ |
||
86 | View Code Duplication | public function testRunWithDefaultSettingsAndXmlRendererAgainstSingleFile() |
|
105 | |||
106 | /** |
||
107 | * testHasViolationsReturnsFalseByDefault |
||
108 | * |
||
109 | * @return void |
||
110 | */ |
||
111 | public function testHasViolationsReturnsFalseByDefault() |
||
116 | |||
117 | /** |
||
118 | * testHasViolationsReturnsFalseForSourceWithoutViolations |
||
119 | * |
||
120 | * @return void |
||
121 | */ |
||
122 | View Code Duplication | public function testHasViolationsReturnsFalseForSourceWithoutViolations() |
|
139 | |||
140 | /** |
||
141 | * testHasViolationsReturnsTrueForSourceWithViolation |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | View Code Duplication | public function testHasViolationsReturnsTrueForSourceWithViolation() |
|
162 | } |
||
163 |
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.