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 | 91 | 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 | 52 | $date = (new \DateTime())  | 
            |
| 84 | 52 | ->setTimestamp($input->getTimestamp())  | 
            |
| 85 | 52 | ->setTimezone($timezone);  | 
            |
| 86 | 183 |         } elseif (is_numeric($input)) { | 
            |
| 87 | 100 | $date = (new \DateTime())  | 
            |
| 88 | 100 | ->setTimestamp($input)  | 
            |
| 89 | 100 | ->setTimezone($timezone);  | 
            |
| 90 | 100 |         } 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 | 102 | public function getTimestamp()  | 
            |
| 130 | |||
| 131 | /**  | 
            ||
| 132 | * @return \DateTimeZone  | 
            ||
| 133 | */  | 
            ||
| 134 | 97 | public function getTimezone()  | 
            |
| 138 | |||
| 139 | /**  | 
            ||
| 140 | * @return int  | 
            ||
| 141 | */  | 
            ||
| 142 | 2 | public function getOffset()  | 
            |
| 146 | |||
| 147 | /**  | 
            ||
| 148 | * @return float  | 
            ||
| 149 | */  | 
            ||
| 150 | 1 | public function getOffsetInMinutes()  | 
            |
| 156 | |||
| 157 | /**  | 
            ||
| 158 | * @return float  | 
            ||
| 159 | */  | 
            ||
| 160 | 1 | public function getOffsetInHours()  | 
            |
| 166 | |||
| 167 | /**  | 
            ||
| 168 | * @param string $format  | 
            ||
| 169 | *  | 
            ||
| 170 | * @return string  | 
            ||
| 171 | */  | 
            ||
| 172 | 153 | public function format($format = 'c')  | 
            |
| 176 | |||
| 177 | /**  | 
            ||
| 178 | * @return LocalDate  | 
            ||
| 179 | */  | 
            ||
| 180 | 1 | public function getClone()  | 
            |
| 184 | |||
| 185 | /**  | 
            ||
| 186 | * Get the time saving shift on this date in seconds  | 
            ||
| 187 | *  | 
            ||
| 188 | * @return int  | 
            ||
| 189 | */  | 
            ||
| 190 | 1 | public function getDaylightSavingShift()  | 
            |
| 196 | |||
| 197 | //// Daylight saving time shift aware methods //////////////////////////////////////////////////////////////////////  | 
            ||
| 198 | |||
| 199 | /**  | 
            ||
| 200 | * Add hours to start of day while also respecting Daylight-saving-time shift  | 
            ||
| 201 | *  | 
            ||
| 202 | * E.g. on 2016-03-27T10:00:00 in Berlin is only 9 hours after the start of the day.  | 
            ||
| 203 | *  | 
            ||
| 204 | * @param $hours  | 
            ||
| 205 | *  | 
            ||
| 206 | * @return LocalDate  | 
            ||
| 207 | */  | 
            ||
| 208 | public function getDstStartOfDayPlusHours ($hours)  | 
            ||
| 218 | |||
| 219 | //// Modification methods //////////////////////////////////////////////////////////////////////////////////////////  | 
            ||
| 220 | |||
| 221 | /**  | 
            ||
| 222 | * @return LocalDate  | 
            ||
| 223 | */  | 
            ||
| 224 | 17 | public function getStartOfDay()  | 
            |
| 228 | |||
| 229 | /**  | 
            ||
| 230 | * Get the nearest start of a day, either the current or the next day depending on the time in the day  | 
            ||
| 231 | *  | 
            ||
| 232 | * @return LocalDate  | 
            ||
| 233 | */  | 
            ||
| 234 | 1 | public function getNearestStartOfDay()  | 
            |
| 242 | |||
| 243 | /**  | 
            ||
| 244 | * @return LocalDate  | 
            ||
| 245 | */  | 
            ||
| 246 | 2 | View Code Duplication | public function getStartOfPreviousDay()  | 
            
| 261 | |||
| 262 | /**  | 
            ||
| 263 | * @return LocalDate  | 
            ||
| 264 | */  | 
            ||
| 265 | 2 | View Code Duplication | public function getStartOfNextDay()  | 
            
