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 BusinessHours 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 BusinessHours, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class BusinessHours implements BusinessHoursInterface |
||
13 | { |
||
14 | /** |
||
15 | * The days. |
||
16 | * |
||
17 | * @var DayInterface[] |
||
18 | */ |
||
19 | protected $days; |
||
20 | |||
21 | /** |
||
22 | * The time zone. |
||
23 | * |
||
24 | * @var \DateTimeZone |
||
25 | */ |
||
26 | protected $timezone; |
||
27 | |||
28 | /** |
||
29 | * Constructor. |
||
30 | * |
||
31 | * @param DayInterface[] $days |
||
32 | * @param \DateTimeZone|null $timezone |
||
33 | */ |
||
34 | public function __construct(array $days, \DateTimeZone $timezone = null) |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | public function getTimezone() |
||
47 | |||
48 | /** |
||
49 | * Get the days. |
||
50 | * |
||
51 | * @return DayInterface[] |
||
52 | */ |
||
53 | public function getDays() |
||
57 | |||
58 | /** |
||
59 | * Add a set of days. |
||
60 | * |
||
61 | * @param DayInterface[] $days The days. |
||
62 | * @throws \InvalidArgumentException If no days are passed. |
||
63 | */ |
||
64 | protected function setDays(array $days) |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function within(\DateTime $date) |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | View Code Duplication | public function getNextChangeDateTime(\DateTime $date) |
|
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | View Code Duplication | public function getPreviousChangeDateTime(\DateTime $date) |
|
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | public function jsonSerialize() |
||
134 | |||
135 | /** |
||
136 | * Get the closest business hours date interval before the given date. |
||
137 | * |
||
138 | * @param \DateTime $date The given date. |
||
139 | * @return DateTimeInterval |
||
140 | */ |
||
141 | View Code Duplication | private function getClosestDateIntervalBefore(\DateTime $date) |
|
162 | |||
163 | /** |
||
164 | * Get the closest business hours date interval after the given date. |
||
165 | * |
||
166 | * @param \DateTime $date The given date. |
||
167 | * @return DateTimeInterval |
||
168 | */ |
||
169 | View Code Duplication | private function getClosestDateIntervalAfter(\DateTime $date) |
|
190 | |||
191 | /** |
||
192 | * Build a new date time interval for a date. |
||
193 | * |
||
194 | * @param \DateTime $date The date. |
||
195 | * @param TimeIntervalInterface $timeInterval |
||
196 | * @return DateTimeInterval |
||
197 | */ |
||
198 | private function buildDateTimeInterval(\DateTime $date, TimeIntervalInterface $timeInterval) |
||
216 | |||
217 | /** |
||
218 | * Get the business hours date before the given date. |
||
219 | * |
||
220 | * @param \DateTime $date |
||
221 | * @return \DateTime |
||
222 | */ |
||
223 | private function getDateBefore(\DateTime $date) |
||
235 | |||
236 | /** |
||
237 | * Get the business hours date after the given date. |
||
238 | * |
||
239 | * @param \DateTime $date |
||
240 | * @return \DateTime |
||
241 | */ |
||
242 | private function getDateAfter(\DateTime $date) |
||
256 | |||
257 | /** |
||
258 | * Get the closest interval endpoint after the given date. |
||
259 | * |
||
260 | * @param \DateTime $date |
||
261 | * @return DateTimeInterval |
||
262 | */ |
||
263 | View Code Duplication | private function getPreviousClosestInterval(\DateTime $date) |
|
277 | |||
278 | /** |
||
279 | * Get the closest interval endpoint after the given date. |
||
280 | * |
||
281 | * @param \DateTime $date |
||
282 | * @return DateTimeInterval |
||
283 | */ |
||
284 | View Code Duplication | private function getNextClosestInterval(\DateTime $date) |
|
298 | |||
299 | /** |
||
300 | * Get the closest business hours day before a given day number (including it). |
||
301 | * |
||
302 | * @param integer $dayNumber |
||
303 | * @return DayInterface|null |
||
304 | */ |
||
305 | private function getClosestDayBefore($dayNumber) |
||
313 | |||
314 | /** |
||
315 | * Get the closest business hours day after a given day number (including it). |
||
316 | * |
||
317 | * @param integer $dayNumber |
||
318 | * @return DayInterface|null |
||
319 | */ |
||
320 | private function getClosestDayAfter($dayNumber) |
||
328 | |||
329 | /** |
||
330 | * Get the business hours day before the day number. |
||
331 | * |
||
332 | * @param integer $dayNumber |
||
333 | * @return DayInterface|null |
||
334 | */ |
||
335 | View Code Duplication | private function getDayBefore($dayNumber) |
|
349 | |||
350 | /** |
||
351 | * Get the business hours day after the day number. |
||
352 | * |
||
353 | * @param integer $dayNumber |
||
354 | * @return DayInterface|null |
||
355 | */ |
||
356 | View Code Duplication | private function getDayAfter($dayNumber) |
|
370 | |||
371 | /** |
||
372 | * Get the day corresponding to the day of the week. |
||
373 | * |
||
374 | * @param integer $dayOfWeek The day of the week. |
||
375 | * @return DayInterface|null |
||
376 | */ |
||
377 | private function getDay($dayOfWeek) |
||
381 | |||
382 | /** |
||
383 | * Add a day. |
||
384 | * |
||
385 | * @param DayInterface $day The day. |
||
386 | */ |
||
387 | private function addDay(DayInterface $day) |
||
391 | |||
392 | /** |
||
393 | * Clone. |
||
394 | */ |
||
395 | public function __clone() |
||
404 | } |
||
405 |
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.