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) |
|
| 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() |
| 257 | |||
| 258 | /** |
||
| 259 | * @return LocalDate |
||
| 260 | */ |
||
| 261 | 2 | View Code Duplication | public function getStartOfNextDay() |
| 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) |
|
| 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) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param \DateInterval|string $interval |
||
| 374 | * |
||
| 375 | * @deprecated use addInterval or subInterval |
||
| 376 | * |
||
| 377 | * @return LocalDate |
||
| 378 | */ |
||
| 379 | public function modifyByInterval($interval) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param \DateInterval|string $interval |
||
| 386 | * |
||
| 387 | * @return LocalDate |
||
| 388 | */ |
||
| 389 | 10 | View Code Duplication | public function addInterval($interval) |
| 400 | |||
| 401 | /** |
||
| 402 | * @param \DateInterval|string $interval |
||
| 403 | * |
||
| 404 | * @return LocalDate |
||
| 405 | */ |
||
| 406 | 10 | View Code Duplication | public function subInterval($interval) |
| 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 |