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 EEH_DTT_Helper 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 EEH_DTT_Helper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class EEH_DTT_Helper |
||
26 | { |
||
27 | |||
28 | |||
29 | /** |
||
30 | * return the timezone set for the WP install |
||
31 | * |
||
32 | * @return string valid timezone string for PHP DateTimeZone() class |
||
33 | */ |
||
34 | public static function get_timezone() |
||
38 | |||
39 | |||
40 | /** |
||
41 | * get_valid_timezone_string |
||
42 | * ensures that a valid timezone string is returned |
||
43 | * |
||
44 | * @access protected |
||
45 | * @param string $timezone_string |
||
46 | * @return string |
||
47 | * @throws \EE_Error |
||
48 | */ |
||
49 | public static function get_valid_timezone_string($timezone_string = '') |
||
58 | |||
59 | |||
60 | /** |
||
61 | * This only purpose for this static method is to validate that the incoming timezone is a valid php timezone. |
||
62 | * |
||
63 | * @static |
||
64 | * @access public |
||
65 | * @param string $timezone_string Timezone string to check |
||
66 | * @param bool $throw_error |
||
67 | * @return bool |
||
68 | * @throws \EE_Error |
||
69 | */ |
||
70 | public static function validate_timezone($timezone_string, $throw_error = true) |
||
92 | |||
93 | |||
94 | /** |
||
95 | * _create_timezone_object_from_timezone_name |
||
96 | * |
||
97 | * @access protected |
||
98 | * @param string $gmt_offset |
||
99 | * @return string |
||
100 | */ |
||
101 | public static function get_timezone_string_from_gmt_offset($gmt_offset = '') |
||
119 | |||
120 | /** |
||
121 | * Gets the site's GMT offset based on either the timezone string |
||
122 | * (in which case teh gmt offset will vary depending on the location's |
||
123 | * observance of daylight savings time) or the gmt_offset wp option |
||
124 | * |
||
125 | * @return int seconds offset |
||
126 | */ |
||
127 | public static function get_site_timezone_gmt_offset() |
||
140 | |||
141 | |||
142 | /** |
||
143 | * _create_timezone_object_from_timezone_name |
||
144 | * |
||
145 | * @access public |
||
146 | * @param int $gmt_offset |
||
147 | * @return int |
||
148 | */ |
||
149 | public static function adjust_invalid_gmt_offsets($gmt_offset = 0) |
||
194 | |||
195 | |||
196 | /** |
||
197 | * get_timezone_string_from_abbreviations_list |
||
198 | * |
||
199 | * @access public |
||
200 | * @param int $gmt_offset |
||
201 | * @return string |
||
202 | * @throws \EE_Error |
||
203 | */ |
||
204 | public static function get_timezone_string_from_abbreviations_list($gmt_offset = 0) |
||
227 | |||
228 | |||
229 | /** |
||
230 | * @access public |
||
231 | * @param string $timezone_string |
||
232 | */ |
||
233 | public static function timezone_select_input($timezone_string = '') |
||
325 | |||
326 | |||
327 | /** |
||
328 | * This method will take an incoming unix timestamp and add the offset to it for the given timezone_string. |
||
329 | * If no unix timestamp is given then time() is used. If no timezone is given then the set timezone string for |
||
330 | * the site is used. |
||
331 | * This is used typically when using a Unix timestamp any core WP functions that expect their specially |
||
332 | * computed timestamp (i.e. date_i18n() ) |
||
333 | * |
||
334 | * @param int $unix_timestamp if 0, then time() will be used. |
||
335 | * @param string $timezone_string timezone_string. If empty, then the current set timezone for the |
||
336 | * site will be used. |
||
337 | * @return int $unix_timestamp with the offset applied for the given timezone. |
||
338 | */ |
||
339 | public static function get_timestamp_with_offset($unix_timestamp = 0, $timezone_string = '') |
||
349 | |||
350 | |||
351 | /** |
||
352 | * _set_date_time_field |
||
353 | * modifies EE_Base_Class EE_Datetime_Field objects |
||
354 | * |
||
355 | * @param EE_Base_Class $obj EE_Base_Class object |
||
356 | * @param DateTime $DateTime PHP DateTime object |
||
357 | * @param string $datetime_field_name the datetime fieldname to be manipulated |
||
358 | * @return EE_Base_Class |
||
359 | */ |
||
360 | protected static function _set_date_time_field(EE_Base_Class $obj, DateTime $DateTime, $datetime_field_name) |
||
374 | |||
375 | |||
376 | /** |
||
377 | * date_time_add |
||
378 | * helper for doing simple datetime calculations on a given datetime from EE_Base_Class |
||
379 | * and modifying it IN the EE_Base_Class so you don't have to do anything else. |
||
380 | * |
||
381 | * @param EE_Base_Class $obj EE_Base_Class object |
||
382 | * @param string $datetime_field_name name of the EE_Datetime_Filed datatype db column to be manipulated |
||
383 | * @param string $period what you are adding. The options are (years, months, days, hours, |
||
384 | * minutes, seconds) defaults to years |
||
385 | * @param integer $value what you want to increment the time by |
||
386 | * @return EE_Base_Class return the EE_Base_Class object so right away you can do something with it |
||
387 | * (chaining) |
||
388 | */ |
||
389 | View Code Duplication | public static function date_time_add(EE_Base_Class $obj, $datetime_field_name, $period = 'years', $value = 1) |
|
396 | |||
397 | |||
398 | /** |
||
399 | * date_time_subtract |
||
400 | * same as date_time_add except subtracting value instead of adding. |
||
401 | * |
||
402 | * @param \EE_Base_Class $obj |
||
403 | * @param string $datetime_field_name name of the EE_Datetime_Filed datatype db column to be manipulated |
||
404 | * @param string $period |
||
405 | * @param int $value |
||
406 | * @return \EE_Base_Class |
||
407 | */ |
||
408 | View Code Duplication | public static function date_time_subtract(EE_Base_Class $obj, $datetime_field_name, $period = 'years', $value = 1) |
|
415 | |||
416 | |||
417 | /** |
||
418 | * Simply takes an incoming DateTime object and does calculations on it based on the incoming parameters |
||
419 | * |
||
420 | * @param DateTime $DateTime DateTime object |
||
421 | * @param string $period a value to indicate what interval is being used in the calculation. The options are |
||
422 | * 'years', 'months', 'days', 'hours', 'minutes', 'seconds'. Defaults to years. |
||
423 | * @param integer $value What you want to increment the date by |
||
424 | * @param string $operand What operand you wish to use for the calculation |
||
425 | * @return \DateTime return whatever type came in. |
||
426 | * @throws \EE_Error |
||
427 | */ |
||
428 | protected static function _modify_datetime_object(DateTime $DateTime, $period = 'years', $value = 1, $operand = '+') |
||
471 | |||
472 | |||
473 | /** |
||
474 | * Simply takes an incoming Unix timestamp and does calculations on it based on the incoming parameters |
||
475 | * |
||
476 | * @param int $timestamp Unix timestamp |
||
477 | * @param string $period a value to indicate what interval is being used in the calculation. The options are |
||
478 | * 'years', 'months', 'days', 'hours', 'minutes', 'seconds'. Defaults to years. |
||
479 | * @param integer $value What you want to increment the date by |
||
480 | * @param string $operand What operand you wish to use for the calculation |
||
481 | * @return \DateTime return whatever type came in. |
||
482 | * @throws \EE_Error |
||
483 | */ |
||
484 | protected static function _modify_timestamp($timestamp, $period = 'years', $value = 1, $operand = '+') |
||
524 | |||
525 | |||
526 | /** |
||
527 | * Simply takes an incoming UTC timestamp or DateTime object and does calculations on it based on the incoming |
||
528 | * parameters and returns the new timestamp or DateTime. |
||
529 | * |
||
530 | * @param int | DateTime $DateTime_or_timestamp DateTime object or Unix timestamp |
||
531 | * @param string $period a value to indicate what interval is being used in the |
||
532 | * calculation. The options are 'years', 'months', 'days', 'hours', |
||
533 | * 'minutes', 'seconds'. Defaults to years. |
||
534 | * @param integer $value What you want to increment the date by |
||
535 | * @param string $operand What operand you wish to use for the calculation |
||
536 | * @return mixed string|DateTime return whatever type came in. |
||
537 | */ |
||
538 | public static function calc_date($DateTime_or_timestamp, $period = 'years', $value = 1, $operand = '+') |
||
549 | |||
550 | |||
551 | /** |
||
552 | * The purpose of this helper method is to receive an incoming format string in php date/time format |
||
553 | * and spit out the js and moment.js equivalent formats. |
||
554 | * Note, if no format string is given, then it is assumed the user wants what is set for WP. |
||
555 | * Note, js date and time formats are those used by the jquery-ui datepicker and the jquery-ui date- |
||
556 | * time picker. |
||
557 | * |
||
558 | * @see http://stackoverflow.com/posts/16725290/ for the code inspiration. |
||
559 | * @param null $date_format_string |
||
560 | * @param null $time_format_string |
||
561 | * @return array |
||
562 | * array( |
||
563 | * 'js' => array ( |
||
564 | * 'date' => //date format |
||
565 | * 'time' => //time format |
||
566 | * ), |
||
567 | * 'moment' => //date and time format. |
||
568 | * ) |
||
569 | */ |
||
570 | public static function convert_php_to_js_and_moment_date_formats( |
||
593 | |||
594 | |||
595 | /** |
||
596 | * This converts incoming format string into js and moment variations. |
||
597 | * |
||
598 | * @param string $format_string incoming php format string |
||
599 | * @return array js and moment formats. |
||
600 | */ |
||
601 | protected static function _php_to_js_moment_converter($format_string) |
||
788 | |||
789 | |||
790 | /** |
||
791 | * This takes an incoming format string and validates it to ensure it will work fine with PHP. |
||
792 | * |
||
793 | * @param string $format_string Incoming format string for php date(). |
||
794 | * @return mixed bool|array If all is okay then TRUE is returned. Otherwise an array of validation |
||
795 | * errors is returned. So for client code calling, check for is_array() to |
||
796 | * indicate failed validations. |
||
797 | */ |
||
798 | public static function validate_format_string($format_string) |
||
820 | |||
821 | |||
822 | /** |
||
823 | * If the the first date starts at midnight on one day, and the next date ends at midnight on the |
||
824 | * very next day then this method will return true. |
||
825 | * If $date_1 = 2015-12-15 00:00:00 and $date_2 = 2015-12-16 00:00:00 then this function will return true. |
||
826 | * If $date_1 = 2015-12-15 03:00:00 and $date_2 = 2015-12_16 03:00:00 then this function will return false. |
||
827 | * If $date_1 = 2015-12-15 00:00:00 and $date_2 = 2015-12-15 00:00:00 then this function will return true. |
||
828 | * |
||
829 | * @param mixed $date_1 |
||
830 | * @param mixed $date_2 |
||
831 | * @return bool |
||
832 | */ |
||
833 | public static function dates_represent_one_24_hour_date($date_1, $date_2) |
||
844 | |||
845 | |||
846 | /** |
||
847 | * This returns the appropriate query interval string that can be used in sql queries involving mysql Date |
||
848 | * Functions. |
||
849 | * |
||
850 | * @param string $timezone_string A timezone string in a valid format to instantiate a DateTimeZone object. |
||
851 | * @param string $field_for_interval The Database field that is the interval is applied to in the query. |
||
852 | * @return string |
||
853 | */ |
||
854 | public static function get_sql_query_interval_for_offset($timezone_string, $field_for_interval) |
||
873 | |||
874 | /** |
||
875 | * Retrieves the site's default timezone and returns it formatted so it's ready for display |
||
876 | * to users. If you want to customize how its displayed feel free to fetch the 'timezone_string' |
||
877 | * and 'gmt_offset' WordPress options directly; or use the filter |
||
878 | * FHEE__EEH_DTT_Helper__get_timezone_string_for_display |
||
879 | * (although note that we remove any HTML that may be added) |
||
880 | * |
||
881 | * @return string |
||
882 | */ |
||
883 | public static function get_timezone_string_for_display() |
||
927 | |||
928 | |||
929 | |||
930 | /** |
||
931 | * So PHP does this awesome thing where if you are trying to get a timestamp |
||
932 | * for a month using a string like "February" or "February 2017", |
||
933 | * and you don't specify a day as part of your string, |
||
934 | * then PHP will use whatever the current day of the month is. |
||
935 | * IF the current day of the month happens to be the 30th or 31st, |
||
936 | * then PHP gets really confused by a date like February 30, |
||
937 | * so instead of saying |
||
938 | * "Hey February only has 28 days (this year)... |
||
939 | * ...you must have meant the last day of the month!" |
||
940 | * PHP does the next most logical thing, and bumps the date up to March 2nd, |
||
941 | * because someone requesting February 30th obviously meant March 1st! |
||
942 | * The way around this is to always set the day to the first, |
||
943 | * so that the month will stay on the month you wanted. |
||
944 | * this method will add that "1" into your date regardless of the format. |
||
945 | * |
||
946 | * @param string $month |
||
947 | * @return string |
||
948 | */ |
||
949 | public static function first_of_month_timestamp($month = '') |
||
964 | |||
965 | /** |
||
966 | * This simply returns the timestamp for tomorrow (midnight next day) in this sites timezone. So it may be midnight |
||
967 | * for this sites timezone, but the timestamp could be some other time GMT. |
||
968 | */ |
||
969 | public static function tomorrow() |
||
977 | |||
978 | }// end class EEH_DTT_Helper |
||
979 |