| Conditions | 22 |
| Paths | 55 |
| Total Lines | 71 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 17 | function smarty_modifier_relativedate($input) |
||
| 18 | { |
||
| 19 | $now = new DateTime(); |
||
| 20 | |||
| 21 | if (gettype($input) === 'object' |
||
| 22 | && (get_class($input) === DateTime::class || get_class($input) === DateTimeImmutable::class) |
||
| 23 | ) { |
||
| 24 | $then = $input; |
||
| 25 | } |
||
| 26 | else { |
||
| 27 | try { |
||
| 28 | $then = new DateTime($input); |
||
| 29 | } |
||
| 30 | catch (Exception $ex) { |
||
| 31 | return $input; |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | $secs = $now->getTimestamp() - $then->getTimestamp(); |
||
| 36 | |||
| 37 | $second = 1; |
||
| 38 | $minute = 60 * $second; |
||
| 39 | $minuteCut = 60 * $second; |
||
| 40 | $hour = 60 * $minute; |
||
| 41 | $hourCut = 90 * $minute; |
||
| 42 | $day = 24 * $hour; |
||
| 43 | $dayCut = 48 * $hour; |
||
| 44 | $week = 7 * $day; |
||
| 45 | $weekCut = 14 * $day; |
||
| 46 | $month = 30 * $day; |
||
| 47 | $monthCut = 60 * $day; |
||
| 48 | $year = 365 * $day; |
||
| 49 | $yearCut = $year * 2; |
||
| 50 | |||
| 51 | $pluralise = true; |
||
| 52 | |||
| 53 | if ($secs <= 10) { |
||
| 54 | $output = "just now"; |
||
| 55 | $pluralise = false; |
||
| 56 | } |
||
| 57 | elseif ($secs > 10 && $secs < $minuteCut) { |
||
| 58 | $output = round($secs / $second) . " second"; |
||
| 59 | } |
||
| 60 | elseif ($secs >= $minuteCut && $secs < $hourCut) { |
||
| 61 | $output = round($secs / $minute) . " minute"; |
||
| 62 | } |
||
| 63 | elseif ($secs >= $hourCut && $secs < $dayCut) { |
||
| 64 | $output = round($secs / $hour) . " hour"; |
||
| 65 | } |
||
| 66 | elseif ($secs >= $dayCut && $secs < $weekCut) { |
||
| 67 | $output = round($secs / $day) . " day"; |
||
| 68 | } |
||
| 69 | elseif ($secs >= $weekCut && $secs < $monthCut) { |
||
| 70 | $output = round($secs / $week) . " week"; |
||
| 71 | } |
||
| 72 | elseif ($secs >= $monthCut && $secs < $yearCut) { |
||
| 73 | $output = round($secs / $month) . " month"; |
||
| 74 | } |
||
| 75 | elseif ($secs >= $yearCut && $secs < $year * 10) { |
||
| 76 | $output = round($secs / $year) . " year"; |
||
| 77 | } |
||
| 78 | else { |
||
| 79 | $output = "a long time ago"; |
||
| 80 | $pluralise = false; |
||
| 81 | } |
||
| 82 | |||
| 83 | if ($pluralise) { |
||
| 84 | $output = (substr($output, 0, 2) <> "1 ") ? $output . "s ago" : $output . " ago"; |
||
| 85 | } |
||
| 86 | |||
| 87 | return $output; |
||
| 88 | } |
||
| 89 |