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 | 54 | 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 | 2 | 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 | 3 | 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 | 51 | 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 | 17 | 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 | 9 | 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) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Remove one or many array items from a given array using "dot" notation. |
||
| 228 | * |
||
| 229 | * @param array $array |
||
| 230 | * @param array|string $keys |
||
| 231 | * @return void |
||
| 232 | */ |
||
| 233 | 8 | public static function forget(&$array, $keys) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Get an item from an array using "dot" notation. |
||
| 272 | * |
||
| 273 | * @param \ArrayAccess|array $array |
||
| 274 | * @param string $key |
||
| 275 | * @param mixed $default |
||
| 276 | * @return mixed |
||
| 277 | */ |
||
| 278 | 6 | public static function get($array, $key, $default = null) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Check if an item or items exist in an array using "dot" notation. |
||
| 309 | * |
||
| 310 | * @param \ArrayAccess|array $array |
||
| 311 | * @param string|array $keys |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | 1 | public static function has($array, $keys) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Determines if an array is associative. |
||
| 343 | * |
||
| 344 | * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. |
||
| 345 | * |
||
| 346 | * @param array $array |
||
| 347 | * @return bool |
||
| 348 | */ |
||
| 349 | 2 | public static function isAssoc(array $array) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Get a subset of the items from the given array. |
||
| 358 | * |
||
| 359 | * @param array $array |
||
| 360 | * @param array|string $keys |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | 2 | public static function only($array, $keys) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Pluck an array of values from an array. |
||
| 370 | * |
||
| 371 | * @param array $array |
||
| 372 | * @param string|array $value |
||
| 373 | * @param string|array|null $key |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | 17 | public static function pluck($array, $value, $key = null) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Explode the "value" and "key" arguments passed to "pluck". |
||
| 406 | * |
||
| 407 | * @param string|array $value |
||
| 408 | * @param string|array|null $key |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | 17 | protected static function explodePluckParameters($value, $key) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Push an item onto the beginning of an array. |
||
| 422 | * |
||
| 423 | * @param array $array |
||
| 424 | * @param mixed $value |
||
| 425 | * @param mixed $key |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | 2 | public static function prepend($array, $value, $key = null) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Get a value from the array, and remove it. |
||
| 441 | * |
||
| 442 | * @param array $array |
||
| 443 | * @param string $key |
||
| 444 | * @param mixed $default |
||
| 445 | * @return mixed |
||
| 446 | */ |
||
| 447 | 4 | public static function pull(&$array, $key, $default = null) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Get one or a specified number of random values from an array. |
||
| 458 | * |
||
| 459 | * @param array $array |
||
| 460 | * @param int|null $number |
||
| 461 | * @return mixed |
||
| 462 | * |
||
| 463 | * @throws \InvalidArgumentException |
||
| 464 | */ |
||
| 465 | 6 | public static function random($array, $number = null) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Set an array item to a given value using "dot" notation. |
||
| 498 | * |
||
| 499 | * If no key is given to the method, the entire array will be replaced. |
||
| 500 | * |
||
| 501 | * @param array $array |
||
| 502 | * @param string $key |
||
| 503 | * @param mixed $value |
||
| 504 | * @return array |
||
| 505 | */ |
||
| 506 | 2 | public static function set(&$array, $key, $value) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Shuffle the given array and return the result. |
||
| 534 | * |
||
| 535 | * @param array $array |
||
| 536 | * @param int|null $seed |
||
| 537 | * @return array |
||
| 538 | */ |
||
| 539 | 2 | public static function shuffle($array, $seed = null) |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Sort the array using the given callback or "dot" notation. |
||
| 554 | * |
||
| 555 | * @param array $array |
||
| 556 | * @param callable|string|null $callback |
||
| 557 | * @return array |
||
| 558 | */ |
||
| 559 | 1 | public static function sort($array, $callback = null) |
|
| 563 | |||
| 564 | /** |
||
| 565 | * Recursively sort an array by keys and values. |
||
| 566 | * |
||
| 567 | * @param array $array |
||
| 568 | * @return array |
||
| 569 | */ |
||
| 570 | 1 | public static function sortRecursive($array) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Convert the array into a query string. |
||
| 589 | * |
||
| 590 | * @param array $array |
||
| 591 | * @return string |
||
| 592 | */ |
||
| 593 | 1 | public static function query($array) |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Filter the array using the given callback. |
||
| 600 | * |
||
| 601 | * @param array $array |
||
| 602 | * @param callable $callback |
||
| 603 | * @return array |
||
| 604 | */ |
||
| 605 | 34 | public static function where($array, callable $callback) |
|
| 609 | |||
| 610 | /** |
||
| 611 | * If the given value is not an array and not null, wrap it in one. |
||
| 612 | * |
||
| 613 | * @param mixed $value |
||
| 614 | * @return array |
||
| 615 | */ |
||
| 616 | 7 | public static function wrap($value) |
|
| 624 | } |
||
| 625 |