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 | * Maximum length for a timestamp. |
||
26 | */ |
||
27 | private $MAX_LENGTH_TIMESTAMP = 33; |
||
28 | |||
29 | /** |
||
30 | * Lowest positive timestamp. |
||
31 | */ |
||
32 | private $TIMESTAMP_ZERO = '+0000000000000000-01-01T00:00:00Z'; |
||
33 | |||
34 | /** |
||
35 | * Highest positive timestamp strictly earlier than the lowest positive timestamp with |
||
36 | * a length of $MAX_LENGTH_TIMESTAMP + 1. |
||
37 | */ |
||
38 | private $HIGHEST_TIMESTAMP = '+9999999999999999-12-31T23:59:59Z'; |
||
39 | |||
40 | /** |
||
41 | * This returns a Unix timestamp from a TimeValue similar to PHP's mk_time() (or strtotime()), |
||
42 | * but with no range limitations. Data type is float because PHP's 32 bit integer would |
||
43 | * clip in the year 2038. |
||
44 | * |
||
45 | * @param TimeValue $timeValue |
||
46 | * |
||
47 | * @return float seconds since 1970-01-01T00:00:00Z |
||
48 | */ |
||
49 | 754 | public function getTimestamp( TimeValue $timeValue ) { |
|
52 | |||
53 | /** |
||
54 | * Returns the lowest possible Unix timestamp from a TimeValue considering its precision |
||
55 | * and its before value. Data type is float because PHP's 32 bit integer would clip in the |
||
56 | * year 2038. |
||
57 | * |
||
58 | * @param TimeValue $timeValue |
||
59 | * |
||
60 | * @return float seconds since 1970-01-01T00:00:00Z |
||
61 | */ |
||
62 | 1085 | public function getLowerTimestamp( TimeValue $timeValue ) { |
|
87 | |||
88 | /** |
||
89 | * Returns the highest possible Unix timestamp from a TimeValue considering its precision |
||
90 | * and its after value. Data type is float because PHP's 32 bit integer would clip in the |
||
91 | * year 2038. |
||
92 | * |
||
93 | * @param TimeValue $timeValue |
||
94 | * |
||
95 | * @return float seconds since 1970-01-01T00:00:00Z |
||
96 | */ |
||
97 | 1085 | public function getHigherTimestamp( TimeValue $timeValue ) { |
|
122 | |||
123 | /** |
||
124 | * @param string $time an ISO 8601 date and time |
||
125 | * @param int $timezone offset from UTC in minutes |
||
126 | * |
||
127 | * @throws InvalidArgumentException |
||
128 | * @return float seconds since 1970-01-01T00:00:00Z |
||
129 | */ |
||
130 | 2204 | private function getSecondsSinceUnixEpoch( $time, $timezone = 0 ) { |
|
159 | |||
160 | /** |
||
161 | * @param string $calendar only TimeValue::CALENDAR_GREGORIAN is supported |
||
162 | * @param float $month |
||
163 | * @param float $year |
||
164 | * |
||
165 | * @throws InvalidArgumentException if $calendar is not supported |
||
166 | * @return int |
||
167 | */ |
||
168 | 97 | public function getDaysInMonth( $calendar, $month, $year ) { |
|
174 | |||
175 | /** |
||
176 | * @param float $year |
||
177 | * |
||
178 | * @return bool if the year is a leap year in the Gregorian calendar |
||
179 | */ |
||
180 | 2238 | public function isLeapYear( $year ) { |
|
187 | |||
188 | /** |
||
189 | * @param float $year |
||
190 | * |
||
191 | * @return float The number of leap years since year 1. To be more precise: The number of |
||
192 | * leap days in the range between 31 December of year 1 and 31 December of the given year. |
||
193 | */ |
||
194 | 2238 | public function getNumberOfLeapYears( $year ) { |
|
198 | |||
199 | /** |
||
200 | * @param int $precision One of the TimeValue::PRECISION_... constants |
||
201 | * |
||
202 | * @throws InvalidArgumentException |
||
203 | * @return float number of seconds in one unit of the given precision |
||
204 | */ |
||
205 | 2178 | public function getSecondsForPrecision( $precision ) { |
|
228 | |||
229 | /** |
||
230 | * @param $timestamp |
||
231 | * @param $precision |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | 1409 | private function timestampAbsFloor( $timestamp, $precision ) { |
|
248 | |||
249 | /** |
||
250 | * @param $timestamp |
||
251 | * @param $precision |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | 1409 | private function timestampAbsCeiling( $timestamp, $precision ) { |
|
285 | |||
286 | /** |
||
287 | * @param int $precision |
||
288 | * |
||
289 | * @return int lowest number of characters in an ISO 8601 timestamp string |
||
290 | * that are irrelevant given $precision |
||
291 | */ |
||
292 | 2170 | private function charsAffectedByPrecision( $precision ) { |
|
340 | |||
341 | } |
||
342 |