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 |
||
| 25 | class WorkingTime |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var DateTime |
||
| 29 | */ |
||
| 30 | public $dateTime; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | public $workingDays; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var null|array |
||
| 39 | */ |
||
| 40 | public $weekends; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var null|array |
||
| 44 | */ |
||
| 45 | public $holidays; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * WorkingTime constructor. |
||
| 49 | * |
||
| 50 | * @param array $workTimeConfig |
||
| 51 | * @param string $dateTime |
||
| 52 | * @throws \Exception |
||
| 53 | */ |
||
| 54 | 16 | public function __construct(array $workTimeConfig, string $dateTime = 'now') |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Фурмирует строку дня. |
||
| 64 | * |
||
| 65 | * @param null|string $date |
||
| 66 | * @param string $format |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | 12 | private function buildDataPartString(?string $date, string $format): string |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Формирует дату из строки. |
||
| 76 | * |
||
| 77 | * @param string $date |
||
| 78 | * @return DateTime |
||
| 79 | * @throws \Exception |
||
| 80 | */ |
||
| 81 | 7 | private function buildDate(string $date = null): DateTime |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Проверяет является ли дата праздничным днём. |
||
| 92 | * |
||
| 93 | * @param string $date |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | 11 | public function isHoliday(string $date = null): bool |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Проверяет является ли дата выходным днём. |
||
| 108 | * |
||
| 109 | * @param string $date |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | 11 | public function isWeekend(string $date = null): bool |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Проверяет евляется ли дата рабочим днём. |
||
| 124 | * |
||
| 125 | * Формат даты - "Y-m-d" |
||
| 126 | * @param string $date |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | 10 | public function isWorkingDate(string $date = null): bool |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Проверяет евляется ли время рабочим. |
||
| 136 | * |
||
| 137 | * @param string|null $time Формат времени - "H:i" или полный "Y-m-d H:i" |
||
| 138 | * @return bool |
||
| 139 | * @throws \InvalidArgumentException |
||
| 140 | * @throws \Exception |
||
| 141 | */ |
||
| 142 | 2 | public function isWorkingTime(string $time = null): bool |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Возвращает следующий рабочий день. |
||
| 164 | * |
||
| 165 | * @param string|null $date Формат даты - "Y-m-d" |
||
| 166 | * @return string |
||
| 167 | * @throws \Exception |
||
| 168 | */ |
||
| 169 | 6 | public function nextWorkingDay(string $date = null): string |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Возвращает ближайшее рабочее время. Либо null если текущее время уже рабочее. |
||
| 185 | * |
||
| 186 | * @param string|null $date |
||
| 187 | * @return null|string |
||
| 188 | * @throws \Exception |
||
| 189 | */ |
||
| 190 | 7 | public function nextWorkingTime(string $date = null): ?string |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Возвращает дату время начала следующего дня. |
||
| 229 | * |
||
| 230 | * @param string|null $date |
||
| 231 | * @return string |
||
| 232 | * @throws \Exception |
||
| 233 | */ |
||
| 234 | 2 | public function nextWorkingDayStart(string $date = null): string |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Возвращает дату время начала следующего дня. |
||
| 245 | * |
||
| 246 | * @param string|null $date |
||
| 247 | * @return string |
||
| 248 | * @throws \Exception |
||
| 249 | */ |
||
| 250 | 1 | private function nextWorkingDayEnd(string $date = null): string |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Возвращает длинну рабочего дня в минутах. |
||
| 261 | * |
||
| 262 | * @param string|null $date Формат даты - "Y-m-d" |
||
| 263 | * @return int |
||
| 264 | * @throws \Exception |
||
| 265 | */ |
||
| 266 | 6 | public function getJobMinutesInDay(string $date = null): int |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Прибавляет заданное количество минут к дате с учетом рабочего времени. |
||
| 286 | * |
||
| 287 | * @param int $minutes |
||
| 288 | * @param string $date |
||
| 289 | * @return DateTime |
||
| 290 | * @throws \Exception |
||
| 291 | */ |
||
| 292 | 3 | private function modifyDate(int $minutes, string $date): DateTime |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Прибавляет заданное количество минут к дате с учетом рабочего времени. |
||
| 319 | * |
||
| 320 | * @param int $minutes |
||
| 321 | * @param string $date |
||
| 322 | * @return string |
||
| 323 | * @throws \Exception |
||
| 324 | */ |
||
| 325 | 3 | public function modify(int $minutes, string $date = null): string |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Возвращает рабочее время в минутах в заданном временном интервале. |
||
| 344 | * |
||
| 345 | * @param string $startDate |
||
| 346 | * @param string $endDate |
||
| 347 | * @return int |
||
| 348 | * @throws \InvalidArgumentException |
||
| 349 | * @throws \Exception |
||
| 350 | */ |
||
| 351 | 2 | public function calculatingWorkingTime(string $startDate, string $endDate): int |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Проверяет является ли строка корректной датой. |
||
| 403 | * |
||
| 404 | * @param $date |
||
| 405 | * @param string $format |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | 5 | public static function validateDate(string $date, string $format = 'Y-m-d H:i:s'): bool |
|
| 413 | } |
||
| 414 |
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.