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 |
||
12 | View Code Duplication | class MethodOneTryCatchTest extends AbstractProcessTest |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * @covers MS\PHPMD\Rule\CleanCode\MethodOneTryCatch |
||
16 | */ |
||
17 | public function testMethodOneTryCatchRule() |
||
18 | { |
||
19 | $output = $this |
||
20 | ->runPhpmd('Utility/TryThings.php', 'cleancode.xml') |
||
21 | ->getOutput(); |
||
22 | |||
23 | $this->assertContains('Utility/TryThings.php:39 This method contains more than one try statement. Swap out the try statement in an extra method. It increase the readability.', $output); |
||
24 | $this->assertContains('Utility/TryThings.php:53 This method contains more than one try statement. Swap out the try statement in an extra method. It increase the readability.', $output); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @covers MS\PHPMD\Rule\CleanCode\MethodOneTryCatch |
||
29 | */ |
||
30 | public function testRuleWithoutTry() |
||
38 | } |
||
39 |
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.