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 |
||
| 11 | class Dates extends DateTime |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var string[] $humainReadableI18n Words used in method to transform |
||
| 15 | * date difference to humain readable. |
||
| 16 | */ |
||
| 17 | protected static $humainReadableI18n = [ |
||
| 18 | 'now' => 'now', |
||
| 19 | 'since' => 'since', |
||
| 20 | 'yesterday' => 'yesterday', |
||
| 21 | 'the' => 'the', |
||
| 22 | 'at' => 'at' |
||
| 23 | ]; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string[] $humainReadableFormats Date and time format used in |
||
| 27 | * method to transform date difference to humain readable. |
||
| 28 | */ |
||
| 29 | protected static $humainReadableFormats = [ |
||
| 30 | 'dateSameYear' => 'm-d', |
||
| 31 | 'dateDifferentYear' => 'Y-m-d', |
||
| 32 | 'time' => 'H:i' |
||
| 33 | ]; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Return attribute humainReadableI18n value |
||
| 37 | * |
||
| 38 | * @return string[] |
||
| 39 | */ |
||
| 40 | public static function getHumainReadableI18n() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Define new value to a key in attribute humainReadableI18n |
||
| 47 | * |
||
| 48 | * @param string $key The key in humainReadableI18n |
||
| 49 | * @param string $value The new value for key |
||
| 50 | * |
||
| 51 | * @return void |
||
| 52 | */ |
||
| 53 | public static function setHumainReadableI18nKey($key, $value) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Define new value to the attribute humainReadableI18n |
||
| 60 | * |
||
| 61 | * @param string[] $value The new value for attribute |
||
| 62 | * |
||
| 63 | * @return void |
||
| 64 | */ |
||
| 65 | public static function setHumainReadableI18n($value) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Return attribute humainReadableFormats value |
||
| 72 | * |
||
| 73 | * @return string[] |
||
| 74 | */ |
||
| 75 | public static function getHumainReadableFormats() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Define new value to a key in attribute humainReadableFormats |
||
| 82 | * |
||
| 83 | * @param string $key The key in humainReadableFormats |
||
| 84 | * @param string $value The new value for key |
||
| 85 | * |
||
| 86 | * @return void |
||
| 87 | */ |
||
| 88 | public static function setHumainReadableFormatsKey($key, $value) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Define new value to the attribute humainReadableFormats |
||
| 95 | * |
||
| 96 | * @param string[] $value The new value for attribute |
||
| 97 | * |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | public static function setHumainReadableFormats($value) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Return the date. Format is Y-m-d H:i:sO |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function getDate() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Return a full numeric representation of a year, 4 digits |
||
| 117 | * |
||
| 118 | * @return int |
||
| 119 | */ |
||
| 120 | public function getYear() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Return the numeric representation of a month, with leading zeros |
||
| 127 | * |
||
| 128 | * @return int |
||
| 129 | */ |
||
| 130 | public function getMonth() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Return the day of the month, 2 digits with leading zeros |
||
| 137 | * |
||
| 138 | * @return int |
||
| 139 | */ |
||
| 140 | public function getDay() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Return 24-hour format with leading zeros. 2 digits |
||
| 147 | * |
||
| 148 | * @return int |
||
| 149 | */ |
||
| 150 | public function getHour() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Return minutes, with leading zeros. 2 digits |
||
| 157 | * |
||
| 158 | * @return int |
||
| 159 | */ |
||
| 160 | public function getMinute() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Return second, with leading zeros. 2 digits |
||
| 167 | * |
||
| 168 | * @return int |
||
| 169 | */ |
||
| 170 | public function getSecond() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Return the difference to Greenwich time (GMT) |
||
| 177 | * with colon between hours and minutes |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function getZone() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Override modify DateTime method to allow personnal keyword |
||
| 188 | * |
||
| 189 | * @param string $modify A date/time string |
||
| 190 | * |
||
| 191 | * @return \BFW\Dates |
||
| 192 | */ |
||
| 193 | public function modify($modify) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Get DateTime equivalent keyword for a personal keyword |
||
| 209 | * |
||
| 210 | * @return \stdClass |
||
| 211 | */ |
||
| 212 | protected function getModifyOthersKeywors() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Use personal keyword on modify method |
||
| 242 | * |
||
| 243 | * @param string $modify A date/time string |
||
| 244 | * |
||
| 245 | * @throws Exception If bad pattern or unknown keyword |
||
| 246 | */ |
||
| 247 | protected function modifyOthersKeywords($modify) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Return date's SQL format (postgresql format). |
||
| 276 | * The return Must be an array or a string. |
||
| 277 | * |
||
| 278 | * @param boolean $returnArray (default false) True to return an array. |
||
| 279 | * |
||
| 280 | * @return string[]|string |
||
| 281 | */ |
||
| 282 | public function getSqlFormat($returnArray = false) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * List all timezone existing in current php version |
||
| 296 | * |
||
| 297 | * @return string[] |
||
| 298 | */ |
||
| 299 | public function lstTimeZone() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * List all continent define in php DateTimeZone. |
||
| 306 | * |
||
| 307 | * @return string[] |
||
| 308 | */ |
||
| 309 | public function lstTimeZoneContinent() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Liste des pays possible pour un continent donné |
||
| 327 | * |
||
| 328 | * @param string $continent Le continent dans lequel on veux la liste des pays |
||
| 329 | * |
||
| 330 | * @return array La liste des pays pour le continent donné |
||
| 331 | */ |
||
| 332 | |||
| 333 | /** |
||
| 334 | * List all available country for a continent |
||
| 335 | * |
||
| 336 | * @param string $continent |
||
| 337 | * |
||
| 338 | * @return string[] |
||
| 339 | */ |
||
| 340 | public function lstTimeZonePays($continent) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Transform a date to a format humain readable |
||
| 358 | * |
||
| 359 | * @param boolean $returnDateAndTime (default true) True to return date and |
||
| 360 | * time concat with a space. False to have only date. |
||
| 361 | * @param boolean $toLower (default false) True to have return a lower text |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public function humainReadable($returnDateAndTime = true) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Format date to humain readable when date is now |
||
| 398 | * |
||
| 399 | * @param \stdClas $returnTxt Text returned by humainReadable |
||
| 400 | * |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | protected function humainDateNow(&$returnTxt) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Format date to humain readable when date is today |
||
| 411 | * |
||
| 412 | * @param \stdClas $returnTxt Text returned by humainReadable |
||
| 413 | * @param \DateInterval $diff Interval between now and date to read |
||
| 414 | * |
||
| 415 | * @return void |
||
| 416 | */ |
||
| 417 | protected function humainDateToday(&$returnTxt, $diff) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Format date to humain readable when date is yesterday |
||
| 433 | * |
||
| 434 | * @param \stdClas $returnTxt Text returned by humainReadable |
||
| 435 | * |
||
| 436 | * @return void |
||
| 437 | */ |
||
| 438 | protected function humainDateYesterday(&$returnTxt) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Format date to humain readable when date is not now, today or yesterday |
||
| 451 | * |
||
| 452 | * @param \stdClas $returnTxt Text returned by humainReadable |
||
| 453 | * @param \DateTime $actual DateTime object for now |
||
| 454 | * |
||
| 455 | * @return void |
||
| 456 | */ |
||
| 457 | protected function humainDateOther(&$returnTxt, $actual) |
||
| 476 | } |
||
| 477 |
If you suppress an error, we recommend checking for the error condition explicitly: