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 | /** |
||
| 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 | * @param TimeValue $timeValue |
||
| 45 | * |
||
| 46 | * @return float seconds since 1970-01-01T00:00:00Z |
||
| 47 | */ |
||
| 48 | 36 | public function getTimestamp( TimeValue $timeValue ) { |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Returns the lowest possible Unix timestamp from a TimeValue considering its precision |
||
| 54 | * and its before value. Data type is float because PHP's 32 bit integer would clip in the |
||
| 55 | * year 2038. |
||
| 56 | * |
||
| 57 | * @param TimeValue $timeValue |
||
| 58 | * |
||
| 59 | * @return float seconds since 1970-01-01T00:00:00Z |
||
| 60 | */ |
||
| 61 | 1 | public function getLowerTimestamp( TimeValue $timeValue ) { |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Returns the highest possible Unix timestamp from a TimeValue considering its precision |
||
| 76 | * and its after value. Data type is float because PHP's 32 bit integer would clip in the |
||
| 77 | * year 2038. |
||
| 78 | * |
||
| 79 | * @param TimeValue $timeValue |
||
| 80 | * |
||
| 81 | * @return float seconds since 1970-01-01T00:00:00Z |
||
| 82 | */ |
||
| 83 | 1 | public function getHigherTimestamp( TimeValue $timeValue ) { |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $time an ISO 8601 date and time |
||
| 98 | * @param int $timezone offset from UTC in minutes |
||
| 99 | * |
||
| 100 | * @throws InvalidArgumentException |
||
| 101 | * @return float seconds since 1970-01-01T00:00:00Z |
||
| 102 | */ |
||
| 103 | 36 | private function getSecondsSinceUnixEpoch( $time, $timezone = 0 ) { |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param float $year |
||
| 135 | * |
||
| 136 | * @return bool if the year is a leap year in the Gregorian calendar |
||
| 137 | */ |
||
| 138 | 70 | public function isLeapYear( $year ) { |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param float $year |
||
| 148 | * |
||
| 149 | * @return float The number of leap years since year 1. To be more precise: The number of |
||
| 150 | * leap days in the range between 31 December of year 1 and 31 December of the given year. |
||
| 151 | */ |
||
| 152 | 70 | public function getNumberOfLeapYears( $year ) { |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @param int $precision One of the TimeValue::PRECISION_... constants |
||
| 159 | * |
||
| 160 | * @throws InvalidArgumentException |
||
| 161 | * @return float number of seconds in one unit of the given precision |
||
| 162 | */ |
||
| 163 | 10 | public function getSecondsForPrecision( $precision ) { |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @param $timestamp |
||
| 189 | * @param $precision |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | 2 | private function timestampAbsFloor( $timestamp, $precision ) { |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param $timestamp |
||
| 209 | * @param $precision |
||
| 210 | * |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | 2 | private function timestampAbsCeiling( $timestamp, $precision ) { |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @param $precision |
||
| 230 | * |
||
| 231 | * @return int |
||
| 232 | */ |
||
| 233 | 2 | private function charsAffectedByPrecision( $precision ) { |
|
| 281 | |||
| 282 | } |
||
| 283 |