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:
Complex classes like Customer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Customer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Customer |
||
13 | { |
||
14 | |||
15 | const CUSTOMER_PREFERRED_LANGUAGE_NL = 'nl-BE'; |
||
16 | const CUSTOMER_PREFERRED_LANGUAGE_FR = 'fr-BE'; |
||
17 | const CUSTOMER_PREFERRED_LANGUAGE_EN = 'en-US'; |
||
18 | |||
19 | const CUSTOMER_TITLE_MR = 'Mr.'; |
||
20 | const CUSTOMER_TITLE_MS = 'Ms.'; |
||
21 | |||
22 | /** |
||
23 | * @var bool |
||
24 | */ |
||
25 | private $activated; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $userID; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $firstName; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $lastName; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $companyName; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | private $street; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $number; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $email; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | private $mobilePrefix = '0032'; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | private $mobileNumber; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | private $postalCode; |
||
76 | |||
77 | /** |
||
78 | * @var array |
||
79 | */ |
||
80 | private $packStations = array(); |
||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | private $town; |
||
86 | |||
87 | /** |
||
88 | * @var string |
||
89 | */ |
||
90 | private $preferredLanguage; |
||
91 | |||
92 | /** |
||
93 | * @var string |
||
94 | */ |
||
95 | private $title; |
||
96 | |||
97 | /** |
||
98 | * @var bool |
||
99 | */ |
||
100 | private $isComfortZoneUser; |
||
101 | |||
102 | /** |
||
103 | * @var \DateTime |
||
104 | */ |
||
105 | private $dateOfBirth; |
||
106 | |||
107 | /** |
||
108 | * @var string |
||
109 | */ |
||
110 | private $deliveryCode; |
||
111 | |||
112 | /** |
||
113 | * @var bool |
||
114 | */ |
||
115 | private $optIn; |
||
116 | |||
117 | /** |
||
118 | * @var bool |
||
119 | */ |
||
120 | private $receivePromotions; |
||
121 | |||
122 | /** |
||
123 | * @var bool |
||
124 | */ |
||
125 | private $useInformationForThirdParty; |
||
126 | |||
127 | /** |
||
128 | * @var string |
||
129 | */ |
||
130 | private $userName; |
||
131 | |||
132 | /** |
||
133 | * @param boolean $activated |
||
134 | */ |
||
135 | 1 | public function setActivated($activated) |
|
139 | |||
140 | /** |
||
141 | * @return boolean |
||
142 | */ |
||
143 | 1 | public function getActivated() |
|
147 | |||
148 | /** |
||
149 | * @param string $companyName |
||
150 | */ |
||
151 | 1 | public function setCompanyName($companyName) |
|
155 | |||
156 | /** |
||
157 | * @return string |
||
158 | */ |
||
159 | 1 | public function getCompanyName() |
|
163 | |||
164 | /** |
||
165 | * @param \DateTime $dateOfBirth |
||
166 | */ |
||
167 | 1 | public function setDateOfBirth($dateOfBirth) |
|
171 | |||
172 | /** |
||
173 | * @return \DateTime |
||
174 | */ |
||
175 | 1 | public function getDateOfBirth() |
|
179 | |||
180 | /** |
||
181 | * @param string $deliveryCode |
||
182 | */ |
||
183 | 1 | public function setDeliveryCode($deliveryCode) |
|
187 | |||
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | 1 | public function getDeliveryCode() |
|
195 | |||
196 | /** |
||
197 | * @param string $email |
||
198 | */ |
||
199 | 2 | public function setEmail($email) |
|
203 | |||
204 | /** |
||
205 | * @return string |
||
206 | */ |
||
207 | 2 | public function getEmail() |
|
211 | |||
212 | /** |
||
213 | * @param string $firstName |
||
214 | */ |
||
215 | 2 | public function setFirstName($firstName) |
|
219 | |||
220 | /** |
||
221 | * @return string |
||
222 | */ |
||
223 | 2 | public function getFirstName() |
|
227 | |||
228 | /** |
||
229 | * @param boolean $isComfortZoneUser |
||
230 | */ |
||
231 | 1 | public function setIsComfortZoneUser($isComfortZoneUser) |
|
235 | |||
236 | /** |
||
237 | * @return boolean |
||
238 | */ |
||
239 | 1 | public function getIsComfortZoneUser() |
|
243 | |||
244 | /** |
||
245 | * @param string $lastName |
||
246 | */ |
||
247 | 2 | public function setLastName($lastName) |
|
251 | |||
252 | /** |
||
253 | * @return string |
||
254 | */ |
||
255 | 2 | public function getLastName() |
|
259 | |||
260 | /** |
||
261 | * @param string $mobileNumber |
||
262 | */ |
||
263 | 2 | public function setMobileNumber($mobileNumber) |
|
267 | |||
268 | /** |
||
269 | * @return string |
||
270 | */ |
||
271 | 2 | public function getMobileNumber() |
|
275 | |||
276 | /** |
||
277 | * @param string $mobilePrefix |
||
278 | */ |
||
279 | 1 | public function setMobilePrefix($mobilePrefix) |
|
283 | |||
284 | /** |
||
285 | * @return string |
||
286 | */ |
||
287 | 2 | public function getMobilePrefix() |
|
291 | |||
292 | /** |
||
293 | * @param string $number |
||
294 | */ |
||
295 | 2 | public function setNumber($number) |
|
299 | |||
300 | /** |
||
301 | * @return string |
||
302 | */ |
||
303 | 2 | public function getNumber() |
|
307 | |||
308 | /** |
||
309 | * @param boolean $optIn |
||
310 | */ |
||
311 | 1 | public function setOptIn($optIn) |
|
315 | |||
316 | /** |
||
317 | * @return boolean |
||
318 | */ |
||
319 | 1 | public function getOptIn() |
|
323 | |||
324 | /** |
||
325 | * @param CustomerPackStation $packStation |
||
326 | */ |
||
327 | 1 | public function addPackStation(CustomerPackStation $packStation) |
|
331 | |||
332 | /** |
||
333 | * @param array $packStations |
||
334 | */ |
||
335 | 1 | public function setPackStations($packStations) |
|
339 | |||
340 | /** |
||
341 | * @return array |
||
342 | */ |
||
343 | 2 | public function getPackStations() |
|
347 | |||
348 | /** |
||
349 | * @param string $postalCode |
||
350 | */ |
||
351 | 2 | public function setPostalCode($postalCode) |
|
355 | |||
356 | /** |
||
357 | * @return string |
||
358 | */ |
||
359 | 2 | public function getPostalCode() |
|
363 | |||
364 | /** |
||
365 | * @param string $preferredLanguage |
||
366 | * @throws BpostInvalidValueException |
||
367 | */ |
||
368 | 3 | public function setPreferredLanguage($preferredLanguage) |
|
380 | |||
381 | /** |
||
382 | * @return string |
||
383 | */ |
||
384 | 2 | public function getPreferredLanguage() |
|
388 | |||
389 | /** |
||
390 | * @return array |
||
391 | */ |
||
392 | 3 | public static function getPossiblePreferredLanguageValues() |
|
400 | |||
401 | /** |
||
402 | * @param boolean $receivePromotions |
||
403 | */ |
||
404 | 1 | public function setReceivePromotions($receivePromotions) |
|
408 | |||
409 | /** |
||
410 | * @return boolean |
||
411 | */ |
||
412 | 1 | public function getReceivePromotions() |
|
416 | |||
417 | /** |
||
418 | * @param string $street |
||
419 | */ |
||
420 | 2 | public function setStreet($street) |
|
424 | |||
425 | /** |
||
426 | * @return string |
||
427 | */ |
||
428 | 2 | public function getStreet() |
|
432 | |||
433 | /** |
||
434 | * @param string $title |
||
435 | * @throws BpostInvalidValueException |
||
436 | */ |
||
437 | 3 | public function setTitle($title) |
|
445 | |||
446 | /** |
||
447 | * @return string |
||
448 | */ |
||
449 | 2 | public function getTitle() |
|
453 | |||
454 | /** |
||
455 | * @return array |
||
456 | */ |
||
457 | 3 | public static function getPossibleTitleValues() |
|
464 | |||
465 | /** |
||
466 | * @param string $town |
||
467 | */ |
||
468 | 1 | public function setTown($town) |
|
472 | |||
473 | /** |
||
474 | * @return string |
||
475 | */ |
||
476 | 1 | public function getTown() |
|
480 | |||
481 | /** |
||
482 | * @param boolean $useInformationForThirdParty |
||
483 | */ |
||
484 | 1 | public function setUseInformationForThirdParty($useInformationForThirdParty) |
|
488 | |||
489 | /** |
||
490 | * @return boolean |
||
491 | */ |
||
492 | 1 | public function getUseInformationForThirdParty() |
|
496 | |||
497 | /** |
||
498 | * @param string $userID |
||
499 | */ |
||
500 | 1 | public function setUserID($userID) |
|
504 | |||
505 | /** |
||
506 | * @return string |
||
507 | */ |
||
508 | 1 | public function getUserID() |
|
512 | |||
513 | /** |
||
514 | * @param string $userName |
||
515 | */ |
||
516 | 1 | public function setUserName($userName) |
|
520 | |||
521 | /** |
||
522 | * @return string |
||
523 | */ |
||
524 | 1 | public function getUserName() |
|
528 | |||
529 | /** |
||
530 | * Return the object as an array for usage in the XML |
||
531 | * |
||
532 | * @param \DOMDocument $document |
||
533 | * @return \DOMElement |
||
534 | */ |
||
535 | 1 | public function toXML(\DOMDocument $document) |
|
638 | |||
639 | /** |
||
640 | * @param \SimpleXMLElement $xml |
||
641 | * |
||
642 | * @return Customer |
||
643 | * @throws BpostInvalidValueException |
||
644 | * @throws BpostXmlNoUserIdFoundException |
||
645 | */ |
||
646 | 1 | public static function createFromXML(\SimpleXMLElement $xml) |
|
734 | } |
||
735 |
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.