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 | class Product implements NodeInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $description1; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $description2; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $description3; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $commodityCode; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $partNumber; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $originCountryCode; |
||
43 | |||
44 | /** |
||
45 | * @var Unit |
||
46 | */ |
||
47 | private $unit; |
||
48 | |||
49 | /** |
||
50 | * @var PackingListInfo |
||
51 | */ |
||
52 | private $packingListInfo; |
||
53 | |||
54 | /** |
||
55 | * @param null|object $attributes |
||
56 | */ |
||
57 | 1 | public function __construct($attributes = null) |
|
74 | |||
75 | /** |
||
76 | * @param null|DOMDocument $document |
||
77 | * |
||
78 | * @return DOMElement |
||
79 | */ |
||
80 | 1 | public function toNode(DOMDocument $document = null) |
|
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | */ |
||
115 | 1 | public function getDescription1() |
|
119 | |||
120 | /** |
||
121 | * @param string $description |
||
122 | * |
||
123 | * @return $this |
||
124 | */ |
||
125 | View Code Duplication | public function setDescription1($description) |
|
135 | |||
136 | /** |
||
137 | * @return string |
||
138 | */ |
||
139 | 1 | public function getDescription2() |
|
143 | |||
144 | /** |
||
145 | * @param string $description |
||
146 | * |
||
147 | * @return $this |
||
148 | */ |
||
149 | View Code Duplication | public function setDescription2($description) |
|
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | 1 | public function getDescription3() |
|
167 | |||
168 | /** |
||
169 | * @param string $description |
||
170 | * |
||
171 | * @return $this |
||
172 | */ |
||
173 | View Code Duplication | public function setDescription3($description) |
|
183 | |||
184 | /** |
||
185 | * @return string |
||
186 | */ |
||
187 | 1 | public function getCommodityCode() |
|
191 | |||
192 | /** |
||
193 | * @param string $code |
||
194 | * |
||
195 | * @return $this |
||
196 | */ |
||
197 | public function setCommodityCode($code) |
||
203 | |||
204 | /** |
||
205 | * @param Unit $unit |
||
206 | * |
||
207 | * @return $this |
||
208 | */ |
||
209 | public function setUnit(Unit $unit) |
||
215 | |||
216 | /** |
||
217 | * @return Unit |
||
218 | */ |
||
219 | 1 | public function getUnit() |
|
223 | |||
224 | /** |
||
225 | * @param $number |
||
226 | * |
||
227 | * @return $this |
||
228 | */ |
||
229 | public function setPartNumber($number) |
||
235 | |||
236 | /** |
||
237 | * @return string |
||
238 | */ |
||
239 | 1 | public function getPartNumber() |
|
243 | |||
244 | /** |
||
245 | * @param string $countryCode |
||
246 | * |
||
247 | * @return $this |
||
248 | */ |
||
249 | public function setOriginCountryCode($countryCode) |
||
255 | |||
256 | /** |
||
257 | * @return string |
||
258 | */ |
||
259 | 1 | public function getOriginCountryCode() |
|
263 | |||
264 | /** |
||
265 | * @param PackingListInfo $info |
||
266 | * |
||
267 | * @return $this |
||
268 | */ |
||
269 | public function setPackingListInfo(PackingListInfo $info) |
||
275 | |||
276 | /** |
||
277 | * @return PackingListInfo |
||
278 | */ |
||
279 | 1 | public function getPackingListInfo() |
|
283 | } |
||
284 |
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.