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 Date 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 Date, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class Date { |
||
| 40 | /** @var string Optional qualifier, such as BEF, FROM, ABT */ |
||
| 41 | public $qual1; |
||
| 42 | |||
| 43 | /** @var CalendarDate The first (or only) date */ |
||
| 44 | private $date1; |
||
| 45 | |||
| 46 | /** @var string Optional qualifier, such as TO, AND*/ |
||
| 47 | public $qual2; |
||
| 48 | |||
| 49 | /** @var CalendarDate Optional second date */ |
||
| 50 | private $date2; |
||
| 51 | |||
| 52 | /** @var string ptional text, as included with an INTerpreted date */ |
||
| 53 | private $text; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Create a date, from GEDCOM data. |
||
| 57 | * |
||
| 58 | * @param string $date A date in GEDCOM format |
||
| 59 | */ |
||
| 60 | public function __construct($date) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * When we copy a date object, we need to create copies of |
||
| 81 | * its child objects. |
||
| 82 | */ |
||
| 83 | public function __clone() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Convert a calendar date, such as "12 JUN 1943" into calendar date object. |
||
| 92 | * |
||
| 93 | * A GEDCOM date range may have two calendar dates. |
||
| 94 | * |
||
| 95 | * @param string $date |
||
| 96 | * |
||
| 97 | * @throws \DomainException |
||
| 98 | * |
||
| 99 | * @return CalendarDate |
||
|
|
|||
| 100 | */ |
||
| 101 | private function parseDate($date) { |
||
| 102 | // Valid calendar escape specified? - use it |
||
| 103 | if (preg_match('/^(@#D(?:GREGORIAN|JULIAN|HEBREW|HIJRI|JALALI|FRENCH R|ROMAN)+@) ?(.*)/', $date, $match)) { |
||
| 104 | $cal = $match[1]; |
||
| 105 | $date = $match[2]; |
||
| 106 | } else { |
||
| 107 | $cal = ''; |
||
| 108 | } |
||
| 109 | // A date with a month: DM, M, MY or DMY |
||
| 110 | if (preg_match('/^(\d?\d?) ?(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL|VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP|MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH|FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN) ?((?:\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)?)$/', $date, $match)) { |
||
| 111 | $d = $match[1]; |
||
| 112 | $m = $match[2]; |
||
| 113 | $y = $match[3]; |
||
| 114 | } elseif (preg_match('/^(\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)$/', $date, $match)) { |
||
| 115 | // A date with just a year |
||
| 116 | $d = ''; |
||
| 117 | $m = ''; |
||
| 118 | $y = $match[1]; |
||
| 119 | } else { |
||
| 120 | // An invalid date - do the best we can. |
||
| 121 | $d = ''; |
||
| 122 | $m = ''; |
||
| 123 | $y = ''; |
||
| 124 | // Look for a 3/4 digit year anywhere in the date |
||
| 125 | if (preg_match('/\b(\d{3,4})\b/', $date, $match)) { |
||
| 126 | $y = $match[1]; |
||
| 127 | } |
||
| 128 | // Look for a month anywhere in the date |
||
| 129 | View Code Duplication | if (preg_match('/(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL|VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP|MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH|FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)/', $date, $match)) { |
|
| 130 | $m = $match[1]; |
||
| 131 | // Look for a day number anywhere in the date |
||
| 132 | if (preg_match('/\b(\d\d?)\b/', $date, $match)) { |
||
| 133 | $d = $match[1]; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | // Unambiguous dates - override calendar escape |
||
| 139 | if (preg_match('/^(TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL)$/', $m)) { |
||
| 140 | $cal = '@#DHEBREW@'; |
||
| 141 | } else { |
||
| 142 | if (preg_match('/^(VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP)$/', $m)) { |
||
| 143 | $cal = '@#DFRENCH R@'; |
||
| 144 | View Code Duplication | } else { |
|
| 145 | if (preg_match('/^(MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH)$/', $m)) { |
||
| 146 | $cal = '@#DHIJRI@'; // This is a WT extension |
||
| 147 | } else { |
||
| 148 | if (preg_match('/^(FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)$/', $m)) { |
||
| 149 | $cal = '@#DJALALI@'; // This is a WT extension |
||
| 150 | } elseif (preg_match('/^\d{1,4}( B\.C\.)|\d\d\d\d\/\d\d$/', $y)) { |
||
| 151 | $cal = '@#DJULIAN@'; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | // Ambiguous dates - don't override calendar escape |
||
| 158 | View Code Duplication | if ($cal == '') { |
|
| 159 | if (preg_match('/^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)$/', $m)) { |
||
| 160 | $cal = '@#DGREGORIAN@'; |
||
| 161 | } else { |
||
| 162 | if (preg_match('/^[345]\d\d\d$/', $y)) { |
||
| 163 | // Year 3000-5999 |
||
| 164 | $cal = '@#DHEBREW@'; |
||
| 165 | } else { |
||
| 166 | $cal = '@#DGREGORIAN@'; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | // Now construct an object of the correct type |
||
| 171 | switch ($cal) { |
||
| 172 | case '@#DGREGORIAN@': |
||
| 173 | return new GregorianDate([$y, $m, $d]); |
||
| 174 | case '@#DJULIAN@': |
||
| 175 | return new JulianDate([$y, $m, $d]); |
||
| 176 | case '@#DHEBREW@': |
||
| 177 | return new JewishDate([$y, $m, $d]); |
||
| 178 | case '@#DHIJRI@': |
||
| 179 | return new HijriDate([$y, $m, $d]); |
||
| 180 | case '@#DFRENCH R@': |
||
| 181 | return new FrenchDate([$y, $m, $d]); |
||
| 182 | case '@#DJALALI@': |
||
| 183 | return new JalaliDate([$y, $m, $d]); |
||
| 184 | case '@#DROMAN@': |
||
| 185 | return new RomanDate([$y, $m, $d]); |
||
| 186 | default: |
||
| 187 | throw new \DomainException('Invalid calendar'); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * A list of supported calendars and their names. |
||
| 193 | * |
||
| 194 | * @return string[] |
||
| 195 | */ |
||
| 196 | public static function calendarNames() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Convert a date to the preferred format and calendar(s) display. |
||
| 209 | * |
||
| 210 | * @param bool|null $url Wrap the date in a link to calendar.php |
||
| 211 | * @param string|null $date_format Override the default date format |
||
| 212 | * @param bool|null $convert_calendars Convert the date into other calendars |
||
| 213 | * |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public function display($url = false, $date_format = null, $convert_calendars = true) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Get the earliest calendar date from this GEDCOM date. |
||
| 344 | * |
||
| 345 | * In the date “FROM 1900 TO 1910”, this would be 1900. |
||
| 346 | * |
||
| 347 | * @return CalendarDate |
||
| 348 | */ |
||
| 349 | public function minimumDate() { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get the latest calendar date from this GEDCOM date. |
||
| 355 | * |
||
| 356 | * In the date “FROM 1900 TO 1910”, this would be 1910. |
||
| 357 | * |
||
| 358 | * @return CalendarDate |
||
| 359 | */ |
||
| 360 | public function maximumDate() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Get the earliest Julian day number from this GEDCOM date. |
||
| 370 | * |
||
| 371 | * @return int |
||
| 372 | */ |
||
| 373 | public function minimumJulianDay() { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get the latest Julian day number from this GEDCOM date. |
||
| 379 | * |
||
| 380 | * @return int |
||
| 381 | */ |
||
| 382 | public function maximumJulianDay() { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get the middle Julian day number from the GEDCOM date. |
||
| 388 | * |
||
| 389 | * For a month-only date, this would be somewhere around the 16th day. |
||
| 390 | * For a year-only date, this would be somewhere around 1st July. |
||
| 391 | * |
||
| 392 | * @return int |
||
| 393 | */ |
||
| 394 | public function julianDay() { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Offset this date by N years, and round to the whole year. |
||
| 400 | * |
||
| 401 | * This is typically used to create an estimated death date, |
||
| 402 | * which is before a certain number of years after the birth date. |
||
| 403 | * |
||
| 404 | * @param int $years a number of years, positive or negative |
||
| 405 | * @param string $qualifier typically “BEF” or “AFT” |
||
| 406 | * |
||
| 407 | * @return Date |
||
| 408 | */ |
||
| 409 | public function addYears(int $years, string $qualifier = '') { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Calculate the the age of a person, on a date. |
||
| 424 | * |
||
| 425 | * @param Date $d1 |
||
| 426 | * @param Date $d2 |
||
| 427 | * @param int $format |
||
| 428 | * |
||
| 429 | * @throws \InvalidArgumentException |
||
| 430 | * |
||
| 431 | * @return int|string |
||
| 432 | */ |
||
| 433 | public static function getAge(Date $d1, Date $d2 = null, $format = 0) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Calculate the years/months/days between two events |
||
| 480 | * Return a gedcom style age string: "1y 2m 3d" (for fact details) |
||
| 481 | * |
||
| 482 | * @param Date $d1 |
||
| 483 | * @param Date|null $d2 |
||
| 484 | * |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | public static function getAgeGedcom(Date $d1, Date $d2 = null) { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Compare two dates, so they can be sorted. |
||
| 504 | * |
||
| 505 | * return <0 if $a<$b |
||
| 506 | * return >0 if $b>$a |
||
| 507 | * return 0 if dates same/overlap |
||
| 508 | * BEF/AFT sort as the day before/after |
||
| 509 | * |
||
| 510 | * @param Date $a |
||
| 511 | * @param Date $b |
||
| 512 | * |
||
| 513 | * @return int |
||
| 514 | */ |
||
| 515 | public static function compare(Date $a, Date $b) { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Check whether a gedcom date contains usable calendar date(s). |
||
| 560 | * |
||
| 561 | * An incomplete date such as "12 AUG" would be invalid, as |
||
| 562 | * we cannot sort it. |
||
| 563 | * |
||
| 564 | * @return bool |
||
| 565 | */ |
||
| 566 | public function isOK() { |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Calculate the gregorian year for a date. This should NOT be used internally |
||
| 572 | * within WT - we should keep the code "calendar neutral" to allow support for |
||
| 573 | * jewish/arabic users. This is only for interfacing with external entities, |
||
| 574 | * such as the ancestry.com search interface or the dated fact icons. |
||
| 575 | * |
||
| 576 | * @return int |
||
| 577 | */ |
||
| 578 | public function gregorianYear() { |
||
| 588 | } |
||
| 589 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.