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 PhpCompatLessFiveSixHelper 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 PhpCompatLessFiveSixHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class PhpCompatLessFiveSixHelper extends AbstractHelper |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * PhpCompatLessFiveSixHelper constructor. |
||
23 | * |
||
24 | * @throws DomainException |
||
25 | */ |
||
26 | View Code Duplication | public function __construct() |
|
41 | |||
42 | /** |
||
43 | * Returns a timezone string for the provided gmt_offset. |
||
44 | * |
||
45 | * @param float|string $gmt_offset |
||
46 | * @return string |
||
47 | * @throws EE_Error |
||
48 | */ |
||
49 | public function getTimezoneStringFromGmtOffset($gmt_offset = '') |
||
50 | { |
||
51 | $gmt_offset_or_timezone_string = $this->sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset); |
||
52 | if (is_string($gmt_offset_or_timezone_string)) { |
||
53 | return $gmt_offset_or_timezone_string; |
||
54 | } |
||
55 | //well we know its a float, so let's roll with it. |
||
56 | $gmt_offset = $gmt_offset_or_timezone_string; |
||
57 | // convert GMT offset to seconds |
||
58 | $gmt_offset *= HOUR_IN_SECONDS; |
||
59 | // although we don't know the TZ abbreviation, we know the UTC offset |
||
60 | $timezone_string = timezone_name_from_abbr(null, $gmt_offset); |
||
61 | //only use this timezone_string IF it's current offset matches the given offset |
||
62 | if (! empty($timezone_string)) { |
||
63 | $offset = null; |
||
64 | try { |
||
65 | $offset = $this->getTimezoneOffset(new DateTimeZone($timezone_string)); |
||
66 | if ($offset !== $gmt_offset) { |
||
67 | $timezone_string = false; |
||
68 | } |
||
69 | } catch (Exception $e) { |
||
70 | $timezone_string = false; |
||
71 | } |
||
72 | } |
||
73 | // better have a valid timezone string by now, but if not, sigh... loop thru the timezone_abbreviations_list() |
||
74 | //... |
||
75 | $timezone_string = $timezone_string !== false |
||
76 | ? $timezone_string |
||
77 | : $this->getTimezoneStringFromAbbreviationsList($gmt_offset); |
||
78 | return $timezone_string; |
||
79 | } |
||
80 | |||
81 | |||
82 | /** |
||
83 | * @param int $gmt_offset |
||
84 | * @param bool $coerce If true, we attempt to coerce with our adjustment table |
||
85 | * @see self::adjustInvalidGmtOffset |
||
86 | * @return string |
||
87 | * @throws EE_Error |
||
88 | */ |
||
89 | protected function getTimezoneStringFromAbbreviationsList($gmt_offset = 0, $coerce = true) |
||
131 | |||
132 | |||
133 | /** |
||
134 | * Depending on PHP version, |
||
135 | * there might not be valid current timezone strings to match these gmt_offsets in its timezone tables. |
||
136 | * To get around that, for these fringe timezones we bump them to a known valid offset. |
||
137 | * This method should ONLY be called after first verifying an timezone_string cannot be retrieved for the offset. |
||
138 | * |
||
139 | * @param int $gmt_offset |
||
140 | * @return int |
||
141 | */ |
||
142 | public function adjustInvalidGmtOffsets($gmt_offset = 0) |
||
238 | } |
||
239 |