| 280 | |||
| 281 | /**  | 
            ||
| 282 | * @return LocalDate  | 
            ||
| 283 | */  | 
            ||
| 284 | 1 | public function getEndOfDay()  | 
            |
| 288 | |||
| 289 | /**  | 
            ||
| 290 | * @param int $hour  | 
            ||
| 291 | * @param int $minute  | 
            ||
| 292 | * @param int $second  | 
            ||
| 293 | *  | 
            ||
| 294 | * @return LocalDate  | 
            ||
| 295 | */  | 
            ||
| 296 | 17 | public function modifyTime($hour, $minute, $second)  | 
            |
| 308 | |||
| 309 | /**  | 
            ||
| 310 | * @param int $numSeconds  | 
            ||
| 311 | *  | 
            ||
| 312 | * @return LocalDate  | 
            ||
| 313 | */  | 
            ||
| 314 | 84 | public function modifyBySeconds($numSeconds)  | 
            |
| 321 | |||
| 322 | /**  | 
            ||
| 323 | * @param float $numMinutes  | 
            ||
| 324 | *  | 
            ||
| 325 | * @return LocalDate  | 
            ||
| 326 | */  | 
            ||
| 327 | 18 | public function modifyByMinutes($numMinutes)  | 
            |
| 331 | |||
| 332 | /**  | 
            ||
| 333 | * @param float $numHours  | 
            ||
| 334 | *  | 
            ||
| 335 | * @return LocalDate  | 
            ||
| 336 | */  | 
            ||
| 337 | 19 | public function modifyByHours($numHours)  | 
            |
| 341 | |||
| 342 | /**  | 
            ||
| 343 | * @param float $numDays  | 
            ||
| 344 | *  | 
            ||
| 345 | * @return LocalDate  | 
            ||
| 346 | */  | 
            ||
| 347 | 29 | public function modifyByDays($numDays)  | 
            |
| 351 | |||
| 352 | /**  | 
            ||
| 353 | * @param \DateInterval|string $interval  | 
            ||
| 354 | *  | 
            ||
| 355 | * @deprecated use addInterval or subInterval  | 
            ||
| 356 | *  | 
            ||
| 357 | * @return LocalDate  | 
            ||
| 358 | */  | 
            ||
| 359 | public function modifyByInterval($interval)  | 
            ||
| 363 | |||
| 364 | /**  | 
            ||
| 365 | * @param \DateInterval|string $interval  | 
            ||
| 366 | *  | 
            ||
| 367 | * @return LocalDate  | 
            ||
| 368 | */  | 
            ||
| 369 | 6 | View Code Duplication | public function addInterval($interval)  | 
            
| 380 | |||
| 381 | /**  | 
            ||
| 382 | * @param \DateInterval|string $interval  | 
            ||
| 383 | *  | 
            ||
| 384 | * @return LocalDate  | 
            ||
| 385 | */  | 
            ||
| 386 | 6 | View Code Duplication | public function subInterval($interval)  | 
            
| 397 | |||
| 398 | /**  | 
            ||
| 399 | * @param $minutesInterval  | 
            ||
| 400 | *  | 
            ||
| 401 | * @return LocalDate  | 
            ||
| 402 | */  | 
            ||
| 403 | 1 | public function alignToMinutesInterval($minutesInterval)  | 
            |
| 414 | |||
| 415 | /**  | 
            ||
| 416 | * @return int  | 
            ||
| 417 | */  | 
            ||
| 418 | 1 | public function getMinutesIntoDay()  | 
            |
| 425 | |||
| 426 | /**  | 
            ||
| 427 | * Get the hours portion of the current time  | 
            ||
| 428 | *  | 
            ||
| 429 | * @return int  | 
            ||
| 430 | */  | 
            ||
| 431 | 1 | public function getHours()  | 
            |
| 435 | |||
| 436 | /**  | 
            ||
| 437 | * @return int  | 
            ||
| 438 | */  | 
            ||
| 439 | 1 | public function getWeekday()  | 
            |
| 443 | |||
| 444 | //// COMPARISON METHODS //////////////////////////////////////////////////////////////////////////////////////////  | 
            ||
