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 OpeningHoursForDay 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 OpeningHoursForDay, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class OpeningHoursForDay implements ArrayAccess, Countable, IteratorAggregate |
||
16 | { |
||
17 | use DataTrait; |
||
18 | |||
19 | /** @var \Spatie\OpeningHours\TimeRange[] */ |
||
20 | protected $openingHours = []; |
||
21 | |||
22 | public static function fromStrings(array $strings) |
||
49 | |||
50 | public function isOpenAt(Time $time) |
||
60 | |||
61 | public function isOpenAtNight(Time $time) |
||
71 | |||
72 | /** |
||
73 | * @param callable[] $filters |
||
74 | * |
||
75 | * @return Time|bool |
||
76 | */ |
||
77 | public function openingHoursFilter(array $filters) |
||
91 | |||
92 | /** |
||
93 | * @param Time $time |
||
94 | * |
||
95 | * @return bool|Time |
||
96 | */ |
||
97 | public function nextOpen(Time $time) |
||
105 | |||
106 | /** |
||
107 | * @param Time $time |
||
108 | * |
||
109 | * @return bool|TimeRange |
||
110 | */ |
||
111 | public function nextOpenRange(Time $time) |
||
119 | |||
120 | /** |
||
121 | * @param Time $time |
||
122 | * |
||
123 | * @return bool|Time |
||
124 | */ |
||
125 | View Code Duplication | public function nextClose(Time $time) |
|
136 | |||
137 | /** |
||
138 | * @param Time $time |
||
139 | * |
||
140 | * @return bool|TimeRange |
||
141 | */ |
||
142 | View Code Duplication | public function nextCloseRange(Time $time) |
|
153 | |||
154 | protected function findNextOpenRangeInFreeTime(Time $time, TimeRange $timeRange) |
||
160 | |||
161 | protected function findNextOpenInFreeTime(Time $time, TimeRange $timeRange) |
||
169 | |||
170 | protected function findNextCloseInWorkingHours(Time $time, TimeRange $timeRange) |
||
176 | |||
177 | protected function findNextCloseRangeInWorkingHours(Time $time, TimeRange $timeRange) |
||
183 | |||
184 | protected function findNextCloseRangeInFreeTime(Time $time, TimeRange $timeRange) |
||
190 | |||
191 | protected function findNextCloseInFreeTime(Time $time, TimeRange $timeRange) |
||
199 | |||
200 | protected static function getHoursFromRange($range) |
||
207 | |||
208 | public function offsetExists($offset): bool |
||
212 | |||
213 | public function offsetGet($offset) |
||
217 | |||
218 | public function offsetSet($offset, $value) |
||
222 | |||
223 | public function offsetUnset($offset) |
||
227 | |||
228 | public function count(): int |
||
232 | |||
233 | public function getIterator() |
||
237 | |||
238 | /** |
||
239 | * @param Time $time |
||
240 | * |
||
241 | * @return TimeRange[] |
||
242 | */ |
||
243 | public function forTime(Time $time): Generator |
||
253 | |||
254 | /** |
||
255 | * @param Time $time |
||
256 | * |
||
257 | * @return TimeRange[] |
||
258 | */ |
||
259 | public function forNightTime(Time $time): Generator |
||
269 | |||
270 | public function isEmpty(): bool |
||
274 | |||
275 | public function map(callable $callback): array |
||
279 | |||
280 | protected function guardAgainstTimeRangeOverlaps(array $openingHours) |
||
288 | |||
289 | public function __toString() |
||
298 | } |
||
299 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.