Conditions | 10 |
Paths | 10 |
Total Lines | 34 |
Code Lines | 26 |
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 |
||
10 | public function ago() |
||
11 | { |
||
12 | if(!$this->created_at) { |
||
13 | return 'n/a'; |
||
14 | } |
||
15 | $time = Carbon::parse($this->created_at); |
||
16 | $diff = $time->diffInSeconds(Carbon::now()); |
||
17 | if ($diff == 0) { |
||
18 | return 'Just now'; |
||
19 | } |
||
20 | $intervals = array( |
||
21 | 1 => array('year', 31556926), |
||
22 | $diff < 31556926 => array('month', 2628000), |
||
23 | $diff < 2629744 => array('week', 604800), |
||
24 | $diff < 604800 => array('day', 86400), |
||
25 | $diff < 86400 => array('hr', 3600), |
||
26 | $diff < 3600 => array('min', 60), |
||
27 | $diff < 60 => array('second', 1), |
||
28 | ); |
||
29 | $value = floor($diff / $intervals[1][1]); |
||
30 | $ago = $value.' '.$intervals[1][0].($value > 1 ? 's' : ''); |
||
31 | |||
32 | // $ago = $value . ' ' . $intervals[1][0]; |
||
33 | $days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); |
||
|
|||
34 | $day = $time->format('d'); |
||
35 | // return $intervals[1][0]; |
||
36 | if ($ago == '1 day') { |
||
37 | return 'Yesterday at ' . $time->format('H:i'); |
||
38 | } elseif ($value <= 59 && $intervals[1][0] == 'second' || $intervals[1][0] == 'min' || $intervals[1][0] == 'hr') { |
||
39 | return $ago; |
||
40 | } elseif($time->year == Carbon::now()->year) { |
||
41 | return $time->format('M d') . ' at ' . $time->format('H:i'); |
||
42 | } else { |
||
43 | return $time->format('M d, Y') . ' at ' . $time->format('H:i'); |
||
44 | } |
||
46 | } |