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) |
|
| 45 | { |
||
| 46 | 11 | $tz = $dateTime->getTimezone(); |
|
| 47 | |||
| 48 | // normalize input coming from Javascript or Java etc... |
||
| 49 | 11 | if ($tz->getName() === 'Z') { |
|
| 50 | 2 | $tz = new \DateTimeZone('Etc/UTC'); |
|
| 51 | 2 | } |
|
| 52 | |||
| 53 | 11 | return new LocalDate($dateTime, $tz); |
|
| 54 | } |
||
| 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 | 183 | public function __construct($input, $timezone) |
|
| 61 | { |
||
| 62 | 183 | if (! $timezone instanceof \DateTimeZone) { |
|
| 63 | 172 | $timezone = new \DateTimeZone((string) $timezone); |
|
| 64 | 172 | } |
|
| 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 | 183 | if ($input instanceof \DateTime) { |
|
| 83 | 53 | $date = (new \DateTime()) |
|
| 84 | 53 | ->setTimestamp($input->getTimestamp()) |
|
| 85 | 53 | ->setTimezone($timezone); |
|
| 86 | 183 | } elseif (is_numeric($input)) { |
|
| 87 | 101 | $date = (new \DateTime()) |
|
| 88 | 101 | ->setTimestamp($input) |
|
| 89 | 101 | ->setTimezone($timezone); |
|
| 90 | 101 | } else { |
|
| 91 | // when we have string input, we immediately use the timezone |
||
| 92 | 141 | $date = new \DateTime($input, $timezone); |
|
| 93 | |||
| 94 | // since the given 2nd parameter is ignored for dates like '...+02:00' we check if we need to convert the |
||
| 95 | // timezone |
||
| 96 | 141 | if ($date->getTimezone()->getOffset($date) !== $timezone->getOffset($date)) { |
|
| 97 | 5 | $date = (new \DateTime()) |
|
| 98 | 5 | ->setTimestamp($date->getTimestamp()) |
|
| 99 | 5 | ->setTimezone($timezone); |
|
| 100 | 5 | } |
|
| 101 | } |
||
| 102 | |||
| 103 | 183 | $this->date = $date; |
|
| 104 | 183 | $this->timezone = $timezone; |
|
| 105 | 183 | } |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | 1 | public function __toString() |
|
| 111 | { |
||
| 112 | 1 | return $this->format(); |
|
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return \DateTime |
||
| 117 | */ |
||
| 118 | 8 | public function getDate() |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | 103 | public function getTimestamp() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @return \DateTimeZone |
||
| 133 | */ |
||
| 134 | 98 | public function getTimezone() |
|
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * Get the offset of the timezone in seconds |
||
| 142 | * |
||
| 143 | * @return int |
||
| 144 | * |
||
| 145 | * @see DateTime::getOffset |
||
| 146 | */ |
||
| 147 | 2 | public function getOffset() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @return float |
||
| 154 | */ |
||
| 155 | 1 | public function getOffsetInMinutes() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @return float |
||
| 164 | */ |
||
| 165 | 1 | public function getOffsetInHours() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @param string $format |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | 153 | public function format($format = 'c') |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @return LocalDate |
||
| 184 | */ |
||
| 185 | 2 | public function getClone() |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Get the time saving shift on this date in seconds |
||
| 192 | * |
||
| 193 | * @return int |
||
| 194 | */ |
||
| 195 | 1 | public function getDaylightSavingShift() |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @return LocalDate |
||
| 204 | */ |
||
| 205 | 18 | public function getStartOfDay() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Get the nearest start of a day, either the current or the next day depending on the time in the day |
||
| 212 | * |
||
| 213 | * Every time BEFORE 12:00 will result in the start of that day. |
||
| 214 | * Every time AFTER and INCLUDING 12:00 will result in the start of the next day. |
||
| 215 | * |
||
| 216 | * @return LocalDate |
||
| 217 | */ |
||
| 218 | 1 | public function getNearestStartOfDay() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @return LocalDate |
||
| 229 | */ |
||
| 230 | 2 | View Code Duplication | public function getStartOfPreviousDay() |
| 245 | |||
| 246 | /** |
||
| 247 | * @return LocalDate |
||
| 248 | */ |
||
| 249 | 2 | View Code Duplication | public function getStartOfNextDay() |
| 264 | |||
| 265 | /** |
||
| 266 | * @return LocalDate |
||
| 267 | */ |
||
| 268 | 1 | public function getEndOfDay() |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @param int $hour |
||
| 275 | * @param int $minute |
||
| 276 | * @param int $second |
||
| 277 | * |
||
| 278 | * @return LocalDate |
||
| 279 | */ |
||
| 280 | 18 | public function modifyTime($hour, $minute, $second) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @param int $numSeconds |
||
| 295 | * |
||
| 296 | * @return LocalDate |
||
| 297 | */ |
||
| 298 | 85 | public function modifyBySeconds($numSeconds) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @param float $numMinutes |
||
| 308 | * |
||
| 309 | * @return LocalDate |
||
| 310 | */ |
||
| 311 | 19 | public function modifyByMinutes($numMinutes) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @param float $numHours |
||
| 318 | * |
||
| 319 | * @return LocalDate |
||
| 320 | */ |
||
| 321 | 19 | public function modifyByHours($numHours) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param float $numDays |
||
| 328 | * |
||
| 329 | * @return LocalDate |
||
| 330 | */ |
||
| 331 | 29 | public function modifyByDays($numDays) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * @param \DateInterval|string $interval |
||
| 338 | * |
||
| 339 | * @deprecated use addInterval or subInterval |
||
| 340 | * |
||
| 341 | * @return LocalDate |
||
| 342 | */ |
||
| 343 | public function modifyByInterval($interval) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param \DateInterval|string $interval |
||
| 350 | * |
||
| 351 | * @return LocalDate |
||
| 352 | */ |
||
| 353 | 6 | View Code Duplication | public function addInterval($interval) |
| 364 | |||
| 365 | /** |
||
| 366 | * @param \DateInterval|string $interval |
||
| 367 | * |
||
| 368 | * @return LocalDate |
||
| 369 | */ |
||
| 370 | 6 | View Code Duplication | public function subInterval($interval) |
| 381 | |||
| 382 | /** |
||
| 383 | * @param $minutesInterval |
||
| 384 | * |
||
| 385 | * @return LocalDate |
||
| 386 | */ |
||
| 387 | 1 | public function alignToMinutesInterval($minutesInterval) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @return int |
||
| 405 | */ |
||
| 406 | 1 | public function getMinutesIntoDay() |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Get the hours portion of the current time |
||
| 416 | * |
||
| 417 | * @return int |
||
| 418 | */ |
||
| 419 | 1 | public function getHours() |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @return int |
||
| 426 | */ |
||
| 427 | 1 | public function getWeekday() |
|
| 431 | |||
| 432 | //// COMPARISON METHODS ////////////////////////////////////////////////////////////////////////////////////////// |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Returns the data that is earlier, either the current or the other |
||
| 436 | * |
||
| 437 | * @param LocalDate|\DateTime $other |
||
| 438 | * |
||
| 439 | * @return LocalDate |
||
| 440 | */ |
||
| 441 | 1 | public function min($other) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Returns the data that is later, either the current or the other |
||
| 450 | * |
||
| 451 | * @param LocalDate|\DateTime $other |
||
| 452 | * |
||
| 453 | * @return LocalDate |
||
| 454 | */ |
||
| 455 | 1 | public function max($other) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Get the number of seconds between this and the other. |
||
| 464 | * |
||
| 465 | * The result will be negative when this is after the other |
||
| 466 | * |
||
| 467 | * @param LocalDate|\DateTime $other |
||
| 468 | * |
||
| 469 | * @return float |
||
| 470 | */ |
||
| 471 | 82 | public function diffInSeconds($other) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Get the number of minutes between this and the other. |
||
| 480 | * |
||
| 481 | * The result will be negative when this is after the other |
||
| 482 | * |
||
| 483 | * @param LocalDate|\DateTime $other |
||
| 484 | * |
||
| 485 | * @return float |
||
| 486 | */ |
||
| 487 | 18 | public function diffInMinutes($other) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Get the number of minutes between this and the other. |
||
| 496 | * |
||
| 497 | * The result will be negative when this is after the other |
||
| 498 | * |
||
| 499 | * @param LocalDate|\DateTime $other |
||
| 500 | * |
||
| 501 | * @return float |
||
| 502 | */ |
||
| 503 | 18 | public function diffInHours($other) |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Get the number of minutes between this and the other. |
||
| 512 | * |
||
| 513 | * The result will be negative when this is after the other |
||
| 514 | * |
||
| 515 | * @param LocalDate|\DateTime $other |
||
| 516 | * |
||
| 517 | * @return float |
||
| 518 | */ |
||
| 519 | 28 | public function diffInDays($other) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * @param LocalDate|\DateTime $other |
||
| 528 | * |
||
| 529 | * @return bool |
||
| 530 | */ |
||
| 531 | 1 | public function isBefore($other) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * @param LocalDate|\DateTime $other |
||
| 540 | * |
||
| 541 | * @return bool |
||
| 542 | */ |
||
| 543 | 1 | public function isBeforeOrEqual($other) |
|
| 549 | |||
| 550 | /** |
||
| 551 | * @param LocalDate|\DateTime $other |
||
| 552 | * |
||
| 553 | * @return bool |
||
| 554 | */ |
||
| 555 | 10 | public function isEqual($other) |
|
| 561 | |||
| 562 | /** |
||
| 563 | * @param LocalDate|\DateTime $other |
||
| 564 | * |
||
| 565 | * @return bool |
||
| 566 | */ |
||
| 567 | 1 | public function isAfter($other) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * @param LocalDate|\DateTime $other |
||
| 576 | * |
||
| 577 | * @return bool |
||
| 578 | */ |
||
| 579 | 1 | public function isAfterOrEqual($other) |
|
| 585 | |||
| 586 | //// PRIVATE HELPER ////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @param mixed $input |
||
| 590 | * |
||
| 591 | * @return LocalDate |
||
| 592 | */ |
||
| 593 | 96 | private function ensure($input) |
|
| 601 | } |
||
| 602 |