Completed
Push — master ( 16548c...92aba0 )
by mehdi
03:02
created

TimeAgo::priority()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace OpenCafe\Tools;
4
5
use OpenCafe\Tools\Lang;
6
use OpenCafe\Datium;
7
use stdClass;
8
9
/**
10
* Calculate Time ago with current date difference
11
*
12
* @package OpenCafe\Datium
13
* @since Jun 17, 2016
14
*/
15
class TimeAgo
16
{
17
18
    private $language;
19
20
    private $output;
21
22
    /**
23
    * TimeAgo Class constructure
24
    *
25
    * @param DateTime $time_difference The time to calculate with now
26
    * @param string $lang Language
27
    */
28
    public function __construct($time_difference, $lang )
29
    {
30
31
      $this->language = $lang;
32
33
      $now   = Datium::now()->object();
34
35
      $this->output =  Datium::diff( $now, $time_difference );
36
37
      $this->year = $this->output->year;
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
      $this->month = $this->output->month;
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...
40
41
      $this->day = $this->output->day;
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...
42
43
      $this->hour = $this->output->hour;
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...
44
45
      $this->minute = $this->output->minute;
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...
46
47
      $this->second = $this->output->second;
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...
48
49
    }
50
51
    /**
52
    * Return difference period as an object
53
    *
54
    * @return object
55
    */
56
    public function all()
57
    {
58
59
      return $result->output;
0 ignored issues
show
Bug introduced by
The variable $result does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
60
61
    }
62
63
    /**
64
    * Read ago config file
65
    *
66
    * @param integer $date date index in ago config file
0 ignored issues
show
Bug introduced by
There is no parameter named $date. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
67
    * @param string $type duration type on ago config file
68
    */
69
    public function read( $value, $type )
70
    {
71
72
      $config = include 'config/ago.php';
73
74
      if( array_key_exists( $value, $config[ $type ] ) ) {
75
76
        return Lang::get( $this->language, $config[ $type ][ $value ] );
77
78
      } else {
79
80
        return $value . " " . Lang::get( $this->language, $config[ $type ][ '*' ] );
81
82
      }
83
84
    }
85
86
    /**
87
    * Show priority of duration
88
    *
89
    * @param integer $date date index in ago config file
90
    * @param string $type duration type on ago config file
91
    */
92
    public function priority( $date, $type )
93
    {
94
95
      if( $date != 0 ) {
96
97
        return $this->read( $date, $type );
98
99
      } else {
100
101
        return false;
102
103
      }
104
105
    }
106
107
    /**
108
    * Return fainal TimeAgo result
109
    *
110
    * @return string
111
    */
112
    public function get()
113
    {
114
115
      $duration = [
116
        'year'   =>  $this->year,
117
        'month'  =>  $this->month,
118
        'day'    =>  $this->day,
119
        'hour'   =>  $this->hour,
120
        'minute' =>  $this->minute,
121
        'second' =>  $this->second
122
      ];
123
124
      foreach( $duration as $index => $value ) {
125
126
        if( $this->priority( $value, $index ) != false ) {
127
128
          return $this->priority( $value, $index );
129
130
        }
131
132
      }
133
134
    }
135
}
136