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 | /** |
||
4 | * Copyright 2015 BuzzBoard, Inc. |
||
5 | * |
||
6 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
7 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
8 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||
9 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
10 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||
11 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||
12 | * DEALINGS IN THE SOFTWARE. |
||
13 | * |
||
14 | */ |
||
15 | |||
16 | namespace BuzzBoard\Helpers; |
||
17 | |||
18 | use BuzzBoard\Exceptions\InvalidArgumentException; |
||
19 | |||
20 | /** |
||
21 | * Class BuzzBoard\Helpers\Time |
||
22 | * |
||
23 | * @package BuzzBoard |
||
24 | */ |
||
25 | class Time { |
||
26 | |||
27 | /** |
||
28 | * Returns time ago in words. |
||
29 | * @param int $time |
||
30 | * @return string |
||
31 | */ |
||
32 | public static function getTimeAgoInWords($time = null) { |
||
33 | $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); |
||
34 | $lengths = array("60", "60", "24", "7", "4.35", "12", "10"); |
||
35 | |||
36 | if (null === $time) { |
||
37 | throw new InvalidArgumentException(); |
||
38 | } |
||
39 | |||
40 | $now = time(); |
||
41 | |||
42 | $difference = $now - $time; |
||
43 | $tense = "ago"; |
||
0 ignored issues
–
show
|
|||
44 | |||
45 | for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths) - 1; $j++) { |
||
46 | $difference /= $lengths[$j]; |
||
47 | } |
||
48 | |||
49 | $difference = round($difference); |
||
50 | |||
51 | if ($difference != 1) { |
||
52 | $periods[$j].= "s"; |
||
53 | } |
||
54 | |||
55 | return "$difference $periods[$j] 'ago' "; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Checks whether currently is daytime |
||
60 | * @param float $latitude |
||
61 | * @param float $longitude |
||
62 | * @static |
||
63 | * @return bool |
||
64 | */ |
||
65 | public static function getIsDaytime($latitude, $longitude) { |
||
66 | $time = time(); |
||
67 | $sunrise = strtotime('today ' . static::getSunriseTime($latitude, $longitude)); |
||
68 | $sunset = strtotime('today ' . static::getSunsetTime($latitude, $longitude)); |
||
69 | if ($time >= $sunrise && $time <= $sunset) { |
||
70 | return true; |
||
71 | } |
||
72 | return false; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Returns sunrise time |
||
77 | * @param float $latitude |
||
78 | * @param float $longitude |
||
79 | * @param null|int $timestamp If not set, current timestamp will be used. |
||
80 | * @param null|int $gmtOffset If not set, current GMT offset will be used. |
||
81 | * @static |
||
82 | * @return string |
||
83 | */ |
||
84 | View Code Duplication | public static function getSunriseTime($latitude, $longitude, $timestamp = null, $gmtOffset = null) { |
|
0 ignored issues
–
show
This method seems to be duplicated in 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. ![]() |
|||
85 | $timestamp = self::checkTimestamp($timestamp); |
||
86 | if (!$gmtOffset) { |
||
0 ignored issues
–
show
The expression
$gmtOffset of type null|integer is loosely compared to false ; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
![]() |
|||
87 | $gmtOffset = self::getGmtOffset(); |
||
88 | } |
||
89 | return date_sunrise($timestamp, SUNFUNCS_RET_STRING, $latitude, $longitude, ini_get('date.sunrise_zenith'), $gmtOffset); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Returns sunset time |
||
94 | * @param float $latitude |
||
95 | * @param float $longitude |
||
96 | * @param null|int $timestamp If not set, current timestamp will be used. |
||
97 | * @param null|int $gmtOffset If not set, current GMT offset will be used. |
||
98 | * @static |
||
99 | * @return string |
||
100 | */ |
||
101 | View Code Duplication | public static function getSunsetTime($latitude, $longitude, $timestamp = null, $gmtOffset = null) { |
|
0 ignored issues
–
show
This method seems to be duplicated in 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. ![]() |
|||
102 | $timestamp = self::checkTimestamp($timestamp); |
||
103 | if ($gmtOffset === null) { |
||
104 | $gmtOffset = self::getGmtOffset(); |
||
105 | } |
||
106 | return date_sunset($timestamp, SUNFUNCS_RET_STRING, $latitude, $longitude, ini_get('date.sunrise_zenith'), $gmtOffset); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Calculates and returns current GMT offset |
||
111 | * @return int |
||
112 | */ |
||
113 | public static function getGmtOffset() { |
||
114 | $gmtOffset = date('Z') / 60 / 60; |
||
115 | $gmtOffset = round($gmtOffset); |
||
116 | return $gmtOffset; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Calculates daytime duration |
||
121 | * @param float $latitude |
||
122 | * @param float $longitude |
||
123 | * @param null|int $timestamp If not set, current timestamp will be used. |
||
124 | * @static |
||
125 | * @return int |
||
126 | */ |
||
127 | public static function getDaytimeDuration($latitude, $longitude, $timestamp = null) { |
||
128 | $startTime = strtotime('today ' . self::getSunriseTime($latitude, $longitude, $timestamp)); |
||
129 | $endTime = strtotime('today ' . self::getSunsetTime($latitude, $longitude, $timestamp)); |
||
130 | return $endTime - $startTime; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Converts seconds to human readable format (H:i:s) string. |
||
135 | * @param int $seconds |
||
136 | * @param bool $hideSeconds |
||
137 | * @static |
||
138 | * @return string |
||
139 | */ |
||
140 | public static function getHmsFromSeconds($seconds, $hideSeconds = false) { |
||
141 | $hours = intval(intval($seconds) / 3600); |
||
142 | $hm = $hours; |
||
143 | $minutes = intval(($seconds / 60) % 60); |
||
144 | $hm .= ':' . str_pad($minutes, 2, "0", STR_PAD_LEFT); |
||
145 | if (!$hideSeconds) { |
||
146 | $seconds = intval($seconds % 60); |
||
147 | $hm .= ':' . str_pad($seconds, 2, "0", STR_PAD_LEFT); |
||
148 | } |
||
149 | return $hm; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Returns ISO8601 full datetime |
||
154 | * E.g.: 2014-12-14T09:31:12+00:00 |
||
155 | * @param null|int $timestamp If not set, current timestamp will be used. |
||
156 | * @static |
||
157 | * @return string |
||
158 | */ |
||
159 | public static function getIso8601Date($timestamp = null) { |
||
160 | $timestamp = self::checkTimestamp($timestamp); |
||
161 | return date("c", $timestamp); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Returns RFC 2822 formatted date. |
||
166 | * Usually used in RSS feeds. |
||
167 | * E.g.: Sun, 14 Dec 2014 09:39:45 +0000 |
||
168 | * @param null|int $timestamp If not set, current timestamp will be used. |
||
169 | * @static |
||
170 | * @return string |
||
171 | */ |
||
172 | public static function getRfc2822Date($timestamp = null) { |
||
173 | $timestamp = self::checkTimestamp($timestamp); |
||
174 | return date('r', $timestamp); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Converts timestamp to numeric date (YYYYMMDD) |
||
179 | * E.g.: 20141214 |
||
180 | * @param null|int $timestamp If not set, current timestamp will be used. |
||
181 | * @static |
||
182 | * @return int |
||
183 | */ |
||
184 | public static function getNumericDate($timestamp = null) { |
||
185 | $timestamp = self::checkTimestamp($timestamp); |
||
186 | return (int) date('Ymd', $timestamp); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Converts timestamp to numeric month (YYYYMM) |
||
191 | * E.g.: 201412 |
||
192 | * @param null|int $timestamp If not set, current timestamp will be used. |
||
193 | * @static |
||
194 | * @return int |
||
195 | */ |
||
196 | public static function getNumericMonth($timestamp = null) { |
||
197 | $timestamp = self::checkTimestamp($timestamp); |
||
198 | return (int) date('Ym', $timestamp); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Return datetime. |
||
203 | * Uses same format as mysql. |
||
204 | * E.g.: 2014-12-14 09:46:01 |
||
205 | * @param null|int $timestamp If not set, current timestamp will be used. |
||
206 | * @return string |
||
207 | */ |
||
208 | public static function getTime($timestamp = null) { |
||
209 | $timestamp = self::checkTimestamp($timestamp); |
||
210 | return date("Y-m-d H:i:s", $timestamp); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Return current date |
||
215 | * @param null|int $timestamp If not set, current timestamp will be used. |
||
216 | * @return string |
||
217 | */ |
||
218 | public static function getDate($timestamp = null) { |
||
219 | $timestamp = self::checkTimestamp($timestamp); |
||
220 | return date("Y-m-d", $timestamp); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Return current timestamp |
||
225 | * @param null|int $timestamp If not set, current timestamp will be used. |
||
226 | * @return current timestamp |
||
227 | */ |
||
228 | protected function checkTimestamp($timestamp = null) { |
||
229 | if ($timestamp === null) { |
||
230 | $timestamp = time(); |
||
231 | } |
||
232 | return $timestamp; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Return formatted week number. |
||
237 | * We use ISO-8601 standard, weeks starting on Monday. |
||
238 | * E.g.: 2014-W52 |
||
239 | * @return string |
||
240 | */ |
||
241 | public static function getWeek() { |
||
242 | return date('o-\\WW'); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Return time difference between two timestamps with microseconds. |
||
247 | * @param float $start |
||
248 | * @param null|float $end If not set, current microtime will be used. |
||
249 | * @return float Returns difference in seconds |
||
250 | */ |
||
251 | public static function difference($start, $end = null) { |
||
252 | if ($end === null) { |
||
253 | $end = microtime(true); |
||
254 | } |
||
255 | $totalTime = ($end - $start); |
||
256 | return $totalTime; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Checks is given timestamp is today. |
||
261 | * @param int $timestamp |
||
262 | * @return boolean |
||
263 | */ |
||
264 | public static function isToday($timestamp) { |
||
265 | return date('Y-m-d', $timestamp) == date('Y-m-d'); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Checks is given timestamp is yesterday. |
||
270 | * @param int $timestamp |
||
271 | * @return boolean |
||
272 | */ |
||
273 | public static function isYesterday($timestamp) { |
||
274 | return date('Y-m-d', $timestamp) == date('Y-m-d', strtotime('yesterday')); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Checks is given timestamp is in this week |
||
279 | * We use ISO-8601 standard, weeks starting on Monday. |
||
280 | * @param int $timestamp |
||
281 | * @return boolean True if date is in this week |
||
282 | */ |
||
283 | public static function isThisWeek($timestamp) { |
||
284 | return date('o-\\WW', $timestamp) == date('o-\\WW'); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Checks is given date is in this month |
||
289 | * @param int $date |
||
290 | * @return boolean True if date is in this month |
||
291 | */ |
||
292 | public static function isThisMonth($date) { |
||
293 | return date('m Y', $date) == date('m Y', time()); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Checks is given timestamp is in this year |
||
298 | * @param int $timestamp |
||
299 | * @return boolean True if date is in this year |
||
300 | */ |
||
301 | public static function isThisYear($timestamp) { |
||
302 | return date('Y', $timestamp) == date('Y', time()); |
||
303 | } |
||
304 | |||
305 | } |
||
306 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.