GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TimeHelperTraits::ago()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 34
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 34
rs 7.6666
c 0
b 0
f 0
cc 10
nc 10
nop 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace Raystech\StarterKit\Traits;
4
5
use Carbon\Carbon;
6
7
trait TimeHelperTraits
8
{
9
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');
0 ignored issues
show
Unused Code introduced by
The assignment to $days is dead and can be removed.
Loading history...
34
    $day  = $time->format('d');
0 ignored issues
show
Unused Code introduced by
The assignment to $day is dead and can be removed.
Loading history...
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
    }
45
  }
46
}