Conditions | 21 |
Paths | 54 |
Total Lines | 66 |
Code Lines | 44 |
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 | $then = new DateTime($input); |
||
28 | } |
||
29 | |||
30 | $secs = $now->getTimestamp() - $then->getTimestamp(); |
||
31 | |||
32 | $second = 1; |
||
33 | $minute = 60 * $second; |
||
34 | $minuteCut = 60 * $second; |
||
35 | $hour = 60 * $minute; |
||
36 | $hourCut = 90 * $minute; |
||
37 | $day = 24 * $hour; |
||
38 | $dayCut = 48 * $hour; |
||
39 | $week = 7 * $day; |
||
40 | $weekCut = 14 * $day; |
||
41 | $month = 30 * $day; |
||
42 | $monthCut = 60 * $day; |
||
43 | $year = 365 * $day; |
||
44 | $yearCut = $year * 2; |
||
45 | |||
46 | $pluralise = true; |
||
47 | |||
48 | if ($secs <= 10) { |
||
49 | $output = "just now"; |
||
50 | $pluralise = false; |
||
51 | } |
||
52 | elseif ($secs > 10 && $secs < $minuteCut) { |
||
53 | $output = round($secs / $second) . " second"; |
||
54 | } |
||
55 | elseif ($secs >= $minuteCut && $secs < $hourCut) { |
||
56 | $output = round($secs / $minute) . " minute"; |
||
57 | } |
||
58 | elseif ($secs >= $hourCut && $secs < $dayCut) { |
||
59 | $output = round($secs / $hour) . " hour"; |
||
60 | } |
||
61 | elseif ($secs >= $dayCut && $secs < $weekCut) { |
||
62 | $output = round($secs / $day) . " day"; |
||
63 | } |
||
64 | elseif ($secs >= $weekCut && $secs < $monthCut) { |
||
65 | $output = round($secs / $week) . " week"; |
||
66 | } |
||
67 | elseif ($secs >= $monthCut && $secs < $yearCut) { |
||
68 | $output = round($secs / $month) . " month"; |
||
69 | } |
||
70 | elseif ($secs >= $yearCut && $secs < $year * 10) { |
||
71 | $output = round($secs / $year) . " year"; |
||
72 | } |
||
73 | else { |
||
74 | $output = "a long time ago"; |
||
75 | $pluralise = false; |
||
76 | } |
||
77 | |||
78 | if ($pluralise) { |
||
79 | $output = (substr($output, 0, 2) <> "1 ") ? $output . "s ago" : $output . " ago"; |
||
80 | } |
||
81 | |||
82 | return $output; |
||
83 | } |