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 |
||
16 | class OpeningHoursForDay implements ArrayAccess, Countable, IteratorAggregate |
||
17 | { |
||
18 | use DataTrait, RangeFinder; |
||
19 | |||
20 | /** @var \Spatie\OpeningHours\TimeRange[] */ |
||
21 | protected $openingHours = []; |
||
22 | |||
23 | public static function fromStrings(array $strings) |
||
50 | |||
51 | public function isOpenAt(Time $time) |
||
61 | |||
62 | public function isOpenAtNight(Time $time) |
||
72 | |||
73 | /** |
||
74 | * @param callable[] $filters |
||
75 | * @param bool $reverse |
||
76 | * |
||
77 | * @return Time|bool |
||
78 | */ |
||
79 | public function openingHoursFilter(array $filters, bool $reverse = false) |
||
93 | |||
94 | /** |
||
95 | * @param Time $time |
||
96 | * |
||
97 | * @return bool|Time |
||
98 | */ |
||
99 | public function nextOpen(Time $time) |
||
107 | |||
108 | /** |
||
109 | * @param Time $time |
||
110 | * |
||
111 | * @return bool|TimeRange |
||
112 | */ |
||
113 | public function nextOpenRange(Time $time) |
||
121 | |||
122 | /** |
||
123 | * @param Time $time |
||
124 | * |
||
125 | * @return bool|Time |
||
126 | */ |
||
127 | View Code Duplication | public function nextClose(Time $time) |
|
138 | |||
139 | /** |
||
140 | * @param Time $time |
||
141 | * |
||
142 | * @return bool|TimeRange |
||
143 | */ |
||
144 | View Code Duplication | public function nextCloseRange(Time $time) |
|
155 | |||
156 | /** |
||
157 | * @param Time $time |
||
158 | * |
||
159 | * @return bool|Time |
||
160 | */ |
||
161 | View Code Duplication | public function previousOpen(Time $time) |
|
172 | |||
173 | /** |
||
174 | * @param Time $time |
||
175 | * |
||
176 | * @return bool|TimeRange |
||
177 | */ |
||
178 | public function previousOpenRange(Time $time) |
||
186 | |||
187 | /** |
||
188 | * @param Time $time |
||
189 | * |
||
190 | * @return bool|Time |
||
191 | */ |
||
192 | public function previousClose(Time $time) |
||
200 | |||
201 | /** |
||
202 | * @param Time $time |
||
203 | * |
||
204 | * @return bool|TimeRange |
||
205 | */ |
||
206 | public function previousCloseRange(Time $time) |
||
214 | |||
215 | protected static function getHoursFromRange($range) |
||
222 | |||
223 | public function offsetExists($offset): bool |
||
227 | |||
228 | public function offsetGet($offset) |
||
232 | |||
233 | public function offsetSet($offset, $value) |
||
237 | |||
238 | public function offsetUnset($offset) |
||
242 | |||
243 | public function count(): int |
||
247 | |||
248 | public function getIterator() |
||
252 | |||
253 | /** |
||
254 | * @param Time $time |
||
255 | * |
||
256 | * @return TimeRange[] |
||
257 | */ |
||
258 | public function forTime(Time $time): Generator |
||
268 | |||
269 | /** |
||
270 | * @param Time $time |
||
271 | * |
||
272 | * @return TimeRange[] |
||
273 | */ |
||
274 | public function forNightTime(Time $time): Generator |
||
284 | |||
285 | public function isEmpty(): bool |
||
289 | |||
290 | public function map(callable $callback): array |
||
294 | |||
295 | protected function guardAgainstTimeRangeOverlaps(array $openingHours) |
||
303 | |||
304 | public function __toString() |
||
313 | } |
||
314 |
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.