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 |
||
16 | class Customer |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * Customer Name. |
||
21 | * Cannot be Null |
||
22 | * Correspond to (cName) |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | private $name; |
||
27 | /** |
||
28 | * Mobile number. |
||
29 | * Must be at least 9 digits |
||
30 | * Mandatory |
||
31 | * Correspond to (cMobile) |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $mobile; |
||
36 | /** |
||
37 | * Address Line 1. |
||
38 | * Either of Address fields to be filled duly |
||
39 | * Correspond to (cAddr1) |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | private $addressLine1; |
||
44 | /** |
||
45 | * Destination City Name. |
||
46 | * Required |
||
47 | * Correspond to (cCity) |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | private $city; |
||
52 | /** |
||
53 | * Country. |
||
54 | * Required |
||
55 | * Correspond to (Cntry) |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | private $country; |
||
60 | |||
61 | /** Optional Properties */ |
||
62 | |||
63 | /** |
||
64 | * Address Line 2. |
||
65 | * Either of Address fields to be filled duly |
||
66 | * Correspond to (cAddr2) |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | private $addressLine2; |
||
71 | /** |
||
72 | * Zip Code. |
||
73 | * Optional |
||
74 | * Correspond to (cZip) |
||
75 | * |
||
76 | * @var string |
||
77 | */ |
||
78 | private $zipCode; |
||
79 | /** |
||
80 | * PO Box. |
||
81 | * Optional |
||
82 | * Correspond to (cPOBox) |
||
83 | * |
||
84 | * @var string |
||
85 | */ |
||
86 | private $POBox; |
||
87 | /** |
||
88 | * Telephone 1. |
||
89 | * Optional |
||
90 | * Correspond to cTel1 |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | private $tel1; |
||
95 | /** |
||
96 | * Telephone 2. |
||
97 | * Optional |
||
98 | * Correspond to cTel2 |
||
99 | * |
||
100 | * @var string |
||
101 | */ |
||
102 | private $tel2; |
||
103 | /** |
||
104 | * Email Address. |
||
105 | * Optional |
||
106 | * Correspond to (cEmail) |
||
107 | * |
||
108 | * @var string |
||
109 | */ |
||
110 | private $email; |
||
111 | |||
112 | /** |
||
113 | * Customer constructor. |
||
114 | * |
||
115 | * @param $name |
||
116 | * @param $mobile |
||
117 | * @param $addressLine1 |
||
118 | * @param $city |
||
119 | * @param string $country |
||
120 | */ |
||
121 | View Code Duplication | public function __construct( |
|
134 | |||
135 | /** |
||
136 | * Add customer details to the shipment object. |
||
137 | * |
||
138 | * @param AddShipment|AddShip $shipment |
||
139 | * |
||
140 | * @return AddShipment|AddShip |
||
141 | */ |
||
142 | public function prepareForShipment($shipment): AbstractStructBase |
||
159 | |||
160 | /** ************************************************************************************************************** |
||
161 | * Setters and Getters |
||
162 | * **************************************************************************************************************/ |
||
163 | |||
164 | /** |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getName(): string |
||
171 | |||
172 | /** |
||
173 | * @param string $name |
||
174 | * |
||
175 | * @return Customer |
||
176 | */ |
||
177 | public function setName(string $name): self |
||
183 | |||
184 | /** |
||
185 | * @return string |
||
186 | */ |
||
187 | public function getMobile(): string |
||
191 | |||
192 | /** |
||
193 | * @param string $mobile |
||
194 | * |
||
195 | * @return Customer |
||
196 | */ |
||
197 | public function setMobile(string $mobile): self |
||
203 | |||
204 | /** |
||
205 | * @return string |
||
206 | */ |
||
207 | public function getAddressLine1(): string |
||
211 | |||
212 | /** |
||
213 | * @param string $addressLine1 |
||
214 | * |
||
215 | * @return Customer |
||
216 | */ |
||
217 | public function setAddressLine1(string $addressLine1): self |
||
223 | |||
224 | /** |
||
225 | * @return string |
||
226 | */ |
||
227 | public function getCity(): string |
||
231 | |||
232 | /** |
||
233 | * @param string $city |
||
234 | * |
||
235 | * @return Customer |
||
236 | */ |
||
237 | public function setCity(string $city): self |
||
243 | |||
244 | /** |
||
245 | * @return string |
||
246 | */ |
||
247 | public function getCountry(): string |
||
251 | |||
252 | /** |
||
253 | * @param string $country |
||
254 | * |
||
255 | * @return Customer |
||
256 | */ |
||
257 | public function setCountry(string $country): self |
||
263 | |||
264 | /** |
||
265 | * @return string |
||
266 | */ |
||
267 | public function getAddressLine2() |
||
271 | |||
272 | /** |
||
273 | * @param string $addressLine2 |
||
274 | * |
||
275 | * @return Customer |
||
276 | */ |
||
277 | public function setAddressLine2(string $addressLine2): self |
||
283 | |||
284 | /** |
||
285 | * @return string |
||
286 | */ |
||
287 | public function getZipCode(): string |
||
291 | |||
292 | /** |
||
293 | * @param string $zipCode |
||
294 | * |
||
295 | * @return Customer |
||
296 | */ |
||
297 | public function setZipCode(string $zipCode): self |
||
303 | |||
304 | /** |
||
305 | * @return string |
||
306 | */ |
||
307 | public function getPOBox(): string |
||
311 | |||
312 | /** |
||
313 | * @param string $POBox |
||
314 | * |
||
315 | * @return Customer |
||
316 | */ |
||
317 | public function setPOBox(string $POBox): self |
||
323 | |||
324 | /** |
||
325 | * @return string |
||
326 | */ |
||
327 | public function getTel1(): string |
||
331 | |||
332 | /** |
||
333 | * @param string $tel1 |
||
334 | * |
||
335 | * @return Customer |
||
336 | */ |
||
337 | public function setTel1(string $tel1): self |
||
343 | |||
344 | /** |
||
345 | * @return string |
||
346 | */ |
||
347 | public function getTel2(): string |
||
351 | |||
352 | /** |
||
353 | * @param string $tel2 |
||
354 | * |
||
355 | * @return Customer |
||
356 | */ |
||
357 | public function setTel2(string $tel2): self |
||
363 | |||
364 | /** |
||
365 | * @return string |
||
366 | */ |
||
367 | public function getEmail(): string |
||
371 | |||
372 | /** |
||
373 | * @param string $email |
||
374 | * |
||
375 | * @return Customer |
||
376 | */ |
||
377 | public function setEmail(string $email): self |
||
383 | } |
||
384 |
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.