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 |
||
21 | class BusinessHours implements BusinessHoursInterface |
||
22 | { |
||
23 | /** |
||
24 | * The days. |
||
25 | * |
||
26 | * @var DayInterface[] |
||
27 | */ |
||
28 | protected $days; |
||
29 | |||
30 | /** |
||
31 | * The time zone. |
||
32 | * |
||
33 | * @var \DateTimeZone |
||
34 | 27 | */ |
|
35 | protected $timezone; |
||
36 | 27 | ||
37 | 24 | /** |
|
38 | 24 | * @param DayInterface[] $days |
|
39 | * @param \DateTimeZone|null $timezone |
||
40 | */ |
||
41 | public function __construct(array $days, \DateTimeZone $timezone = null) |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function getTimezone(): \DateTimeZone |
||
54 | |||
55 | 9 | /** |
|
56 | * Get the days. |
||
57 | * |
||
58 | * @return DayInterface[] |
||
59 | */ |
||
60 | public function getDays(): array |
||
64 | 27 | ||
65 | /** |
||
66 | 27 | * Add a set of days. |
|
67 | 3 | * |
|
68 | * @param DayInterface[] $days The days. |
||
69 | * @throws \InvalidArgumentException If no days are passed. |
||
70 | 24 | */ |
|
71 | protected function setDays(array $days): void |
||
83 | 48 | ||
84 | /** |
||
85 | 48 | * {@inheritdoc} |
|
86 | 39 | */ |
|
87 | public function within(\DateTime $date): bool |
||
98 | 21 | ||
99 | 21 | /** |
|
100 | * {@inheritdoc} |
||
101 | 21 | */ |
|
102 | 9 | public function getNextChangeDateTime(\DateTime $date): \DateTime |
|
114 | 21 | ||
115 | 21 | /** |
|
116 | * {@inheritdoc} |
||
117 | 21 | */ |
|
118 | 9 | public function getPreviousChangeDateTime(\DateTime $date): \DateTime |
|
130 | 3 | ||
131 | 3 | /** |
|
132 | 3 | * {@inheritdoc} |
|
133 | */ |
||
134 | public function jsonSerialize() |
||
141 | 6 | ||
142 | /** |
||
143 | 6 | * Get the closest business hours date interval before the given date. |
|
144 | 6 | * |
|
145 | 6 | * @param \DateTime $date The given date. |
|
146 | * @return DateTimeInterval |
||
147 | 6 | */ |
|
148 | 3 | private function getClosestDateIntervalBefore(\DateTime $date): DateTimeInterval |
|
169 | 6 | ||
170 | /** |
||
171 | 6 | * Get the closest business hours date interval after the given date. |
|
172 | 6 | * |
|
173 | 6 | * @param \DateTime $date The given date. |
|
174 | * @return DateTimeInterval |
||
175 | 6 | */ |
|
176 | 3 | private function getClosestDateIntervalAfter(\DateTime $date): DateTimeInterval |
|
197 | |||
198 | 42 | /** |
|
199 | * Build a new date time interval for a date. |
||
200 | 42 | * |
|
201 | 42 | * @param \DateTime $date The date. |
|
202 | * @param TimeIntervalInterface $timeInterval |
||
203 | 42 | * @return DateTimeInterval |
|
204 | 42 | */ |
|
205 | 42 | private function buildDateTimeInterval(\DateTime $date, TimeIntervalInterface $timeInterval): DateTimeInterval |
|
223 | 6 | ||
224 | /** |
||
225 | 6 | * Get the business hours date before the given date. |
|
226 | 6 | * |
|
227 | * @param \DateTime $date |
||
228 | 6 | * @return \DateTime |
|
229 | 6 | */ |
|
230 | 6 | private function getDateBefore(\DateTime $date): \DateTime |
|
242 | 6 | ||
243 | /** |
||
244 | 6 | * Get the business hours date after the given date. |
|
245 | 6 | * |
|
246 | * @param \DateTime $date |
||
247 | 6 | * @return \DateTime |
|
248 | 6 | */ |
|
249 | private function getDateAfter(\DateTime $date): \DateTime |
||
263 | 21 | ||
264 | /** |
||
265 | 21 | * Get the closest interval endpoint after the given date. |
|
266 | 21 | * |
|
267 | 21 | * @param \DateTime $date |
|
268 | * @return DateTimeInterval |
||
269 | 21 | */ |
|
270 | 18 | private function getPreviousClosestInterval(\DateTime $date): DateTimeInterval |
|
284 | 21 | ||
285 | /** |
||
286 | 21 | * Get the closest interval endpoint after the given date. |
|
287 | 21 | * |
|
288 | 21 | * @param \DateTime $date |
|
289 | * @return DateTimeInterval |
||
290 | 21 | */ |
|
291 | 18 | private function getNextClosestInterval(\DateTime $date): DateTimeInterval |
|
305 | 12 | ||
306 | /** |
||
307 | 12 | * Get the closest business hours day before a given day number (including it). |
|
308 | 12 | * |
|
309 | * @param integer $dayNumber |
||
310 | * @return DayInterface|null |
||
311 | 3 | */ |
|
312 | private function getClosestDayBefore(int $dayNumber) |
||
320 | 6 | ||
321 | /** |
||
322 | 6 | * Get the closest business hours day after a given day number (including it). |
|
323 | 3 | * |
|
324 | * @param integer $dayNumber |
||
325 | * @return DayInterface|null |
||
326 | 3 | */ |
|
327 | private function getClosestDayAfter($dayNumber) |
||
335 | 3 | ||
336 | /** |
||
337 | 3 | * Get the business hours day before the day number. |
|
338 | * |
||
339 | 3 | * @param integer $dayNumber |
|
340 | 3 | * @return DayInterface|null |
|
341 | */ |
||
342 | 3 | private function getDayBefore(int $dayNumber) |
|
356 | 3 | ||
357 | /** |
||
358 | 3 | * Get the business hours day after the day number. |
|
359 | * |
||
360 | 3 | * @param integer $dayNumber |
|
361 | 3 | * @return DayInterface|null |
|
362 | */ |
||
363 | 3 | private function getDayAfter($dayNumber) |
|
377 | 48 | ||
378 | /** |
||
379 | 48 | * Get the day corresponding to the day of the week. |
|
380 | * |
||
381 | * @param integer $dayOfWeek The day of the week. |
||
382 | * @return DayInterface|null |
||
383 | */ |
||
384 | private function getDay($dayOfWeek): ?DayInterface |
||
388 | |||
389 | 24 | /** |
|
390 | 24 | * Add a day. |
|
391 | * |
||
392 | * @param DayInterface $day The day. |
||
393 | */ |
||
394 | private function addDay(DayInterface $day): void |
||
398 | 6 | ||
399 | 6 | public function __clone() |
|
408 | } |
||
409 |