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 OpeningHours 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 OpeningHours, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class OpeningHours |
||
| 14 | { |
||
| 15 | /** @var \Spatie\OpeningHours\Day[] */ |
||
| 16 | protected $openingHours = []; |
||
| 17 | |||
| 18 | /** @var array */ |
||
| 19 | protected $exceptions = []; |
||
| 20 | |||
| 21 | /** @var DateTimeZone|null */ |
||
| 22 | protected $timezone = null; |
||
| 23 | |||
| 24 | public function __construct($timezone = null) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param array $data |
||
| 35 | * |
||
| 36 | * @return static |
||
| 37 | */ |
||
| 38 | public static function create(array $data) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param array $data |
||
| 45 | * |
||
| 46 | * @return bool |
||
| 47 | */ |
||
| 48 | public static function isValid(array $data): bool |
||
| 58 | |||
| 59 | public function fill(array $data) |
||
| 71 | |||
| 72 | public function forWeek(): array |
||
| 76 | |||
| 77 | public function forDay(string $day): OpeningHoursForDay |
||
| 83 | |||
| 84 | public function forDate(DateTimeInterface $date): OpeningHoursForDay |
||
| 90 | |||
| 91 | public function exceptions(): array |
||
| 95 | |||
| 96 | public function isOpenOn(string $day): bool |
||
| 100 | |||
| 101 | public function isClosedOn(string $day): bool |
||
| 105 | |||
| 106 | public function isOpenAt(DateTimeInterface $dateTime): bool |
||
| 114 | |||
| 115 | public function isClosedAt(DateTimeInterface $dateTime): bool |
||
| 119 | |||
| 120 | public function isOpen(): bool |
||
| 124 | |||
| 125 | public function isClosed(): bool |
||
| 129 | |||
| 130 | public function nextOpen(DateTimeInterface $dateTime): DateTime |
||
| 150 | |||
| 151 | public function regularClosingDays(): array |
||
| 157 | |||
| 158 | public function regularClosingDaysISO(): array |
||
| 162 | |||
| 163 | public function exceptionalClosingDates(): array |
||
| 173 | |||
| 174 | public function setTimezone($timezone) |
||
| 178 | |||
| 179 | protected function parseOpeningHoursAndExceptions(array $data): array |
||
| 190 | |||
| 191 | protected function setOpeningHoursFromStrings(string $day, array $openingHours) |
||
| 197 | |||
| 198 | protected function setExceptionsFromStrings(array $exceptions) |
||
| 210 | |||
| 211 | protected function normalizeDayName(string $day) |
||
| 221 | |||
| 222 | protected function applyTimezone(DateTimeInterface $date) |
||
| 230 | |||
| 231 | public function filter(callable $callback): array |
||
| 235 | |||
| 236 | public function map(callable $callback): array |
||
| 240 | |||
| 241 | public function flatMap(callable $callback): array |
||
| 245 | |||
| 246 | public function filterExceptions(callable $callback): array |
||
| 250 | |||
| 251 | public function mapExceptions(callable $callback): array |
||
| 255 | |||
| 256 | public function flatMapExceptions(callable $callback): array |
||
| 260 | |||
| 261 | public function asStructuredData(): array |
||
| 298 | } |
||
| 299 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.