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 GetAvailRates 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 GetAvailRates, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | View Code Duplication | class GetAvailRates |
|
|
|||
6 | { |
||
7 | |||
8 | /** |
||
9 | * @var ArrayOfInt $iGuids |
||
10 | */ |
||
11 | protected $iGuids = null; |
||
12 | |||
13 | /** |
||
14 | * @var \DateTime $dtArrivalDate |
||
15 | */ |
||
16 | protected $dtArrivalDate = null; |
||
17 | |||
18 | /** |
||
19 | * @var \DateTime $dtDepartureDate |
||
20 | */ |
||
21 | protected $dtDepartureDate = null; |
||
22 | |||
23 | /** |
||
24 | * @var int $intGuestCount |
||
25 | */ |
||
26 | protected $intGuestCount = null; |
||
27 | |||
28 | /** |
||
29 | * @var string $strChildrens |
||
30 | */ |
||
31 | protected $strChildrens = null; |
||
32 | |||
33 | /** |
||
34 | * @var string $strISOLanguageCode |
||
35 | */ |
||
36 | protected $strISOLanguageCode = null; |
||
37 | |||
38 | /** |
||
39 | * @var int $intID_AccommodationType |
||
40 | */ |
||
41 | protected $intID_AccommodationType = null; |
||
42 | |||
43 | /** |
||
44 | * @var int $intRoomQty |
||
45 | */ |
||
46 | protected $intRoomQty = null; |
||
47 | |||
48 | /** |
||
49 | * @var string $strListOfPromoRates |
||
50 | */ |
||
51 | protected $strListOfPromoRates = null; |
||
52 | |||
53 | /** |
||
54 | * @var boolean $IsPromoByRate |
||
55 | */ |
||
56 | protected $IsPromoByRate = null; |
||
57 | |||
58 | /** |
||
59 | * @var int $intRateSpecial |
||
60 | */ |
||
61 | protected $intRateSpecial = null; |
||
62 | |||
63 | /** |
||
64 | * @var string $strAttributes |
||
65 | */ |
||
66 | protected $strAttributes = null; |
||
67 | |||
68 | /** |
||
69 | * @var string $strLocations |
||
70 | */ |
||
71 | protected $strLocations = null; |
||
72 | |||
73 | /** |
||
74 | * @var string $strRateCodes |
||
75 | */ |
||
76 | protected $strRateCodes = null; |
||
77 | |||
78 | /** |
||
79 | * @var string $strRoomTypes |
||
80 | */ |
||
81 | protected $strRoomTypes = null; |
||
82 | |||
83 | /** |
||
84 | * @var string $strBuildings |
||
85 | */ |
||
86 | protected $strBuildings = null; |
||
87 | |||
88 | /** |
||
89 | * @var string $strBeddingIDs |
||
90 | */ |
||
91 | protected $strBeddingIDs = null; |
||
92 | |||
93 | /** |
||
94 | * @var string $strAttributeGroupings |
||
95 | */ |
||
96 | protected $strAttributeGroupings = null; |
||
97 | |||
98 | /** |
||
99 | * @var string $strLocationsGroupings |
||
100 | */ |
||
101 | protected $strLocationsGroupings = null; |
||
102 | |||
103 | /** |
||
104 | * @param ArrayOfInt $iGuids |
||
105 | * @param \DateTime $dtArrivalDate |
||
106 | * @param \DateTime $dtDepartureDate |
||
107 | * @param int $intGuestCount |
||
108 | * @param string $strChildrens |
||
109 | * @param string $strISOLanguageCode |
||
110 | * @param int $intID_AccommodationType |
||
111 | * @param int $intRoomQty |
||
112 | * @param string $strListOfPromoRates |
||
113 | * @param boolean $IsPromoByRate |
||
114 | * @param int $intRateSpecial |
||
115 | * @param string $strAttributes |
||
116 | * @param string $strLocations |
||
117 | * @param string $strRateCodes |
||
118 | * @param string $strRoomTypes |
||
119 | * @param string $strBuildings |
||
120 | * @param string $strBeddingIDs |
||
121 | * @param string $strAttributeGroupings |
||
122 | * @param string $strLocationsGroupings |
||
123 | */ |
||
124 | public function __construct($iGuids, \DateTime $dtArrivalDate, \DateTime $dtDepartureDate, $intGuestCount, $strChildrens, $strISOLanguageCode, $intID_AccommodationType, $intRoomQty, $strListOfPromoRates, $IsPromoByRate, $intRateSpecial, $strAttributes, $strLocations, $strRateCodes, $strRoomTypes, $strBuildings, $strBeddingIDs, $strAttributeGroupings, $strLocationsGroupings) |
||
146 | |||
147 | /** |
||
148 | * @return ArrayOfInt |
||
149 | */ |
||
150 | public function getIGuids() |
||
154 | |||
155 | /** |
||
156 | * @param ArrayOfInt $iGuids |
||
157 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
158 | */ |
||
159 | public function setIGuids($iGuids) |
||
164 | |||
165 | /** |
||
166 | * @return \DateTime |
||
167 | */ |
||
168 | public function getDtArrivalDate() |
||
180 | |||
181 | /** |
||
182 | * @param \DateTime $dtArrivalDate |
||
183 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
184 | */ |
||
185 | public function setDtArrivalDate(\DateTime $dtArrivalDate) |
||
190 | |||
191 | /** |
||
192 | * @return \DateTime |
||
193 | */ |
||
194 | public function getDtDepartureDate() |
||
206 | |||
207 | /** |
||
208 | * @param \DateTime $dtDepartureDate |
||
209 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
210 | */ |
||
211 | public function setDtDepartureDate(\DateTime $dtDepartureDate) |
||
216 | |||
217 | /** |
||
218 | * @return int |
||
219 | */ |
||
220 | public function getIntGuestCount() |
||
224 | |||
225 | /** |
||
226 | * @param int $intGuestCount |
||
227 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
228 | */ |
||
229 | public function setIntGuestCount($intGuestCount) |
||
234 | |||
235 | /** |
||
236 | * @return string |
||
237 | */ |
||
238 | public function getStrChildrens() |
||
242 | |||
243 | /** |
||
244 | * @param string $strChildrens |
||
245 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
246 | */ |
||
247 | public function setStrChildrens($strChildrens) |
||
252 | |||
253 | /** |
||
254 | * @return string |
||
255 | */ |
||
256 | public function getStrISOLanguageCode() |
||
260 | |||
261 | /** |
||
262 | * @param string $strISOLanguageCode |
||
263 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
264 | */ |
||
265 | public function setStrISOLanguageCode($strISOLanguageCode) |
||
270 | |||
271 | /** |
||
272 | * @return int |
||
273 | */ |
||
274 | public function getIntID_AccommodationType() |
||
278 | |||
279 | /** |
||
280 | * @param int $intID_AccommodationType |
||
281 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
282 | */ |
||
283 | public function setIntID_AccommodationType($intID_AccommodationType) |
||
288 | |||
289 | /** |
||
290 | * @return int |
||
291 | */ |
||
292 | public function getIntRoomQty() |
||
296 | |||
297 | /** |
||
298 | * @param int $intRoomQty |
||
299 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
300 | */ |
||
301 | public function setIntRoomQty($intRoomQty) |
||
306 | |||
307 | /** |
||
308 | * @return string |
||
309 | */ |
||
310 | public function getStrListOfPromoRates() |
||
314 | |||
315 | /** |
||
316 | * @param string $strListOfPromoRates |
||
317 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
318 | */ |
||
319 | public function setStrListOfPromoRates($strListOfPromoRates) |
||
324 | |||
325 | /** |
||
326 | * @return boolean |
||
327 | */ |
||
328 | public function getIsPromoByRate() |
||
332 | |||
333 | /** |
||
334 | * @param boolean $IsPromoByRate |
||
335 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
336 | */ |
||
337 | public function setIsPromoByRate($IsPromoByRate) |
||
342 | |||
343 | /** |
||
344 | * @return int |
||
345 | */ |
||
346 | public function getIntRateSpecial() |
||
350 | |||
351 | /** |
||
352 | * @param int $intRateSpecial |
||
353 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
354 | */ |
||
355 | public function setIntRateSpecial($intRateSpecial) |
||
360 | |||
361 | /** |
||
362 | * @return string |
||
363 | */ |
||
364 | public function getStrAttributes() |
||
368 | |||
369 | /** |
||
370 | * @param string $strAttributes |
||
371 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
372 | */ |
||
373 | public function setStrAttributes($strAttributes) |
||
378 | |||
379 | /** |
||
380 | * @return string |
||
381 | */ |
||
382 | public function getStrLocations() |
||
386 | |||
387 | /** |
||
388 | * @param string $strLocations |
||
389 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
390 | */ |
||
391 | public function setStrLocations($strLocations) |
||
396 | |||
397 | /** |
||
398 | * @return string |
||
399 | */ |
||
400 | public function getStrRateCodes() |
||
404 | |||
405 | /** |
||
406 | * @param string $strRateCodes |
||
407 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
408 | */ |
||
409 | public function setStrRateCodes($strRateCodes) |
||
414 | |||
415 | /** |
||
416 | * @return string |
||
417 | */ |
||
418 | public function getStrRoomTypes() |
||
422 | |||
423 | /** |
||
424 | * @param string $strRoomTypes |
||
425 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
426 | */ |
||
427 | public function setStrRoomTypes($strRoomTypes) |
||
432 | |||
433 | /** |
||
434 | * @return string |
||
435 | */ |
||
436 | public function getStrBuildings() |
||
440 | |||
441 | /** |
||
442 | * @param string $strBuildings |
||
443 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
444 | */ |
||
445 | public function setStrBuildings($strBuildings) |
||
450 | |||
451 | /** |
||
452 | * @return string |
||
453 | */ |
||
454 | public function getStrBeddingIDs() |
||
458 | |||
459 | /** |
||
460 | * @param string $strBeddingIDs |
||
461 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
462 | */ |
||
463 | public function setStrBeddingIDs($strBeddingIDs) |
||
468 | |||
469 | /** |
||
470 | * @return string |
||
471 | */ |
||
472 | public function getStrAttributeGroupings() |
||
476 | |||
477 | /** |
||
478 | * @param string $strAttributeGroupings |
||
479 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
480 | */ |
||
481 | public function setStrAttributeGroupings($strAttributeGroupings) |
||
486 | |||
487 | /** |
||
488 | * @return string |
||
489 | */ |
||
490 | public function getStrLocationsGroupings() |
||
494 | |||
495 | /** |
||
496 | * @param string $strLocationsGroupings |
||
497 | * @return \Gueststream\PMS\IQWare\API\GetAvailRates |
||
498 | */ |
||
499 | public function setStrLocationsGroupings($strLocationsGroupings) |
||
504 | } |
||
505 |
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.