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