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 ) { |
||
| 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 ) { |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $time an ISO 8601 date and time |
||
| 99 | * @param int $timezone offset from UTC in minutes |
||
| 100 | * |
||
| 101 | * @throws InvalidArgumentException |
||
| 102 | * @return float seconds since 1970-01-01T00:00:00Z |
||
| 103 | */ |
||
| 104 | 8 | private function getSecondsSinceUnixEpoch( $time, $timezone = 0 ) { |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param float $year |
||
| 136 | * |
||
| 137 | * @return bool if the year is a leap year in the Gregorian calendar |
||
| 138 | */ |
||
| 139 | public function isLeapYear( $year ) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param float $year |
||
| 149 | * |
||
| 150 | * @return float The number of leap years since year 1. To be more precise: The number of |
||
| 151 | * leap days in the range between 31 December of year 1 and 31 December of the given year. |
||
| 152 | */ |
||
| 153 | public function getNumberOfLeapYears( $year ) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param int $precision One of the TimeValue::PRECISION_... constants |
||
| 160 | * |
||
| 161 | * @throws InvalidArgumentException |
||
| 162 | * @return float number of seconds in one unit of the given precision |
||
| 163 | */ |
||
| 164 | public function getSecondsForPrecision( $precision ) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param $timestamp |
||
| 190 | * @param $precision |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | private function timestampAbsFloor( $timestamp, $precision ) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param $timestamp |
||
| 210 | * @param $precision |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | private function timestampAbsCeiling( $timestamp, $precision ) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param $precision |
||
| 247 | * |
||
| 248 | * @return int |
||
| 249 | */ |
||
| 250 | private function charsAffectedByPrecision( $precision ) { |
||
| 298 | |||
| 299 | } |
||
| 300 |