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 Dates 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 Dates, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Dates extends DateTime |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @const ERR_MODIFY_PATTERN_NOT_MATCH Exception code if the pattern used |
||
| 16 | * into the method modify() not match with the regex. |
||
| 17 | */ |
||
| 18 | const ERR_MODIFY_PATTERN_NOT_MATCH = 1306001; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @const ERR_MODIFY_UNKNOWN_MODIFIER Exception code if the modifier used |
||
| 22 | * into the method modify() is unknown. |
||
| 23 | */ |
||
| 24 | const ERR_MODIFY_UNKNOWN_MODIFIER = 1306002; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string[] $humanReadableI18n Words used in method to transform |
||
| 28 | * date difference to human readable. |
||
| 29 | */ |
||
| 30 | protected static $humanReadableI18n = [ |
||
| 31 | 'now' => 'now', |
||
| 32 | 'since' => 'since', |
||
| 33 | 'in' => 'in', |
||
| 34 | 'yesterday' => 'yesterday', |
||
| 35 | 'tomorrow' => 'tomorrow', |
||
| 36 | 'the' => 'the', |
||
| 37 | 'at' => 'at' |
||
| 38 | ]; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string[] $humanReadableFormats Date and time formats used in |
||
| 42 | * method to transform date difference to human readable. |
||
| 43 | */ |
||
| 44 | protected static $humanReadableFormats = [ |
||
| 45 | 'dateSameYear' => 'm-d', |
||
| 46 | 'dateDifferentYear' => 'Y-m-d', |
||
| 47 | 'time' => 'H:i' |
||
| 48 | ]; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string[] $modifyNewKeywords Add new keywords which can be used |
||
| 52 | * with the modify method. The key is the new keyword and the value the |
||
| 53 | * corresponding keyword into DateTime::modify method. |
||
| 54 | */ |
||
| 55 | protected static $modifyNewKeywords = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Return the value of the humanReadableI18n property |
||
| 59 | * |
||
| 60 | * @return string[] |
||
| 61 | */ |
||
| 62 | public static function getHumanReadableI18n() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Define a new value for a key of the humanReadableI18n property |
||
| 69 | * |
||
| 70 | * @param string $key The key in humanReadableI18n |
||
| 71 | * @param string $value The new value for the key |
||
| 72 | * |
||
| 73 | * @return void |
||
| 74 | */ |
||
| 75 | public static function setHumanReadableI18nKey($key, $value) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Define a new value to the property humanReadableI18n |
||
| 82 | * |
||
| 83 | * @param string[] $value The new value for the property |
||
| 84 | * |
||
| 85 | * @return void |
||
| 86 | */ |
||
| 87 | public static function setHumanReadableI18n($value) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Return the value of the humanReadableFormats property |
||
| 94 | * |
||
| 95 | * @return string[] |
||
| 96 | */ |
||
| 97 | public static function getHumanReadableFormats() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Define a new value for a key of the humanReadableFormats property |
||
| 104 | * |
||
| 105 | * @param string $key The key in humanReadableFormats |
||
| 106 | * @param string $value The new value for the key |
||
| 107 | * |
||
| 108 | * @return void |
||
| 109 | */ |
||
| 110 | public static function setHumanReadableFormatsKey($key, $value) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Define a new value to the property humanReadableFormats |
||
| 117 | * |
||
| 118 | * @param string[] $value The new value for the property |
||
| 119 | * |
||
| 120 | * @return void |
||
| 121 | */ |
||
| 122 | public static function setHumanReadableFormats($value) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Return the value of the modifyNewKeywords property |
||
| 129 | * |
||
| 130 | * @return string[] |
||
| 131 | */ |
||
| 132 | public static function getModifyNewKeywords() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Define a new value to the property modifyNewKeywords |
||
| 139 | * |
||
| 140 | * @param string[] $value The new value for the property |
||
| 141 | * |
||
| 142 | * @return void |
||
| 143 | */ |
||
| 144 | public static function setModifyNewKeywords($value) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Return the date. Format is Y-m-d H:i:sO |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | public function getDate() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Return a numeric representation of a year, 4 digits. |
||
| 161 | * |
||
| 162 | * @return int |
||
| 163 | */ |
||
| 164 | public function getYear() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Return the numeric representation of a month, without leading zeros. |
||
| 171 | * The returned int format can not have leading zeros. |
||
| 172 | * |
||
| 173 | * @return int |
||
| 174 | */ |
||
| 175 | public function getMonth() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Return the day of the month without leading zeros. |
||
| 182 | * The returned int format can not have leading zeros. |
||
| 183 | * |
||
| 184 | * @return int |
||
| 185 | */ |
||
| 186 | public function getDay() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Return 24-hour format without leading zeros. |
||
| 193 | * The returned int format can not have leading zeros. |
||
| 194 | * |
||
| 195 | * @return int |
||
| 196 | */ |
||
| 197 | public function getHour() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Return minutes, without leading zeros. |
||
| 204 | * The returned int format can not have leading zeros. |
||
| 205 | * |
||
| 206 | * @return int |
||
| 207 | */ |
||
| 208 | public function getMinute() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Return second, without leading zeros. |
||
| 215 | * The returned int format can not have leading zeros. |
||
| 216 | * |
||
| 217 | * @return int |
||
| 218 | */ |
||
| 219 | public function getSecond() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Return the difference to Greenwich time (GMT) |
||
| 226 | * with colon between hours and minutes |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function getZone() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Override modify DateTime method to allow personal keywords |
||
| 237 | * |
||
| 238 | * @param string $modify A date/time string |
||
| 239 | * |
||
| 240 | * @return \BFW\Dates |
||
| 241 | */ |
||
| 242 | public function modify($modify) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get DateTime equivalent keyword for a personal keyword declared into |
||
| 260 | * the property modifyNewKeywords. |
||
| 261 | * |
||
| 262 | * @return \stdClass |
||
| 263 | */ |
||
| 264 | protected function obtainNewKeywordsForModify() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Use personal keyword on modify method |
||
| 282 | * |
||
| 283 | * @param string $modify A date/time string |
||
| 284 | * |
||
| 285 | * @throws Exception If bad pattern or unknown keyword |
||
| 286 | */ |
||
| 287 | protected function modifyWithOthersKeywords($modify) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Return date's SQL format (postgresql format). |
||
| 322 | * The return can be an array or a string. |
||
| 323 | * |
||
| 324 | * @param boolean $returnArray (default false) True to return an array. |
||
| 325 | * |
||
| 326 | * @return string[]|string |
||
| 327 | */ |
||
| 328 | public function getSqlFormat($returnArray = false) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * List all timezone existing in current php version |
||
| 342 | * |
||
| 343 | * @return string[] |
||
| 344 | */ |
||
| 345 | public function lstTimeZone() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * List all continent define in php DateTimeZone. |
||
| 352 | * |
||
| 353 | * @return string[] |
||
| 354 | */ |
||
| 355 | public function lstTimeZoneContinent() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * List all available country for a continent |
||
| 373 | * |
||
| 374 | * @param string $continent The continent for which we want |
||
| 375 | * the countries list |
||
| 376 | * |
||
| 377 | * @return string[] |
||
| 378 | */ |
||
| 379 | public function lstTimeZoneCountries($continent) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Transform a date to a human readable format |
||
| 395 | * |
||
| 396 | * @param boolean $returnDateAndTime (default true) True to return date and |
||
| 397 | * time concatenated with a space. False to have only date. |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | public function humanReadable($returnDateAndTime = true) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Format date to human readable when the date is now |
||
| 437 | * |
||
| 438 | * @param \stdClass $parsedTxt Texts returned by humanReadable method |
||
| 439 | * |
||
| 440 | * @return void |
||
| 441 | */ |
||
| 442 | protected function humanDateNow($parsedTxt) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Format date to human readable when date is today |
||
| 450 | * |
||
| 451 | * @param \stdClass $parsedTxt Texts returned by humanReadable method |
||
| 452 | * @param \DateInterval $diff Interval between now and date to read |
||
| 453 | * |
||
| 454 | * @return void |
||
| 455 | */ |
||
| 456 | protected function humanDateToday($parsedTxt, $diff) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Format date to human readable when date is yesterday |
||
| 477 | * |
||
| 478 | * @param \stdClass $parsedTxt Texts returned by humanReadable method |
||
| 479 | * |
||
| 480 | * @return void |
||
| 481 | */ |
||
| 482 | View Code Duplication | protected function humanDateYesterday($parsedTxt) |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Format date to human readable when date is tomorrow |
||
| 495 | * |
||
| 496 | * @param \stdClass $parsedTxt Texts returned by humanReadable method |
||
| 497 | * |
||
| 498 | * @return void |
||
| 499 | */ |
||
| 500 | View Code Duplication | protected function humanDateTomorrow($parsedTxt) |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Format date to human readable when date is not now, today or yesterday |
||
| 513 | * |
||
| 514 | * @param \stdClass $parsedTxt Texts returned by humanReadable method |
||
| 515 | * @param \DateTime $current DateTime object for now |
||
| 516 | * |
||
| 517 | * @return void |
||
| 518 | */ |
||
| 519 | protected function humanDateOther($parsedTxt, $current) |
||
| 538 | } |
||
| 539 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.