Complex classes like WorkingTime 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 WorkingTime, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class WorkingTime |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var \DateTime |
||
| 27 | */ |
||
| 28 | public $dateTime; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | public $workingDays; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | public $weekends; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | public $holidays; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * WorkingTime constructor. |
||
| 47 | * |
||
| 48 | * @param array $workTimeConfig |
||
| 49 | * @param string $dateTime |
||
| 50 | */ |
||
| 51 | 14 | public function __construct(array $workTimeConfig, string $dateTime = 'now') |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Фурмирует строку дня. |
||
| 61 | * @param null|string $date |
||
| 62 | * @param string $format |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | 11 | private function buildDataPartString(?string $date, string $format): string |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Формирует дату из строки. |
||
| 78 | * |
||
| 79 | * @param string $date |
||
| 80 | * @return \DateTime |
||
| 81 | */ |
||
| 82 | 7 | private function buildDate(string $date = null): \DateTime |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Проверяет является ли дата праздничным днём. |
||
| 93 | * |
||
| 94 | * @param string $date |
||
| 95 | * @return bool |
||
| 96 | */ |
||
| 97 | 11 | public function isHoliday(string $date = null): bool |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Проверяет является ли дата выходным днём. |
||
| 110 | * |
||
| 111 | * @param string $date |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | 11 | public function isWeekend(string $date = null): bool |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Проверяет евляется ли дата рабочим днём. |
||
| 127 | * |
||
| 128 | * Формат даты - "Y-m-d" |
||
| 129 | * @param string $date |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | 10 | public function isWorkingDate(string $date = null): bool |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Проверяет евляется ли время рабочим. |
||
| 145 | * |
||
| 146 | * @param string|null $time Формат времени - "H:i" или полный "Y-m-d H:i" |
||
| 147 | * @return bool |
||
| 148 | * @throws \InvalidArgumentException |
||
| 149 | */ |
||
| 150 | 2 | public function isWorkingTime(string $time = null): bool |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Возвращает следующий рабочий день. |
||
| 174 | * |
||
| 175 | * @param string|null $date Формат даты - "Y-m-d" |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | 6 | public function nextWorkingDay(string $date = null): string |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Возвращает ближайшее рабочее время. Либо null если текущее время уже рабочее. |
||
| 195 | * |
||
| 196 | * @param string|null $date |
||
| 197 | * @return null|string |
||
| 198 | */ |
||
| 199 | 7 | public function nextWorkingTime(string $date = null): ?string |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Возвращает дату время начала следующего дня. |
||
| 237 | * |
||
| 238 | * @param string|null $date |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | 2 | public function nextWorkingDayStart(string $date = null): string |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Возвращает дату время начала следующего дня. |
||
| 252 | * |
||
| 253 | * @param string|null $date |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | 1 | private function nextWorkingDayEnd(string $date = null): string |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Возвращает длинну рабочего дня в минутах. |
||
| 267 | * |
||
| 268 | * @param string|null $date Формат даты - "Y-m-d" |
||
| 269 | * @return int |
||
| 270 | */ |
||
| 271 | 6 | public function getJobMinutesInDay(string $date = null): int |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Прибавляет заданное количество минут к дате с учетом рабочего времени. |
||
| 291 | * |
||
| 292 | * @param int $minutes |
||
| 293 | * @param string $date |
||
| 294 | * @return \DateTime |
||
| 295 | */ |
||
| 296 | 3 | private function modifyDate(int $minutes, string $date): \DateTime |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Прибавляет заданное количество минут к дате с учетом рабочего времени. |
||
| 323 | * |
||
| 324 | * @param int $minutes |
||
| 325 | * @param string $date |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | 3 | public function modify(int $minutes, string $date = null): string |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Возвращает рабочее время в минутах в заданном временном интервале. |
||
| 345 | * |
||
| 346 | * @param string $startDate |
||
| 347 | * @param string $endDate |
||
| 348 | * @return int |
||
| 349 | * @throws \InvalidArgumentException |
||
| 350 | */ |
||
| 351 | 1 | public function calculatingWorkingTime(string $startDate, string $endDate): int |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Проверяет является ли строка корректной датой. |
||
| 388 | * |
||
| 389 | * @param $date |
||
| 390 | * @param string $format |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | 4 | public static function validateDate(string $date, string $format = 'Y-m-d H:i:s'): bool |
|
| 398 | } |
||
| 399 |