Complex classes like AbstractHelper 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 AbstractHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | abstract class AbstractHelper implements HelperInterface |
||
12 | { |
||
13 | |||
14 | |||
15 | /** |
||
16 | * Ensures that a valid timezone string is returned. |
||
17 | * |
||
18 | * @param string $timezone_string When not provided then attempt to use the timezone_string set in the WP Time |
||
19 | * settings (or derive from set UTC offset). |
||
20 | * @return string |
||
21 | * @throws EE_Error |
||
22 | */ |
||
23 | public function getValidTimezoneString($timezone_string = '') |
||
32 | |||
33 | |||
34 | |||
35 | /** |
||
36 | * The only purpose for this static method is to validate that the incoming timezone is a valid php timezone. |
||
37 | * |
||
38 | * @param string $timezone_string |
||
39 | * @param bool $throw_error |
||
40 | * @return bool |
||
41 | * @throws EE_Error |
||
42 | */ |
||
43 | public function validateTimezone($timezone_string, $throw_error = true) |
||
68 | |||
69 | |||
70 | /** |
||
71 | * Gets the site's GMT offset based on either the timezone string |
||
72 | * (in which case the gmt offset will vary depending on the location's |
||
73 | * observance of daylight savings time) or the gmt_offset wp option |
||
74 | * |
||
75 | * @return int seconds offset |
||
76 | */ |
||
77 | public function getSiteTimezoneGmtOffset() |
||
90 | |||
91 | |||
92 | /** |
||
93 | * Get Timezone offset for given timezone object |
||
94 | * |
||
95 | * @param DateTimeZone $date_time_zone |
||
96 | * @param null|int $time |
||
97 | * @return int |
||
98 | * @throws DomainException |
||
99 | */ |
||
100 | public function getTimezoneOffset(DateTimeZone $date_time_zone, $time = null) |
||
113 | |||
114 | |||
115 | /** |
||
116 | * Provide a timezone select input |
||
117 | * |
||
118 | * @param string $timezone_string |
||
119 | * @return string |
||
120 | * @throws EE_Error |
||
121 | */ |
||
122 | public function timezoneSelectInput($timezone_string = '') |
||
215 | |||
216 | |||
217 | /** |
||
218 | * This method will take an incoming unix timestamp and add the offset to it for the given timezone_string. |
||
219 | * If no unix timestamp is given then time() is used. If no timezone is given then the set timezone string for |
||
220 | * the site is used. |
||
221 | * This is used typically when using a Unix timestamp any core WP functions that expect their specially |
||
222 | * computed timestamp (i.e. date_i18n() ) |
||
223 | * |
||
224 | * @param int $unix_timestamp if 0, then time() will be used. |
||
225 | * @param string $timezone_string timezone_string. If empty, then the current set timezone for the |
||
226 | * site will be used. |
||
227 | * @return int unix_timestamp value with the offset applied for the given timezone. |
||
228 | * @throws EE_Error |
||
229 | */ |
||
230 | public function getTimestampWithOffset($unix_timestamp = 0, $timezone_string = '') |
||
239 | |||
240 | |||
241 | /** |
||
242 | * Get Timezone Transitions |
||
243 | * |
||
244 | * @param DateTimeZone $date_time_zone |
||
245 | * @param int|null $time |
||
246 | * @param bool $first_only |
||
247 | * @return array|mixed |
||
248 | */ |
||
249 | public function getTimezoneTransitions(DateTimeZone $date_time_zone, $time = null, $first_only = true) |
||
256 | |||
257 | |||
258 | |||
259 | /** |
||
260 | * Default to just returning the provided $gmt_offset. Children can override if adjustment needed. |
||
261 | * |
||
262 | * @param int $gmt_offset |
||
263 | * @return int |
||
264 | */ |
||
265 | public function adjustInvalidGmtOffsets($gmt_offset = 0) |
||
269 | |||
270 | |||
271 | |||
272 | /** |
||
273 | * This receives an incoming gmt_offset and santizes it. If the provide value is an empty string, then this will |
||
274 | * attempt to get the offset from the timezone string. If this returns a string, then a timezone string was |
||
275 | * successfully derived from existing timezone_string in the db. If not, then a float is returned for the provided |
||
276 | * offset. |
||
277 | * @param float|string $gmt_offset |
||
278 | * @return float|string |
||
279 | */ |
||
280 | protected function sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset) |
||
299 | } |
||
300 |