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 DailyActivity 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 DailyActivity, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | View Code Duplication | class DailyActivity |
|
|
|||
6 | { |
||
7 | |||
8 | /** |
||
9 | * @var int $ID_Account |
||
10 | */ |
||
11 | protected $ID_Account = null; |
||
12 | |||
13 | /** |
||
14 | * @var int $AccountNo |
||
15 | */ |
||
16 | protected $AccountNo = null; |
||
17 | |||
18 | /** |
||
19 | * @var int $OOE |
||
20 | */ |
||
21 | protected $OOE = null; |
||
22 | |||
23 | /** |
||
24 | * @var int $OOR |
||
25 | */ |
||
26 | protected $OOR = null; |
||
27 | |||
28 | /** |
||
29 | * @var int $OOO |
||
30 | */ |
||
31 | protected $OOO = null; |
||
32 | |||
33 | /** |
||
34 | * @var \DateTime $AccountDate |
||
35 | */ |
||
36 | protected $AccountDate = null; |
||
37 | |||
38 | /** |
||
39 | * @var float $TotRoomRev |
||
40 | */ |
||
41 | protected $TotRoomRev = null; |
||
42 | |||
43 | /** |
||
44 | * @var float $TotComm |
||
45 | */ |
||
46 | protected $TotComm = null; |
||
47 | |||
48 | /** |
||
49 | * @var float $TotTax1 |
||
50 | */ |
||
51 | protected $TotTax1 = null; |
||
52 | |||
53 | /** |
||
54 | * @var float $TotTax2 |
||
55 | */ |
||
56 | protected $TotTax2 = null; |
||
57 | |||
58 | /** |
||
59 | * @var float $TotTax3 |
||
60 | */ |
||
61 | protected $TotTax3 = null; |
||
62 | |||
63 | /** |
||
64 | * @var float $TotOtherCB |
||
65 | */ |
||
66 | protected $TotOtherCB = null; |
||
67 | |||
68 | /** |
||
69 | * @var float $TotReferal |
||
70 | */ |
||
71 | protected $TotReferal = null; |
||
72 | |||
73 | /** |
||
74 | * @var float $TotCommCC |
||
75 | */ |
||
76 | protected $TotCommCC = null; |
||
77 | |||
78 | /** |
||
79 | * @var float $DailyPct |
||
80 | */ |
||
81 | protected $DailyPct = null; |
||
82 | |||
83 | /** |
||
84 | * @var int $BookingType |
||
85 | */ |
||
86 | protected $BookingType = null; |
||
87 | |||
88 | /** |
||
89 | * @var int $SeqNo |
||
90 | */ |
||
91 | protected $SeqNo = null; |
||
92 | |||
93 | /** |
||
94 | * @var \DateTime $ArrivalDate |
||
95 | */ |
||
96 | protected $ArrivalDate = null; |
||
97 | |||
98 | /** |
||
99 | * @var \DateTime $DepartureDate |
||
100 | */ |
||
101 | protected $DepartureDate = null; |
||
102 | |||
103 | /** |
||
104 | * @param int $ID_Account |
||
105 | * @param int $AccountNo |
||
106 | * @param int $OOE |
||
107 | * @param int $OOR |
||
108 | * @param int $OOO |
||
109 | * @param \DateTime $AccountDate |
||
110 | * @param float $TotRoomRev |
||
111 | * @param float $TotComm |
||
112 | * @param float $TotTax1 |
||
113 | * @param float $TotTax2 |
||
114 | * @param float $TotTax3 |
||
115 | * @param float $TotOtherCB |
||
116 | * @param float $TotReferal |
||
117 | * @param float $TotCommCC |
||
118 | * @param float $DailyPct |
||
119 | * @param int $BookingType |
||
120 | * @param int $SeqNo |
||
121 | * @param \DateTime $ArrivalDate |
||
122 | * @param \DateTime $DepartureDate |
||
123 | */ |
||
124 | public function __construct($ID_Account, $AccountNo, $OOE, $OOR, $OOO, \DateTime $AccountDate, $TotRoomRev, $TotComm, $TotTax1, $TotTax2, $TotTax3, $TotOtherCB, $TotReferal, $TotCommCC, $DailyPct, $BookingType, $SeqNo, \DateTime $ArrivalDate, \DateTime $DepartureDate) |
||
146 | |||
147 | /** |
||
148 | * @return int |
||
149 | */ |
||
150 | public function getID_Account() |
||
154 | |||
155 | /** |
||
156 | * @param int $ID_Account |
||
157 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
158 | */ |
||
159 | public function setID_Account($ID_Account) |
||
164 | |||
165 | /** |
||
166 | * @return int |
||
167 | */ |
||
168 | public function getAccountNo() |
||
172 | |||
173 | /** |
||
174 | * @param int $AccountNo |
||
175 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
176 | */ |
||
177 | public function setAccountNo($AccountNo) |
||
182 | |||
183 | /** |
||
184 | * @return int |
||
185 | */ |
||
186 | public function getOOE() |
||
190 | |||
191 | /** |
||
192 | * @param int $OOE |
||
193 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
194 | */ |
||
195 | public function setOOE($OOE) |
||
200 | |||
201 | /** |
||
202 | * @return int |
||
203 | */ |
||
204 | public function getOOR() |
||
208 | |||
209 | /** |
||
210 | * @param int $OOR |
||
211 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
212 | */ |
||
213 | public function setOOR($OOR) |
||
218 | |||
219 | /** |
||
220 | * @return int |
||
221 | */ |
||
222 | public function getOOO() |
||
226 | |||
227 | /** |
||
228 | * @param int $OOO |
||
229 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
230 | */ |
||
231 | public function setOOO($OOO) |
||
236 | |||
237 | /** |
||
238 | * @return \DateTime |
||
239 | */ |
||
240 | public function getAccountDate() |
||
252 | |||
253 | /** |
||
254 | * @param \DateTime $AccountDate |
||
255 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
256 | */ |
||
257 | public function setAccountDate(\DateTime $AccountDate) |
||
262 | |||
263 | /** |
||
264 | * @return float |
||
265 | */ |
||
266 | public function getTotRoomRev() |
||
270 | |||
271 | /** |
||
272 | * @param float $TotRoomRev |
||
273 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
274 | */ |
||
275 | public function setTotRoomRev($TotRoomRev) |
||
280 | |||
281 | /** |
||
282 | * @return float |
||
283 | */ |
||
284 | public function getTotComm() |
||
288 | |||
289 | /** |
||
290 | * @param float $TotComm |
||
291 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
292 | */ |
||
293 | public function setTotComm($TotComm) |
||
298 | |||
299 | /** |
||
300 | * @return float |
||
301 | */ |
||
302 | public function getTotTax1() |
||
306 | |||
307 | /** |
||
308 | * @param float $TotTax1 |
||
309 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
310 | */ |
||
311 | public function setTotTax1($TotTax1) |
||
316 | |||
317 | /** |
||
318 | * @return float |
||
319 | */ |
||
320 | public function getTotTax2() |
||
324 | |||
325 | /** |
||
326 | * @param float $TotTax2 |
||
327 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
328 | */ |
||
329 | public function setTotTax2($TotTax2) |
||
334 | |||
335 | /** |
||
336 | * @return float |
||
337 | */ |
||
338 | public function getTotTax3() |
||
342 | |||
343 | /** |
||
344 | * @param float $TotTax3 |
||
345 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
346 | */ |
||
347 | public function setTotTax3($TotTax3) |
||
352 | |||
353 | /** |
||
354 | * @return float |
||
355 | */ |
||
356 | public function getTotOtherCB() |
||
360 | |||
361 | /** |
||
362 | * @param float $TotOtherCB |
||
363 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
364 | */ |
||
365 | public function setTotOtherCB($TotOtherCB) |
||
370 | |||
371 | /** |
||
372 | * @return float |
||
373 | */ |
||
374 | public function getTotReferal() |
||
378 | |||
379 | /** |
||
380 | * @param float $TotReferal |
||
381 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
382 | */ |
||
383 | public function setTotReferal($TotReferal) |
||
388 | |||
389 | /** |
||
390 | * @return float |
||
391 | */ |
||
392 | public function getTotCommCC() |
||
396 | |||
397 | /** |
||
398 | * @param float $TotCommCC |
||
399 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
400 | */ |
||
401 | public function setTotCommCC($TotCommCC) |
||
406 | |||
407 | /** |
||
408 | * @return float |
||
409 | */ |
||
410 | public function getDailyPct() |
||
414 | |||
415 | /** |
||
416 | * @param float $DailyPct |
||
417 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
418 | */ |
||
419 | public function setDailyPct($DailyPct) |
||
424 | |||
425 | /** |
||
426 | * @return int |
||
427 | */ |
||
428 | public function getBookingType() |
||
432 | |||
433 | /** |
||
434 | * @param int $BookingType |
||
435 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
436 | */ |
||
437 | public function setBookingType($BookingType) |
||
442 | |||
443 | /** |
||
444 | * @return int |
||
445 | */ |
||
446 | public function getSeqNo() |
||
450 | |||
451 | /** |
||
452 | * @param int $SeqNo |
||
453 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
454 | */ |
||
455 | public function setSeqNo($SeqNo) |
||
460 | |||
461 | /** |
||
462 | * @return \DateTime |
||
463 | */ |
||
464 | public function getArrivalDate() |
||
476 | |||
477 | /** |
||
478 | * @param \DateTime $ArrivalDate |
||
479 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
480 | */ |
||
481 | public function setArrivalDate(\DateTime $ArrivalDate) |
||
486 | |||
487 | /** |
||
488 | * @return \DateTime |
||
489 | */ |
||
490 | public function getDepartureDate() |
||
502 | |||
503 | /** |
||
504 | * @param \DateTime $DepartureDate |
||
505 | * @return \Gueststream\PMS\IQWare\API\DailyActivity |
||
506 | */ |
||
507 | public function setDepartureDate(\DateTime $DepartureDate) |
||
512 | } |
||
513 |
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.