Complex classes like SugarDateTime 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 SugarDateTime, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class SugarDateTime extends DateTime |
||
| 47 | { |
||
| 48 | // Recognized properties and their formats |
||
| 49 | protected $formats = array( |
||
| 50 | "sec" => "s", |
||
| 51 | "min" => "i", |
||
| 52 | "hour" => "G", |
||
| 53 | "zhour" => "H", |
||
| 54 | "day" => "j", |
||
| 55 | "zday" => "d", |
||
| 56 | "days_in_month" => "t", |
||
| 57 | "day_of_week" => "w", |
||
| 58 | "day_of_year" => "z", |
||
| 59 | "week" => "W", |
||
| 60 | "month" => "n", |
||
| 61 | "zmonth" => "m", |
||
| 62 | "year" => "Y", |
||
| 63 | "am_pm" => "A", |
||
| 64 | "hour_12" => "g", |
||
| 65 | ); |
||
| 66 | |||
| 67 | // Property aliases |
||
| 68 | protected $var_gets = array( |
||
| 69 | "24_hour" => "hour", |
||
| 70 | "day_of_week" => "day_of_week_long", |
||
| 71 | "day_of_week_short" => "day_of_week_short", |
||
| 72 | "month_name" => "month_long", |
||
| 73 | "hour" => "hour_12", |
||
| 74 | ); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var DateTimeZone |
||
| 78 | */ |
||
| 79 | protected static $_gmt; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Calendar strings |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $_strings; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * For testing - if we allowed to use PHP date parse |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | public static $use_php_parser = true; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * For testing - if we allowed to use strptime() |
||
| 95 | * @var bool |
||
| 96 | */ |
||
| 97 | public static $use_strptime = true; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Copy of DateTime::createFromFormat |
||
| 101 | * |
||
| 102 | * Needed to return right type of the object |
||
| 103 | * |
||
| 104 | * @param string $format Format like in date() |
||
| 105 | * @param string $time Time to parse |
||
| 106 | * @param DateTimeZone $timezone |
||
| 107 | * @return SugarDateTime |
||
| 108 | * @see DateTime::createFromFormat |
||
| 109 | */ |
||
| 110 | 117 | public static function createFromFormat($format, $time, $timezone = null) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Internal _createFromFormat implementation for 5.2 |
||
| 136 | * @internal |
||
| 137 | * @param string $format Format like in date() |
||
| 138 | * @param string $time Time string to parse |
||
| 139 | * @param DateTimeZone $timezone TZ |
||
| 140 | * @return SugarDateTime |
||
| 141 | * @see DateTime::createFromFormat |
||
| 142 | */ |
||
| 143 | protected static function _createFromFormat($format, $time, DateTimeZone $timezone = null) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Load language Calendar strings |
||
| 191 | * @internal |
||
| 192 | * @param string $name string section to return |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | protected function _getStrings($name) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Fetch property of the date by name |
||
| 205 | * @param string $var Property name |
||
| 206 | * @return mixed |
||
| 207 | */ |
||
| 208 | 21 | public function __get($var) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Implement some get_ methods that fetch variables |
||
| 242 | * |
||
| 243 | * @param string $name |
||
| 244 | * @param array $args |
||
| 245 | * @return mixed |
||
| 246 | */ |
||
| 247 | public function __call($name, $args) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get specific hour of today |
||
| 279 | * @param int $hour_index |
||
| 280 | * @return SugarDateTime |
||
| 281 | */ |
||
| 282 | public function get_datetime_by_index_today($hour_index) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get the last second of current hour |
||
| 296 | * @return SugarDateTime |
||
| 297 | */ |
||
| 298 | function get_hour_end_time() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Get the last second of the current day |
||
| 307 | * @return SugarDateTime |
||
| 308 | */ |
||
| 309 | function get_day_end_time() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get the beginning of i's day of the week |
||
| 317 | * @param int $day_index Day, 0 is Sunday, 1 is Monday, etc. |
||
| 318 | * @return SugarDateTime |
||
| 319 | */ |
||
| 320 | function get_day_by_index_this_week($day_index) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get the beginning of the last day of i's the month |
||
| 330 | * @deprecated |
||
| 331 | * FIXME: no idea why this function exists and what's the use of it |
||
| 332 | * @param int $month_index Month, January is 0 |
||
| 333 | * @return SugarDateTime |
||
| 334 | */ |
||
| 335 | function get_day_by_index_this_year($month_index) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get the beginning of i's day of the month |
||
| 346 | * @param int $day_index 0 is the first day of the month (sic!) |
||
| 347 | * @return SugarDateTime |
||
| 348 | */ |
||
| 349 | function get_day_by_index_this_month($day_index) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get new date, modified by date expression |
||
| 357 | * |
||
| 358 | * @example $yesterday = $today->get("yesterday"); |
||
| 359 | * |
||
| 360 | * @param string $expression |
||
| 361 | * @return SugarDateTime |
||
| 362 | */ |
||
| 363 | 12 | function get($expression) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Create from ISO 8601 datetime |
||
| 372 | * @param string $str |
||
| 373 | * @return SugarDateTime |
||
| 374 | */ |
||
| 375 | static public function parse_utc_date_time($str) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Create a list of time slots for calendar view |
||
| 382 | * Times must be in user TZ |
||
| 383 | * @param string $view Which view we are using - day, week, month |
||
| 384 | * @param SugarDateTime $start_time Start time |
||
| 385 | * @param SugarDateTime $end_time End time |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | static function getHashList($view, $start_time, $end_time) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Get the beginning of the given day |
||
| 420 | * @param int $day Day, starting with 1, default is current |
||
| 421 | * @param int $month Month, starting with 1, default is current |
||
| 422 | * @param int $year Year, default is current |
||
| 423 | * @return SugarDateTime |
||
| 424 | */ |
||
| 425 | 1 | function get_day_begin($day = null, $month = null, $year = null) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Get the last second of the given day |
||
| 438 | * @param int $day Day, starting with 1, default is current |
||
| 439 | * @param int $month Month, starting with 1, default is current |
||
| 440 | * @param int $year Year, default is current |
||
| 441 | * @return SugarDateTime |
||
| 442 | */ |
||
| 443 | function get_day_end($day = null, $month = null, $year = null) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Get the beginning of the first day of the year |
||
| 456 | * @param int $year |
||
| 457 | * @return SugarDateTime |
||
| 458 | */ |
||
| 459 | function get_year_begin($year) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Print datetime in standard DB format |
||
| 469 | * |
||
| 470 | * Set $tz parameter to false if you are sure that the date is in UTC. |
||
| 471 | * |
||
| 472 | * @param bool $tz do conversion to UTC |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | 20 | function asDb($tz = true) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Print date in standard DB format |
||
| 488 | * |
||
| 489 | * Set $tz parameter to false if you are sure that the date is in UTC. |
||
| 490 | * |
||
| 491 | * @param bool $tz do conversion to UTC |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | function asDbDate($tz = true) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Get query string for the date, year=%d&month=%d&day=%d&hour=%d |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | function get_date_str() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Convert date to string - 'r' format, like: Thu, 21 Dec 2000 16:01:07 +0200 |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | function __toString() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Match between tm_ parts and date() format strings |
||
| 525 | * @var array |
||
| 526 | */ |
||
| 527 | protected static $parts_match = array( |
||
| 528 | 'Y' => 'tm_year', |
||
| 529 | 'm' => 'tm_mon', |
||
| 530 | 'n' => 'tm_mon', |
||
| 531 | 'd' => 'tm_mday', |
||
| 532 | 'H' => 'tm_hour', |
||
| 533 | 'h' => 'tm_hour', |
||
| 534 | 'i' => 'tm_min', |
||
| 535 | 's' => 'tm_sec', |
||
| 536 | ); |
||
| 537 | |||
| 538 | protected static $data_init = array( |
||
| 539 | "tm_hour" => 0, |
||
| 540 | "tm_min" => 0, |
||
| 541 | "tm_sec" => 0, |
||
| 542 | ); |
||
| 543 | |||
| 544 | protected static $strptime_short_mon, $strptime_long_mon; |
||
| 545 | /** |
||
| 546 | * DateTime homebrew parser |
||
| 547 | * |
||
| 548 | * Since some OSes and PHP versions (please upgrade to 5.3!) do not support built-in parsing functions, |
||
| 549 | * we have to restort to this ugliness. |
||
| 550 | * @internal |
||
| 551 | * @param string $time Time formatted string |
||
| 552 | * @param string $format Format, as accepted by strptime() |
||
| 553 | * @return array Parsed parts |
||
| 554 | */ |
||
| 555 | protected function _strptime($time, $format) |
||
| 626 | |||
| 627 | // 5.2 compatibility - 5.2 functions don't return $this, let's help them |
||
| 628 | |||
| 629 | /** |
||
| 630 | * (non-PHPdoc) |
||
| 631 | * @see DateTime::setDate() |
||
| 632 | * @param $year |
||
| 633 | * @param $month |
||
| 634 | * @param $day |
||
| 635 | * @return SugarDateTime |
||
| 636 | */ |
||
| 637 | 2 | public function setDate ($year, $month, $day) |
|
| 642 | |||
| 643 | /** |
||
| 644 | * (non-PHPdoc) |
||
| 645 | * @see DateTime::setTime() |
||
| 646 | * @param $hour |
||
| 647 | * @param $minute |
||
| 648 | * @param int $sec |
||
| 649 | * @return SugarDateTime |
||
| 650 | */ |
||
| 651 | 26 | public function setTime($hour, $minute, $sec = 0) |
|
| 656 | |||
| 657 | /** |
||
| 658 | * (non-PHPdoc) |
||
| 659 | * @see DateTime::modify() |
||
| 660 | * @param $modify |
||
| 661 | * @return SugarDateTime |
||
| 662 | */ |
||
| 663 | 70 | public function modify($modify) |
|
| 679 | |||
| 680 | /** |
||
| 681 | * (non-PHPdoc) |
||
| 682 | * @see DateTime::setTimezone() |
||
| 683 | * @param DateTimeZone $timezone |
||
| 684 | * @return SugarDateTime |
||
| 685 | */ |
||
| 686 | 188 | public function setTimezone ($timezone) |
|
| 691 | |||
| 692 | } |
||
| 693 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.