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 | 94 | 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 | 4 | 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 | 86 | 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) |
|
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) |
|
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) |
|
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 | 1 | public static function has($array, $keys) |
|
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 | */ |
||
353 | 2 | public static function isAssoc(array $array) |
|
359 | |||
360 | /** |
||
361 | * Get a subset of the items from the given array. |
||
362 | * |
||
363 | * @param array $array |
||
364 | * @param array|string $keys |
||
365 | * @return array |
||
366 | */ |
||
367 | 2 | public static function only($array, $keys) |
|
371 | |||
372 | /** |
||
373 | * Pluck an array of values from an array. |
||
374 | * |
||
375 | * @param array $array |
||
376 | * @param string|array $value |
||
377 | * @param string|array|null $key |
||
378 | * @return array |
||
379 | */ |
||
380 | 23 | public static function pluck($array, $value, $key = null) |
|
407 | |||
408 | /** |
||
409 | * Explode the "value" and "key" arguments passed to "pluck". |
||
410 | * |
||
411 | * @param string|array $value |
||
412 | * @param string|array|null $key |
||
413 | * @return array |
||
414 | */ |
||
415 | 23 | protected static function explodePluckParameters($value, $key) |
|
423 | |||
424 | /** |
||
425 | * Push an item onto the beginning of an array. |
||
426 | * |
||
427 | * @param array $array |
||
428 | * @param mixed $value |
||
429 | * @param mixed $key |
||
430 | * @return array |
||
431 | */ |
||
432 | 3 | 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 | * @param string $key |
||
448 | * @param mixed $default |
||
449 | * @return mixed |
||
450 | */ |
||
451 | 4 | 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 | * @param int|null $number |
||
465 | * @return mixed |
||
466 | * |
||
467 | * @throws \InvalidArgumentException |
||
468 | */ |
||
469 | 9 | public static function random($array, $number = null) |
|
499 | |||
500 | /** |
||
501 | * Set an array item to a given value using "dot" notation. |
||
502 | * |
||
503 | * If no key is given to the method, the entire array will be replaced. |
||
504 | * |
||
505 | * @param array $array |
||
506 | * @param string $key |
||
507 | * @param mixed $value |
||
508 | * @return array |
||
509 | */ |
||
510 | 2 | public static function set(&$array, $key, $value) |
|
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 | */ |
||
543 | 2 | public static function shuffle($array, $seed = null) |
|
555 | |||
556 | /** |
||
557 | * Sort the array using the given callback or "dot" notation. |
||
558 | * |
||
559 | * @param array $array |
||
560 | * @param callable|string|null $callback |
||
561 | * @return array |
||
562 | */ |
||
563 | 1 | public static function sort($array, $callback = null) |
|
567 | |||
568 | /** |
||
569 | * Recursively sort an array by keys and values. |
||
570 | * |
||
571 | * @param array $array |
||
572 | * @return array |
||
573 | */ |
||
574 | 1 | public static function sortRecursive($array) |
|
590 | |||
591 | /** |
||
592 | * Convert the array into a query string. |
||
593 | * |
||
594 | * @param array $array |
||
595 | * @return string |
||
596 | */ |
||
597 | 1 | public static function query($array) |
|
601 | |||
602 | /** |
||
603 | * Filter the array using the given callback. |
||
604 | * |
||
605 | * @param array $array |
||
606 | * @param callable $callback |
||
607 | * @return array |
||
608 | */ |
||
609 | 52 | public static function where($array, callable $callback) |
|
613 | |||
614 | /** |
||
615 | * If the given value is not an array and not null, wrap it in one. |
||
616 | * |
||
617 | * @param mixed $value |
||
618 | * @return array |
||
619 | */ |
||
620 | 13 | public static function wrap($value) |
|
628 | } |
||
629 |