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 | 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 | private function buildDataPartString(?string $date, string $format): string |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Формирует дату из строки. |
||
| 78 | * |
||
| 79 | * @param string $date |
||
| 80 | * @return \DateTime |
||
| 81 | */ |
||
| 82 | private function buildDate(string $date = null): \DateTime |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Проверяет является ли дата праздничным днём. |
||
| 93 | * |
||
| 94 | * @param string $date |
||
| 95 | * @return bool |
||
| 96 | */ |
||
| 97 | public function isHoliday(string $date = null): bool |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Проверяет является ли дата выходным днём. |
||
| 109 | * |
||
| 110 | * @param string $date |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | public function isWeekend(string $date = null): bool |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Проверяет евляется ли дата рабочим днём. |
||
| 125 | * |
||
| 126 | * Формат даты - "Y-m-d" |
||
| 127 | * @param string $date |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | public function isWorkingDate(string $date = null): bool |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Проверяет евляется ли время рабочим. |
||
| 137 | * |
||
| 138 | * @param string|null $time Формат времени - "H:i" или полный "Y-m-d H:i" |
||
| 139 | * @return bool |
||
| 140 | * @throws \InvalidArgumentException |
||
| 141 | */ |
||
| 142 | public function isWorkingTime(string $time = null): bool |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Возвращает следующий рабочий день. |
||
| 166 | * |
||
| 167 | * @param string|null $date Формат даты - "Y-m-d" |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public function nextWorkingDay(string $date = null): string |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Возвращает ближайшее рабочее время. Либо null если текущее время уже рабочее. |
||
| 187 | * |
||
| 188 | * @param string|null $date |
||
| 189 | * @return null|string |
||
| 190 | */ |
||
| 191 | public function nextWorkingTime(string $date = null): ?string |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Возвращает дату время начала следующего дня. |
||
| 229 | * |
||
| 230 | * @param string|null $date |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function nextWorkingDayStart(string $date = null): string |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Возвращает дату время начала следующего дня. |
||
| 244 | * |
||
| 245 | * @param string|null $date |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | private function nextWorkingDayEnd(string $date = null): string |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Возвращает длинну рабочего дня в минутах. |
||
| 259 | * |
||
| 260 | * @param string|null $date Формат даты - "Y-m-d" |
||
| 261 | * @return int |
||
| 262 | */ |
||
| 263 | public function getJobMinutesInDay(string $date = null): int |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Прибавляет заданное количество минут к дате с учетом рабочего времени. |
||
| 283 | * |
||
| 284 | * @param int $minutes |
||
| 285 | * @param string $date |
||
| 286 | * @return \DateTime |
||
| 287 | */ |
||
| 288 | private function modifyDate(int $minutes, string $date): \DateTime |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Прибавляет заданное количество минут к дате с учетом рабочего времени. |
||
| 315 | * |
||
| 316 | * @param int $minutes |
||
| 317 | * @param string $date |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | public function modify(int $minutes, string $date = null): string |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Возвращает рабочее время в минутах в заданном временном интервале. |
||
| 337 | * |
||
| 338 | * @param string $startDate |
||
| 339 | * @param string $endDate |
||
| 340 | * @return int |
||
| 341 | * @throws \InvalidArgumentException |
||
| 342 | */ |
||
| 343 | public function calculatingWorkingTime(string $startDate, string $endDate): int |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Проверяет является ли строка корректной датой. |
||
| 383 | * |
||
| 384 | * @param $date |
||
| 385 | * @param string $format |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | public static function validateDate(string $date, string $format = 'Y-m-d H:i:s'): bool |
||
| 393 | } |
||
| 394 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.