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 | public function inStamp($past, $now = null) { |
||
| 135 | /** |
||
| 136 | * Fetches the different between $past and $now in a spoken format. |
||
| 137 | * NOTE: both past and now should be parseable by strtotime |
||
| 138 | * @param string $past the past date to use |
||
| 139 | * @param string $now the current time, defaults to now (can be an other time though) |
||
| 140 | * @return string the difference in spoken format, e.g. 1 day ago |
||
| 141 | */ |
||
| 142 | public function inWords($past, $now = "now") |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Fetches the date difference between the two given dates. |
||
| 158 | * NOTE: both past and now should be parseable by strtotime |
||
| 159 | * |
||
| 160 | * @param string $past the "past" time to parse |
||
| 161 | * @param string $now the "now" time to parse |
||
| 162 | * @return array the difference in dates, using the two dates |
||
| 163 | */ |
||
| 164 | public function dateDifference($past, $now = "now") |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Translates the given $label, and adds the given $time. |
||
| 241 | * @param string $label the label to translate |
||
| 242 | * @param string $time the time to add to the translated text. |
||
| 243 | * @return string the translated label text including the time. |
||
| 244 | */ |
||
| 245 | protected function translate($label, $time = '') |
||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * Changes the timezone |
||
| 260 | */ |
||
| 261 | protected function changeTimezone() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Restores a previous timezone |
||
| 272 | */ |
||
| 273 | protected function restoreTimezone() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Checks if the given past is empty |
||
| 283 | * @param string $past the "past" to check |
||
| 284 | * @return bool true if empty, else false |
||
| 285 | */ |
||
| 286 | private function isPastEmpty($past) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Checks if the time difference is less than 29seconds |
||
| 293 | * @param int $timeDifference the time difference in seconds |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | private function isLessThan29Seconds($timeDifference) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Checks if the time difference is less than 1min 29seconds |
||
| 303 | * @param int $timeDifference the time difference in seconds |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | private function isLessThan1Min29Seconds($timeDifference) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Checks if the time difference is less than 44mins 29seconds |
||
| 313 | * @param int $timeDifference the time difference in seconds |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | private function isLessThan44Min29Secs($timeDifference) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Checks if the time difference is less than 1hour 29mins 59seconds |
||
| 324 | * @param int $timeDifference the time difference in seconds |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | private function isLessThan1Hour29Mins59Seconds($timeDifference) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Checks if the time difference is less than 23hours 59mins 29seconds |
||
| 336 | * @param int $timeDifference the time difference in seconds |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | private function isLessThan23Hours59Mins29Seconds($timeDifference) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Checks if the time difference is less than 27hours 59mins 29seconds |
||
| 355 | * @param int $timeDifference the time difference in seconds |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | private function isLessThan47Hours59Mins29Seconds($timeDifference) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Checks if the time difference is less than 29days 23hours 59mins 29seconds |
||
| 375 | * @param int $timeDifference the time difference in seconds |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | private function isLessThan29Days23Hours59Mins29Seconds($timeDifference) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Checks if the time difference is less than 59days 23hours 59mins 29seconds |
||
| 396 | * @param int $timeDifference the time difference in seconds |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | private function isLessThan59Days23Hours59Mins29Secs($timeDifference) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Checks if the time difference is less than 1 year |
||
| 418 | * @param int $timeDifference the time difference in seconds |
||
| 419 | * @return bool |
||
| 420 | */ |
||
| 421 | private function isLessThan1Year($timeDifference) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Checks if the time difference is less than 2 years |
||
| 435 | * @param int $timeDifference the time difference in seconds |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | private function isLessThan2Years($timeDifference) |
||
| 444 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.