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 | class PhpCs implements ToolInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var Undefined |
||
11 | */ |
||
12 | private $undefined; |
||
13 | /** |
||
14 | * @var Enabled |
||
15 | */ |
||
16 | private $enabled; |
||
17 | /** |
||
18 | * @var PhpCsStandard |
||
19 | */ |
||
20 | private $standard; |
||
21 | /** |
||
22 | * @var Ignore |
||
23 | */ |
||
24 | private $ignore; |
||
25 | |||
26 | /** |
||
27 | * PhpCs constructor. |
||
28 | * |
||
29 | * @param Undefined $undefined |
||
30 | * @param Enabled $enabled |
||
31 | * @param PhpCsStandard $standard |
||
32 | * @param Ignore $ignore |
||
33 | */ |
||
34 | 6 | public function __construct(Undefined $undefined, Enabled $enabled, PhpCsStandard $standard, Ignore $ignore) |
|
41 | |||
42 | /** |
||
43 | * @return bool |
||
44 | */ |
||
45 | 3 | public function isEnabled() |
|
49 | |||
50 | /** |
||
51 | * @return bool |
||
52 | */ |
||
53 | 2 | public function isUndefined() |
|
57 | |||
58 | /** |
||
59 | * @return PhpCsStandard |
||
60 | */ |
||
61 | 3 | public function getStandard() |
|
65 | |||
66 | /** |
||
67 | * @return Ignore |
||
68 | */ |
||
69 | 3 | public function getIgnore() |
|
73 | |||
74 | /** |
||
75 | * @return ToolInterface |
||
76 | */ |
||
77 | 2 | View Code Duplication | public function setEnabled(Enabled $enabled) |
|
|||
78 | { |
||
79 | 2 | return new self( |
|
80 | 2 | new Undefined(false), |
|
81 | $enabled, |
||
82 | 2 | new PhpCsStandard(null), |
|
83 | 2 | new Ignore(null) |
|
84 | ); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param PhpCsStandard $standard |
||
89 | * |
||
90 | * @return PhpCs |
||
91 | */ |
||
92 | 1 | public function addStandard(PhpCsStandard $standard) |
|
93 | { |
||
94 | 1 | return new self( |
|
95 | 1 | $this->undefined, |
|
96 | 1 | $this->enabled, |
|
97 | $standard, |
||
98 | 1 | $this->ignore |
|
99 | ); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param Ignore $ignore |
||
104 | */ |
||
105 | 1 | public function addIgnore(Ignore $ignore) |
|
109 | } |
||
110 |
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.