Complex classes like TimeValueCalculator 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 TimeValueCalculator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class TimeValueCalculator { |
||
17 | |||
18 | /** |
||
19 | * Average length of a year in the Gregorian calendar in seconds, calculated via |
||
20 | * 365 + 1 / 4 - 1 / 100 + 1 / 400 = 365.2425 days. |
||
21 | */ |
||
22 | const SECONDS_PER_GREGORIAN_YEAR = 31556952; |
||
23 | |||
24 | /** |
||
25 | * Lowest positive timestamp. |
||
26 | */ |
||
27 | const TIMESTAMP_ZERO = '+0000000000000000-01-01T00:00:00Z'; |
||
28 | |||
29 | /** |
||
30 | * Highest positive timestamp. |
||
31 | */ |
||
32 | const HIGHEST_TIMESTAMP = '+9999999999999999-12-31T23:59:59Z'; |
||
33 | 34 | ||
34 | 34 | /** |
|
35 | * Maximum length for a timestamp. |
||
36 | */ |
||
37 | const MAX_LENGTH_TIMESTAMP = 33; |
||
38 | |||
39 | /** |
||
40 | * This returns the Unix timestamp from a TimeValue. |
||
41 | * This is similar to PHP's mk_time() (or strtotime()), but with no range limitations. |
||
42 | * Data type is float because PHP's 32 bit integer would clip in the year 2038. |
||
43 | * |
||
44 | 34 | * @param TimeValue $timeValue |
|
45 | * |
||
46 | 34 | * @return float seconds since 1970-01-01T00:00:00Z |
|
47 | */ |
||
48 | public function getTimestamp( TimeValue $timeValue ) { |
||
51 | |||
52 | /** |
||
53 | * Returns the lowest possible Unix timestamp from a TimeValue considering its precision |
||
54 | 34 | * and its before value. Data type is float because PHP's 32 bit integer would clip in the |
|
55 | * year 2038. |
||
56 | 34 | * |
|
57 | 34 | * @param TimeValue $timeValue |
|
58 | * |
||
59 | * @return float seconds since 1970-01-01T00:00:00Z |
||
60 | 34 | */ |
|
61 | 34 | public function getLowerTimestamp( TimeValue $timeValue ) { |
|
74 | |||
75 | /** |
||
76 | * Returns the highest possible Unix timestamp from a TimeValue considering its precision |
||
77 | * and its after value. Data type is float because PHP's 32 bit integer would clip in the |
||
78 | * year 2038. |
||
79 | 68 | * |
|
80 | 68 | * @param TimeValue $timeValue |
|
81 | 68 | * |
|
82 | 68 | * @return float seconds since 1970-01-01T00:00:00Z |
|
83 | 68 | */ |
|
84 | 68 | public function getHigherTimestamp( TimeValue $timeValue ) { |
|
97 | |||
98 | /** |
||
99 | * @param string $time an ISO 8601 date and time |
||
100 | * @param int $timezone offset from UTC in minutes |
||
101 | * |
||
102 | * @throws InvalidArgumentException |
||
103 | * @return float seconds since 1970-01-01T00:00:00Z |
||
104 | 8 | */ |
|
105 | 8 | private function getSecondsSinceUnixEpoch( $time, $timezone = 0 ) { |
|
134 | |||
135 | /** |
||
136 | * @param float $year |
||
137 | * |
||
138 | * @return bool if the year is a leap year in the Gregorian calendar |
||
139 | */ |
||
140 | public function isLeapYear( $year ) { |
||
147 | |||
148 | /** |
||
149 | * @param float $year |
||
150 | * |
||
151 | * @return float The number of leap years since year 1. To be more precise: The number of |
||
152 | * leap days in the range between 31 December of year 1 and 31 December of the given year. |
||
153 | */ |
||
154 | public function getNumberOfLeapYears( $year ) { |
||
158 | |||
159 | /** |
||
160 | * @param int $precision One of the TimeValue::PRECISION_... constants |
||
161 | * |
||
162 | * @throws InvalidArgumentException |
||
163 | * @return float number of seconds in one unit of the given precision |
||
164 | */ |
||
165 | public function getSecondsForPrecision( $precision ) { |
||
188 | |||
189 | /** |
||
190 | * @param $timestamp |
||
191 | * @param $precision |
||
192 | * @return string |
||
193 | */ |
||
194 | private function timestampAbsFloor($timestamp, $precision) { |
||
203 | |||
204 | /** |
||
205 | * @param $timestamp |
||
206 | * @param $precision |
||
207 | * @return string |
||
208 | */ |
||
209 | private function timestampAbsCeiling($timestamp, $precision) { |
||
219 | |||
220 | /** |
||
221 | * @param $precision |
||
222 | * @return int |
||
223 | */ |
||
224 | private function charsAffectedByPrecision($precision) { |
||
272 | |||
273 | } |
||
274 |