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 LocalDate 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 LocalDate, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class LocalDate |
||
15 | { |
||
16 | /** @var \DateTime */ |
||
17 | private $date; |
||
18 | /** @var \DateTimeZone */ |
||
19 | private $timezone; |
||
20 | |||
21 | /** |
||
22 | * @param int $timestamp The unix timestamp |
||
23 | * @param \DateTimeZone|string $timezone The timezone or a string the DateTimeZone c'tor can understand |
||
24 | * |
||
25 | * @return LocalDate |
||
26 | */ |
||
27 | 92 | public static function fromTimestamp($timestamp, $timezone) |
|
31 | |||
32 | /** |
||
33 | * @return LocalDate |
||
34 | */ |
||
35 | 1 | public static function now() |
|
39 | |||
40 | /** |
||
41 | * @param \DateTime $dateTime |
||
42 | * @return LocalDate |
||
43 | */ |
||
44 | 11 | public static function raw(\DateTime $dateTime) |
|
55 | |||
56 | /** |
||
57 | * @param \DateTime|string|float $input The date or a string the DateTime c'tor can understand or a timestamp |
||
58 | * @param \DateTimeZone|string $timezone The timezone or a string the DateTimeZone c'tor can understand |
||
59 | */ |
||
60 | 190 | public function __construct($input, $timezone) |
|
61 | { |
||
62 | 190 | if (! $timezone instanceof \DateTimeZone) { |
|
63 | 179 | $timezone = new \DateTimeZone((string) $timezone); |
|
64 | } |
||
65 | |||
66 | ///// |
||
67 | // We have to trick PHP a bit here, but why? |
||
68 | // |
||
69 | // Apparently things behave different, when setting a timezone like |
||
70 | // a) 'Europe/Berlin' |
||
71 | // b) '+02:00' |
||
72 | // |
||
73 | // When the date already has a timezone set then |
||
74 | // a) will only set the timezone |
||
75 | // b) will set the timezone and will also change the timestamp by 2 hours |
||
76 | // |
||
77 | // Therefore we create a fresh date, that does not have a timezone yet, set the timestamp, and then apply the |
||
78 | // timezone |
||
79 | // |
||
80 | // See the unit tests for more as well |
||
81 | //// |
||
82 | 190 | if ($input instanceof \DateTime) { |
|
83 | 60 | $date = (new \DateTime()) |
|
84 | 60 | ->setTimestamp($input->getTimestamp()) |
|
85 | 60 | ->setTimezone($timezone); |
|
86 | 161 | } elseif (is_numeric($input)) { |
|
87 | 101 | $date = (new \DateTime()) |
|
88 | 101 | ->setTimestamp($input) |
|
89 | 101 | ->setTimezone($timezone); |
|
90 | } else { |
||
91 | // when we have string input, we immediately use the timezone |
||
92 | 145 | $tmp = new \DateTime($input, $timezone); |
|
93 | // we reconstruct the date time again in order to set the timezone on the inner one |
||
94 | 145 | $date = (new \DateTime()) |
|
95 | 145 | ->setTimestamp($tmp->getTimestamp()) |
|
96 | 145 | ->setTimezone($timezone); |
|
97 | } |
||
98 | |||
99 | 190 | $this->date = $date; |
|
100 | 190 | $this->timezone = $timezone; |
|
101 | 190 | } |
|
102 | |||
103 | /** |
||
104 | * @return string |
||
105 | */ |
||
106 | 1 | public function __toString() |
|
110 | |||
111 | /** |
||
112 | * @return \DateTime |
||
113 | */ |
||
114 | 57 | public function getDate() |
|
118 | |||
119 | /** |
||
120 | * @return int |
||
121 | */ |
||
122 | 103 | public function getTimestamp() |
|
126 | |||
127 | /** |
||
128 | * @return \DateTimeZone |
||
129 | */ |
||
130 | 138 | public function getTimezone() |
|
134 | |||
135 | /** |
||
136 | * @return int |
||
137 | */ |
||
138 | 2 | public function getOffset() |
|
142 | |||
143 | /** |
||
144 | * @return float |
||
145 | */ |
||
146 | 1 | public function getOffsetInMinutes() |
|
152 | |||
153 | /** |
||
154 | * @return float |
||
155 | */ |
||
156 | 1 | public function getOffsetInHours() |
|
162 | |||
163 | /** |
||
164 | * @param string $format |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | 160 | public function format($format = 'c') |
|
172 | |||
173 | /** |
||
174 | * @return LocalDate |
||
175 | */ |
||
176 | 1 | public function getClone() |
|
180 | |||
181 | /** |
||
182 | * Get the time saving shift on this date in seconds |
||
183 | * |
||
184 | * @return int |
||
185 | */ |
||
186 | 1 | public function getDaylightSavingShift() |
|
192 | |||
193 | //// Daylight saving time shift aware methods ////////////////////////////////////////////////////////////////////// |
||
194 | |||
195 | /** |
||
196 | * Add hours to start of day while also respecting Daylight-saving-time shift |
||
197 | * |
||
198 | * E.g. on 2016-03-27T10:00:00 in Berlin is only 9 hours after the start of the day. |
||
199 | * |
||
200 | * @param $hours |
||
201 | * |
||
202 | * @return LocalDate |
||
203 | */ |
||
204 | public function getDstStartOfDayPlusHours ($hours) |
||
214 | |||
215 | //// Modification methods ////////////////////////////////////////////////////////////////////////////////////////// |
||
216 | |||
217 | /** |
||
218 | * @return LocalDate |
||
219 | */ |
||
220 | 18 | public function getStartOfDay() |
|
224 | |||
225 | /** |
||
226 | * Get the nearest start of a day, either the current or the next day depending on the time in the day |
||
227 | * |
||
228 | * @return LocalDate |
||
229 | */ |
||
230 | 1 | public function getNearestStartOfDay() |
|
238 | |||
239 | /** |
||
240 | * @return LocalDate |
||
241 | */ |
||
242 | 2 | View Code Duplication | public function getStartOfPreviousDay() |
243 | { |
||
244 | 2 | $dateCloned = clone $this->getDate(); |
|
245 | |||
246 | // take switches between summer and winter time into account |
||
247 | 2 | if ($dateCloned->format('H') > 21) { |
|
248 | 1 | $dateCloned->modify('-27 hours'); |
|
249 | } else { |
||
250 | 2 | $dateCloned->modify('-24 hours'); |
|
251 | } |
||
252 | |||
253 | 2 | $temp = new LocalDate($dateCloned, $this->timezone); |
|
254 | |||
255 | 2 | return $temp->getStartOfDay(); |
|
256 | } |
||
257 | |||
258 | /** |
||
259 | * @return LocalDate |
||
260 | */ |
||
261 | 2 | View Code Duplication | public function getStartOfNextDay() |
262 | { |
||
263 | 2 | $dateCloned = clone $this->getDate(); |
|
264 | |||
265 | // take switches between summer and winter time into account |
||
266 | 2 | if ($dateCloned->format('H') < 3) { |
|
267 | 1 | $dateCloned->modify('+27 hours'); |
|
268 | } else { |
||
269 | 2 | $dateCloned->modify('+24 hours'); |
|
270 | } |
||
271 | |||
272 | 2 | $temp = new LocalDate($dateCloned, $this->timezone); |
|
273 | |||
274 | 2 | return $temp->getStartOfDay(); |
|
275 | } |
||
276 | |||
277 | /** |
||
278 | * @return LocalDate |
||
279 | */ |
||
280 | 1 | public function getEndOfDay() |
|
284 | |||
285 | /** |
||
286 | * @param int $hour |
||
287 | * @param int $minute |
||
288 | * @param int $second |
||
289 | * |
||
290 | * @return LocalDate |
||
291 | */ |
||
292 | 18 | public function modifyTime($hour, $minute, $second) |
|
304 | |||
305 | /** |
||
306 | * @param int $numSeconds |
||
307 | * |
||
308 | * @return LocalDate |
||
309 | */ |
||
310 | 85 | public function modifyBySeconds($numSeconds) |
|
311 | { |
||
312 | 85 | return LocalDate::fromTimestamp( |
|
313 | 85 | $this->getTimestamp() + (int) $numSeconds, |
|
314 | 85 | $this->getTimezone() |
|
315 | ); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @param float $numMinutes |
||
320 | * |
||
321 | * @return LocalDate |
||
322 | */ |
||
323 | 19 | public function modifyByMinutes($numMinutes) |
|
327 | |||
328 | /** |
||
329 | * @param float $numHours |
||
330 | * |
||
331 | * @return LocalDate |
||
332 | */ |
||
333 | 19 | public function modifyByHours($numHours) |
|
337 | |||
338 | /** |
||
339 | * @param float $numDays |
||
340 | * |
||
341 | * @return LocalDate |
||
342 | */ |
||
343 | 29 | public function modifyByDays($numDays) |
|
347 | |||
348 | /** |
||
349 | * @param int $numDays |
||
350 | * |
||
351 | * @return LocalDate |
||
352 | */ |
||
353 | public function modifyByDaysDaylightSavingAware($numDays) |
||
354 | { |
||
355 | $numDays = (int) $numDays; |
||
356 | $timestamp = $this->getTimestamp() + (86400 * $numDays); |
||
357 | |||
358 | $newDate = LocalDate::fromTimestamp($timestamp, $this->timezone); |
||
359 | |||
360 | // calculate the datetime savings offsets |
||
361 | $timezoneInitialDateOffset = $this->timezone->getOffset($this->date); |
||
362 | $timezoneNewDateOffset = $this->timezone->getOffset($newDate->date); |
||
363 | |||
364 | // fix the new date if it's needed |
||
365 | if ($timezoneInitialDateOffset !== $timezoneNewDateOffset) { |
||
366 | $newDate = LocalDate::fromTimestamp($timestamp + ($timezoneInitialDateOffset - $timezoneNewDateOffset), $this->timezone); |
||
367 | } |
||
368 | |||
369 | return $newDate; |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * @param \DateInterval|string $interval |
||
374 | * |
||
375 | * @deprecated use addInterval or subInterval |
||
376 | * |
||
377 | * @return LocalDate |
||
378 | */ |
||
379 | public function modifyByInterval($interval) |
||
380 | { |
||
381 | return $this->addInterval($interval); |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * @param \DateInterval|string $interval |
||
386 | * |
||
387 | * @return LocalDate |
||
388 | */ |
||
389 | 10 | View Code Duplication | public function addInterval($interval) |
390 | { |
||
391 | 10 | if (!$interval instanceof \DateInterval) { |
|
392 | 10 | $interval = new \DateInterval($interval); |
|
393 | } |
||
394 | |||
395 | 10 | $dateCloned = clone $this->date; |
|
396 | 10 | $dateCloned->add($interval); |
|
397 | |||
398 | 10 | return new LocalDate($dateCloned, $this->timezone); |
|
399 | } |
||
400 | |||
401 | /** |
||
402 | * @param \DateInterval|string $interval |
||
403 | * |
||
404 | * @return LocalDate |
||
405 | */ |
||
406 | 10 | View Code Duplication | public function subInterval($interval) |
407 | { |
||
408 | 10 | if (!$interval instanceof \DateInterval) { |
|
409 | 10 | $interval = new \DateInterval($interval); |
|
410 | } |
||
411 | |||
412 | 10 | $dateCloned = clone $this->date; |
|
413 | 10 | $dateCloned->sub($interval); |
|
414 | |||
415 | 10 | return new LocalDate($dateCloned, $this->timezone); |
|
416 | } |
||
417 | |||
418 | /** |
||
419 | * @param $minutesInterval |
||
420 | * |
||
421 | * @return LocalDate |
||
422 | */ |
||
423 | 1 | public function alignToMinutesInterval($minutesInterval) |
|
438 | |||
439 | /** |
||
440 | * @return int |
||
441 | */ |
||
442 | 1 | public function getMinutesIntoDay() |
|
449 | |||
450 | /** |
||
451 | * Get the hours portion of the current time |
||
452 | * |
||
453 | * @return int |
||
454 | */ |
||
455 | 1 | public function getHours() |
|
459 | |||
460 | /** |
||
461 | * Get the weekday with Sun = 0, Mon = 1, ... Sat = 6 |
||
462 | * |
||
463 | * @return int |
||
464 | */ |
||
465 | 1 | public function getWeekday() |
|
475 | |||
476 | //// COMPARISON METHODS ////////////////////////////////////////////////////////////////////////////////////////// |
||
477 | |||
478 | /** |
||
479 | * Returns the data that is earlier, either the current or the other |
||
480 | * |
||
481 | * @param LocalDate|\DateTime $other |
||
482 | * |
||
483 | * @return LocalDate |
||
484 | */ |
||
485 | 1 | public function min($other) |
|
491 | |||
492 | /** |
||
493 | * Returns the data that is later, either the current or the other |
||
494 | * |
||
495 | * @param LocalDate|\DateTime $other |
||
496 | * |
||
497 | * @return LocalDate |
||
498 | */ |
||
499 | 1 | public function max($other) |
|
505 | |||
506 | /** |
||
507 | * Get the number of seconds between this and the other. |
||
508 | * |
||
509 | * The result will be negative when this is after the other |
||
510 | * |
||
511 | * @param LocalDate|\DateTime $other |
||
512 | * |
||
513 | * @return float |
||
514 | */ |
||
515 | 82 | public function diffInSeconds($other) |
|
521 | |||
522 | /** |
||
523 | * Get the number of minutes between this and the other. |
||
524 | * |
||
525 | * The result will be negative when this is after the other |
||
526 | * |
||
527 | * @param LocalDate|\DateTime $other |
||
528 | * |
||
529 | * @return float |
||
530 | */ |
||
531 | 18 | public function diffInMinutes($other) |
|
537 | |||
538 | /** |
||
539 | * Get the number of minutes between this and the other. |
||
540 | * |
||
541 | * The result will be negative when this is after the other |
||
542 | * |
||
543 | * @param LocalDate|\DateTime $other |
||
544 | * |
||
545 | * @return float |
||
546 | */ |
||
547 | 18 | public function diffInHours($other) |
|
553 | |||
554 | /** |
||
555 | * Get the number of minutes between this and the other. |
||
556 | * |
||
557 | * The result will be negative when this is after the other |
||
558 | * |
||
559 | * @param LocalDate|\DateTime $other |
||
560 | * |
||
561 | * @return float |
||
562 | */ |
||
563 | 28 | public function diffInDays($other) |
|
569 | |||
570 | /** |
||
571 | * @param LocalDate|\DateTime $other |
||
572 | * |
||
573 | * @return bool |
||
574 | */ |
||
575 | 1 | public function isBefore($other) |
|
581 | |||
582 | /** |
||
583 | * @param LocalDate|\DateTime $other |
||
584 | * |
||
585 | * @return bool |
||
586 | */ |
||
587 | 1 | public function isBeforeOrEqual($other) |
|
593 | |||
594 | /** |
||
595 | * @param LocalDate|\DateTime $other |
||
596 | * |
||
597 | * @return bool |
||
598 | */ |
||
599 | 10 | public function isEqual($other) |
|
605 | |||
606 | /** |
||
607 | * @param LocalDate|\DateTime $other |
||
608 | * |
||
609 | * @return bool |
||
610 | */ |
||
611 | 1 | public function isAfter($other) |
|
617 | |||
618 | /** |
||
619 | * @param LocalDate|\DateTime $other |
||
620 | * |
||
621 | * @return bool |
||
622 | */ |
||
623 | 1 | public function isAfterOrEqual($other) |
|
629 | |||
630 | //// PRIVATE HELPER ////////////////////////////////////////////////////////////////////////////////////////////// |
||
631 | |||
632 | /** |
||
633 | * @param mixed $input |
||
634 | * |
||
635 | * @return LocalDate |
||
636 | */ |
||
637 | 96 | private function ensure($input) |
|
649 | } |
||
650 |