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 |
||
31 | class CommandLineOptionsTest extends AbstractTest |
||
32 | { |
||
33 | /** |
||
34 | * @var resource |
||
35 | */ |
||
36 | private $stderrStreamFilter; |
||
37 | |||
38 | /** |
||
39 | * @return void |
||
40 | */ |
||
41 | View Code Duplication | protected function tearDown() |
|
50 | |||
51 | /** |
||
52 | * testAssignsInputArgumentToInputProperty |
||
53 | * |
||
54 | * @return void |
||
55 | * @since 1.1.0 |
||
56 | */ |
||
57 | View Code Duplication | public function testAssignsInputArgumentToInputProperty() |
|
64 | |||
65 | /** |
||
66 | * testAssignsFormatArgumentToReportFormatProperty |
||
67 | * |
||
68 | * @return void |
||
69 | * @since 1.1.0 |
||
70 | */ |
||
71 | View Code Duplication | public function testAssignsFormatArgumentToReportFormatProperty() |
|
78 | |||
79 | /** |
||
80 | * testAssignsRuleSetsArgumentToRuleSetProperty |
||
81 | * |
||
82 | * @return void |
||
83 | * @since 1.1.0 |
||
84 | */ |
||
85 | View Code Duplication | public function testAssignsRuleSetsArgumentToRuleSetProperty() |
|
92 | |||
93 | /** |
||
94 | * testThrowsExpectedExceptionWhenRequiredArgumentsNotSet |
||
95 | * |
||
96 | * @return void |
||
97 | * @since 1.1.0 |
||
98 | * @expectedException \InvalidArgumentException |
||
99 | */ |
||
100 | public function testThrowsExpectedExceptionWhenRequiredArgumentsNotSet() |
||
105 | |||
106 | /** |
||
107 | * testAssignsInputFileOptionToInputPathProperty |
||
108 | * |
||
109 | * @return void |
||
110 | * @since 1.1.0 |
||
111 | */ |
||
112 | View Code Duplication | public function testAssignsInputFileOptionToInputPathProperty() |
|
121 | |||
122 | /** |
||
123 | * testAssignsFormatArgumentCorrectWhenCalledWithInputFile |
||
124 | * |
||
125 | * @return void |
||
126 | * @since 1.1.0 |
||
127 | */ |
||
128 | View Code Duplication | public function testAssignsFormatArgumentCorrectWhenCalledWithInputFile() |
|
137 | |||
138 | /** |
||
139 | * testAssignsRuleSetsArgumentCorrectWhenCalledWithInputFile |
||
140 | * |
||
141 | * @return void |
||
142 | * @since 1.1.0 |
||
143 | */ |
||
144 | View Code Duplication | public function testAssignsRuleSetsArgumentCorrectWhenCalledWithInputFile() |
|
153 | |||
154 | /** |
||
155 | * testThrowsExpectedExceptionWhenInputFileNotExists |
||
156 | * |
||
157 | * @return void |
||
158 | * @since 1.1.0 |
||
159 | * @expectedException \InvalidArgumentException |
||
160 | */ |
||
161 | public function testThrowsExpectedExceptionWhenInputFileNotExists() |
||
166 | |||
167 | /** |
||
168 | * testHasVersionReturnsFalseByDefault |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | public function testHasVersionReturnsFalseByDefault() |
||
179 | |||
180 | /** |
||
181 | * testCliOptionsAcceptsVersionArgument |
||
182 | * |
||
183 | * @return void |
||
184 | */ |
||
185 | public function testCliOptionsAcceptsVersionArgument() |
||
192 | |||
193 | /** |
||
194 | * Tests if ignoreViolationsOnExit returns false by default |
||
195 | * |
||
196 | * @return void |
||
197 | */ |
||
198 | public function testIgnoreViolationsOnExitReturnsFalseByDefault() |
||
205 | |||
206 | /** |
||
207 | * Tests if CLI options accepts ignoreViolationsOnExit argument |
||
208 | * |
||
209 | * @return void |
||
210 | */ |
||
211 | public function testCliOptionsAcceptsIgnoreViolationsOnExitArgument() |
||
218 | |||
219 | /** |
||
220 | * Tests if CLI usage contains ignoreViolationsOnExit option |
||
221 | * |
||
222 | * @return void |
||
223 | */ |
||
224 | View Code Duplication | public function testCliUsageContainsIgnoreViolationsOnExitOption() |
|
231 | |||
232 | /** |
||
233 | * Tests if CLI usage contains the auto-discovered renderers |
||
234 | * |
||
235 | * @return void |
||
236 | */ |
||
237 | View Code Duplication | public function testCliUsageContainsAutoDiscoveredRenderers() |
|
244 | |||
245 | /** |
||
246 | * testCliUsageContainsStrictOption |
||
247 | * |
||
248 | * @return void |
||
249 | */ |
||
250 | View Code Duplication | public function testCliUsageContainsStrictOption() |
|
257 | |||
258 | /** |
||
259 | * testCliOptionsIsStrictReturnsFalseByDefault |
||
260 | * |
||
261 | * @return void |
||
262 | * @since 1.2.0 |
||
263 | */ |
||
264 | public function testCliOptionsIsStrictReturnsFalseByDefault() |
||
271 | |||
272 | /** |
||
273 | * testCliOptionsAcceptsStrictArgument |
||
274 | * |
||
275 | * @return void |
||
276 | * @since 1.2.0 |
||
277 | */ |
||
278 | public function testCliOptionsAcceptsStrictArgument() |
||
285 | |||
286 | /** |
||
287 | * @return void |
||
288 | */ |
||
289 | View Code Duplication | public function testCliOptionsAcceptsMinimumpriorityArgument() |
|
296 | |||
297 | /** |
||
298 | * @return void |
||
299 | */ |
||
300 | View Code Duplication | public function testCliOptionsAcceptsMaximumpriorityArgument() |
|
307 | |||
308 | /** |
||
309 | * @return void |
||
310 | */ |
||
311 | View Code Duplication | public function testGetMinimumPriorityReturnsLowestValueByDefault() |
|
318 | |||
319 | /** |
||
320 | * @return void |
||
321 | */ |
||
322 | public function testGetCoverageReportReturnsNullByDefault() |
||
329 | |||
330 | /** |
||
331 | * @return void |
||
332 | */ |
||
333 | View Code Duplication | public function testGetCoverageReportWithCliOption() |
|
348 | |||
349 | /** |
||
350 | * @param string $reportFormat |
||
351 | * @param string $expectedClass |
||
352 | * @return void |
||
353 | * @dataProvider dataProviderCreateRenderer |
||
354 | */ |
||
355 | public function testCreateRenderer($reportFormat, $expectedClass) |
||
362 | |||
363 | /** |
||
364 | * @return array |
||
365 | */ |
||
366 | public function dataProviderCreateRenderer() |
||
378 | |||
379 | /** |
||
380 | * @param string $reportFormat |
||
381 | * @return void |
||
382 | * @expectedException \InvalidArgumentException |
||
383 | * @expectedExceptionMessageRegExp (^Can\'t ) |
||
384 | * @dataProvider dataProviderCreateRendererThrowsException |
||
385 | */ |
||
386 | public function testCreateRendererThrowsException($reportFormat) |
||
392 | |||
393 | /** |
||
394 | * @return array |
||
395 | */ |
||
396 | public function dataProviderCreateRendererThrowsException() |
||
403 | |||
404 | /** |
||
405 | * @param string $deprecatedName |
||
406 | * @param string $newName |
||
407 | * @dataProvider dataProviderDeprecatedCliOptions |
||
408 | */ |
||
409 | public function testDeprecatedCliOptions($deprecatedName, $newName) |
||
427 | |||
428 | /** |
||
429 | * @return array |
||
430 | */ |
||
431 | public function dataProviderDeprecatedCliOptions() |
||
438 | |||
439 | /** |
||
440 | * @param array $options |
||
441 | * @param array $expected |
||
442 | * @return void |
||
443 | * @dataProvider dataProviderGetReportFiles |
||
444 | */ |
||
445 | View Code Duplication | public function testGetReportFiles(array $options, array $expected) |
|
452 | |||
453 | public function dataProviderGetReportFiles() |
||
481 | } |
||
482 |
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.