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