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 |
||
35 | class ParserTest extends AbstractTest |
||
36 | { |
||
37 | /** |
||
38 | * Tests that the metrics adapter delegates a node to a registered rule-set. |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | View Code Duplication | public function testAdapterDelegatesClassNodeToRuleSet() |
|
54 | |||
55 | /** |
||
56 | * Tests that the metrics adapter does not delegate a node without source |
||
57 | * code file to a registered rule-set. |
||
58 | * |
||
59 | * @return void |
||
60 | */ |
||
61 | View Code Duplication | public function testAdapterDoesNotDelegateNonSourceClassNodeToRuleSet() |
|
73 | |||
74 | /** |
||
75 | * Tests that the metrics adapter delegates a node to a registered rule-set. |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | View Code Duplication | public function testAdapterDelegatesMethodNodeToRuleSet() |
|
86 | |||
87 | /** |
||
88 | * Tests that the metrics adapter does not delegate a node without source |
||
89 | * code file to a registered rule-set. |
||
90 | * |
||
91 | * @return void |
||
92 | */ |
||
93 | View Code Duplication | public function testAdapterDoesNotDelegateNonSourceMethodNodeToRuleSet() |
|
100 | |||
101 | /** |
||
102 | * Tests that the metrics adapter delegates a node to a registered rule-set. |
||
103 | * |
||
104 | * @return void |
||
105 | */ |
||
106 | View Code Duplication | public function testAdapterDelegatesFunctionNodeToRuleSet() |
|
113 | |||
114 | /** |
||
115 | * Tests that the metrics adapter does not delegate a node without source |
||
116 | * code file to a registered rule-set. |
||
117 | * |
||
118 | * @return void |
||
119 | */ |
||
120 | View Code Duplication | public function testAdapterDoesNotDelegateNonSourceFunctionNodeToRuleSet() |
|
121 | { |
||
122 | $adapter = new Parser($this->getPHPDependMock()); |
||
123 | $adapter->addRuleSet($this->getRuleSetMock()); |
||
124 | $adapter->setReport($this->getReportMock(0)); |
||
125 | $adapter->visitFunction($this->getPHPDependFunctionMock(null)); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * testParserStoreParsingExceptionsInReport |
||
130 | * |
||
131 | * @return void |
||
132 | * @since 1.2.1 |
||
133 | */ |
||
134 | public function testParserStoreParsingExceptionsInReport() |
||
150 | |||
151 | /** |
||
152 | * Creates a mocked PDepend instance. |
||
153 | * |
||
154 | * @return \PDepend\Engine |
||
155 | */ |
||
156 | private function getPHPDependMock() |
||
160 | |||
161 | /** |
||
162 | * Creates a mocked PDepend class instance. |
||
163 | * |
||
164 | * @return \PDepend\Source\AST\ASTClass |
||
165 | */ |
||
166 | protected function getPHPDependClassMock() |
||
184 | |||
185 | /** |
||
186 | * Creates a mocked PHP_Depend function instance. |
||
187 | * |
||
188 | * @param string $fileName Optional file name for the source file. |
||
189 | * @return PHP_Depend_Code_Function |
||
190 | */ |
||
191 | View Code Duplication | protected function getPHPDependFunctionMock($fileName = '/foo/bar.php') |
|
200 | |||
201 | /** |
||
202 | * Creates a mocked PHP_Depend method instance. |
||
203 | * |
||
204 | * @param string $fileName Optional file name for the source file. |
||
205 | * @return PHP_Depend_Code_CodeMethod |
||
206 | */ |
||
207 | View Code Duplication | protected function getPHPDependMethodMock($fileName = '/foo/bar.php') |
|
216 | |||
217 | /** |
||
218 | * Creates a mocked PHP_Depend file instance. |
||
219 | * |
||
220 | * @param string $fileName The temporary file name. |
||
221 | * @return PHP_Depend_Code_File |
||
222 | */ |
||
223 | protected function getPHPDependFileMock($fileName) |
||
232 | } |
||
233 |
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.