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 | ||
| 34 | 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 | 34 | * |
|
| 45 | * @param TimeValue $timeValue |
||
| 46 | 34 | * |
|
| 47 | * @return float seconds since 1970-01-01T00:00:00Z |
||
| 48 | */ |
||
| 49 | public function getTimestamp( TimeValue $timeValue ) { |
||
| 52 | |||
| 53 | /** |
||
| 54 | 34 | * 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 | 34 | * year 2038. |
|
| 57 | 34 | * |
|
| 58 | * @param TimeValue $timeValue |
||
| 59 | * |
||
| 60 | 34 | * @return float seconds since 1970-01-01T00:00:00Z |
|
| 61 | 34 | */ |
|
| 62 | public function getLowerTimestamp( TimeValue $timeValue ) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns the highest possible Unix timestamp from a TimeValue considering its precision |
||
| 92 | * and its after value. Data type is float because PHP's 32 bit integer would clip in the |
||
| 93 | 68 | * year 2038. |
|
| 94 | 68 | * |
|
| 95 | 68 | * @param TimeValue $timeValue |
|
| 96 | * |
||
| 97 | * @return float seconds since 1970-01-01T00:00:00Z |
||
| 98 | */ |
||
| 99 | public function getHigherTimestamp( TimeValue $timeValue ) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $time an ISO 8601 date and time |
||
| 128 | * @param int $timezone offset from UTC in minutes |
||
| 129 | * |
||
| 130 | * @throws InvalidArgumentException |
||
| 131 | * @return float seconds since 1970-01-01T00:00:00Z |
||
| 132 | */ |
||
| 133 | private function getSecondsSinceUnixEpoch( $time, $timezone = 0 ) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $calendar only TimeValue::CALENDAR_GREGORIAN is supported |
||
| 165 | * @param float $month |
||
| 166 | * @param float $year |
||
| 167 | * |
||
| 168 | * @throws InvalidArgumentException if $calendar is not supported |
||
| 169 | * @return int |
||
| 170 | */ |
||
| 171 | public function getDaysInMonth( $calendar, $month, $year ) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param float $year |
||
| 180 | * |
||
| 181 | * @return bool if the year is a leap year in the Gregorian calendar |
||
| 182 | */ |
||
| 183 | public function isLeapYear( $year ) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param float $year |
||
| 193 | * |
||
| 194 | * @return float The number of leap years since year 1. To be more precise: The number of |
||
| 195 | * leap days in the range between 31 December of year 1 and 31 December of the given year. |
||
| 196 | */ |
||
| 197 | public function getNumberOfLeapYears( $year ) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param int $precision One of the TimeValue::PRECISION_... constants |
||
| 204 | * |
||
| 205 | * @throws InvalidArgumentException |
||
| 206 | * @return float number of seconds in one unit of the given precision |
||
| 207 | */ |
||
| 208 | public function getSecondsForPrecision( $precision ) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param $timestamp |
||
| 234 | * @param $precision |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | private function timestampAbsFloor( $timestamp, $precision ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param $timestamp |
||
| 254 | * @param $precision |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | private function timestampAbsCeiling( $timestamp, $precision ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param int $precision |
||
| 291 | * |
||
| 292 | * @return int lowest number of characters in an ISO 8601 timestamp string |
||
| 293 | * that are irrelevant given $precision |
||
| 294 | */ |
||
| 295 | private function charsAffectedByPrecision( $precision ) { |
||
| 343 | |||
| 344 | } |
||
| 345 |