Total Complexity | 114 |
Total Lines | 631 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Arr often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Arr, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Arr |
||
10 | { |
||
11 | /** |
||
12 | * Determine whether the given value is array accessible. |
||
13 | * |
||
14 | * @param mixed $value |
||
15 | * @return bool |
||
16 | */ |
||
17 | public static function accessible($value) |
||
18 | { |
||
19 | return is_array($value) || $value instanceof ArrayAccess; |
||
20 | } |
||
21 | /** |
||
22 | * Add an element to an array using "dot" notation if it doesn't exist. |
||
23 | * |
||
24 | * @param array $array |
||
25 | * @param string $key |
||
26 | * @param mixed $value |
||
27 | * @return array |
||
28 | */ |
||
29 | public static function add($array, $key, $value) |
||
30 | { |
||
31 | if (is_null(static::get($array, $key))) { |
||
32 | static::set($array, $key, $value); |
||
33 | } |
||
34 | return $array; |
||
35 | } |
||
36 | /** |
||
37 | * Collapse an array of arrays into a single array. |
||
38 | * |
||
39 | * @param array $array |
||
40 | * @return array |
||
41 | */ |
||
42 | public static function collapse($array) |
||
43 | { |
||
44 | $results = []; |
||
45 | foreach ($array as $values) { |
||
46 | if ($values instanceof Collection) { |
||
|
|||
47 | $values = $values->all(); |
||
48 | } elseif (! is_array($values)) { |
||
49 | continue; |
||
50 | } |
||
51 | $results = array_merge($results, $values); |
||
52 | } |
||
53 | return $results; |
||
54 | } |
||
55 | /** |
||
56 | * Cross join the given arrays, returning all possible permutations. |
||
57 | * |
||
58 | * @param array ...$arrays |
||
59 | * @return array |
||
60 | */ |
||
61 | public static function crossJoin(...$arrays) |
||
62 | { |
||
63 | $results = [[]]; |
||
64 | foreach ($arrays as $index => $array) { |
||
65 | $append = []; |
||
66 | foreach ($results as $product) { |
||
67 | foreach ($array as $item) { |
||
68 | $product[$index] = $item; |
||
69 | $append[] = $product; |
||
70 | } |
||
71 | } |
||
72 | $results = $append; |
||
73 | } |
||
74 | return $results; |
||
75 | } |
||
76 | /** |
||
77 | * Divide an array into two arrays. One with keys and the other with values. |
||
78 | * |
||
79 | * @param array $array |
||
80 | * @return array |
||
81 | */ |
||
82 | public static function divide($array) |
||
83 | { |
||
84 | return [array_keys($array), array_values($array)]; |
||
85 | } |
||
86 | /** |
||
87 | * Flatten a multi-dimensional associative array with dots. |
||
88 | * |
||
89 | * @param array $array |
||
90 | * @param string $prepend |
||
91 | * @return array |
||
92 | */ |
||
93 | public static function dot($array, $prepend = '') |
||
94 | { |
||
95 | $results = []; |
||
96 | foreach ($array as $key => $value) { |
||
97 | if (is_array($value) && ! empty($value)) { |
||
98 | $results = array_merge($results, static::dot($value, $prepend.$key.'.')); |
||
99 | } else { |
||
100 | $results[$prepend.$key] = $value; |
||
101 | } |
||
102 | } |
||
103 | return $results; |
||
104 | } |
||
105 | /** |
||
106 | * Get all of the given array except for a specified array of keys. |
||
107 | * |
||
108 | * @param array $array |
||
109 | * @param array|string $keys |
||
110 | * @return array |
||
111 | */ |
||
112 | public static function except($array, $keys) |
||
113 | { |
||
114 | static::forget($array, $keys); |
||
115 | return $array; |
||
116 | } |
||
117 | /** |
||
118 | * Determine if the given key exists in the provided array. |
||
119 | * |
||
120 | * @param \ArrayAccess|array $array |
||
121 | * @param string|int $key |
||
122 | * @return bool |
||
123 | */ |
||
124 | public static function exists($array, $key) |
||
125 | { |
||
126 | if ($array instanceof ArrayAccess) { |
||
127 | return $array->offsetExists($key); |
||
128 | } |
||
129 | return array_key_exists($key, $array); |
||
130 | } |
||
131 | /** |
||
132 | * Return the first element in an array passing a given truth test. |
||
133 | * |
||
134 | * @param array $array |
||
135 | * @param callable|null $callback |
||
136 | * @param mixed $default |
||
137 | * @return mixed |
||
138 | */ |
||
139 | public static function first($array, callable $callback = null, $default = null) |
||
140 | { |
||
141 | if (is_null($callback)) { |
||
142 | if (empty($array)) { |
||
143 | return value($default); |
||
144 | } |
||
145 | foreach ($array as $item) { |
||
146 | return $item; |
||
147 | } |
||
148 | } |
||
149 | foreach ($array as $key => $value) { |
||
150 | if (call_user_func($callback, $value, $key)) { |
||
151 | return $value; |
||
152 | } |
||
153 | } |
||
154 | return value($default); |
||
155 | } |
||
156 | /** |
||
157 | * Return the last element in an array passing a given truth test. |
||
158 | * |
||
159 | * @param array $array |
||
160 | * @param callable|null $callback |
||
161 | * @param mixed $default |
||
162 | * @return mixed |
||
163 | */ |
||
164 | public static function last($array, callable $callback = null, $default = null) |
||
165 | { |
||
166 | if (is_null($callback)) { |
||
167 | return empty($array) ? value($default) : end($array); |
||
168 | } |
||
169 | return static::first(array_reverse($array, true), $callback, $default); |
||
170 | } |
||
171 | /** |
||
172 | * Flatten a multi-dimensional array into a single level. |
||
173 | * |
||
174 | * @param array $array |
||
175 | * @param int $depth |
||
176 | * @return array |
||
177 | */ |
||
178 | public static function flatten($array, $depth = INF) |
||
179 | { |
||
180 | $result = []; |
||
181 | foreach ($array as $item) { |
||
182 | $item = $item instanceof Collection ? $item->all() : $item; |
||
183 | if (! is_array($item)) { |
||
184 | $result[] = $item; |
||
185 | } elseif ($depth === 1) { |
||
186 | $result = array_merge($result, array_values($item)); |
||
187 | } else { |
||
188 | $result = array_merge($result, static::flatten($item, $depth - 1)); |
||
189 | } |
||
190 | } |
||
191 | return $result; |
||
192 | } |
||
193 | /** |
||
194 | * Remove one or many array items from a given array using "dot" notation. |
||
195 | * |
||
196 | * @param array $array |
||
197 | * @param array|string $keys |
||
198 | * @return void |
||
199 | */ |
||
200 | public static function forget(&$array, $keys) |
||
201 | { |
||
202 | $original = &$array; |
||
203 | $keys = (array) $keys; |
||
204 | if (count($keys) === 0) { |
||
205 | return; |
||
206 | } |
||
207 | foreach ($keys as $key) { |
||
208 | // if the exact key exists in the top-level, remove it |
||
209 | if (static::exists($array, $key)) { |
||
210 | unset($array[$key]); |
||
211 | continue; |
||
212 | } |
||
213 | $parts = explode('.', $key); |
||
214 | // clean up before each pass |
||
215 | $array = &$original; |
||
216 | while (count($parts) > 1) { |
||
217 | $part = array_shift($parts); |
||
218 | if (isset($array[$part]) && is_array($array[$part])) { |
||
219 | $array = &$array[$part]; |
||
220 | } else { |
||
221 | continue 2; |
||
222 | } |
||
223 | } |
||
224 | unset($array[array_shift($parts)]); |
||
225 | } |
||
226 | } |
||
227 | /** |
||
228 | * Get an item from an array using "dot" notation. |
||
229 | * |
||
230 | * @param \ArrayAccess|array $array |
||
231 | * @param string $key |
||
232 | * @param mixed $default |
||
233 | * @return mixed |
||
234 | */ |
||
235 | public static function get($array, $key, $default = null) |
||
257 | } |
||
258 | /** |
||
259 | * Check if an item or items exist in an array using "dot" notation. |
||
260 | * |
||
261 | * @param \ArrayAccess|array $array |
||
262 | * @param string|array $keys |
||
263 | * @return bool |
||
264 | */ |
||
265 | public static function has($array, $keys) |
||
266 | { |
||
267 | if (is_null($keys)) { |
||
268 | return false; |
||
269 | } |
||
270 | $keys = (array) $keys; |
||
271 | if (! $array) { |
||
272 | return false; |
||
273 | } |
||
274 | if ($keys === []) { |
||
275 | return false; |
||
276 | } |
||
277 | foreach ($keys as $key) { |
||
278 | $subKeyArray = $array; |
||
279 | if (static::exists($array, $key)) { |
||
280 | continue; |
||
281 | } |
||
282 | foreach (explode('.', $key) as $segment) { |
||
283 | if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) { |
||
284 | $subKeyArray = $subKeyArray[$segment]; |
||
285 | } else { |
||
286 | return false; |
||
287 | } |
||
288 | } |
||
289 | } |
||
290 | return true; |
||
291 | } |
||
292 | /** |
||
293 | * Determines if an array is associative. |
||
294 | * |
||
295 | * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. |
||
296 | * |
||
297 | * @param array $array |
||
298 | * @return bool |
||
299 | */ |
||
300 | public static function isAssoc(array $array) |
||
301 | { |
||
302 | $keys = array_keys($array); |
||
303 | return array_keys($keys) !== $keys; |
||
304 | } |
||
305 | /** |
||
306 | * Get a subset of the items from the given array. |
||
307 | * |
||
308 | * @param array $array |
||
309 | * @param array|string $keys |
||
310 | * @return array |
||
311 | */ |
||
312 | public static function only($array, $keys) |
||
313 | { |
||
314 | return array_intersect_key($array, array_flip((array) $keys)); |
||
315 | } |
||
316 | /** |
||
317 | * Pluck an array of values from an array. |
||
318 | * |
||
319 | * @param array $array |
||
320 | * @param string|array $value |
||
321 | * @param string|array|null $key |
||
322 | * @return array |
||
323 | */ |
||
324 | public static function pluck($array, $value, $key = null) |
||
325 | { |
||
326 | $results = []; |
||
327 | [$value, $key] = static::explodePluckParameters($value, $key); |
||
328 | foreach ($array as $item) { |
||
329 | $itemValue = data_get($item, $value); |
||
330 | // If the key is "null", we will just append the value to the array and keep |
||
331 | // looping. Otherwise we will key the array using the value of the key we |
||
332 | // received from the developer. Then we'll return the final array form. |
||
333 | if (is_null($key)) { |
||
334 | $results[] = $itemValue; |
||
335 | } else { |
||
336 | $itemKey = data_get($item, $key); |
||
337 | if (is_object($itemKey) && method_exists($itemKey, '__toString')) { |
||
338 | $itemKey = (string) $itemKey; |
||
339 | } |
||
340 | $results[$itemKey] = $itemValue; |
||
341 | } |
||
342 | } |
||
343 | return $results; |
||
344 | } |
||
345 | /** |
||
346 | * Explode the "value" and "key" arguments passed to "pluck". |
||
347 | * |
||
348 | * @param string|array $value |
||
349 | * @param string|array|null $key |
||
350 | * @return array |
||
351 | */ |
||
352 | protected static function explodePluckParameters($value, $key) |
||
353 | { |
||
354 | $value = is_string($value) ? explode('.', $value) : $value; |
||
355 | $key = is_null($key) || is_array($key) ? $key : explode('.', $key); |
||
356 | return [$value, $key]; |
||
357 | } |
||
358 | /** |
||
359 | * Push an item onto the beginning of an array. |
||
360 | * |
||
361 | * @param array $array |
||
362 | * @param mixed $value |
||
363 | * @param mixed $key |
||
364 | * @return array |
||
365 | */ |
||
366 | public static function prepend($array, $value, $key = null) |
||
367 | { |
||
368 | if (is_null($key)) { |
||
369 | array_unshift($array, $value); |
||
370 | } else { |
||
371 | $array = [$key => $value] + $array; |
||
372 | } |
||
373 | return $array; |
||
374 | } |
||
375 | /** |
||
376 | * Get a value from the array, and remove it. |
||
377 | * |
||
378 | * @param array $array |
||
379 | * @param string $key |
||
380 | * @param mixed $default |
||
381 | * @return mixed |
||
382 | */ |
||
383 | public static function pull(&$array, $key, $default = null) |
||
384 | { |
||
385 | $value = static::get($array, $key, $default); |
||
386 | static::forget($array, $key); |
||
387 | return $value; |
||
388 | } |
||
389 | /** |
||
390 | * Get one or a specified number of random values from an array. |
||
391 | * |
||
392 | * @param array $array |
||
393 | * @param int|null $number |
||
394 | * @return mixed |
||
395 | * |
||
396 | * @throws \InvalidArgumentException |
||
397 | */ |
||
398 | public static function random($array, $number = null) |
||
399 | { |
||
400 | $requested = is_null($number) ? 1 : $number; |
||
401 | $count = count($array); |
||
402 | if ($requested > $count) { |
||
403 | throw new InvalidArgumentException( |
||
404 | "You requested {$requested} items, but there are only {$count} items available." |
||
405 | ); |
||
406 | } |
||
407 | if (is_null($number)) { |
||
408 | return $array[array_rand($array)]; |
||
409 | } |
||
410 | if ((int) $number === 0) { |
||
411 | return []; |
||
412 | } |
||
413 | $keys = array_rand($array, $number); |
||
414 | $results = []; |
||
415 | foreach ((array) $keys as $key) { |
||
416 | $results[] = $array[$key]; |
||
417 | } |
||
418 | return $results; |
||
419 | } |
||
420 | /** |
||
421 | * Set an array item to a given value using "dot" notation. |
||
422 | * |
||
423 | * If no key is given to the method, the entire array will be replaced. |
||
424 | * |
||
425 | * @param array $array |
||
426 | * @param string $key |
||
427 | * @param mixed $value |
||
428 | * @return array |
||
429 | */ |
||
430 | public static function set(&$array, $key, $value) |
||
431 | { |
||
432 | if (is_null($key)) { |
||
433 | return $array = $value; |
||
434 | } |
||
435 | $keys = explode('.', $key); |
||
436 | while (count($keys) > 1) { |
||
437 | $key = array_shift($keys); |
||
438 | // If the key doesn't exist at this depth, we will just create an empty array |
||
439 | // to hold the next value, allowing us to create the arrays to hold final |
||
440 | // values at the correct depth. Then we'll keep digging into the array. |
||
441 | if (! isset($array[$key]) || ! is_array($array[$key])) { |
||
442 | $array[$key] = []; |
||
443 | } |
||
444 | $array = &$array[$key]; |
||
445 | } |
||
446 | $array[array_shift($keys)] = $value; |
||
447 | return $array; |
||
448 | } |
||
449 | /** |
||
450 | * Shuffle the given array and return the result. |
||
451 | * |
||
452 | * @param array $array |
||
453 | * @param int|null $seed |
||
454 | * @return array |
||
455 | */ |
||
456 | public static function shuffle($array, $seed = null) |
||
457 | { |
||
458 | if (is_null($seed)) { |
||
459 | shuffle($array); |
||
460 | } else { |
||
461 | srand($seed); |
||
462 | usort($array, function () { |
||
463 | return rand(-1, 1); |
||
464 | }); |
||
465 | } |
||
466 | return $array; |
||
467 | } |
||
468 | /** |
||
469 | * Sort the array using the given callback or "dot" notation. |
||
470 | * |
||
471 | * @param array $array |
||
472 | * @param callable|string|null $callback |
||
473 | * @return array |
||
474 | */ |
||
475 | public static function sort($array, $callback = null) |
||
476 | { |
||
477 | return Collection::make($array)->sortBy($callback)->all(); |
||
478 | } |
||
479 | /** |
||
480 | * Recursively sort an array by keys and values. |
||
481 | * |
||
482 | * @param array $array |
||
483 | * @return array |
||
484 | */ |
||
485 | public static function sortRecursive($array) |
||
498 | } |
||
499 | /** |
||
500 | * Convert the array into a query string. |
||
501 | * |
||
502 | * @param array $array |
||
503 | * @return string |
||
504 | */ |
||
505 | public static function query($array) |
||
506 | { |
||
507 | return http_build_query($array, null, '&', PHP_QUERY_RFC3986); |
||
508 | } |
||
509 | /** |
||
510 | * Filter the array using the given callback. |
||
511 | * |
||
512 | * @param array $array |
||
513 | * @param callable $callback |
||
514 | * @return array |
||
515 | */ |
||
516 | public static function where($array, callable $callback) |
||
517 | { |
||
518 | return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH); |
||
519 | } |
||
520 | /** |
||
521 | * If the given value is not an array and not null, wrap it in one. |
||
522 | * |
||
523 | * @param mixed $value |
||
524 | * @return array |
||
525 | */ |
||
526 | public static function wrap($value) |
||
527 | { |
||
528 | if (is_null($value)) { |
||
529 | return []; |
||
530 | } |
||
531 | return is_array($value) ? $value : [$value]; |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * @param array $removeArray |
||
536 | * @param array $referenceArray |
||
537 | * @return array |
||
538 | */ |
||
539 | public static function removeSameValues($removeArray=array(),$referenceArray=array()) |
||
540 | { |
||
541 | $list = []; |
||
542 | |||
543 | foreach ($removeArray as $remove){ |
||
544 | if(!in_array($remove,$referenceArray)){ |
||
545 | $list[]=$remove; |
||
546 | } |
||
547 | } |
||
548 | return array_merge($referenceArray,$list); |
||
549 | } |
||
550 | |||
551 | /** |
||
552 | * @param $array |
||
553 | * @param $key |
||
554 | * @return null |
||
555 | */ |
||
556 | public static function isset($array,$key) |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * @param $array |
||
566 | * @param null $withKey |
||
567 | * @return bool |
||
568 | */ |
||
569 | public static function isArrayWithCount($array,$withKey=null) |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * @param $array1 |
||
580 | * @param $array2 |
||
581 | * @return int |
||
582 | */ |
||
583 | public static function arrayDiffKey($array1,$array2) |
||
584 | { |
||
585 | $forArray1 = array_diff_key($array1,$array2); |
||
586 | $forArray2 = array_diff_key($array2,$array1); |
||
587 | |||
588 | if(count($forArray1)=="0" && count($forArray2)=="0"){ |
||
589 | return true; |
||
590 | } |
||
591 | return false; |
||
592 | } |
||
593 | |||
594 | /** |
||
595 | * overwrite with |
||
596 | * |
||
597 | * @param $original |
||
598 | * @param array $overwrite |
||
599 | */ |
||
600 | public static function overwriteWith($original,$overwrite=array()) |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * remove key from array |
||
614 | * |
||
615 | * @param $data |
||
616 | * @param array $remove |
||
617 | * @return array |
||
618 | */ |
||
619 | public static function removeKey($data,$remove=array()) |
||
620 | { |
||
621 | $list = []; |
||
622 | |||
623 | foreach($data as $key=>$value){ |
||
624 | if(!in_array($key,$remove)){ |
||
625 | $list[$key] = $value; |
||
626 | } |
||
627 | } |
||
628 | |||
629 | return $list; |
||
630 | } |
||
631 | |||
632 | /** |
||
633 | * @param array $data |
||
634 | * @param $key |
||
635 | * @param $value |
||
636 | */ |
||
637 | public static function setNested($data=array(),$key,$value) |
||
642 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths