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; |
||
17 | class EE_Datetime_Field extends EE_Model_Field_Base |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * The pattern we're looking for is if only the characters 0-9 are found and there are only |
||
22 | * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
||
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 | * @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 | * @var string |
||
106 | */ |
||
107 | protected $_timezone_string; |
||
108 | |||
109 | |||
110 | /** |
||
111 | * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
||
112 | * offsets for comparison purposes). |
||
113 | * @var int |
||
114 | */ |
||
115 | protected $_blog_offset; |
||
116 | |||
117 | |||
118 | |||
119 | /** |
||
120 | * @param string $table_column |
||
121 | * @param string $nice_name |
||
122 | * @param bool $nullable |
||
123 | * @param string $default_value |
||
124 | * @param string $timezone_string |
||
125 | * @param string $date_format |
||
126 | * @param string $time_format |
||
127 | * @param string $pretty_date_format |
||
128 | * @param string $pretty_time_format |
||
129 | * @throws \EE_Error |
||
130 | */ |
||
131 | public function __construct( |
||
152 | |||
153 | |||
154 | /** |
||
155 | * @return string |
||
156 | */ |
||
157 | public function get_wpdb_data_type() |
||
161 | |||
162 | |||
163 | |||
164 | /** |
||
165 | * @return DateTimeZone |
||
166 | * @throws \EE_Error |
||
167 | */ |
||
168 | public function get_UTC_DateTimeZone() |
||
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 | * |
||
195 | * @return DateTime |
||
196 | */ |
||
197 | public function prepare_for_set($value_inputted_for_field_on_model_object) |
||
201 | |||
202 | |||
203 | /** |
||
204 | * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
||
205 | * |
||
206 | * getters need to know whether we're just returning the date or the time or both. By default we return both. |
||
207 | * |
||
208 | * @param bool $pretty If we're returning the pretty formats or standard format string. |
||
209 | * @return string The final assembled format string. |
||
210 | */ |
||
211 | protected function _get_date_time_output($pretty = false) |
||
229 | |||
230 | |||
231 | /** |
||
232 | * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
||
233 | * returned (using the format properties) |
||
234 | * |
||
235 | * @param string $what acceptable values are 'time' or 'date'. |
||
236 | * Any other value will be set but will always result |
||
237 | * in both 'date' and 'time' being returned. |
||
238 | * @return void |
||
239 | */ |
||
240 | public function set_date_time_output($what = null) |
||
244 | |||
245 | |||
246 | |||
247 | /** |
||
248 | * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
||
249 | * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
||
250 | * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
||
251 | * We also set some other properties in this method. |
||
252 | * |
||
253 | * @param string $timezone_string A valid timezone string as described by @link |
||
254 | * http://www.php.net/manual/en/timezones.php |
||
255 | * @return void |
||
256 | * @throws \EE_Error |
||
257 | */ |
||
258 | public function set_timezone($timezone_string) |
||
269 | |||
270 | |||
271 | |||
272 | /** |
||
273 | * _create_timezone_object_from_timezone_name |
||
274 | * |
||
275 | * @access protected |
||
276 | * @param string $timezone_string |
||
277 | * @return \DateTimeZone |
||
278 | * @throws \EE_Error |
||
279 | */ |
||
280 | protected function _create_timezone_object_from_timezone_string($timezone_string = '') |
||
284 | |||
285 | |||
286 | /** |
||
287 | * This just returns whatever is set for the current timezone. |
||
288 | * |
||
289 | * @access public |
||
290 | * @return string timezone string |
||
291 | */ |
||
292 | public function get_timezone() |
||
296 | |||
297 | |||
298 | /** |
||
299 | * set the $_date_format property |
||
300 | * |
||
301 | * @access public |
||
302 | * |
||
303 | * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
||
304 | * @param bool $pretty Whether to set pretty format or not. |
||
305 | * |
||
306 | * @return void |
||
307 | */ |
||
308 | public function set_date_format($format, $pretty = false) |
||
316 | |||
317 | |||
318 | /** |
||
319 | * return the $_date_format property value. |
||
320 | * |
||
321 | * @param bool $pretty Whether to get pretty format or not. |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | public function get_date_format($pretty = false) |
||
329 | |||
330 | |||
331 | /** |
||
332 | * set the $_time_format property |
||
333 | * |
||
334 | * @access public |
||
335 | * |
||
336 | * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
||
337 | * @param bool $pretty Whether to set pretty format or not. |
||
338 | * |
||
339 | * @return void |
||
340 | */ |
||
341 | public function set_time_format($format, $pretty = false) |
||
349 | |||
350 | |||
351 | /** |
||
352 | * return the $_time_format property value. |
||
353 | * |
||
354 | * @param bool $pretty Whether to get pretty format or not. |
||
355 | * |
||
356 | * @return string |
||
357 | */ |
||
358 | public function get_time_format($pretty = false) |
||
362 | |||
363 | |||
364 | /** |
||
365 | * set the $_pretty_date_format property |
||
366 | * |
||
367 | * @access public |
||
368 | * |
||
369 | * @param string $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
||
370 | * |
||
371 | * @return void |
||
372 | */ |
||
373 | public function set_pretty_date_format($format) |
||
377 | |||
378 | |||
379 | /** |
||
380 | * set the $_pretty_time_format property |
||
381 | * |
||
382 | * @access public |
||
383 | * |
||
384 | * @param string $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
||
385 | * |
||
386 | * @return void |
||
387 | */ |
||
388 | public function set_pretty_time_format($format) |
||
392 | |||
393 | |||
394 | /** |
||
395 | * Only sets the time portion of the datetime. |
||
396 | * |
||
397 | * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
||
398 | * @param DateTime $current current DateTime object for the datetime field |
||
399 | * |
||
400 | * @return DateTime |
||
401 | */ |
||
402 | View Code Duplication | public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current) |
|
422 | |||
423 | |||
424 | /** |
||
425 | * Only sets the date portion of the datetime. |
||
426 | * |
||
427 | * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
||
428 | * @param DateTime $current current DateTime object for the datetime field |
||
429 | * |
||
430 | * @return DateTime |
||
431 | */ |
||
432 | View Code Duplication | public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current) |
|
452 | |||
453 | |||
454 | |||
455 | /** |
||
456 | * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
||
457 | * datetime gets to this stage it should ALREADY be in UTC time |
||
458 | * |
||
459 | * @param DateTime $DateTime |
||
460 | * @return string formatted date time for given timezone |
||
461 | * @throws \EE_Error |
||
462 | */ |
||
463 | public function prepare_for_get($DateTime) |
||
467 | |||
468 | |||
469 | |||
470 | /** |
||
471 | * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
||
472 | * from the set wp timezone. If so, then it returns the datetime string formatted via |
||
473 | * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
||
474 | * abbreviation to the date_string. |
||
475 | * |
||
476 | * @param mixed $DateTime |
||
477 | * @param null $schema |
||
478 | * @return string |
||
479 | * @throws \EE_Error |
||
480 | */ |
||
481 | public function prepare_for_pretty_echoing($DateTime, $schema = null) |
||
485 | |||
486 | |||
487 | /** |
||
488 | * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
||
489 | * timezone). |
||
490 | * |
||
491 | * @param DateTime $DateTime |
||
492 | * @param bool|string $schema |
||
493 | * @return string |
||
494 | * @throws \EE_Error |
||
495 | */ |
||
496 | protected function _prepare_for_display($DateTime, $schema = false) |
||
546 | |||
547 | |||
548 | /** |
||
549 | * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
||
550 | * timezone). |
||
551 | * |
||
552 | * @param mixed $datetime_value u |
||
553 | * |
||
554 | * @return string mysql timestamp in UTC |
||
555 | * @throws \EE_Error |
||
556 | */ |
||
557 | public function prepare_for_use_in_db($datetime_value) |
||
585 | |||
586 | |||
587 | |||
588 | /** |
||
589 | * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
||
590 | * allowed) |
||
591 | * |
||
592 | * @param string $datetime_string mysql timestamp in UTC |
||
593 | * @return mixed null | DateTime |
||
594 | * @throws \EE_Error |
||
595 | */ |
||
596 | public function prepare_for_set_from_db($datetime_string) |
||
628 | |||
629 | |||
630 | |||
631 | /** |
||
632 | * All this method does is determine if we're going to display the timezone string or not on any output. |
||
633 | * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
||
634 | * If so, then true. |
||
635 | * |
||
636 | * @return bool true for yes false for no |
||
637 | * @throws \EE_Error |
||
638 | */ |
||
639 | protected function _display_timezone() |
||
654 | |||
655 | |||
656 | /** |
||
657 | * This method returns a php DateTime object for setting on the EE_Base_Class model. |
||
658 | * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
||
659 | * with. |
||
660 | * |
||
661 | * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
||
662 | * in the format that is set on the date_field (or DateTime |
||
663 | * object)! |
||
664 | * |
||
665 | * @return DateTime |
||
666 | */ |
||
667 | protected function _get_date_object($date_string) |
||
726 | |||
727 | |||
728 | |||
729 | /** |
||
730 | * get_timezone_transitions |
||
731 | * |
||
732 | * @param \DateTimeZone $DateTimeZone |
||
733 | * @param int $time |
||
734 | * @param bool $first_only |
||
735 | * @return mixed |
||
736 | */ |
||
737 | public function get_timezone_transitions(DateTimeZone $DateTimeZone, $time = null, $first_only = true) |
||
744 | |||
745 | |||
746 | |||
747 | /** |
||
748 | * get_timezone_offset |
||
749 | * |
||
750 | * @param \DateTimeZone $DateTimeZone |
||
751 | * @param int $time |
||
752 | * @return mixed |
||
753 | * @throws \DomainException |
||
754 | */ |
||
755 | public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
||
763 | |||
764 | |||
765 | |||
766 | /** |
||
767 | * This will take an incoming timezone string and return the abbreviation for that timezone |
||
768 | * |
||
769 | * @param string $timezone_string |
||
770 | * @return string abbreviation |
||
771 | * @throws \EE_Error |
||
772 | */ |
||
773 | public function get_timezone_abbrev($timezone_string) |
||
780 | |||
781 | /** |
||
782 | * Overrides the parent to allow for having a dynamic "now" value |
||
783 | * |
||
784 | * @return mixed |
||
785 | */ |
||
786 | public function get_default_value() |
||
794 | |||
795 | |||
796 | } |
||
797 |
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.