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 |
||
15 | View Code Duplication | class OrderDetailAttribute extends Base |
|
|
|||
16 | { |
||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | protected $id; |
||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | protected $orderDetailId; |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $attribute1; |
||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $attribute2; |
||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $attribute3; |
||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $attribute4; |
||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $attribute5; |
||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $attribute6; |
||
49 | |||
50 | /** |
||
51 | * @return int |
||
52 | */ |
||
53 | public function getId() |
||
57 | |||
58 | /** |
||
59 | * @param int $id |
||
60 | * |
||
61 | * @return OrderDetailAttribute |
||
62 | */ |
||
63 | public function setId($id) |
||
69 | |||
70 | /** |
||
71 | * @return int |
||
72 | */ |
||
73 | public function getOrderDetailId() |
||
77 | |||
78 | /** |
||
79 | * @param int $orderDetailId |
||
80 | * |
||
81 | * @return OrderDetailAttribute |
||
82 | */ |
||
83 | public function setOrderDetailId($orderDetailId) |
||
89 | |||
90 | /** |
||
91 | * @return string |
||
92 | */ |
||
93 | public function getAttribute1() |
||
97 | |||
98 | /** |
||
99 | * @param string $attribute1 |
||
100 | * |
||
101 | * @return OrderDetailAttribute |
||
102 | */ |
||
103 | public function setAttribute1($attribute1) |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getAttribute2() |
||
117 | |||
118 | /** |
||
119 | * @param string $attribute2 |
||
120 | * |
||
121 | * @return OrderDetailAttribute |
||
122 | */ |
||
123 | public function setAttribute2($attribute2) |
||
129 | |||
130 | /** |
||
131 | * @return string |
||
132 | */ |
||
133 | public function getAttribute3() |
||
137 | |||
138 | /** |
||
139 | * @param string $attribute3 |
||
140 | * |
||
141 | * @return OrderDetailAttribute |
||
142 | */ |
||
143 | public function setAttribute3($attribute3) |
||
149 | |||
150 | /** |
||
151 | * @return string |
||
152 | */ |
||
153 | public function getAttribute4() |
||
157 | |||
158 | /** |
||
159 | * @param string $attribute4 |
||
160 | * |
||
161 | * @return OrderDetailAttribute |
||
162 | */ |
||
163 | public function setAttribute4($attribute4) |
||
169 | |||
170 | /** |
||
171 | * @return string |
||
172 | */ |
||
173 | public function getAttribute5() |
||
177 | |||
178 | /** |
||
179 | * @param string $attribute5 |
||
180 | * |
||
181 | * @return OrderDetailAttribute |
||
182 | */ |
||
183 | public function setAttribute5($attribute5) |
||
189 | |||
190 | /** |
||
191 | * @return string |
||
192 | */ |
||
193 | public function getAttribute6() |
||
197 | |||
198 | /** |
||
199 | * @param string $attribute6 |
||
200 | * |
||
201 | * @return OrderDetailAttribute |
||
202 | */ |
||
203 | public function setAttribute6($attribute6) |
||
209 | } |
||
210 |
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.