| 445 | |||
| 446 | /**  | 
            ||
| 447 | * Returns the data that is earlier, either the current or the other  | 
            ||
| 448 | *  | 
            ||
| 449 | * @param LocalDate|\DateTime $other  | 
            ||
| 450 | *  | 
            ||
| 451 | * @return LocalDate  | 
            ||
| 452 | */  | 
            ||
| 453 | 1 | public function min($other)  | 
            |
| 459 | |||
| 460 | /**  | 
            ||
| 461 | * Returns the data that is later, either the current or the other  | 
            ||
| 462 | *  | 
            ||
| 463 | * @param LocalDate|\DateTime $other  | 
            ||
| 464 | *  | 
            ||
| 465 | * @return LocalDate  | 
            ||
| 466 | */  | 
            ||
| 467 | 1 | public function max($other)  | 
            |
| 473 | |||
| 474 | /**  | 
            ||
| 475 | * Get the number of seconds between this and the other.  | 
            ||
| 476 | *  | 
            ||
| 477 | * The result will be negative when this is after the other  | 
            ||
| 478 | *  | 
            ||
| 479 | * @param LocalDate|\DateTime $other  | 
            ||
| 480 | *  | 
            ||
| 481 | * @return float  | 
            ||
| 482 | */  | 
            ||
| 483 | 82 | public function diffInSeconds($other)  | 
            |
| 489 | |||
| 490 | /**  | 
            ||
| 491 | * Get the number of minutes between this and the other.  | 
            ||
| 492 | *  | 
            ||
| 493 | * The result will be negative when this is after the other  | 
            ||
| 494 | *  | 
            ||
| 495 | * @param LocalDate|\DateTime $other  | 
            ||
| 496 | *  | 
            ||
| 497 | * @return float  | 
            ||
| 498 | */  | 
            ||
| 499 | 18 | public function diffInMinutes($other)  | 
            |
| 505 | |||
| 506 | /**  | 
            ||
| 507 | * Get the number of minutes 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 | 18 | public function diffInHours($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 | 28 | public function diffInDays($other)  | 
            |
| 537 | |||
| 538 | /**  | 
            ||
| 539 | * @param LocalDate|\DateTime $other  | 
            ||
| 540 | *  | 
            ||
| 541 | * @return bool  | 
            ||
| 542 | */  | 
            ||
| 543 | 1 | public function isBefore($other)  | 
            |
| 549 | |||
| 550 | /**  | 
            ||
| 551 | * @param LocalDate|\DateTime $other  | 
            ||
| 552 | *  | 
            ||
| 553 | * @return bool  | 
            ||
| 554 | */  | 
            ||
| 555 | 1 | public function isBeforeOrEqual($other)  | 
            |
| 561 | |||
| 562 | /**  | 
            ||
| 563 | * @param LocalDate|\DateTime $other  | 
            ||
| 564 | *  | 
            ||
| 565 | * @return bool  | 
            ||
| 566 | */  | 
            ||
| 567 | 10 | public function isEqual($other)  | 
            |
| 573 | |||
| 574 | /**  | 
            ||
| 575 | * @param LocalDate|\DateTime $other  | 
            ||
| 576 | *  | 
            ||
| 577 | * @return bool  | 
            ||
| 578 | */  | 
            ||
| 579 | 1 | public function isAfter($other)  | 
            |
| 585 | |||
| 586 | /**  | 
            ||
| 587 | * @param LocalDate|\DateTime $other  | 
            ||
| 588 | *  | 
            ||
| 589 | * @return bool  | 
            ||
| 590 | */  | 
            ||
| 591 | 1 | public function isAfterOrEqual($other)  | 
            |
| 597 | |||
| 598 | //// PRIVATE HELPER //////////////////////////////////////////////////////////////////////////////////////////////  | 
            ||
| 599 | |||
| 600 | /**  | 
            ||
| 601 | * @param mixed $input  | 
            ||
| 602 | *  | 
            ||
| 603 | * @return LocalDate  | 
            ||
| 604 | */  | 
            ||
| 605 | 96 | private function ensure($input)  | 
            |
| 617 | }  | 
            ||
| 618 |