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 EE_Datetime_Field 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 EE_Datetime_Field, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class EE_Datetime_Field extends EE_Model_Field_Base |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The pattern we're looking for is if only the characters 0-9 are found and there are only |
||
| 20 | * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
||
| 21 | * |
||
| 22 | * @type string unix_timestamp_regex |
||
| 23 | */ |
||
| 24 | const unix_timestamp_regex = '/[0-9]{10,}/'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @type string mysql_timestamp_format |
||
| 28 | */ |
||
| 29 | const mysql_timestamp_format = 'Y-m-d H:i:s'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @type string mysql_date_format |
||
| 33 | */ |
||
| 34 | const mysql_date_format = 'Y-m-d'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @type string mysql_time_format |
||
| 38 | */ |
||
| 39 | const mysql_time_format = 'H:i:s'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Const for using in the default value. If the field's default is set to this, |
||
| 43 | * then we will return the time of calling `get_default_value()`, not |
||
| 44 | * just the current time at construction |
||
| 45 | */ |
||
| 46 | const now = 'now'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The following properties hold the default formats for date and time. |
||
| 50 | * Defaults are set via the constructor and can be overridden on class instantiation. |
||
| 51 | * However they can also be overridden later by the set_format() method |
||
| 52 | * (and corresponding set_date_format, set_time_format methods); |
||
| 53 | */ |
||
| 54 | /** |
||
| 55 | * @type string $_date_format |
||
| 56 | */ |
||
| 57 | protected $_date_format = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @type string $_time_format |
||
| 61 | */ |
||
| 62 | protected $_time_format = ''; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @type string $_pretty_date_format |
||
| 66 | */ |
||
| 67 | protected $_pretty_date_format = ''; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @type string $_pretty_time_format |
||
| 71 | */ |
||
| 72 | protected $_pretty_time_format = ''; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @type DateTimeZone $_DateTimeZone |
||
| 76 | */ |
||
| 77 | protected $_DateTimeZone; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @type DateTimeZone $_UTC_DateTimeZone |
||
| 81 | */ |
||
| 82 | protected $_UTC_DateTimeZone; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @type DateTimeZone $_blog_DateTimeZone |
||
| 86 | */ |
||
| 87 | protected $_blog_DateTimeZone; |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * This property holds how we want the output returned when getting a datetime string. It is set for the |
||
| 92 | * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
||
| 93 | * and time returned via getters. |
||
| 94 | * |
||
| 95 | * @var mixed (null|string) |
||
| 96 | */ |
||
| 97 | protected $_date_time_output; |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * timezone string |
||
| 102 | * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
||
| 103 | * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
||
| 104 | * coming out of the object to be in. Default timezone is the current WP timezone option setting |
||
| 105 | * |
||
| 106 | * @var string |
||
| 107 | */ |
||
| 108 | protected $_timezone_string; |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
||
| 113 | * offsets for comparison purposes). |
||
| 114 | * |
||
| 115 | * @var int |
||
| 116 | */ |
||
| 117 | protected $_blog_offset; |
||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $table_column |
||
| 123 | * @param string $nice_name |
||
| 124 | * @param bool $nullable |
||
| 125 | * @param string $default_value |
||
| 126 | * @param string $timezone_string |
||
| 127 | * @param string $date_format |
||
| 128 | * @param string $time_format |
||
| 129 | * @param string $pretty_date_format |
||
| 130 | * @param string $pretty_time_format |
||
| 131 | * @throws EE_Error |
||
| 132 | * @throws InvalidArgumentException |
||
| 133 | */ |
||
| 134 | public function __construct( |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * @return DateTimeZone |
||
| 159 | * @throws \EE_Error |
||
| 160 | */ |
||
| 161 | public function get_UTC_DateTimeZone() |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * @return DateTimeZone |
||
| 171 | * @throws \EE_Error |
||
| 172 | */ |
||
| 173 | public function get_blog_DateTimeZone() |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * this prepares any incoming date data and make sure its converted to a utc unix timestamp |
||
| 183 | * |
||
| 184 | * @param string|int $value_inputted_for_field_on_model_object could be a string formatted date time or int unix |
||
| 185 | * timestamp |
||
| 186 | * @return DateTime |
||
| 187 | */ |
||
| 188 | public function prepare_for_set($value_inputted_for_field_on_model_object) |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
||
| 196 | * getters need to know whether we're just returning the date or the time or both. By default we return both. |
||
| 197 | * |
||
| 198 | * @param bool $pretty If we're returning the pretty formats or standard format string. |
||
| 199 | * @return string The final assembled format string. |
||
| 200 | */ |
||
| 201 | protected function _get_date_time_output($pretty = false) |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
||
| 223 | * returned (using the format properties) |
||
| 224 | * |
||
| 225 | * @param string $what acceptable values are 'time' or 'date'. |
||
| 226 | * Any other value will be set but will always result |
||
| 227 | * in both 'date' and 'time' being returned. |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | public function set_date_time_output($what = null) |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
||
| 238 | * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
||
| 239 | * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
||
| 240 | * We also set some other properties in this method. |
||
| 241 | * |
||
| 242 | * @param string $timezone_string A valid timezone string as described by @link |
||
| 243 | * http://www.php.net/manual/en/timezones.php |
||
| 244 | * @return void |
||
| 245 | * @throws InvalidArgumentException |
||
| 246 | * @throws InvalidDataTypeException |
||
| 247 | * @throws InvalidInterfaceException |
||
| 248 | */ |
||
| 249 | public function set_timezone($timezone_string) |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * _create_timezone_object_from_timezone_name |
||
| 264 | * |
||
| 265 | * @access protected |
||
| 266 | * @param string $timezone_string |
||
| 267 | * @return \DateTimeZone |
||
| 268 | * @throws InvalidArgumentException |
||
| 269 | * @throws InvalidDataTypeException |
||
| 270 | * @throws InvalidInterfaceException |
||
| 271 | */ |
||
| 272 | protected function _create_timezone_object_from_timezone_string($timezone_string = '') |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * This just returns whatever is set for the current timezone. |
||
| 280 | * |
||
| 281 | * @access public |
||
| 282 | * @return string timezone string |
||
| 283 | */ |
||
| 284 | public function get_timezone() |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * set the $_date_format property |
||
| 292 | * |
||
| 293 | * @access public |
||
| 294 | * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
||
| 295 | * @param bool $pretty Whether to set pretty format or not. |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | public function set_date_format($format, $pretty = false) |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * return the $_date_format property value. |
||
| 310 | * |
||
| 311 | * @param bool $pretty Whether to get pretty format or not. |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function get_date_format($pretty = false) |
||
| 318 | |||
| 319 | |||
| 320 | /** |
||
| 321 | * set the $_time_format property |
||
| 322 | * |
||
| 323 | * @access public |
||
| 324 | * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
||
| 325 | * @param bool $pretty Whether to set pretty format or not. |
||
| 326 | * @return void |
||
| 327 | */ |
||
| 328 | public function set_time_format($format, $pretty = false) |
||
| 336 | |||
| 337 | |||
| 338 | /** |
||
| 339 | * return the $_time_format property value. |
||
| 340 | * |
||
| 341 | * @param bool $pretty Whether to get pretty format or not. |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | public function get_time_format($pretty = false) |
||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * set the $_pretty_date_format property |
||
| 352 | * |
||
| 353 | * @access public |
||
| 354 | * @param string $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
||
| 355 | * @return void |
||
| 356 | */ |
||
| 357 | public function set_pretty_date_format($format) |
||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * set the $_pretty_time_format property |
||
| 365 | * |
||
| 366 | * @access public |
||
| 367 | * @param string $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
||
| 368 | * @return void |
||
| 369 | */ |
||
| 370 | public function set_pretty_time_format($format) |
||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * Only sets the time portion of the datetime. |
||
| 378 | * |
||
| 379 | * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
||
| 380 | * @param DateTime $current current DateTime object for the datetime field |
||
| 381 | * @return DateTime |
||
| 382 | */ |
||
| 383 | View Code Duplication | public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current) |
|
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * Only sets the date portion of the datetime. |
||
| 404 | * |
||
| 405 | * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
||
| 406 | * @param DateTime $current current DateTime object for the datetime field |
||
| 407 | * @return DateTime |
||
| 408 | */ |
||
| 409 | View Code Duplication | public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current) |
|
| 426 | |||
| 427 | |||
| 428 | /** |
||
| 429 | * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
||
| 430 | * datetime gets to this stage it should ALREADY be in UTC time |
||
| 431 | * |
||
| 432 | * @param DateTime $DateTime |
||
| 433 | * @return string formatted date time for given timezone |
||
| 434 | * @throws \EE_Error |
||
| 435 | */ |
||
| 436 | public function prepare_for_get($DateTime) |
||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
||
| 444 | * from the set wp timezone. If so, then it returns the datetime string formatted via |
||
| 445 | * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
||
| 446 | * abbreviation to the date_string. |
||
| 447 | * |
||
| 448 | * @param mixed $DateTime |
||
| 449 | * @param null $schema |
||
| 450 | * @return string |
||
| 451 | * @throws \EE_Error |
||
| 452 | */ |
||
| 453 | public function prepare_for_pretty_echoing($DateTime, $schema = null) |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
||
| 461 | * timezone). |
||
| 462 | * |
||
| 463 | * @param DateTime $DateTime |
||
| 464 | * @param bool|string $schema |
||
| 465 | * @return string |
||
| 466 | * @throws \EE_Error |
||
| 467 | */ |
||
| 468 | protected function _prepare_for_display($DateTime, $schema = false) |
||
| 516 | |||
| 517 | |||
| 518 | /** |
||
| 519 | * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
||
| 520 | * timezone). |
||
| 521 | * |
||
| 522 | * @param mixed $datetime_value u |
||
| 523 | * @return string mysql timestamp in UTC |
||
| 524 | * @throws \EE_Error |
||
| 525 | */ |
||
| 526 | public function prepare_for_use_in_db($datetime_value) |
||
| 556 | |||
| 557 | |||
| 558 | /** |
||
| 559 | * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
||
| 560 | * allowed) |
||
| 561 | * |
||
| 562 | * @param string $datetime_string mysql timestamp in UTC |
||
| 563 | * @return mixed null | DateTime |
||
| 564 | * @throws \EE_Error |
||
| 565 | */ |
||
| 566 | public function prepare_for_set_from_db($datetime_string) |
||
| 597 | |||
| 598 | |||
| 599 | /** |
||
| 600 | * All this method does is determine if we're going to display the timezone string or not on any output. |
||
| 601 | * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
||
| 602 | * If so, then true. |
||
| 603 | * |
||
| 604 | * @return bool true for yes false for no |
||
| 605 | * @throws \EE_Error |
||
| 606 | */ |
||
| 607 | protected function _display_timezone() |
||
| 622 | |||
| 623 | |||
| 624 | /** |
||
| 625 | * This method returns a php DateTime object for setting on the EE_Base_Class model. |
||
| 626 | * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
||
| 627 | * with. |
||
| 628 | * |
||
| 629 | * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
||
| 630 | * in the format that is set on the date_field (or DateTime |
||
| 631 | * object)! |
||
| 632 | * @return DateTime |
||
| 633 | */ |
||
| 634 | protected function _get_date_object($date_string) |
||
| 692 | |||
| 693 | |||
| 694 | |||
| 695 | /** |
||
| 696 | * get_timezone_transitions |
||
| 697 | * |
||
| 698 | * @param \DateTimeZone $DateTimeZone |
||
| 699 | * @param int $time |
||
| 700 | * @param bool $first_only |
||
| 701 | * @return mixed |
||
| 702 | */ |
||
| 703 | public function get_timezone_transitions(DateTimeZone $DateTimeZone, $time = null, $first_only = true) |
||
| 707 | |||
| 708 | |||
| 709 | |||
| 710 | /** |
||
| 711 | * get_timezone_offset |
||
| 712 | * |
||
| 713 | * @param \DateTimeZone $DateTimeZone |
||
| 714 | * @param int $time |
||
| 715 | * @return mixed |
||
| 716 | * @throws \DomainException |
||
| 717 | */ |
||
| 718 | public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
||
| 722 | |||
| 723 | |||
| 724 | /** |
||
| 725 | * This will take an incoming timezone string and return the abbreviation for that timezone |
||
| 726 | * |
||
| 727 | * @param string $timezone_string |
||
| 728 | * @return string abbreviation |
||
| 729 | * @throws \EE_Error |
||
| 730 | */ |
||
| 731 | public function get_timezone_abbrev($timezone_string) |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Overrides the parent to allow for having a dynamic "now" value |
||
| 741 | * |
||
| 742 | * @return mixed |
||
| 743 | */ |
||
| 744 | public function get_default_value() |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Gets the default datetime object from the field's default time |
||
| 755 | * @since 4.9.66.p |
||
| 756 | * @return DbSafeDateTime|null |
||
| 757 | * @throws InvalidArgumentException |
||
| 758 | * @throws InvalidDataTypeException |
||
| 759 | * @throws InvalidInterfaceException |
||
| 760 | */ |
||
| 761 | public function getDefaultDateTimeObj() |
||
| 775 | |||
| 776 | public function getSchemaDescription() |
||
| 783 | } |
||
| 784 |
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.