Complex classes like TimeAgo 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 TimeAgo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class TimeAgo |
||
| 11 | { |
||
| 12 | // defines the number of seconds per "unit" |
||
| 13 | private $secondsPerMinute=60; |
||
| 14 | private $secondsPerHour=3600; |
||
| 15 | private $secondsPerDay=86400; |
||
| 16 | private $secondsPerMonth=2592000; |
||
| 17 | private $secondsPerYear=31536000; // 31622400 seconds on leap years though... |
||
| 18 | private $timezone; |
||
| 19 | private $previousTimezone; |
||
| 20 | |||
| 21 | private static $timeAgoStrings=array( |
||
| 22 | 'aboutOneDay' => "1 天前", |
||
| 23 | 'aboutOneHour' => "大约 1 小时前", |
||
| 24 | 'aboutOneMonth' => "大约 1 个月前", |
||
| 25 | 'aboutOneYear' => "大约 1 年前", |
||
| 26 | 'days' => "%s 天前", |
||
| 27 | 'hours' => "%s 小时前", |
||
| 28 | 'lessThanAMinute' => "1 分钟内", |
||
| 29 | 'lessThanOneHour' => "%s 分钟前", |
||
| 30 | 'months' => "%s 个月前", |
||
| 31 | 'oneMinute' => "1 分钟前", |
||
| 32 | 'years' => "超过 %s 年前" |
||
| 33 | ); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * TimeAgo constructor. |
||
| 37 | * @param null|DateTimeZone $timezone the timezone to use (uses system if none is given) |
||
| 38 | */ |
||
| 39 | public function __construct($timezone=null) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * 析构函数用来恢复时区 |
||
| 48 | */ |
||
| 49 | public function __destruct() { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param integer $past |
||
| 55 | * @param integer $now |
||
| 56 | */ |
||
| 57 | public function inStamp($past, $now=null) { |
||
| 139 | /** |
||
| 140 | * Fetches the different between $past and $now in a spoken format. |
||
| 141 | * NOTE: both past and now should be parseable by strtotime |
||
| 142 | * @param string $past the past date to use |
||
| 143 | * @param string $now the current time, defaults to now (can be an other time though) |
||
| 144 | * @return string the difference in spoken format, e.g. 1 day ago |
||
| 145 | */ |
||
| 146 | public function inWords($past, $now="now") |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Fetches the date difference between the two given dates. |
||
| 162 | * NOTE: both past and now should be parseable by strtotime |
||
| 163 | * |
||
| 164 | * @param string $past the "past" time to parse |
||
| 165 | * @param string $now the "now" time to parse |
||
| 166 | * @return array the difference in dates, using the two dates |
||
| 167 | */ |
||
| 168 | public function dateDifference($past, $now="now") |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Translates the given $label, and adds the given $time. |
||
| 245 | * @param string $label the label to translate |
||
| 246 | * @param string $time the time to add to the translated text. |
||
| 247 | * @return string the translated label text including the time. |
||
| 248 | */ |
||
| 249 | protected function translate($label, $time='') |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * Changes the timezone |
||
| 264 | */ |
||
| 265 | protected function changeTimezone() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Restores a previous timezone |
||
| 276 | */ |
||
| 277 | protected function restoreTimezone() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Checks if the given past is empty |
||
| 287 | * @param string $past the "past" to check |
||
| 288 | * @return bool true if empty, else false |
||
| 289 | */ |
||
| 290 | private function isPastEmpty($past) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Checks if the time difference is less than 29seconds |
||
| 297 | * @param int $timeDifference the time difference in seconds |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | private function isLessThan29Seconds($timeDifference) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Checks if the time difference is less than 1min 29seconds |
||
| 307 | * @param int $timeDifference the time difference in seconds |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | private function isLessThan1Min29Seconds($timeDifference) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Checks if the time difference is less than 44mins 29seconds |
||
| 317 | * @param int $timeDifference the time difference in seconds |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | private function isLessThan44Min29Secs($timeDifference) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Checks if the time difference is less than 1hour 29mins 59seconds |
||
| 328 | * @param int $timeDifference the time difference in seconds |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | private function isLessThan1Hour29Mins59Seconds($timeDifference) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Checks if the time difference is less than 23hours 59mins 29seconds |
||
| 340 | * @param int $timeDifference the time difference in seconds |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | private function isLessThan23Hours59Mins29Seconds($timeDifference) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Checks if the time difference is less than 27hours 59mins 29seconds |
||
| 359 | * @param int $timeDifference the time difference in seconds |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | private function isLessThan47Hours59Mins29Seconds($timeDifference) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Checks if the time difference is less than 29days 23hours 59mins 29seconds |
||
| 379 | * @param int $timeDifference the time difference in seconds |
||
| 380 | * @return bool |
||
| 381 | */ |
||
| 382 | private function isLessThan29Days23Hours59Mins29Seconds($timeDifference) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Checks if the time difference is less than 59days 23hours 59mins 29seconds |
||
| 400 | * @param int $timeDifference the time difference in seconds |
||
| 401 | * @return bool |
||
| 402 | */ |
||
| 403 | private function isLessThan59Days23Hours59Mins29Secs($timeDifference) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Checks if the time difference is less than 1 year |
||
| 422 | * @param int $timeDifference the time difference in seconds |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | private function isLessThan1Year($timeDifference) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Checks if the time difference is less than 2 years |
||
| 439 | * @param int $timeDifference the time difference in seconds |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | private function isLessThan2Years($timeDifference) |
||
| 448 | } |