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 iterable $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 iterable ...$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 iterable $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 iterable $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 iterable $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|null $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 | 2 | public static function has($array, $keys) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Determine if any of the keys exist in an array using "dot" notation. |
||
| 347 | * |
||
| 348 | * @param \ArrayAccess|array $array |
||
| 349 | * @param string|array $keys |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | 1 | public static function hasAny($array, $keys) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Determines if an array is associative. |
||
| 379 | * |
||
| 380 | * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. |
||
| 381 | * |
||
| 382 | * @param array $array |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | 2 | public static function isAssoc(array $array) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Get a subset of the items from the given array. |
||
| 394 | * |
||
| 395 | * @param array $array |
||
| 396 | * @param array|string $keys |
||
| 397 | * @return array |
||
| 398 | */ |
||
| 399 | 2 | public static function only($array, $keys) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Pluck an array of values from an array. |
||
| 406 | * |
||
| 407 | * @param iterable $array |
||
| 408 | * @param string|array $value |
||
| 409 | * @param string|array|null $key |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | 23 | public static function pluck($array, $value, $key = null) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Explode the "value" and "key" arguments passed to "pluck". |
||
| 442 | * |
||
| 443 | * @param string|array $value |
||
| 444 | * @param string|array|null $key |
||
| 445 | * @return array |
||
| 446 | */ |
||
| 447 | 23 | protected static function explodePluckParameters($value, $key) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Push an item onto the beginning of an array. |
||
| 458 | * |
||
| 459 | * @param array $array |
||
| 460 | * @param mixed $value |
||
| 461 | * @param mixed $key |
||
| 462 | * @return array |
||
| 463 | */ |
||
| 464 | 3 | public static function prepend($array, $value, $key = null) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Get a value from the array, and remove it. |
||
| 477 | * |
||
| 478 | * @param array $array |
||
| 479 | * @param string $key |
||
| 480 | * @param mixed $default |
||
| 481 | * @return mixed |
||
| 482 | */ |
||
| 483 | 4 | public static function pull(&$array, $key, $default = null) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Get one or a specified number of random values from an array. |
||
| 494 | * |
||
| 495 | * @param array $array |
||
| 496 | * @param int|null $number |
||
| 497 | * @return mixed |
||
| 498 | * |
||
| 499 | * @throws \InvalidArgumentException |
||
| 500 | */ |
||
| 501 | 9 | public static function random($array, $number = null) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Set an array item to a given value using "dot" notation. |
||
| 534 | * |
||
| 535 | * If no key is given to the method, the entire array will be replaced. |
||
| 536 | * |
||
| 537 | * @param array $array |
||
| 538 | * @param string|null $key |
||
| 539 | * @param mixed $value |
||
| 540 | * @return array |
||
| 541 | */ |
||
| 542 | 2 | public static function set(&$array, $key, $value) |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Shuffle the given array and return the result. |
||
| 570 | * |
||
| 571 | * @param array $array |
||
| 572 | * @param int|null $seed |
||
| 573 | * @return array |
||
| 574 | */ |
||
| 575 | 2 | public static function shuffle($array, $seed = null) |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Sort the array using the given callback or "dot" notation. |
||
| 590 | * |
||
| 591 | * @param array $array |
||
| 592 | * @param callable|string|null $callback |
||
| 593 | * @return array |
||
| 594 | */ |
||
| 595 | 1 | public static function sort($array, $callback = null) |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Recursively sort an array by keys and values. |
||
| 602 | * |
||
| 603 | * @param array $array |
||
| 604 | * @return array |
||
| 605 | */ |
||
| 606 | 1 | public static function sortRecursive($array) |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Convert the array into a query string. |
||
| 625 | * |
||
| 626 | * @param array $array |
||
| 627 | * @return string |
||
| 628 | */ |
||
| 629 | 1 | public static function query($array) |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Filter the array using the given callback. |
||
| 636 | * |
||
| 637 | * @param array $array |
||
| 638 | * @param callable $callback |
||
| 639 | * @return array |
||
| 640 | */ |
||
| 641 | 56 | public static function where($array, callable $callback) |
|
| 645 | |||
| 646 | /** |
||
| 647 | * If the given value is not an array and not null, wrap it in one. |
||
| 648 | * |
||
| 649 | * @param mixed $value |
||
| 650 | * @return array |
||
| 651 | */ |
||
| 652 | 12 | public static function wrap($value) |
|
| 660 | } |
||
| 661 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: