Conditions | 9 |
Paths | 26 |
Total Lines | 54 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
54 | public static function humanize($raw) |
||
55 | { |
||
56 | // convert to timestamp |
||
57 | $timestamp = $raw; |
||
58 | if (!Obj::isInt($raw)) { |
||
59 | $timestamp = self::convertToTimestamp($timestamp); |
||
60 | } |
||
61 | // calculate difference between tomorrow day midnight and passed date |
||
62 | $diff = time() - $timestamp; |
||
63 | |||
64 | // date in future, lets return as is |
||
65 | if ($diff < 0) { |
||
66 | return self::convertToDatetime($timestamp, static::FORMAT_TO_SECONDS); |
||
67 | } |
||
68 | |||
69 | // calculate delta and make offset sub. Maybe usage instance of Datetime is better, but localization is sucks! |
||
70 | $deltaSec = $diff % 60; |
||
71 | $diff /= 60; |
||
72 | |||
73 | $deltaMin = $diff % 60; |
||
74 | $diff /= 60; |
||
75 | |||
76 | $deltaHour = $diff % 24; |
||
77 | $diff /= 24; |
||
78 | |||
79 | $deltaDays = ($diff > 1) ? (int)floor($diff) : (int)$diff; |
||
80 | |||
81 | // sounds like more then 1 day's ago |
||
82 | if ($deltaDays > 1) { |
||
83 | if ($deltaDays > 14) { // sounds like more then 2 week ago, just return as is |
||
84 | return self::convertToDatetime($timestamp, static::FORMAT_TO_HOUR); |
||
85 | } |
||
86 | |||
87 | return App::$Translate->get('DateHuman', '%days% days ago', ['days' => (int)$deltaDays]); |
||
88 | } |
||
89 | |||
90 | // sounds like yesterday |
||
91 | if ($deltaDays === 1) { |
||
92 | return App::$Translate->get('DateHuman', 'Yestarday, %hi%', ['hi' => self::convertToDatetime($timestamp, 'H:i')]); |
||
93 | } |
||
94 | |||
95 | // sounds like today, more then 1 hour ago |
||
96 | if ($deltaHour >= 1) { |
||
97 | return App::$Translate->get('DateHuman', '%h% hours ago', ['h' => $deltaHour]); |
||
98 | } |
||
99 | |||
100 | // sounds like last hour ago |
||
101 | if ($deltaMin >= 1) { |
||
102 | return App::$Translate->get('DateHuman', '%m% minutes ago', ['m' => $deltaMin]); |
||
103 | } |
||
104 | |||
105 | // just few seconds left, lets return it |
||
106 | return App::$Translate->get('DateHuman', '%s% seconds ago', ['s' => $deltaSec]); |
||
107 | } |
||
108 | } |