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 | * @var string[] $humanReadableI18n Words used in method to transform |
||
| 16 | * date difference to human readable. |
||
| 17 | */ |
||
| 18 | protected static $humanReadableI18n = [ |
||
| 19 | 'now' => 'now', |
||
| 20 | 'since' => 'since', |
||
| 21 | 'in' => 'in', |
||
| 22 | 'yesterday' => 'yesterday', |
||
| 23 | 'the' => 'the', |
||
| 24 | 'at' => 'at' |
||
| 25 | ]; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string[] $humanReadableFormats Date and time formats used in |
||
| 29 | * method to transform date difference to human readable. |
||
| 30 | */ |
||
| 31 | protected static $humanReadableFormats = [ |
||
| 32 | 'dateSameYear' => 'm-d', |
||
| 33 | 'dateDifferentYear' => 'Y-m-d', |
||
| 34 | 'time' => 'H:i' |
||
| 35 | ]; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string[] $modifyNewKeywords Add new keywords which can be used |
||
| 39 | * with the modify method. The key is the new keyword and the value the |
||
| 40 | * corresponding keyword into DateTime::modify method. |
||
| 41 | */ |
||
| 42 | protected static $modifyNewKeywords = [ |
||
| 43 | 'an' => 'year', |
||
| 44 | 'ans' => 'year', |
||
| 45 | 'mois' => 'month', |
||
| 46 | 'jour' => 'day', |
||
| 47 | 'jours' => 'day', |
||
| 48 | 'heure' => 'hour', |
||
| 49 | 'heures' => 'hour', |
||
| 50 | 'minutes' => 'minute', |
||
| 51 | 'seconde' => 'second', |
||
| 52 | 'secondes' => 'second' |
||
| 53 | ]; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Return the value of the humanReadableI18n property |
||
| 57 | * |
||
| 58 | * @return string[] |
||
| 59 | */ |
||
| 60 | public static function getHumanReadableI18n() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Define a new value for a key of the humanReadableI18n property |
||
| 67 | * |
||
| 68 | * @param string $key The key in humanReadableI18n |
||
| 69 | * @param string $value The new value for the key |
||
| 70 | * |
||
| 71 | * @return void |
||
| 72 | */ |
||
| 73 | public static function setHumanReadableI18nKey($key, $value) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Define a new value to the property humanReadableI18n |
||
| 80 | * |
||
| 81 | * @param string[] $value The new value for the property |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | public static function setHumanReadableI18n($value) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Return the value of the humanReadableFormats property |
||
| 92 | * |
||
| 93 | * @return string[] |
||
| 94 | */ |
||
| 95 | public static function getHumanReadableFormats() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Define a new value for a key of the humanReadableFormats property |
||
| 102 | * |
||
| 103 | * @param string $key The key in humanReadableFormats |
||
| 104 | * @param string $value The new value for the key |
||
| 105 | * |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | public static function setHumanReadableFormatsKey($key, $value) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Define a new value to the property humanReadableFormats |
||
| 115 | * |
||
| 116 | * @param string[] $value The new value for the property |
||
| 117 | * |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | public static function setHumanReadableFormats($value) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Return the value of the modifyNewKeywords property |
||
| 127 | * |
||
| 128 | * @return string[] |
||
| 129 | */ |
||
| 130 | public static function getModifyNewKeywords() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Define a new value to the property modifyNewKeywords |
||
| 137 | * |
||
| 138 | * @param string[] $value The new value for the property |
||
| 139 | * |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | public static function setModifyNewKeywords($value) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Return the date. Format is Y-m-d H:i:sO |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function getDate() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Return a numeric representation of a year, 4 digits. |
||
| 159 | * |
||
| 160 | * @return int |
||
| 161 | */ |
||
| 162 | public function getYear() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return the numeric representation of a month, without leading zeros. |
||
| 169 | * The returned int format can not have leading zeros. |
||
| 170 | * |
||
| 171 | * @return int |
||
| 172 | */ |
||
| 173 | public function getMonth() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Return the day of the month without leading zeros. |
||
| 180 | * The returned int format can not have leading zeros. |
||
| 181 | * |
||
| 182 | * @return int |
||
| 183 | */ |
||
| 184 | public function getDay() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Return 24-hour format without leading zeros. |
||
| 191 | * The returned int format can not have leading zeros. |
||
| 192 | * |
||
| 193 | * @return int |
||
| 194 | */ |
||
| 195 | public function getHour() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Return minutes, without leading zeros. |
||
| 202 | * The returned int format can not have leading zeros. |
||
| 203 | * |
||
| 204 | * @return int |
||
| 205 | */ |
||
| 206 | public function getMinute() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Return second, without leading zeros. |
||
| 213 | * The returned int format can not have leading zeros. |
||
| 214 | * |
||
| 215 | * @return int |
||
| 216 | */ |
||
| 217 | public function getSecond() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Return the difference to Greenwich time (GMT) |
||
| 224 | * with colon between hours and minutes |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public function getZone() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Override modify DateTime method to allow personal keywords |
||
| 235 | * |
||
| 236 | * @param string $modify A date/time string |
||
| 237 | * |
||
| 238 | * @return \BFW\Dates |
||
| 239 | */ |
||
| 240 | public function modify($modify) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get DateTime equivalent keyword for a personal keyword declared into |
||
| 257 | * the property modifyNewKeywords. |
||
| 258 | * |
||
| 259 | * @return \stdClass |
||
| 260 | */ |
||
| 261 | protected function getNewKeywordsForModify() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Use personal keyword on modify method |
||
| 279 | * |
||
| 280 | * @param string $modify A date/time string |
||
| 281 | * |
||
| 282 | * @throws Exception If bad pattern or unknown keyword |
||
| 283 | */ |
||
| 284 | protected function modifyWithOthersKeywords($modify) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Return date's SQL format (postgresql format). |
||
| 314 | * The return can be an array or a string. |
||
| 315 | * |
||
| 316 | * @param boolean $returnArray (default false) True to return an array. |
||
| 317 | * |
||
| 318 | * @return string[]|string |
||
| 319 | */ |
||
| 320 | public function getSqlFormat($returnArray = false) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * List all timezone existing in current php version |
||
| 334 | * |
||
| 335 | * @return string[] |
||
| 336 | */ |
||
| 337 | public function lstTimeZone() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * List all continent define in php DateTimeZone. |
||
| 344 | * |
||
| 345 | * @return string[] |
||
| 346 | */ |
||
| 347 | public function lstTimeZoneContinent() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * List all available country for a continent |
||
| 365 | * |
||
| 366 | * @param string $continent The continent for which we want |
||
| 367 | * the countries list |
||
| 368 | * |
||
| 369 | * @return string[] |
||
| 370 | */ |
||
| 371 | public function lstTimeZonePays($continent) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Transform a date to a human readable format |
||
| 387 | * |
||
| 388 | * @param boolean $returnDateAndTime (default true) True to return date and |
||
| 389 | * time concatenated with a space. False to have only date. |
||
| 390 | * |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | public function humanReadable($returnDateAndTime = true) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Format date to human readable when the date is now |
||
| 426 | * |
||
| 427 | * @param \stdClass $parsedTxt Texts returned by humanReadable method |
||
| 428 | * |
||
| 429 | * @return void |
||
| 430 | */ |
||
| 431 | protected function humanDateNow($parsedTxt) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Format date to human readable when date is today |
||
| 439 | * |
||
| 440 | * @param \stdClass $parsedTxt Texts returned by humanReadable method |
||
| 441 | * @param \DateInterval $diff Interval between now and date to read |
||
| 442 | * |
||
| 443 | * @return void |
||
| 444 | */ |
||
| 445 | protected function humanDateToday($parsedTxt, $diff) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Format date to human readable when date is yesterday |
||
| 466 | * |
||
| 467 | * @param \stdClass $parsedTxt Texts returned by humanReadable method |
||
| 468 | * |
||
| 469 | * @return void |
||
| 470 | */ |
||
| 471 | protected function humanDateYesterday($parsedTxt) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Format date to human readable when date is not now, today or yesterday |
||
| 484 | * |
||
| 485 | * @param \stdClass $parsedTxt Texts returned by humanReadable method |
||
| 486 | * @param \DateTime $current DateTime object for now |
||
| 487 | * |
||
| 488 | * @return void |
||
| 489 | */ |
||
| 490 | protected function humanDateOther($parsedTxt, $current) |
||
| 509 | } |
||
| 510 |
If you suppress an error, we recommend checking for the error condition explicitly: