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 BusinessHours implements BusinessHoursInterface |
||
12 | { |
||
13 | /** |
||
14 | * The days. |
||
15 | * |
||
16 | * @var DayInterface[] |
||
17 | */ |
||
18 | protected $days; |
||
19 | |||
20 | /** |
||
21 | * The time zone. |
||
22 | * |
||
23 | * @var \DateTimeZone |
||
24 | */ |
||
25 | protected $timezone; |
||
26 | |||
27 | /** |
||
28 | * Constructor. |
||
29 | * |
||
30 | * @param DayInterface[] $days |
||
31 | * @param \DateTimeZone|null $timezone |
||
32 | */ |
||
33 | public function __construct(array $days, \DateTimeZone $timezone = null) |
||
38 | |||
39 | /** |
||
40 | * Get the days. |
||
41 | * |
||
42 | * @return DayInterface[] |
||
43 | */ |
||
44 | public function getDays() |
||
48 | |||
49 | /** |
||
50 | * Add a set of days. |
||
51 | * |
||
52 | * @param DayInterface[] $days The days. |
||
53 | * @throws \InvalidArgumentException If no days are passed. |
||
54 | */ |
||
55 | public function setDays(array $days) |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function within(\DateTime $date) |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function getNextChangeDateTime(\DateTime $date = null) |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | public function closestDateInterval(\DateTime $date) |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public function jsonSerialize() |
||
122 | |||
123 | /** |
||
124 | * Get the closest business hours date interval after the given date. |
||
125 | * |
||
126 | * @param \DateTime $date |
||
127 | * @return DateTimeInterval |
||
128 | */ |
||
129 | private function getClosestDateIntervalAfter(\DateTime $date) |
||
178 | |||
179 | /** |
||
180 | * Get the business hours date after the given date (excluding holidays). |
||
181 | * |
||
182 | * @param \DateTime $date |
||
183 | * @return \DateTime |
||
184 | */ |
||
185 | private function getDateAfter(\DateTime $date) |
||
199 | |||
200 | /** |
||
201 | * Get the closest interval endpoint after the given date. |
||
202 | * |
||
203 | * @param \DateTime $date |
||
204 | * @return DateTimeInterval |
||
205 | */ |
||
206 | private function getClosestInterval(\DateTime $date) |
||
234 | |||
235 | /** |
||
236 | * Get the closest business hours day before a given day number (including it). |
||
237 | * |
||
238 | * @param integer $dayNumber |
||
239 | * @return DayInterface|null |
||
240 | */ |
||
241 | private function getClosestDayBefore($dayNumber) |
||
249 | |||
250 | /** |
||
251 | * Get the closest business hours day after a given day number (including it). |
||
252 | * |
||
253 | * @param integer $dayNumber |
||
254 | * @return DayInterface|null |
||
255 | */ |
||
256 | private function getClosestDayAfter($dayNumber) |
||
264 | |||
265 | /** |
||
266 | * Get the business hours day before the day number. |
||
267 | * |
||
268 | * @param integer $dayNumber |
||
269 | * @return DayInterface|null |
||
270 | */ |
||
271 | View Code Duplication | private function getDayBefore($dayNumber) |
|
285 | |||
286 | /** |
||
287 | * Get the business hours day after the day number. |
||
288 | * |
||
289 | * @param integer $dayNumber |
||
290 | * @return DayInterface|null |
||
291 | */ |
||
292 | View Code Duplication | private function getDayAfter($dayNumber) |
|
306 | |||
307 | /** |
||
308 | * Get the day corresponding to the day number. |
||
309 | * |
||
310 | * @param integer $dayNumber |
||
311 | * @return DayInterface|null |
||
312 | */ |
||
313 | private function getDay($dayNumber) |
||
317 | |||
318 | /** |
||
319 | * Add a day. |
||
320 | * |
||
321 | * @param DayInterface $day The day. |
||
322 | */ |
||
323 | private function addDay(DayInterface $day) |
||
327 | } |
||
328 |
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.