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 | * Scabbia2 Helpers Component |
||
4 | * https://github.com/eserozvataf/scabbia2 |
||
5 | * |
||
6 | * For the full copyright and license information, please view the LICENSE |
||
7 | * file that was distributed with this source code. |
||
8 | * |
||
9 | * @link https://github.com/eserozvataf/scabbia2-helpers for the canonical source repository |
||
10 | * @copyright 2010-2016 Eser Ozvataf. (http://eser.ozvataf.com/) |
||
11 | * @license http://www.apache.org/licenses/LICENSE-2.0 - Apache License, Version 2.0 |
||
12 | */ |
||
13 | |||
14 | namespace Scabbia\Helpers; |
||
15 | |||
16 | /** |
||
17 | * A bunch of utility methods for array operations |
||
18 | * |
||
19 | * @package Scabbia\Helpers |
||
20 | * @author Eser Ozvataf <[email protected]> |
||
21 | * @since 1.0.0 |
||
22 | * |
||
23 | * @scabbia-compile |
||
24 | */ |
||
25 | class Arrays |
||
26 | { |
||
27 | /** |
||
28 | * Flattens given parameters into an array |
||
29 | * |
||
30 | * @param array $uValues values |
||
31 | * |
||
32 | * @return array flatten array |
||
33 | */ |
||
34 | public static function flat(...$uValues) |
||
35 | { |
||
36 | $tArray = []; |
||
37 | |||
38 | foreach ($uValues as $tValue) { |
||
39 | if (is_array($tValue)) { |
||
40 | foreach (self::flat(...$tValue) as $tValue2) { |
||
41 | $tArray[] = $tValue2; |
||
42 | } |
||
43 | |||
44 | continue; |
||
45 | } |
||
46 | |||
47 | $tArray[] = $tValue; |
||
48 | } |
||
49 | |||
50 | return $tArray; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Gets the first element in array, otherwise returns default value |
||
55 | * |
||
56 | * @param array $uArray array |
||
57 | * @param mixed|null $uDefault default value |
||
58 | * |
||
59 | * @return mixed|null first element of array |
||
60 | */ |
||
61 | public static function getFirst(array $uArray, $uDefault = null) |
||
62 | { |
||
63 | $tValue = current($uArray); |
||
64 | if ($tValue === false) { |
||
65 | return $uDefault; |
||
66 | } |
||
67 | |||
68 | return $tValue; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Gets the specified element in array, otherwise returns default value |
||
73 | * |
||
74 | * @param array $uArray array |
||
75 | * @param mixed $uElement key |
||
76 | * @param mixed $uDefault default value |
||
77 | * |
||
78 | * @return mixed|null extracted element |
||
79 | */ |
||
80 | public static function get(array $uArray, $uElement, $uDefault = null) |
||
81 | { |
||
82 | if (!isset($uArray[$uElement])) { |
||
83 | return $uDefault; |
||
84 | } |
||
85 | |||
86 | return $uArray[$uElement]; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Gets the specified elements in array |
||
91 | * |
||
92 | * @param array $uArray array |
||
93 | * @param array $uElements elements |
||
94 | * |
||
95 | * @return array array of extracted elements |
||
96 | */ |
||
97 | public static function getArray(array $uArray, ...$uElements) |
||
98 | { |
||
99 | $tReturn = []; |
||
100 | |||
101 | foreach ($uElements as $tElement) { |
||
102 | $tReturn[$tElement] = isset($uArray[$tElement]) ? $uArray[$tElement] : null; |
||
103 | } |
||
104 | |||
105 | return $tReturn; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Accesses child element by path notation, otherwise returns default value |
||
110 | * |
||
111 | * @param array $uArray array |
||
112 | * @param mixed $uElement key |
||
113 | * @param mixed $uDefault default value |
||
114 | * @param string $uSeparator path separator |
||
115 | * |
||
116 | * @return mixed|null extracted element |
||
117 | */ |
||
118 | public static function getPath(array $uArray, $uElement, $uDefault = null, $uSeparator = "/") |
||
119 | { |
||
120 | $tVariable = $uArray; |
||
121 | |||
122 | foreach (explode($uSeparator, $uElement) as $tKey) { |
||
123 | if (!isset($tVariable[$tKey])) { |
||
124 | return $uDefault; |
||
125 | } |
||
126 | |||
127 | $tVariable = $tVariable[$tKey]; |
||
128 | } |
||
129 | |||
130 | return $tVariable; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Accesses child elements by path notation |
||
135 | * |
||
136 | * @param array $uArray array |
||
137 | * @param array $uElements elements |
||
138 | * |
||
139 | * @return array array of extracted elements |
||
140 | */ |
||
141 | public static function getArrayPath(array $uArray, ...$uElements) |
||
142 | { |
||
143 | $tReturn = []; |
||
144 | |||
145 | foreach ($uElements as $tElement) { |
||
146 | $tVariable = $uArray; |
||
147 | |||
148 | foreach (explode("/", $tElement) as $tKey) { |
||
149 | if (!isset($tVariable[$tKey])) { |
||
150 | $tVariable = null; |
||
151 | break; |
||
152 | } |
||
153 | |||
154 | $tVariable = $tVariable[$tKey]; |
||
155 | } |
||
156 | |||
157 | $tReturn[$tElement] = $tVariable; |
||
158 | } |
||
159 | |||
160 | return $tReturn; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Gets a random element in array |
||
165 | * |
||
166 | * @param array $uArray array |
||
167 | * |
||
168 | * @return mixed|null a random element in the set |
||
169 | */ |
||
170 | public static function getRandom(array $uArray) |
||
171 | { |
||
172 | // mt_srand((int)(microtime(true) * 0xFFFF)); |
||
0 ignored issues
–
show
|
|||
173 | |||
174 | $tCount = count($uArray); |
||
175 | if ($tCount === 0) { |
||
176 | return null; |
||
177 | } |
||
178 | |||
179 | $uValues = array_values($uArray); |
||
180 | |||
181 | return $uValues[mt_rand(0, $tCount - 1)]; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Returns an array filled with the elements in specified range |
||
186 | * |
||
187 | * @param int|float $uMinimum minimum number |
||
188 | * @param int|float $uMaximum maximum number |
||
189 | * @param int|float $uStep step |
||
190 | * @param bool $uWithKeys whether set keys or not |
||
191 | * |
||
192 | * @return array a set contains sequence of numbers in given range |
||
193 | */ |
||
194 | public static function range($uMinimum, $uMaximum, $uStep = 1, $uWithKeys = false) |
||
195 | { |
||
196 | $tReturn = []; |
||
197 | |||
198 | for ($i = $uMinimum; $i <= $uMaximum; $i += $uStep) { |
||
199 | if ($uWithKeys) { |
||
200 | $tReturn[$i] = $i; |
||
201 | continue; |
||
202 | } |
||
203 | |||
204 | $tReturn[] = $i; |
||
205 | } |
||
206 | |||
207 | return $tReturn; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Sorts an array by key |
||
212 | * |
||
213 | * @param array $uArray array |
||
214 | * @param mixed $uField field |
||
215 | * @param string $uOrder order |
||
216 | * |
||
217 | * @return array sorted array |
||
218 | */ |
||
219 | public static function sortByKey(array $uArray, $uField, $uOrder = "asc") |
||
220 | { |
||
221 | $tReturn = []; |
||
222 | if (count($uArray) === 0) { |
||
223 | return $tReturn; |
||
224 | } |
||
225 | |||
226 | $tValues = []; |
||
227 | foreach ($uArray as $tKey => $tValue) { |
||
228 | $tValues[$tKey] = $tValue[$uField]; |
||
229 | } |
||
230 | |||
231 | if ($uOrder === "desc") { |
||
232 | arsort($tValues); |
||
233 | } else { |
||
234 | asort($tValues); |
||
235 | } |
||
236 | |||
237 | foreach (array_keys($tValues) as $tKey) { |
||
238 | $tReturn[] = $uArray[$tKey]; |
||
239 | } |
||
240 | |||
241 | return $tReturn; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Categorizes an array by key |
||
246 | * |
||
247 | * @param array $uArray array |
||
248 | * @param mixed $uKey key |
||
249 | * @param bool $uPreserveKeys preserves keys |
||
250 | * |
||
251 | * @return array categorized array |
||
252 | */ |
||
253 | public static function categorize(array $uArray, $uKey, $uPreserveKeys = false) |
||
254 | { |
||
255 | $tReturn = []; |
||
256 | if (!is_array($uKey)) { |
||
257 | $uKey = [$uKey]; |
||
258 | } |
||
259 | |||
260 | foreach ($uArray as $tRowKey => &$tRow) { |
||
261 | $tRef = & $tReturn; |
||
262 | foreach ($uKey as $tKey) { |
||
263 | $tValue = $tRow[$tKey]; |
||
264 | if (!isset($tRef[$tValue])) { |
||
265 | $tRef[$tValue] = []; |
||
266 | } |
||
267 | $tNewRef = & $tRef[$tValue]; |
||
268 | unset($tRef); |
||
269 | $tRef = & $tNewRef; |
||
270 | } |
||
271 | |||
272 | if ($uPreserveKeys) { |
||
273 | $tRef[$tRowKey] = $tRow; |
||
274 | } else { |
||
275 | $tRef[] = $tRow; |
||
276 | } |
||
277 | } |
||
278 | |||
279 | return $tReturn; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Assigns keys by key |
||
284 | * |
||
285 | * @param array $uArray array |
||
286 | * @param mixed $uKey key |
||
287 | * |
||
288 | * @return array array with new keys |
||
289 | */ |
||
290 | public static function assignKeys(array $uArray, $uKey) |
||
291 | { |
||
292 | $tReturn = []; |
||
293 | |||
294 | foreach ($uArray as $tRow) { |
||
295 | $tReturn[$tRow[$uKey]] = $tRow; |
||
296 | } |
||
297 | |||
298 | return $tReturn; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Extracts specified column from the array |
||
303 | * |
||
304 | * @param array $uArray array |
||
305 | * @param mixed $uKey key |
||
306 | * @param bool $uSkipEmpties whether skip empty entries or not |
||
307 | * @param bool $uDistinct whether returns multiple instances of same entries or not |
||
308 | * |
||
309 | * @return array values of the specified column from a multi-dimensional array |
||
310 | */ |
||
311 | public static function column(array $uArray, $uKey, $uSkipEmpties = false, $uDistinct = false) |
||
312 | { |
||
313 | $tReturn = []; |
||
314 | |||
315 | foreach ($uArray as $tRow) { |
||
316 | if (isset($tRow[$uKey])) { |
||
317 | if (!$uDistinct || !in_array($tRow[$uKey], $tReturn)) { |
||
318 | $tReturn[] = $tRow[$uKey]; |
||
319 | } |
||
320 | } else { |
||
321 | if (!$uSkipEmpties) { |
||
322 | $tReturn[] = null; |
||
323 | } |
||
324 | } |
||
325 | } |
||
326 | |||
327 | return $tReturn; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Extracts specified columns from the array |
||
332 | * |
||
333 | * @param array $uArray array |
||
334 | * @param array $uKeys keys |
||
335 | * |
||
336 | * @return array values of the specified column from a multi-dimensional array |
||
337 | */ |
||
338 | public static function columns(array $uArray, ...$uKeys) |
||
339 | { |
||
340 | $tReturn = []; |
||
341 | |||
342 | foreach ($uArray as $tRow) { |
||
343 | $tReturnRow = []; |
||
344 | |||
345 | foreach ($uKeys as $tKey) { |
||
346 | if (isset($tRow[$tKey])) { |
||
347 | $tReturnRow[$tKey] = $tRow[$tKey]; |
||
348 | } |
||
349 | } |
||
350 | |||
351 | $tReturn[] = $tReturnRow; |
||
352 | } |
||
353 | |||
354 | return $tReturn; |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Gets the first matching row from a multi-dimensional array |
||
359 | * |
||
360 | * @param array $uArray array |
||
361 | * @param mixed $uKey key |
||
362 | * @param mixed $uValue value |
||
363 | * |
||
364 | * @return array|bool entire row matches the condition |
||
365 | */ |
||
366 | View Code Duplication | public static function getRow(array $uArray, $uKey, $uValue) |
|
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. ![]() |
|||
367 | { |
||
368 | foreach ($uArray as $tRow) { |
||
369 | if (isset($tRow[$uKey]) && $tRow[$uKey] === $uValue) { |
||
370 | return $tRow; |
||
371 | } |
||
372 | } |
||
373 | |||
374 | return false; |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Gets the first matching row's key |
||
379 | * |
||
380 | * @param array $uArray array |
||
381 | * @param mixed $uKey key |
||
382 | * @param mixed $uValue value |
||
383 | * |
||
384 | * @return mixed|bool key of row matches the condition |
||
385 | */ |
||
386 | View Code Duplication | public static function getRowKey(array $uArray, $uKey, $uValue) |
|
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. ![]() |
|||
387 | { |
||
388 | foreach ($uArray as $tKey => $tRow) { |
||
389 | if (isset($tRow[$uKey]) && $tRow[$uKey] === $uValue) { |
||
390 | return $tKey; |
||
391 | } |
||
392 | } |
||
393 | |||
394 | return false; |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Gets the matching rows |
||
399 | * |
||
400 | * @param array $uArray array |
||
401 | * @param mixed $uKey key |
||
402 | * @param mixed $uValue value |
||
403 | * |
||
404 | * @return array set of elements matches the condition |
||
405 | */ |
||
406 | View Code Duplication | public static function getRows(array $uArray, $uKey, $uValue) |
|
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. ![]() |
|||
407 | { |
||
408 | $tReturn = []; |
||
409 | |||
410 | foreach ($uArray as $tKey => $tRow) { |
||
411 | if (isset($tRow[$uKey]) && $tRow[$uKey] === $uValue) { |
||
412 | $tReturn[$tKey] = $tRow; |
||
413 | } |
||
414 | } |
||
415 | |||
416 | return $tReturn; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Gets the not matching rows |
||
421 | * |
||
422 | * @param array $uArray array |
||
423 | * @param mixed $uKey key |
||
424 | * @param mixed $uValue value |
||
425 | * |
||
426 | * @return array set of elements not matches the condition |
||
427 | */ |
||
428 | View Code Duplication | public static function getRowsBut(array $uArray, $uKey, $uValue) |
|
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. ![]() |
|||
429 | { |
||
430 | $tReturn = []; |
||
431 | |||
432 | foreach ($uArray as $tKey => $tRow) { |
||
433 | if (isset($tRow[$uKey]) && $tRow[$uKey] !== $uValue) { |
||
434 | $tReturn[$tKey] = $tRow; |
||
435 | } |
||
436 | } |
||
437 | |||
438 | return $tReturn; |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * Combines two arrays properly |
||
443 | * |
||
444 | * @param array $uArray1 first array |
||
445 | * @param array $uArray2 second array |
||
446 | * |
||
447 | * @return array combined array |
||
448 | */ |
||
449 | public static function combine(array $uArray1, array $uArray2) |
||
450 | { |
||
451 | $tArray = []; |
||
452 | |||
453 | for ($i = 0, $tLen = count($uArray1); $i < $tLen; $i++) { |
||
454 | if (!isset($uArray2[$i])) { |
||
455 | $tArray[$uArray1[$i]] = null; |
||
456 | continue; |
||
457 | } |
||
458 | |||
459 | $tArray[$uArray1[$i]] = $uArray2[$i]; |
||
460 | } |
||
461 | |||
462 | return $tArray; |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Combines two or more arrays |
||
467 | * |
||
468 | * @param array $uArgs arguments |
||
469 | * |
||
470 | * @return array combined array |
||
471 | */ |
||
472 | public static function combine2(...$uArgs) |
||
473 | { |
||
474 | $tArray = []; |
||
475 | |||
476 | for ($i = 0; true; $i++) { |
||
477 | $tValues = []; |
||
478 | $tAllNull = true; |
||
479 | |||
480 | foreach ($uArgs as $tArg) { |
||
481 | if (isset($tArg[$i])) { |
||
482 | $tAllNull = false; |
||
483 | $tValues[] = $tArg[$i]; |
||
484 | continue; |
||
485 | } |
||
486 | |||
487 | $tValues[] = null; |
||
488 | } |
||
489 | |||
490 | if ($tAllNull === true) { |
||
491 | break; |
||
492 | } |
||
493 | |||
494 | $tArray[] = $tValues; |
||
495 | } |
||
496 | |||
497 | return $tArray; |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Sorts an array by priority list |
||
502 | * |
||
503 | * @param array $uArray array |
||
504 | * @param array $uPriorities list of priorities |
||
505 | * |
||
506 | * @return array sorted array |
||
507 | */ |
||
508 | public static function sortByPriority(array $uArray, $uPriorities) |
||
509 | { |
||
510 | $tArray = []; |
||
511 | |||
512 | foreach ($uPriorities as $tKey) { |
||
513 | if (!isset($uArray[$tKey])) { |
||
514 | continue; |
||
515 | } |
||
516 | |||
517 | $tArray[$tKey] = $uArray[$tKey]; |
||
518 | } |
||
519 | |||
520 | // union of arrays |
||
521 | return $tArray + $uArray; |
||
522 | } |
||
523 | } |
||
524 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.