This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
|||||||||||
2 | ||||||||||||
3 | namespace Nip\Utility; |
|||||||||||
4 | ||||||||||||
5 | class Time |
|||||||||||
6 | { |
|||||||||||
7 | ||||||||||||
8 | protected $_value = null; |
|||||||||||
9 | protected $_parts = null; |
|||||||||||
10 | protected $_seconds = null; |
|||||||||||
11 | ||||||||||||
12 | public static function fromString($string) |
|||||||||||
13 | { |
|||||||||||
14 | $o = new self(); |
|||||||||||
15 | $o->_value = $string; |
|||||||||||
16 | $o->parseSeconds(); |
|||||||||||
17 | ||||||||||||
18 | return $o; |
|||||||||||
19 | } |
|||||||||||
20 | ||||||||||||
21 | public function parseSeconds() |
|||||||||||
22 | { |
|||||||||||
23 | $parts = $this->getParts(); |
|||||||||||
24 | if (count($parts) == 3) { |
|||||||||||
25 | $seconds = 0; |
|||||||||||
26 | $seconds += $parts['h'] * 3600; |
|||||||||||
27 | $seconds += $parts['m'] * 60; |
|||||||||||
28 | $seconds += $parts['s']; |
|||||||||||
29 | $this->setSeconds($seconds); |
|||||||||||
30 | } |
|||||||||||
31 | } |
|||||||||||
32 | ||||||||||||
33 | public function getParts() |
|||||||||||
34 | { |
|||||||||||
35 | if ($this->_parts === null) { |
|||||||||||
36 | $this->parseParts(); |
|||||||||||
37 | } |
|||||||||||
38 | ||||||||||||
39 | return $this->_parts; |
|||||||||||
40 | } |
|||||||||||
41 | ||||||||||||
42 | /** |
|||||||||||
43 | * @param array $parts |
|||||||||||
44 | */ |
|||||||||||
45 | public function setParts($parts) |
|||||||||||
46 | { |
|||||||||||
47 | $this->_parts = $parts; |
|||||||||||
48 | } |
|||||||||||
49 | ||||||||||||
50 | public function parseParts() |
|||||||||||
51 | { |
|||||||||||
52 | if ($this->_value && substr_count($this->_value, ':') == 2) { |
|||||||||||
53 | $this->parsePartsFromString(); |
|||||||||||
54 | } elseif ($this->_seconds > 0) { |
|||||||||||
55 | $this->parsePartsFromSeconds(); |
|||||||||||
56 | } |
|||||||||||
57 | } |
|||||||||||
58 | ||||||||||||
59 | public function parsePartsFromString() |
|||||||||||
60 | { |
|||||||||||
61 | list ($h, $m, $s) = explode(':', $this->_value); |
|||||||||||
62 | ||||||||||||
63 | $this->setHoursPart($h); |
|||||||||||
64 | $this->setMinutesPart($m); |
|||||||||||
65 | $this->setSecondsPart($s); |
|||||||||||
66 | } |
|||||||||||
67 | ||||||||||||
68 | /** |
|||||||||||
69 | * @param string $v |
|||||||||||
70 | */ |
|||||||||||
71 | public function setHoursPart($v) |
|||||||||||
72 | { |
|||||||||||
73 | $this->setPart('h', $v); |
|||||||||||
74 | } |
|||||||||||
75 | ||||||||||||
76 | /** |
|||||||||||
77 | * @param string $p |
|||||||||||
78 | * @param string $v |
|||||||||||
79 | */ |
|||||||||||
80 | public function setPart($p, $v) |
|||||||||||
81 | { |
|||||||||||
82 | $this->_parts[$p] = $v; |
|||||||||||
83 | } |
|||||||||||
84 | ||||||||||||
85 | /** |
|||||||||||
86 | * @param string $v |
|||||||||||
87 | */ |
|||||||||||
88 | public function setMinutesPart($v) |
|||||||||||
89 | { |
|||||||||||
90 | $this->setPart('m', $v); |
|||||||||||
91 | } |
|||||||||||
92 | ||||||||||||
93 | /** |
|||||||||||
94 | * @param string $v |
|||||||||||
95 | */ |
|||||||||||
96 | public function setSecondsPart($v) |
|||||||||||
97 | { |
|||||||||||
98 | $this->setPart('s', $v); |
|||||||||||
99 | } |
|||||||||||
100 | ||||||||||||
101 | public function parsePartsFromSeconds() |
|||||||||||
102 | { |
|||||||||||
103 | $seconds = $this->getSeconds(); |
|||||||||||
104 | if ($hours = intval((floor($seconds / 3600)))) { |
|||||||||||
105 | $seconds = $seconds - $hours * 3600; |
|||||||||||
106 | } |
|||||||||||
107 | ||||||||||||
108 | $this->setHoursPart($hours); |
|||||||||||
109 | ||||||||||||
110 | if ($minutes = intval((floor($seconds / 60)))) { |
|||||||||||
111 | $seconds = $seconds - $minutes * 60; |
|||||||||||
112 | } |
|||||||||||
113 | ||||||||||||
114 | $this->setMinutesPart($minutes); |
|||||||||||
115 | ||||||||||||
116 | $seconds = round($seconds, 2); |
|||||||||||
117 | $this->setSecondsPart($seconds); |
|||||||||||
118 | ||||||||||||
119 | } |
|||||||||||
120 | ||||||||||||
121 | public function getSeconds() |
|||||||||||
122 | { |
|||||||||||
123 | if ($this->_seconds === null) { |
|||||||||||
124 | $this->parseSeconds(); |
|||||||||||
125 | } |
|||||||||||
126 | ||||||||||||
127 | return $this->_seconds; |
|||||||||||
128 | } |
|||||||||||
129 | ||||||||||||
130 | /** |
|||||||||||
131 | * @param null $seconds |
|||||||||||
132 | */ |
|||||||||||
133 | public function setSeconds($seconds) |
|||||||||||
134 | { |
|||||||||||
135 | $this->_seconds = $seconds; |
|||||||||||
136 | } |
|||||||||||
137 | ||||||||||||
138 | public static function fromSeconds($seconds) |
|||||||||||
139 | { |
|||||||||||
140 | $o = new self(); |
|||||||||||
141 | $o->setSeconds($seconds); |
|||||||||||
142 | ||||||||||||
143 | return $o; |
|||||||||||
144 | } |
|||||||||||
145 | ||||||||||||
146 | public function getFormatedString() |
|||||||||||
147 | { |
|||||||||||
148 | $return = ''; |
|||||||||||
149 | ||||||||||||
150 | $hours = $this->getHoursPart(); |
|||||||||||
151 | if ($hours OR $return) { |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
152 | $return .= ($return ? ' ' : '').str_pad($hours, 2, 0, STR_PAD_LEFT).'h'; |
|||||||||||
153 | } |
|||||||||||
154 | ||||||||||||
155 | $minutes = $this->getMinutesPart(); |
|||||||||||
156 | if ($minutes OR $return) { |
|||||||||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Using logical operators such as
or instead of || is generally not recommended.
PHP has two types of connecting operators (logical operators, and boolean operators):
The difference between these is the order in which they are executed. In most cases,
you would want to use a boolean operator like Let’s take a look at a few examples: // Logical operators have lower precedence:
$f = false or true;
// is executed like this:
($f = false) or true;
// Boolean operators have higher precedence:
$f = false || true;
// is executed like this:
$f = (false || true);
Logical Operators are used for Control-FlowOne case where you explicitly want to use logical operators is for control-flow such as this: $x === 5
or die('$x must be 5.');
// Instead of
if ($x !== 5) {
die('$x must be 5.');
}
Since // The following is currently a parse error.
$x === 5
or throw new RuntimeException('$x must be 5.');
These limitations lead to logical operators rarely being of use in current PHP code. ![]() |
||||||||||||
157 | $return .= ($return ? ' ' : '').str_pad($minutes, 2, 0, STR_PAD_LEFT).'m'; |
|||||||||||
158 | } |
|||||||||||
159 | ||||||||||||
160 | $seconds = $this->getSecondsPart(); |
|||||||||||
161 | View Code Duplication | if ($seconds) { |
||||||||||
0 ignored issues
–
show
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. ![]() |
||||||||||||
162 | $return .= ($return ? ' ' : '').str_pad($seconds, 2, 0, STR_PAD_LEFT).'s'; |
|||||||||||
163 | } |
|||||||||||
164 | ||||||||||||
165 | return $return; |
|||||||||||
166 | } |
|||||||||||
167 | ||||||||||||
168 | public function getHoursPart() |
|||||||||||
169 | { |
|||||||||||
170 | return $this->getPart('h'); |
|||||||||||
171 | } |
|||||||||||
172 | ||||||||||||
173 | public function getPart($p) |
|||||||||||
174 | { |
|||||||||||
175 | if ($this->_parts === null) { |
|||||||||||
176 | $this->parseParts(); |
|||||||||||
177 | } |
|||||||||||
178 | ||||||||||||
179 | return $this->_parts[$p]; |
|||||||||||
180 | } |
|||||||||||
181 | ||||||||||||
182 | public function getMinutesPart() |
|||||||||||
183 | { |
|||||||||||
184 | return $this->getPart('m'); |
|||||||||||
185 | } |
|||||||||||
186 | ||||||||||||
187 | public function getSecondsPart() |
|||||||||||
188 | { |
|||||||||||
189 | return $this->getPart('s'); |
|||||||||||
190 | } |
|||||||||||
191 | ||||||||||||
192 | public function getDefaultString() |
|||||||||||
193 | { |
|||||||||||
194 | $hours = str_pad($this->getHoursPart(), 2, 0, STR_PAD_LEFT); |
|||||||||||
195 | $minutes = str_pad($this->getMinutesPart(), 2, 0, STR_PAD_LEFT); |
|||||||||||
196 | $seconds = str_pad($this->getSecondsPart(), 2, 0, STR_PAD_LEFT); |
|||||||||||
197 | ||||||||||||
198 | return $hours.':'.$minutes.':'.$seconds; |
|||||||||||
199 | } |
|||||||||||
200 | } |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.