Completed
Push — master ( 1f8425...5030da )
by mehdi
02:52
created

TimeAgo   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 39.09 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
c 1
b 0
f 1
lcom 1
cbo 1
dl 43
loc 110
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 29 1
C get() 43 74 13

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace OpenCafe\Tools;
4
5
use OpenCafe\Tools\Lang;
6
7
class TimeAgo {
8
9
  private $language;
10
11
  public function __construct( $timestamp, $lang ) {
12
13
    $this->language = $lang;
14
15
    $time_ago = strtotime($timestamp->format('Y-m-d H:i:s'));
0 ignored issues
show
Unused Code introduced by
$time_ago is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
16
17
    $cur_time   = time();
18
19
    $time_elapsed   = $cur_time - strtotime($timestamp->format('Y-m-d H:i:s'));
20
21
    $this->second    = $time_elapsed ;
0 ignored issues
show
Bug introduced by
The property second does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
23
    $this->minute    = round($time_elapsed / 60 );
0 ignored issues
show
Bug introduced by
The property minute does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
25
    $this->quarter = round($time_elapsed / 900 );
0 ignored issues
show
Bug introduced by
The property quarter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
27
    $this->half = round($time_elapsed / 1800 );
0 ignored issues
show
Bug introduced by
The property half does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
29
    $this->hour     = round($time_elapsed / 3600);
0 ignored issues
show
Bug introduced by
The property hour does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
31
    $this->day     = round($time_elapsed / 86400 );
0 ignored issues
show
Bug introduced by
The property day does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
33
    $this->week     = round($time_elapsed / 604800);
0 ignored issues
show
Bug introduced by
The property week does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
35
    $this->month     = round($time_elapsed / 2600640 );
0 ignored issues
show
Bug introduced by
The property month does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
37
    $this->year     = round($time_elapsed / 31207680 );
0 ignored issues
show
Bug introduced by
The property year does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
39
  }
40
41
  public function get() {
42
43
    // Seconds
44
    if($this->second <= 60) {
45
        return Lang::get($this->language, "just-now" );
46
    }
47
    //Minutes
48
    else if($this->minute <=60){
49 View Code Duplication
        if($this->minute==1){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            return Lang::get($this->language, 'one-minute') . " " .
51
              Lang::get($this->language, 'ago');
52
        }
53
        else{
54
            return "$this->minute " . Lang::get($this->language, 'minutes')
55
              . " " .
56
              Lang::get($this->language, 'ago');
57
        }
58
    }
59
    //Hours
60
    else if($this->hour <=24){
61 View Code Duplication
        if($this->hour==1){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
          return Lang::get($this->language, 'an-hour') . " " .
63
            Lang::get($this->language, 'ago');
64
        }else{
65
          return "$this->hour " . Lang::get($this->language, 'hours')
66
            . " " .
67
            Lang::get($this->language, 'ago');
68
        }
69
    }
70
    //Days
71
    else if($this->day <= 7){
72
        if($this->day==1){
73
          return Lang::get($this->language, 'yesterday');
74
        }else{
75
          return "$this->day " . Lang::get($this->language, 'days')
76
            . " " .
77
            Lang::get($this->language, 'ago');
78
        }
79
    }
80
    //Weeks
81
    else if($this->week <= 4.3){
82 View Code Duplication
        if($this->week==1){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
          return Lang::get($this->language, 'a-week') . " " .
84
            Lang::get($this->language, 'ago');
85
        }else{
86
          return "$this->week " . Lang::get($this->language, 'weeks')
87
            . " " .
88
            Lang::get($this->language, 'ago');
89
        }
90
    }
91
    //Months
92
    else if($this->month <=12){
93 View Code Duplication
        if($this->month==1){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
          return Lang::get($this->language, 'a-month') . " " .
95
            Lang::get($this->language, 'ago');
96
        }else{
97
          return "$this->month " . Lang::get($this->language, 'months')
98
            . " " .
99
            Lang::get($this->language, 'ago');
100
        }
101
    }
102
    //Years
103 View Code Duplication
    else{
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
        if($this->year==1){
105
          return Lang::get($this->language, 'one-year') . " " .
106
            Lang::get($this->language, 'ago');
107
        }else{
108
          return "$this->year " . Lang::get($this->language, 'years')
109
            . " " .
110
            Lang::get($this->language, 'ago');
111
        }
112
    }
113
114
  }
115
116
}
117