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 use EventEspresso\core\domain\entities\DbSafeDateTime; |
||
| 16 | class EE_Datetime_Field extends EE_Model_Field_Base |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The pattern we're looking for is if only the characters 0-9 are found and there are only |
||
| 21 | * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
||
| 22 | * |
||
| 23 | * @type string unix_timestamp_regex |
||
| 24 | */ |
||
| 25 | const unix_timestamp_regex = '/[0-9]{10,}/'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @type string mysql_timestamp_format |
||
| 29 | */ |
||
| 30 | const mysql_timestamp_format = 'Y-m-d H:i:s'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @type string mysql_date_format |
||
| 34 | */ |
||
| 35 | const mysql_date_format = 'Y-m-d'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @type string mysql_time_format |
||
| 39 | */ |
||
| 40 | const mysql_time_format = 'H:i:s'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Const for using in the default value. If the field's default is set to this, |
||
| 44 | * then we will return the time of calling `get_default_value()`, not |
||
| 45 | * just the current time at construction |
||
| 46 | */ |
||
| 47 | const now = 'now'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The following properties hold the default formats for date and time. |
||
| 51 | * Defaults are set via the constructor and can be overridden on class instantiation. |
||
| 52 | * However they can also be overridden later by the set_format() method |
||
| 53 | * (and corresponding set_date_format, set_time_format methods); |
||
| 54 | */ |
||
| 55 | /** |
||
| 56 | * @type string $_date_format |
||
| 57 | */ |
||
| 58 | protected $_date_format = ''; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @type string $_time_format |
||
| 62 | */ |
||
| 63 | protected $_time_format = ''; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @type string $_pretty_date_format |
||
| 67 | */ |
||
| 68 | protected $_pretty_date_format = ''; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @type string $_pretty_time_format |
||
| 72 | */ |
||
| 73 | protected $_pretty_time_format = ''; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @type DateTimeZone $_DateTimeZone |
||
| 77 | */ |
||
| 78 | protected $_DateTimeZone; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @type DateTimeZone $_UTC_DateTimeZone |
||
| 82 | */ |
||
| 83 | protected $_UTC_DateTimeZone; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @type DateTimeZone $_blog_DateTimeZone |
||
| 87 | */ |
||
| 88 | protected $_blog_DateTimeZone; |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * This property holds how we want the output returned when getting a datetime string. It is set for the |
||
| 93 | * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
||
| 94 | * and time returned via getters. |
||
| 95 | * |
||
| 96 | * @var mixed (null|string) |
||
| 97 | */ |
||
| 98 | protected $_date_time_output; |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * timezone string |
||
| 103 | * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
||
| 104 | * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
||
| 105 | * coming out of the object to be in. Default timezone is the current WP timezone option setting |
||
| 106 | * |
||
| 107 | * @var string |
||
| 108 | */ |
||
| 109 | protected $_timezone_string; |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
||
| 114 | * offsets for comparison purposes). |
||
| 115 | * |
||
| 116 | * @var int |
||
| 117 | */ |
||
| 118 | protected $_blog_offset; |
||
| 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 | */ |
||
| 133 | public function __construct( |
||
| 134 | $table_column, |
||
| 135 | $nice_name, |
||
| 136 | $nullable, |
||
| 137 | $default_value, |
||
| 138 | $timezone_string = '', |
||
| 139 | $date_format = '', |
||
| 140 | $time_format = '', |
||
| 141 | $pretty_date_format = '', |
||
| 142 | $pretty_time_format = '' |
||
| 143 | ) { |
||
| 144 | |||
| 145 | $this->_date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
||
| 146 | $this->_time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
||
| 147 | $this->_pretty_date_format = ! empty($pretty_date_format) ? $pretty_date_format : get_option('date_format'); |
||
| 148 | $this->_pretty_time_format = ! empty($pretty_time_format) ? $pretty_time_format : get_option('time_format'); |
||
| 149 | |||
| 150 | parent::__construct($table_column, $nice_name, $nullable, $default_value); |
||
| 151 | $this->set_timezone($timezone_string); |
||
| 152 | |||
| 153 | } |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public function get_wpdb_data_type() |
||
| 160 | { |
||
| 161 | return '%s'; |
||
| 162 | } |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * @return DateTimeZone |
||
| 167 | * @throws \EE_Error |
||
| 168 | */ |
||
| 169 | public function get_UTC_DateTimeZone() |
||
| 170 | { |
||
| 171 | return $this->_UTC_DateTimeZone instanceof DateTimeZone |
||
| 172 | ? $this->_UTC_DateTimeZone |
||
| 173 | : $this->_create_timezone_object_from_timezone_string('UTC'); |
||
| 174 | } |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * @return DateTimeZone |
||
| 179 | * @throws \EE_Error |
||
| 180 | */ |
||
| 181 | public function get_blog_DateTimeZone() |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * this prepares any incoming date data and make sure its converted to a utc unix timestamp |
||
| 191 | * |
||
| 192 | * @param string|int $value_inputted_for_field_on_model_object could be a string formatted date time or int unix |
||
| 193 | * timestamp |
||
| 194 | * @return DateTime |
||
| 195 | */ |
||
| 196 | public function prepare_for_set($value_inputted_for_field_on_model_object) |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
||
| 204 | * getters need to know whether we're just returning the date or the time or both. By default we return both. |
||
| 205 | * |
||
| 206 | * @param bool $pretty If we're returning the pretty formats or standard format string. |
||
| 207 | * @return string The final assembled format string. |
||
| 208 | */ |
||
| 209 | protected function _get_date_time_output($pretty = false) |
||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
||
| 231 | * returned (using the format properties) |
||
| 232 | * |
||
| 233 | * @param string $what acceptable values are 'time' or 'date'. |
||
| 234 | * Any other value will be set but will always result |
||
| 235 | * in both 'date' and 'time' being returned. |
||
| 236 | * @return void |
||
| 237 | */ |
||
| 238 | public function set_date_time_output($what = null) |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
||
| 246 | * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
||
| 247 | * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
||
| 248 | * We also set some other properties in this method. |
||
| 249 | * |
||
| 250 | * @param string $timezone_string A valid timezone string as described by @link |
||
| 251 | * http://www.php.net/manual/en/timezones.php |
||
| 252 | * @return void |
||
| 253 | * @throws \EE_Error |
||
| 254 | */ |
||
| 255 | public function set_timezone($timezone_string) |
||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * _create_timezone_object_from_timezone_name |
||
| 270 | * |
||
| 271 | * @access protected |
||
| 272 | * @param string $timezone_string |
||
| 273 | * @return \DateTimeZone |
||
| 274 | * @throws \EE_Error |
||
| 275 | */ |
||
| 276 | protected function _create_timezone_object_from_timezone_string($timezone_string = '') |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * This just returns whatever is set for the current timezone. |
||
| 284 | * |
||
| 285 | * @access public |
||
| 286 | * @return string timezone string |
||
| 287 | */ |
||
| 288 | public function get_timezone() |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * set the $_date_format property |
||
| 296 | * |
||
| 297 | * @access public |
||
| 298 | * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
||
| 299 | * @param bool $pretty Whether to set pretty format or not. |
||
| 300 | * @return void |
||
| 301 | */ |
||
| 302 | public function set_date_format($format, $pretty = false) |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * return the $_date_format property value. |
||
| 314 | * |
||
| 315 | * @param bool $pretty Whether to get pretty format or not. |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public function get_date_format($pretty = false) |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * set the $_time_format property |
||
| 326 | * |
||
| 327 | * @access public |
||
| 328 | * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
||
| 329 | * @param bool $pretty Whether to set pretty format or not. |
||
| 330 | * @return void |
||
| 331 | */ |
||
| 332 | public function set_time_format($format, $pretty = false) |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * return the $_time_format property value. |
||
| 344 | * |
||
| 345 | * @param bool $pretty Whether to get pretty format or not. |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | public function get_time_format($pretty = false) |
||
| 352 | |||
| 353 | |||
| 354 | /** |
||
| 355 | * set the $_pretty_date_format property |
||
| 356 | * |
||
| 357 | * @access public |
||
| 358 | * @param string $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
||
| 359 | * @return void |
||
| 360 | */ |
||
| 361 | public function set_pretty_date_format($format) |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * set the $_pretty_time_format property |
||
| 369 | * |
||
| 370 | * @access public |
||
| 371 | * @param string $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
||
| 372 | * @return void |
||
| 373 | */ |
||
| 374 | public function set_pretty_time_format($format) |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * Only sets the time portion of the datetime. |
||
| 382 | * |
||
| 383 | * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
||
| 384 | * @param DateTime $current current DateTime object for the datetime field |
||
| 385 | * @return DateTime |
||
| 386 | */ |
||
| 387 | View Code Duplication | public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current) |
|
| 407 | |||
| 408 | |||
| 409 | /** |
||
| 410 | * Only sets the date portion of the datetime. |
||
| 411 | * |
||
| 412 | * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
||
| 413 | * @param DateTime $current current DateTime object for the datetime field |
||
| 414 | * @return DateTime |
||
| 415 | */ |
||
| 416 | View Code Duplication | public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current) |
|
| 436 | |||
| 437 | |||
| 438 | /** |
||
| 439 | * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
||
| 440 | * datetime gets to this stage it should ALREADY be in UTC time |
||
| 441 | * |
||
| 442 | * @param DateTime $DateTime |
||
| 443 | * @return string formatted date time for given timezone |
||
| 444 | * @throws \EE_Error |
||
| 445 | */ |
||
| 446 | public function prepare_for_get($DateTime) |
||
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
||
| 454 | * from the set wp timezone. If so, then it returns the datetime string formatted via |
||
| 455 | * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
||
| 456 | * abbreviation to the date_string. |
||
| 457 | * |
||
| 458 | * @param mixed $DateTime |
||
| 459 | * @param null $schema |
||
| 460 | * @return string |
||
| 461 | * @throws \EE_Error |
||
| 462 | */ |
||
| 463 | public function prepare_for_pretty_echoing($DateTime, $schema = null) |
||
| 467 | |||
| 468 | |||
| 469 | /** |
||
| 470 | * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
||
| 471 | * timezone). |
||
| 472 | * |
||
| 473 | * @param DateTime $DateTime |
||
| 474 | * @param bool|string $schema |
||
| 475 | * @return string |
||
| 476 | * @throws \EE_Error |
||
| 477 | */ |
||
| 478 | protected function _prepare_for_display($DateTime, $schema = false) |
||
| 528 | |||
| 529 | |||
| 530 | /** |
||
| 531 | * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
||
| 532 | * timezone). |
||
| 533 | * |
||
| 534 | * @param mixed $datetime_value u |
||
| 535 | * @return string mysql timestamp in UTC |
||
| 536 | * @throws \EE_Error |
||
| 537 | */ |
||
| 538 | public function prepare_for_use_in_db($datetime_value) |
||
| 563 | |||
| 564 | |||
| 565 | /** |
||
| 566 | * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
||
| 567 | * allowed) |
||
| 568 | * |
||
| 569 | * @param string $datetime_string mysql timestamp in UTC |
||
| 570 | * @return mixed null | DateTime |
||
| 571 | * @throws \EE_Error |
||
| 572 | */ |
||
| 573 | public function prepare_for_set_from_db($datetime_string) |
||
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * All this method does is determine if we're going to display the timezone string or not on any output. |
||
| 609 | * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
||
| 610 | * If so, then true. |
||
| 611 | * |
||
| 612 | * @return bool true for yes false for no |
||
| 613 | * @throws \EE_Error |
||
| 614 | */ |
||
| 615 | protected function _display_timezone() |
||
| 630 | |||
| 631 | |||
| 632 | /** |
||
| 633 | * This method returns a php DateTime object for setting on the EE_Base_Class model. |
||
| 634 | * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
||
| 635 | * with. |
||
| 636 | * |
||
| 637 | * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
||
| 638 | * in the format that is set on the date_field (or DateTime |
||
| 639 | * object)! |
||
| 640 | * @return DateTime |
||
| 641 | */ |
||
| 642 | protected function _get_date_object($date_string) |
||
| 701 | |||
| 702 | |||
| 703 | |||
| 704 | /** |
||
| 705 | * get_timezone_transitions |
||
| 706 | * |
||
| 707 | * @param \DateTimeZone $DateTimeZone |
||
| 708 | * @param int $time |
||
| 709 | * @param bool $first_only |
||
| 710 | * @return mixed |
||
| 711 | */ |
||
| 712 | public function get_timezone_transitions(DateTimeZone $DateTimeZone, $time = null, $first_only = true) |
||
| 719 | |||
| 720 | |||
| 721 | |||
| 722 | /** |
||
| 723 | * get_timezone_offset |
||
| 724 | * |
||
| 725 | * @param \DateTimeZone $DateTimeZone |
||
| 726 | * @param int $time |
||
| 727 | * @return mixed |
||
| 728 | * @throws \DomainException |
||
| 729 | */ |
||
| 730 | public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
||
| 738 | |||
| 739 | |||
| 740 | /** |
||
| 741 | * This will take an incoming timezone string and return the abbreviation for that timezone |
||
| 742 | * |
||
| 743 | * @param string $timezone_string |
||
| 744 | * @return string abbreviation |
||
| 745 | * @throws \EE_Error |
||
| 746 | */ |
||
| 747 | public function get_timezone_abbrev($timezone_string) |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Overrides the parent to allow for having a dynamic "now" value |
||
| 757 | * |
||
| 758 | * @return mixed |
||
| 759 | */ |
||
| 760 | public function get_default_value() |
||
| 768 | |||
| 769 | |||
| 770 | /** |
||
| 771 | * This returns elements used to represent this field in the json schema. |
||
| 772 | * |
||
| 773 | * @link http://json-schema.org/ |
||
| 774 | * @return array |
||
| 775 | */ |
||
| 776 | public function get_schema() |
||
| 784 | } |
||
| 785 |
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.