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 | 'in' => 'in', |
||
| 21 | 'yesterday' => 'yesterday', |
||
| 22 | 'the' => 'the', |
||
| 23 | 'at' => 'at' |
||
| 24 | ]; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string[] $humainReadableFormats Date and time format used in |
||
| 28 | * method to transform date difference to humain readable. |
||
| 29 | */ |
||
| 30 | protected static $humainReadableFormats = [ |
||
| 31 | 'dateSameYear' => 'm-d', |
||
| 32 | 'dateDifferentYear' => 'Y-m-d', |
||
| 33 | 'time' => 'H:i' |
||
| 34 | ]; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Return attribute humainReadableI18n value |
||
| 38 | * |
||
| 39 | * @return string[] |
||
| 40 | */ |
||
| 41 | public static function getHumainReadableI18n() |
||
| 42 | { |
||
| 43 | return self::$humainReadableI18n; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Define new value to a key in attribute humainReadableI18n |
||
| 48 | * |
||
| 49 | * @param string $key The key in humainReadableI18n |
||
| 50 | * @param string $value The new value for key |
||
| 51 | * |
||
| 52 | * @return void |
||
| 53 | */ |
||
| 54 | public static function setHumainReadableI18nKey($key, $value) |
||
| 55 | { |
||
| 56 | self::$humainReadableI18n[$key] = $value; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Define new value to the attribute humainReadableI18n |
||
| 61 | * |
||
| 62 | * @param string[] $value The new value for attribute |
||
| 63 | * |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | public static function setHumainReadableI18n($value) |
||
| 67 | { |
||
| 68 | self::$humainReadableI18n = $value; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Return attribute humainReadableFormats value |
||
| 73 | * |
||
| 74 | * @return string[] |
||
| 75 | */ |
||
| 76 | public static function getHumainReadableFormats() |
||
| 77 | { |
||
| 78 | return self::$humainReadableFormats; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Define new value to a key in attribute humainReadableFormats |
||
| 83 | * |
||
| 84 | * @param string $key The key in humainReadableFormats |
||
| 85 | * @param string $value The new value for key |
||
| 86 | * |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | public static function setHumainReadableFormatsKey($key, $value) |
||
| 90 | { |
||
| 91 | self::$humainReadableFormats[$key] = $value; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Define new value to the attribute humainReadableFormats |
||
| 96 | * |
||
| 97 | * @param string[] $value The new value for attribute |
||
| 98 | * |
||
| 99 | * @return void |
||
| 100 | */ |
||
| 101 | public static function setHumainReadableFormats($value) |
||
| 102 | { |
||
| 103 | self::$humainReadableFormats = $value; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Return the date. Format is Y-m-d H:i:sO |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function getDate() |
||
| 112 | { |
||
| 113 | return parent::format('Y-m-d H:i:sO'); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Return a full numeric representation of a year, 4 digits |
||
| 118 | * |
||
| 119 | * @return int |
||
| 120 | */ |
||
| 121 | public function getYear() |
||
| 122 | { |
||
| 123 | return (int) parent::format('Y'); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Return the numeric representation of a month, with leading zeros |
||
| 128 | * |
||
| 129 | * @return int |
||
| 130 | */ |
||
| 131 | public function getMonth() |
||
| 132 | { |
||
| 133 | return (int) parent::format('m'); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Return the day of the month, 2 digits with leading zeros |
||
| 138 | * |
||
| 139 | * @return int |
||
| 140 | */ |
||
| 141 | public function getDay() |
||
| 142 | { |
||
| 143 | return (int) parent::format('d'); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Return 24-hour format with leading zeros. 2 digits |
||
| 148 | * |
||
| 149 | * @return int |
||
| 150 | */ |
||
| 151 | public function getHour() |
||
| 152 | { |
||
| 153 | return (int) parent::format('H'); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Return minutes, with leading zeros. 2 digits |
||
| 158 | * |
||
| 159 | * @return int |
||
| 160 | */ |
||
| 161 | public function getMinute() |
||
| 162 | { |
||
| 163 | return (int) parent::format('i'); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Return second, with leading zeros. 2 digits |
||
| 168 | * |
||
| 169 | * @return int |
||
| 170 | */ |
||
| 171 | public function getSecond() |
||
| 172 | { |
||
| 173 | return (int) parent::format('s'); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Return the difference to Greenwich time (GMT) |
||
| 178 | * with colon between hours and minutes |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function getZone() |
||
| 183 | { |
||
| 184 | return parent::format('P'); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Override modify DateTime method to allow personnal keyword |
||
| 189 | * |
||
| 190 | * @param string $modify A date/time string |
||
| 191 | * |
||
| 192 | * @return \BFW\Dates |
||
| 193 | */ |
||
| 194 | public function modify($modify) |
||
| 195 | { |
||
| 196 | $dateDepart = clone $this; |
||
| 197 | @parent::modify($modify); //Yeurk, but for personnal pattern, no choice |
||
|
|
|||
| 198 | |||
| 199 | if ($dateDepart != $this) { |
||
| 200 | return $this; |
||
| 201 | } |
||
| 202 | |||
| 203 | $this->modifyOthersKeywords($modify); |
||
| 204 | |||
| 205 | return $this; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get DateTime equivalent keyword for a personal keyword |
||
| 210 | * |
||
| 211 | * @return \stdClass |
||
| 212 | */ |
||
| 213 | protected function getModifyOthersKeywors() |
||
| 214 | { |
||
| 215 | //Liste des possibilités qu'on permet |
||
| 216 | $search = [ |
||
| 217 | 'an', 'ans', |
||
| 218 | 'mois', |
||
| 219 | 'jour', 'jours', |
||
| 220 | 'heure', 'heures', |
||
| 221 | 'minutes', |
||
| 222 | 'seconde', 'secondes' |
||
| 223 | ]; |
||
| 224 | |||
| 225 | //Liste des équivalent pour la fonction modify de DateTime |
||
| 226 | $replace = [ |
||
| 227 | 'year', 'year', |
||
| 228 | 'month', |
||
| 229 | 'day', 'day', |
||
| 230 | 'hour', 'hour', |
||
| 231 | 'minute', |
||
| 232 | 'second', 'second' |
||
| 233 | ]; |
||
| 234 | |||
| 235 | return (object) [ |
||
| 236 | 'search' => $search, |
||
| 237 | 'replace' => $replace |
||
| 238 | ]; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Use personal keyword on modify method |
||
| 243 | * |
||
| 244 | * @param string $modify A date/time string |
||
| 245 | * |
||
| 246 | * @throws Exception If bad pattern or unknown keyword |
||
| 247 | */ |
||
| 248 | protected function modifyOthersKeywords($modify) |
||
| 249 | { |
||
| 250 | $keywords = $this->getModifyOthersKeywors(); |
||
| 251 | $match = []; |
||
| 252 | |||
| 253 | //Regex sur le paramètre pour récupéré le type de modification |
||
| 254 | if (preg_match('#(\+|\-)([0-9]+) ([a-z]+)#i', $modify, $match) !== 1) { |
||
| 255 | throw new Exception('Dates::modify pattern not match.'); |
||
| 256 | } |
||
| 257 | |||
| 258 | $keyword = str_replace( |
||
| 259 | $keywords->search, |
||
| 260 | $keywords->replace, |
||
| 261 | strtolower($match[3]) |
||
| 262 | ); |
||
| 263 | |||
| 264 | $dateDepart = clone $this; |
||
| 265 | //Yeurk, but I preferer sends an Exception, not an error |
||
| 266 | @parent::modify($match[1].$match[2].' '.$keyword); |
||
|
1 ignored issue
–
show
|
|||
| 267 | |||
| 268 | if ($dateDepart == $this) { |
||
| 269 | throw new Exception( |
||
| 270 | 'Dates::modify Parameter '.$match[3].' is unknown.' |
||
| 271 | ); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Return date's SQL format (postgresql format). |
||
| 277 | * The return Must be an array or a string. |
||
| 278 | * |
||
| 279 | * @param boolean $returnArray (default false) True to return an array. |
||
| 280 | * |
||
| 281 | * @return string[]|string |
||
| 282 | */ |
||
| 283 | public function getSqlFormat($returnArray = false) |
||
| 284 | { |
||
| 285 | $date = $this->format('Y-m-d'); |
||
| 286 | $heure = $this->format('H:i:s'); |
||
| 287 | |||
| 288 | if ($returnArray) { |
||
| 289 | return [$date, $heure]; |
||
| 290 | } |
||
| 291 | |||
| 292 | return $date.' '.$heure; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * List all timezone existing in current php version |
||
| 297 | * |
||
| 298 | * @return string[] |
||
| 299 | */ |
||
| 300 | public function lstTimeZone() |
||
| 301 | { |
||
| 302 | return parent::getTimezone()->listIdentifiers(); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * List all continent define in php DateTimeZone. |
||
| 307 | * |
||
| 308 | * @return string[] |
||
| 309 | */ |
||
| 310 | public function lstTimeZoneContinent() |
||
| 311 | { |
||
| 312 | return [ |
||
| 313 | 'africa', |
||
| 314 | 'america', |
||
| 315 | 'antartica', |
||
| 316 | 'arctic', |
||
| 317 | 'asia', |
||
| 318 | 'atlantic', |
||
| 319 | 'australia', |
||
| 320 | 'europe', |
||
| 321 | 'indian', |
||
| 322 | 'pacific' |
||
| 323 | ]; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Liste des pays possible pour un continent donné |
||
| 328 | * |
||
| 329 | * @param string $continent Le continent dans lequel on veux la liste des pays |
||
| 330 | * |
||
| 331 | * @return array La liste des pays pour le continent donné |
||
| 332 | */ |
||
| 333 | |||
| 334 | /** |
||
| 335 | * List all available country for a continent |
||
| 336 | * |
||
| 337 | * @param string $continent |
||
| 338 | * |
||
| 339 | * @return string[] |
||
| 340 | */ |
||
| 341 | public function lstTimeZonePays($continent) |
||
| 342 | { |
||
| 343 | $lst_all = $this->lstTimeZone(); |
||
| 344 | $return = []; |
||
| 345 | |||
| 346 | foreach ($lst_all as $val) { |
||
| 347 | $pos = strpos($val, $continent); |
||
| 348 | |||
| 349 | if ($pos !== false) { |
||
| 350 | $return[] = $val; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | return $return; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Transform a date to a format humain readable |
||
| 359 | * |
||
| 360 | * @param boolean $returnDateAndTime (default true) True to return date and |
||
| 361 | * time concat with a space. False to have only date. |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public function humainReadable($returnDateAndTime = true) |
||
| 366 | { |
||
| 367 | $actual = new Dates; |
||
| 368 | $diff = parent::diff($actual); |
||
| 369 | |||
| 370 | $returnTxt = (object) [ |
||
| 371 | 'date' => '', |
||
| 372 | 'time' => '' |
||
| 373 | ]; |
||
| 374 | |||
| 375 | if ($actual == $this) { |
||
| 376 | //A l'instant |
||
| 377 | $this->humainDateNow($returnTxt); |
||
| 378 | } elseif ($diff->d === 1 && $diff->m === 0 && $diff->y === 0) { |
||
| 379 | //Hier |
||
| 380 | $this->humainDateYesterday($returnTxt); |
||
| 381 | } elseif ($diff->days === 0) { |
||
| 382 | //Aujourd'hui |
||
| 383 | $this->humainDateToday($returnTxt, $diff); |
||
| 384 | } else { |
||
| 385 | $this->humainDateOther($returnTxt, $actual); |
||
| 386 | } |
||
| 387 | |||
| 388 | $txtReturn = $returnTxt->date; |
||
| 389 | if ($returnDateAndTime === true && $returnTxt->time !== '') { |
||
| 390 | $txtReturn .= ' '.$returnTxt->time; |
||
| 391 | } |
||
| 392 | |||
| 393 | return $txtReturn; |
||
| 394 | } |
||
| 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) |
||
| 418 | { |
||
| 419 | $textKey = 'since'; |
||
| 420 | if ($diff->invert === 1) { |
||
| 421 | $textKey = 'in'; |
||
| 422 | } |
||
| 423 | |||
| 424 | $currentClass = get_called_class(); |
||
| 425 | $returnTxt->date = $currentClass::$humainReadableI18n[$textKey].' '; |
||
| 426 | |||
| 427 | if ($diff->h === 0 && $diff->i === 0) { |
||
| 428 | $returnTxt->date .= $diff->s.'s'; |
||
| 429 | } elseif ($diff->h === 0) { |
||
| 430 | $returnTxt->date .= $diff->i.'min'; |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Format date to humain readable when date is yesterday |
||
| 438 | * |
||
| 439 | * @param \stdClas $returnTxt Text returned by humainReadable |
||
| 440 | * |
||
| 441 | * @return void |
||
| 442 | */ |
||
| 443 | protected function humainDateYesterday(&$returnTxt) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Format date to humain readable when date is not now, today or yesterday |
||
| 456 | * |
||
| 457 | * @param \stdClas $returnTxt Text returned by humainReadable |
||
| 458 | * @param \DateTime $actual DateTime object for now |
||
| 459 | * |
||
| 460 | * @return void |
||
| 461 | */ |
||
| 462 | protected function humainDateOther(&$returnTxt, $actual) |
||
| 481 | } |
||
| 482 |
If you suppress an error, we recommend checking for the error condition explicitly: