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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 | use Macroable; |
||
12 | |||
13 | /** |
||
14 | * Determine whether the given value is array accessible. |
||
15 | * |
||
16 | * @param mixed $value |
||
17 | * @return bool |
||
18 | */ |
||
19 | 99 | public static function accessible($value) |
|
23 | |||
24 | /** |
||
25 | * Add an element to an array using "dot" notation if it doesn't exist. |
||
26 | * |
||
27 | * @param array $array |
||
28 | * @param string $key |
||
29 | * @param mixed $value |
||
30 | * @return array |
||
31 | */ |
||
32 | 1 | public static function add($array, $key, $value) |
|
40 | |||
41 | /** |
||
42 | * Collapse an array of arrays into a single array. |
||
43 | * |
||
44 | * @param array $array |
||
45 | * @return array |
||
46 | */ |
||
47 | 5 | public static function collapse($array) |
|
63 | |||
64 | /** |
||
65 | * Cross join the given arrays, returning all possible permutations. |
||
66 | * |
||
67 | * @param array ...$arrays |
||
68 | * @return array |
||
69 | */ |
||
70 | 3 | public static function crossJoin(...$arrays) |
|
90 | |||
91 | /** |
||
92 | * Divide an array into two arrays. One with keys and the other with values. |
||
93 | * |
||
94 | * @param array $array |
||
95 | * @return array |
||
96 | */ |
||
97 | 1 | public static function divide($array) |
|
101 | |||
102 | /** |
||
103 | * Flatten a multi-dimensional associative array with dots. |
||
104 | * |
||
105 | * @param array $array |
||
106 | * @param string $prepend |
||
107 | * @return array |
||
108 | */ |
||
109 | 1 | public static function dot($array, $prepend = '') |
|
123 | |||
124 | /** |
||
125 | * Get all of the given array except for a specified array of keys. |
||
126 | * |
||
127 | * @param array $array |
||
128 | * @param array|string $keys |
||
129 | * @return array |
||
130 | */ |
||
131 | 5 | public static function except($array, $keys) |
|
137 | |||
138 | /** |
||
139 | * Determine if the given key exists in the provided array. |
||
140 | * |
||
141 | * @param \ArrayAccess|array $array |
||
142 | * @param string|int $key |
||
143 | * @return bool |
||
144 | */ |
||
145 | 91 | public static function exists($array, $key) |
|
153 | |||
154 | /** |
||
155 | * Return the first element in an array passing a given truth test. |
||
156 | * |
||
157 | * @param array $array |
||
158 | * @param callable|null $callback |
||
159 | * @param mixed $default |
||
160 | * @return mixed |
||
161 | */ |
||
162 | 29 | public static function first($array, callable $callback = null, $default = null) |
|
182 | |||
183 | /** |
||
184 | * Return the last element in an array passing a given truth test. |
||
185 | * |
||
186 | * @param array $array |
||
187 | * @param callable|null $callback |
||
188 | * @param mixed $default |
||
189 | * @return mixed |
||
190 | */ |
||
191 | 13 | public static function last($array, callable $callback = null, $default = null) |
|
199 | |||
200 | /** |
||
201 | * Flatten a multi-dimensional array into a single level. |
||
202 | * |
||
203 | * @param array $array |
||
204 | * @param int $depth |
||
205 | * @return array |
||
206 | */ |
||
207 | 5 | public static function flatten($array, $depth = INF) |
|
208 | { |
||
209 | 5 | $result = []; |
|
210 | |||
211 | 5 | foreach ($array as $item) { |
|
212 | 5 | $item = $item instanceof Collection ? $item->all() : $item; |
|
213 | |||
214 | 5 | if (! is_array($item)) { |
|
215 | 5 | $result[] = $item; |
|
216 | } else { |
||
217 | 5 | $values = $depth === 1 |
|
218 | 3 | ? array_values($item) |
|
219 | 5 | : static::flatten($item, $depth - 1); |
|
220 | |||
221 | 5 | foreach ($values as $value) { |
|
222 | 5 | $result[] = $value; |
|
223 | } |
||
224 | } |
||
225 | } |
||
226 | |||
227 | 5 | return $result; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * Remove one or many array items from a given array using "dot" notation. |
||
232 | * |
||
233 | * @param array $array |
||
234 | * @param array|string $keys |
||
235 | * @return void |
||
236 | */ |
||
237 | 10 | public static function forget(&$array, $keys) |
|
238 | { |
||
239 | 10 | $original = &$array; |
|
240 | |||
241 | 10 | $keys = (array) $keys; |
|
242 | |||
243 | 10 | if (count($keys) === 0) { |
|
244 | 1 | return; |
|
245 | } |
||
246 | |||
247 | 10 | foreach ($keys as $key) { |
|
248 | // if the exact key exists in the top-level, remove it |
||
249 | 10 | if (static::exists($array, $key)) { |
|
250 | 7 | unset($array[$key]); |
|
251 | |||
252 | 7 | continue; |
|
253 | } |
||
254 | |||
255 | 8 | $parts = explode('.', $key); |
|
256 | |||
257 | // clean up before each pass |
||
258 | 8 | $array = &$original; |
|
259 | |||
260 | 8 | while (count($parts) > 1) { |
|
261 | 3 | $part = array_shift($parts); |
|
262 | |||
263 | 3 | if (isset($array[$part]) && is_array($array[$part])) { |
|
264 | 3 | $array = &$array[$part]; |
|
265 | } else { |
||
266 | 2 | continue 2; |
|
267 | } |
||
268 | } |
||
269 | |||
270 | 7 | unset($array[array_shift($parts)]); |
|
271 | } |
||
272 | 10 | } |
|
273 | |||
274 | /** |
||
275 | * Get an item from an array using "dot" notation. |
||
276 | * |
||
277 | * @param \ArrayAccess|array $array |
||
278 | * @param string|int $key |
||
279 | * @param mixed $default |
||
280 | * @return mixed |
||
281 | */ |
||
282 | 6 | public static function get($array, $key, $default = null) |
|
283 | { |
||
284 | 6 | if (! static::accessible($array)) { |
|
285 | 1 | return value($default); |
|
286 | } |
||
287 | |||
288 | 6 | if (is_null($key)) { |
|
289 | 1 | return $array; |
|
290 | } |
||
291 | |||
292 | 6 | if (static::exists($array, $key)) { |
|
293 | 4 | return $array[$key]; |
|
294 | } |
||
295 | |||
296 | 4 | if (strpos($key, '.') === false) { |
|
297 | 2 | return $array[$key] ?? value($default); |
|
298 | } |
||
299 | |||
300 | 3 | foreach (explode('.', $key) as $segment) { |
|
301 | 3 | if (static::accessible($array) && static::exists($array, $segment)) { |
|
302 | 2 | $array = $array[$segment]; |
|
303 | } else { |
||
304 | 3 | return value($default); |
|
305 | } |
||
306 | } |
||
307 | |||
308 | 1 | return $array; |
|
309 | } |
||
310 | |||
311 | /** |
||
312 | * Check if an item or items exist in an array using "dot" notation. |
||
313 | * |
||
314 | * @param \ArrayAccess|array $array |
||
315 | * @param string|array $keys |
||
316 | * @return bool |
||
317 | */ |
||
318 | 2 | public static function has($array, $keys) |
|
319 | { |
||
320 | 2 | $keys = (array) $keys; |
|
321 | |||
322 | 2 | if (! $array || $keys === []) { |
|
323 | 1 | return false; |
|
324 | } |
||
325 | |||
326 | 2 | foreach ($keys as $key) { |
|
327 | 2 | $subKeyArray = $array; |
|
328 | |||
329 | 2 | if (static::exists($array, $key)) { |
|
330 | 2 | continue; |
|
331 | } |
||
332 | |||
333 | 2 | foreach (explode('.', $key) as $segment) { |
|
334 | 2 | if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) { |
|
335 | 2 | $subKeyArray = $subKeyArray[$segment]; |
|
336 | } else { |
||
337 | 2 | return false; |
|
338 | } |
||
339 | } |
||
340 | } |
||
341 | |||
342 | 2 | return true; |
|
343 | } |
||
344 | |||
345 | /** |
||
346 | * Determines if an array is associative. |
||
347 | * |
||
348 | * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. |
||
349 | * |
||
350 | * @param array $array |
||
351 | * @return bool |
||
352 | 1 | */ |
|
353 | public static function isAssoc(array $array) |
||
354 | 1 | { |
|
355 | $keys = array_keys($array); |
||
356 | |||
357 | return array_keys($keys) !== $keys; |
||
358 | 1 | } |
|
359 | |||
360 | 1 | /** |
|
361 | * Get a subset of the items from the given array. |
||
362 | * |
||
363 | * @param array $array |
||
364 | 1 | * @param array|string $keys |
|
365 | * @return array |
||
366 | */ |
||
367 | public static function only($array, $keys) |
||
368 | 1 | { |
|
369 | 1 | return array_intersect_key($array, array_flip((array) $keys)); |
|
370 | 1 | } |
|
371 | |||
372 | /** |
||
373 | * Pluck an array of values from an array. |
||
374 | 1 | * |
|
375 | * @param array $array |
||
376 | * @param string|array $value |
||
377 | * @param string|array|null $key |
||
378 | * @return array |
||
379 | */ |
||
380 | public static function pluck($array, $value, $key = null) |
||
381 | { |
||
382 | $results = []; |
||
383 | |||
384 | [$value, $key] = static::explodePluckParameters($value, $key); |
||
385 | 2 | ||
386 | foreach ($array as $item) { |
||
387 | 2 | $itemValue = data_get($item, $value); |
|
388 | |||
389 | 2 | // If the key is "null", we will just append the value to the array and keep |
|
390 | // looping. Otherwise we will key the array using the value of the key we |
||
391 | // received from the developer. Then we'll return the final array form. |
||
392 | if (is_null($key)) { |
||
393 | $results[] = $itemValue; |
||
394 | } else { |
||
395 | $itemKey = data_get($item, $key); |
||
396 | |||
397 | if (is_object($itemKey) && method_exists($itemKey, '__toString')) { |
||
398 | $itemKey = (string) $itemKey; |
||
399 | 2 | } |
|
400 | |||
401 | 2 | $results[$itemKey] = $itemValue; |
|
402 | } |
||
403 | } |
||
404 | |||
405 | return $results; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Explode the "value" and "key" arguments passed to "pluck". |
||
410 | * |
||
411 | * @param string|array $value |
||
412 | 23 | * @param string|array|null $key |
|
413 | * @return array |
||
414 | 23 | */ |
|
415 | protected static function explodePluckParameters($value, $key) |
||
416 | 23 | { |
|
417 | $value = is_string($value) ? explode('.', $value) : $value; |
||
418 | 23 | ||
419 | 23 | $key = is_null($key) || is_array($key) ? $key : explode('.', $key); |
|
420 | |||
421 | return [$value, $key]; |
||
422 | } |
||
423 | |||
424 | 23 | /** |
|
425 | 21 | * Push an item onto the beginning of an array. |
|
426 | * |
||
427 | 7 | * @param array $array |
|
428 | * @param mixed $value |
||
429 | 7 | * @param mixed $key |
|
430 | 1 | * @return array |
|
431 | */ |
||
432 | public static function prepend($array, $value, $key = null) |
||
442 | |||
443 | /** |
||
444 | * Get a value from the array, and remove it. |
||
445 | * |
||
446 | * @param array $array |
||
447 | 23 | * @param string $key |
|
448 | * @param mixed $default |
||
449 | 23 | * @return mixed |
|
450 | */ |
||
451 | 23 | public static function pull(&$array, $key, $default = null) |
|
459 | |||
460 | /** |
||
461 | * Get one or a specified number of random values from an array. |
||
462 | * |
||
463 | * @param array $array |
||
464 | 3 | * @param int|null $number |
|
465 | * @return mixed |
||
466 | 3 | * |
|
467 | 3 | * @throws \InvalidArgumentException |
|
468 | */ |
||
469 | 2 | public static function random($array, $number = null) |
|
470 | { |
||
471 | $requested = is_null($number) ? 1 : $number; |
||
472 | 3 | ||
473 | $count = count($array); |
||
474 | |||
475 | if ($requested > $count) { |
||
476 | throw new InvalidArgumentException( |
||
477 | "You requested {$requested} items, but there are only {$count} items available." |
||
478 | ); |
||
479 | } |
||
480 | |||
481 | if (is_null($number)) { |
||
482 | return $array[array_rand($array)]; |
||
483 | 4 | } |
|
484 | |||
485 | 4 | if ((int) $number === 0) { |
|
486 | return []; |
||
487 | 4 | } |
|
488 | |||
489 | 4 | $keys = array_rand($array, $number); |
|
490 | |||
491 | $results = []; |
||
492 | |||
493 | foreach ((array) $keys as $key) { |
||
494 | $results[] = $array[$key]; |
||
495 | } |
||
496 | |||
497 | return $results; |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | 9 | * Set an array item to a given value using "dot" notation. |
|
502 | * |
||
503 | 9 | * If no key is given to the method, the entire array will be replaced. |
|
504 | * |
||
505 | 9 | * @param array $array |
|
506 | * @param string $key |
||
507 | 9 | * @param mixed $value |
|
508 | 3 | * @return array |
|
509 | 3 | */ |
|
510 | public static function set(&$array, $key, $value) |
||
511 | { |
||
512 | if (is_null($key)) { |
||
513 | 6 | return $array = $value; |
|
514 | 3 | } |
|
515 | |||
516 | $keys = explode('.', $key); |
||
517 | 6 | ||
518 | 6 | while (count($keys) > 1) { |
|
519 | $key = array_shift($keys); |
||
520 | |||
521 | 3 | // If the key doesn't exist at this depth, we will just create an empty array |
|
522 | // to hold the next value, allowing us to create the arrays to hold final |
||
523 | 3 | // values at the correct depth. Then we'll keep digging into the array. |
|
524 | if (! isset($array[$key]) || ! is_array($array[$key])) { |
||
525 | 3 | $array[$key] = []; |
|
526 | 3 | } |
|
527 | |||
528 | $array = &$array[$key]; |
||
529 | 3 | } |
|
530 | |||
531 | $array[array_shift($keys)] = $value; |
||
532 | |||
533 | return $array; |
||
534 | } |
||
535 | |||
536 | /** |
||
537 | * Shuffle the given array and return the result. |
||
538 | * |
||
539 | * @param array $array |
||
540 | * @param int|null $seed |
||
541 | * @return array |
||
542 | 3 | */ |
|
543 | public static function shuffle($array, $seed = null) |
||
544 | 3 | { |
|
545 | 1 | if (is_null($seed)) { |
|
546 | shuffle($array); |
||
547 | } else { |
||
548 | 3 | mt_srand($seed); |
|
549 | shuffle($array); |
||
550 | 3 | mt_srand(); |
|
551 | 3 | } |
|
552 | |||
553 | return $array; |
||
554 | } |
||
555 | |||
556 | 3 | /** |
|
557 | 2 | * Sort the array using the given callback or "dot" notation. |
|
558 | * |
||
559 | * @param array $array |
||
560 | 3 | * @param callable|string|null $callback |
|
561 | * @return array |
||
562 | */ |
||
563 | 3 | public static function sort($array, $callback = null) |
|
564 | { |
||
565 | 3 | return Collection::make($array)->sortBy($callback)->all(); |
|
566 | } |
||
567 | |||
568 | /** |
||
569 | * Recursively sort an array by keys and values. |
||
570 | * |
||
571 | * @param array $array |
||
572 | * @return array |
||
573 | */ |
||
574 | public static function sortRecursive($array) |
||
575 | 3 | { |
|
576 | foreach ($array as &$value) { |
||
577 | 3 | if (is_array($value)) { |
|
578 | 1 | $value = static::sortRecursive($value); |
|
579 | } |
||
580 | 2 | } |
|
581 | 2 | ||
582 | 2 | if (static::isAssoc($array)) { |
|
583 | ksort($array); |
||
584 | } else { |
||
585 | 3 | sort($array); |
|
586 | } |
||
587 | |||
588 | return $array; |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * Convert the array into a query string. |
||
593 | * |
||
594 | * @param array $array |
||
595 | 1 | * @return string |
|
596 | */ |
||
597 | 1 | public static function query($array) |
|
598 | { |
||
599 | return http_build_query($array, null, '&', PHP_QUERY_RFC3986); |
||
600 | } |
||
601 | |||
602 | /** |
||
603 | * Filter the array using the given callback. |
||
604 | * |
||
605 | * @param array $array |
||
606 | 1 | * @param callable $callback |
|
607 | * @return array |
||
608 | 1 | */ |
|
609 | 1 | public static function where($array, callable $callback) |
|
610 | 1 | { |
|
611 | return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH); |
||
612 | } |
||
613 | |||
614 | 1 | /** |
|
615 | 1 | * If the given value is not an array and not null, wrap it in one. |
|
616 | * |
||
617 | 1 | * @param mixed $value |
|
618 | * @return array |
||
619 | */ |
||
620 | 1 | public static function wrap($value) |
|
628 | } |
||
629 |