Completed
Push — master ( 197347...f6fd8e )
by mehdi
02:04
created

SimpleDiff::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
dl 0
loc 14
rs 9.4285
c 1
b 0
f 0
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
8
/**
9
* Calculate Time ago with current date difference
10
*
11
* @package OpenCafe\Datium
12
* @since Jun 17, 2016
13
*/
14
class SimpleDiff
15
{
16
17
    /**
18
     * Used language to setted for simple difference
19
     * @var string
20
     */
21
    private $language;
22
23
    /**
24
     * class final result
25
     * @var object
26
     */
27
    private $result;
28
29
    /**
30
     * @var array
31
     */
32
    private $blockList;
33
34
    /**
35
    * SimpleDiff Class constructure
36
    *
37
    * @param DateTime $time_difference The time to calculate with now
0 ignored issues
show
Documentation introduced by
There is no parameter named $time_difference. Did you maybe mean $difference?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
38
    * @param string $lang Language
0 ignored issues
show
Bug introduced by
There is no parameter named $lang. 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...
39
    */
40
    public function __construct($start, $end, $difference )
0 ignored issues
show
Unused Code introduced by
The parameter $start is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $end is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
43
      $this->language = 'en';
44
45
      $this->result =  $difference;
46
47
      $this->blockList = [
48
        'just-now'
49
      ];
50
51
    }
52
53
    /**
54
    * Return difference period as an object
55
    *
56
    * @return object
57
    */
58
    public function all()
59
    {
60
61
      return $result->result;
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...
62
63
    }
64
65
    /**
66
    * Read ago config file
67
    *
68
    * @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...
69
    * @param string $type duration type on ago config file
70
    */
71
    public function read( $value, $type )
72
    {
73
74
      $config = include __DIR__ . '/config/diff.php';
75
76
      if( $this->result->invert ) {
77
78
        $time = ' ' . Lang::get( $this->language, 'remaining' );
79
80
      } else {
81
82
        $time = ' ' . Lang::get( $this->language, 'ago' );
83
84
      }
85
86
      if( isset( $config[ $type ][ $value ] ) && in_array(
87
88
                $config[ $type ][ $value ],
89
90
                $this->blockList
91
92
                ) ) {
93
94
                $time = null;
95
96
                }
97
98
      if( array_key_exists( $value, $config[ $type ] ) ) {
99
100
        $str = Lang::get( $this->language, $config[ $type ][ $value ] )
101
               . $time;
102
103
        return $str;
104
105
      } else {
106
107
        return Lang::getNumbers( $this->language, $value ) . " " . Lang::get(
108
                                          $this->language,
109
                                          $config[ $type ][ '*' ]
110
                                        ) . $time;
111
112
      }
113
114
    }
115
116
    /**
117
    * Show priority of duration
118
    *
119
    * @param integer $date date index in ago config file
120
    * @param string $type duration type on ago config file
121
    */
122
    public function priority( $date, $type )
123
    {
124
125
      if( $date != 0 ) {
126
127
        return $this->read( $date, $type );
128
129
      } else {
130
131
        return false;
132
133
      }
134
135
    }
136
137
    public function lang( $value )
138
    {
139
140
      $this->language = $value;
141
142
      return $this;
143
144
    }
145
146
    /**
147
    * Return fainal SimpleDiff result
148
    *
149
    * @return string
150
    */
151
    public function get()
152
    {
153
154
      $duration = [
155
        'year'   =>  $this->result->year,
156
        'month'  =>  $this->result->month,
157
        'day'    =>  $this->result->day,
158
        'hour'   =>  $this->result->hour,
159
        'minute' =>  $this->result->minute,
160
        'second' =>  $this->result->second
161
      ];
162
163
      foreach( $duration as $index => $value ) {
164
165
        if( $this->priority( $value, $index ) != false ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->priority($value, $index) of type string|false against false; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
166
167
          return $this->priority( $value, $index );
168
169
        }
170
171
      }
172
173
    }
174
}
175