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 |
||
7 | View Code Duplication | class PhpUnitGuardCoverage implements ToolInterface |
|
|
|||
8 | { |
||
9 | /** |
||
10 | * @var Undefined |
||
11 | */ |
||
12 | private $undefined; |
||
13 | /** |
||
14 | * @var Enabled |
||
15 | */ |
||
16 | private $enabled; |
||
17 | /** |
||
18 | * @var Message |
||
19 | */ |
||
20 | private $warningMessage; |
||
21 | |||
22 | /** |
||
23 | * PhpUnitGuardCoverage constructor. |
||
24 | * |
||
25 | * @param Undefined $undefined |
||
26 | * @param Enabled $enabled |
||
27 | * @param Message $warningMessage |
||
28 | */ |
||
29 | 6 | public function __construct(Undefined $undefined, Enabled $enabled, Message $warningMessage) |
|
35 | |||
36 | /** |
||
37 | * @return bool |
||
38 | */ |
||
39 | 3 | public function isEnabled() |
|
43 | |||
44 | /** |
||
45 | * @return bool |
||
46 | */ |
||
47 | 2 | public function isUndefined() |
|
51 | |||
52 | /** |
||
53 | * @return Message |
||
54 | */ |
||
55 | 3 | public function getWarningMessage() |
|
59 | |||
60 | /** |
||
61 | * @param Enabled $enabled |
||
62 | * |
||
63 | * @return PhpUnitGuardCoverage |
||
64 | */ |
||
65 | 2 | public function setEnabled(Enabled $enabled) |
|
66 | { |
||
67 | 2 | return new self( |
|
68 | 2 | new Undefined(false), |
|
69 | 2 | $enabled, |
|
70 | 2 | $this->warningMessage |
|
71 | ); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param Message $message |
||
76 | * |
||
77 | * @return PhpUnitGuardCoverage |
||
78 | */ |
||
79 | 1 | public function setWarningMessage(Message $message) |
|
87 | } |
||
88 |
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.