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 |
||
11 | class Calculator |
||
12 | { |
||
13 | const MONDAY = 1; |
||
14 | const TUESDAY = 2; |
||
15 | const WEDNESDAY = 3; |
||
16 | const THURSDAY = 4; |
||
17 | const FRIDAY = 5; |
||
18 | const SATURDAY = 6; |
||
19 | const SUNDAY = 7; |
||
20 | |||
21 | const WEEK_DAY_FORMAT = 'N'; |
||
22 | const HOLIDAY_FORMAT = 'm-d'; |
||
23 | const FREE_DAY_FORMAT = 'Y-m-d'; |
||
24 | |||
25 | /** @var \DateTime */ |
||
26 | private $date; |
||
27 | |||
28 | /** @var \DateTime[] */ |
||
29 | private $holidays = array(); |
||
30 | |||
31 | /** @var \DateTime[] */ |
||
32 | private $freeDays = array(); |
||
33 | |||
34 | /** @var int[] */ |
||
35 | private $freeWeekDays = array(); |
||
36 | |||
37 | /** |
||
38 | * @param \DateTime $startDate Date to start calculations from |
||
39 | * |
||
40 | * @return $this |
||
41 | */ |
||
42 | 16 | public function setStartDate(\DateTime $startDate) |
|
51 | |||
52 | /** |
||
53 | * @param \DateTime[] $holidays Array of holidays that repeats each year. (Only month and date is used to match). |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | 15 | public function setHolidays(array $holidays) |
|
63 | |||
64 | /** |
||
65 | * @return \DateTime[] |
||
66 | */ |
||
67 | 16 | private function getHolidays() |
|
71 | |||
72 | /** |
||
73 | * @param \DateTime[] $freeDays Array of free days that dose not repeat. |
||
74 | * |
||
75 | * @return $this |
||
76 | */ |
||
77 | 15 | public function setFreeDays(array $freeDays) |
|
83 | |||
84 | /** |
||
85 | * @return \DateTime[] |
||
86 | */ |
||
87 | 16 | private function getFreeDays() |
|
91 | |||
92 | /** |
||
93 | * @param int[] $freeWeekDays Array of days of the week which are not business days. |
||
94 | * |
||
95 | * @return $this |
||
96 | */ |
||
97 | 16 | public function setFreeWeekDays(array $freeWeekDays) |
|
103 | |||
104 | /** |
||
105 | * @return int[] |
||
106 | */ |
||
107 | 17 | private function getFreeWeekDays() |
|
115 | |||
116 | /** |
||
117 | * @param int $howManyDays |
||
118 | * |
||
119 | * @return $this |
||
120 | */ |
||
121 | 17 | View Code Duplication | public function addBusinessDays($howManyDays) |
137 | |||
138 | /** |
||
139 | * @param int $howManyDays |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | View Code Duplication | public function subBusinessDays($howManyDays) |
|
160 | |||
161 | /** |
||
162 | * @return \DateTime |
||
163 | */ |
||
164 | 17 | public function getDate() |
|
172 | |||
173 | /** |
||
174 | * @param \DateTime $date |
||
175 | * |
||
176 | * @return bool |
||
177 | */ |
||
178 | 17 | public function isBusinessDay(\DateTime $date) |
|
194 | |||
195 | /** |
||
196 | * @param \DateTime $date |
||
197 | * |
||
198 | * @return bool |
||
199 | */ |
||
200 | 17 | public function isFreeWeekDayDay(\DateTime $date) |
|
210 | |||
211 | /** |
||
212 | * @param \DateTime $date |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | 16 | public function isHoliday(\DateTime $date) |
|
227 | |||
228 | /** |
||
229 | * @param \DateTime $date |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | 16 | public function isFreeDay(\DateTime $date) |
|
244 | } |
||
245 |
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.