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 |
||
8 | class Arr |
||
9 | { |
||
10 | /** |
||
11 | * Add an element to an array using "dot" notation if it doesn't exist. |
||
12 | * |
||
13 | * @param array $array |
||
14 | * @param string $key |
||
15 | * @param mixed $value |
||
16 | * |
||
17 | * @return array |
||
18 | */ |
||
19 | public static function add(array $array, $key, $value) |
||
27 | |||
28 | /** |
||
29 | * Cross join the given arrays, returning all possible permutations. |
||
30 | * |
||
31 | * @param array ...$arrays |
||
32 | * |
||
33 | * @return array |
||
34 | */ |
||
35 | public static function crossJoin(...$arrays) |
||
55 | |||
56 | /** |
||
57 | * Divide an array into two arrays. One with keys and the other with values. |
||
58 | * |
||
59 | * @param array $array |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | public static function divide(array $array) |
||
67 | |||
68 | /** |
||
69 | * Flatten a multi-dimensional associative array with dots. |
||
70 | * |
||
71 | * @param array $array |
||
72 | * @param string $prepend |
||
73 | * |
||
74 | * @return array |
||
75 | */ |
||
76 | public static function dot(array $array, $prepend = '') |
||
90 | |||
91 | /** |
||
92 | * Get all of the given array except for a specified array of items. |
||
93 | * |
||
94 | * @param array $array |
||
95 | * @param array|string $keys |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | public static function except(array $array, $keys) |
||
105 | |||
106 | /** |
||
107 | * Determine if the given key exists in the provided array. |
||
108 | * |
||
109 | * @param \ArrayAccess|array $array |
||
110 | * @param string|int $key |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | public static function exists(array $array, $key) |
||
118 | |||
119 | /** |
||
120 | * Return the first element in an array passing a given truth test. |
||
121 | * |
||
122 | * @param array $array |
||
123 | * @param callable|null $callback |
||
124 | * @param mixed $default |
||
125 | * |
||
126 | * @return mixed |
||
127 | */ |
||
128 | public static function first(array $array, callable $callback = null, $default = null) |
||
148 | |||
149 | /** |
||
150 | * Return the last element in an array passing a given truth test. |
||
151 | * |
||
152 | * @param array $array |
||
153 | * @param callable|null $callback |
||
154 | * @param mixed $default |
||
155 | * |
||
156 | * @return mixed |
||
157 | */ |
||
158 | public static function last(array $array, callable $callback = null, $default = null) |
||
166 | |||
167 | /** |
||
168 | * Flatten a multi-dimensional array into a single level. |
||
169 | * |
||
170 | * @param array $array |
||
171 | * @param int $depth |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | public static function flatten(array $array, $depth = INF) |
||
189 | |||
190 | /** |
||
191 | * Remove one or many array items from a given array using "dot" notation. |
||
192 | * |
||
193 | * @param array $array |
||
194 | * @param array|string $keys |
||
195 | */ |
||
196 | public static function forget(array &$array, $keys) |
||
232 | |||
233 | /** |
||
234 | * Get an item from an array using "dot" notation. |
||
235 | * |
||
236 | * @param \ArrayAccess|array $array |
||
237 | * @param string $key |
||
238 | * @param mixed $default |
||
239 | * |
||
240 | * @return mixed |
||
241 | */ |
||
242 | public static function get(array $array, $key, $default = null) |
||
262 | |||
263 | /** |
||
264 | * Check if an item or items exist in an array using "dot" notation. |
||
265 | * |
||
266 | * @param \ArrayAccess|array $array |
||
267 | * @param string|array $keys |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | public static function has(array $array, $keys) |
||
305 | |||
306 | /** |
||
307 | * Determines if an array is associative. |
||
308 | * |
||
309 | * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. |
||
310 | * |
||
311 | * @param array $array |
||
312 | * |
||
313 | * @return bool |
||
314 | */ |
||
315 | public static function isAssoc(array $array) |
||
321 | |||
322 | /** |
||
323 | * Get a subset of the items from the given array. |
||
324 | * |
||
325 | * @param array $array |
||
326 | * @param array|string $keys |
||
327 | * |
||
328 | * @return array |
||
329 | */ |
||
330 | public static function only(array $array, $keys) |
||
334 | |||
335 | /** |
||
336 | * Push an item onto the beginning of an array. |
||
337 | * |
||
338 | * @param array $array |
||
339 | * @param mixed $value |
||
340 | * @param mixed $key |
||
341 | * |
||
342 | * @return array |
||
343 | */ |
||
344 | public static function prepend(array $array, $value, $key = null) |
||
354 | |||
355 | /** |
||
356 | * Get a value from the array, and remove it. |
||
357 | * |
||
358 | * @param array $array |
||
359 | * @param string $key |
||
360 | * @param mixed $default |
||
361 | * |
||
362 | * @return mixed |
||
363 | */ |
||
364 | public static function pull(array &$array, $key, $default = null) |
||
372 | |||
373 | /** |
||
374 | * Get a 1 value from an array. |
||
375 | * |
||
376 | * @param array $array |
||
377 | * @param int|null $amount |
||
378 | * |
||
379 | * @return mixed |
||
380 | * |
||
381 | * @throws \InvalidArgumentException |
||
382 | */ |
||
383 | public static function random(array $array, int $amount = null) |
||
399 | |||
400 | /** |
||
401 | * Set an array item to a given value using "dot" notation. |
||
402 | * |
||
403 | * If no key is given to the method, the entire array will be replaced. |
||
404 | * |
||
405 | * @param array $array |
||
406 | * @param string $key |
||
407 | * @param mixed $value |
||
408 | * |
||
409 | * @return array |
||
410 | */ |
||
411 | public static function set(array &$array, string $key, $value) |
||
432 | |||
433 | /** |
||
434 | * Filter the array using the given callback. |
||
435 | * |
||
436 | * @param array $array |
||
437 | * @param callable $callback |
||
438 | * |
||
439 | * @return array |
||
440 | */ |
||
441 | public static function where(array $array, callable $callback) |
||
445 | |||
446 | /** |
||
447 | * If the given value is not an array, wrap it in one. |
||
448 | * |
||
449 | * @param mixed $value |
||
450 | * |
||
451 | * @return array |
||
452 | */ |
||
453 | public static function wrap($value) |
||
457 | } |
||
458 |