Complex classes like Collection 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 Collection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Collection implements \Iterator, \ArrayAccess, \Serializable, \Countable |
||
| 6 | { |
||
| 7 | protected $array = null; |
||
| 8 | protected $stack = []; |
||
| 9 | protected $iterator = null; |
||
| 10 | protected $key = null; |
||
| 11 | protected $val = null; |
||
| 12 | |||
| 13 | 4 | protected static function rangeGenerator($low, $high, $step = 1) |
|
| 20 | /** |
||
| 21 | * Create a collection based on a range generator |
||
| 22 | * @param int|float $low start value |
||
| 23 | * @param int|float $high end value |
||
| 24 | * @param int|float $step increment |
||
| 25 | * @return Collection |
||
| 26 | */ |
||
| 27 | 4 | public static function range($low, $high, $step = 1) : Collection |
|
| 31 | /** |
||
| 32 | * A static alias of the __constructor |
||
| 33 | * @param mixed $input Anything iterable |
||
| 34 | * @return Collection |
||
| 35 | */ |
||
| 36 | 53 | public static function from($input) : Collection |
|
| 40 | /** |
||
| 41 | * Create an instance |
||
| 42 | * @param mixed $input Anything iterable |
||
| 43 | */ |
||
| 44 | 57 | public function __construct($input = []) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Applies all pending operations |
||
| 82 | * @return $this |
||
| 83 | */ |
||
| 84 | 46 | public function squash() : Collection |
|
| 93 | /** |
||
| 94 | * Get an actual array from the collection |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | 45 | public function toArray() : array |
|
| 101 | /** |
||
| 102 | * Gets the first value in the collection or null if empty |
||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | 3 | public function value() |
|
| 112 | |||
| 113 | // iterator |
||
| 114 | 29 | public function key() |
|
| 158 | |||
| 159 | // array access |
||
| 160 | 1 | public function offsetGet($offset) |
|
| 189 | /** |
||
| 190 | * Get the collection length |
||
| 191 | * @return int |
||
| 192 | */ |
||
| 193 | 1 | public function count() |
|
| 200 | |||
| 201 | // mutators |
||
| 202 | /** |
||
| 203 | * Filter values from the collection based on a predicate. The callback will receive the value, key and collection |
||
| 204 | * @param callable $iterator the predicate |
||
| 205 | * @return $this |
||
| 206 | */ |
||
| 207 | 10 | public function filter(callable $iterator) : Collection |
|
| 212 | /** |
||
| 213 | * Pass all values of the collection through a mutator callable, which will receive the value, key and collection |
||
| 214 | * @param callable $iterator the mutator |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | 7 | public function map(callable $iterator) : Collection |
|
| 222 | /** |
||
| 223 | * Pass all values of the collection through a key mutator callable, which will receive the value, key and collection |
||
| 224 | * @param callable $iterator the mutator |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | 1 | public function mapKey(callable $iterator) : Collection |
|
| 232 | /** |
||
| 233 | * Clone the current collection and return it. |
||
| 234 | * @return Collection |
||
| 235 | */ |
||
| 236 | public function clone() : Collection |
||
| 240 | /** |
||
| 241 | * Remove all falsy values from the collection (uses filter internally). |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | 1 | public function compact() : Collection |
|
| 250 | /** |
||
| 251 | * Exclude all listed values from the collection (uses filter internally). |
||
| 252 | * @param iterable $values the values to exclude |
||
| 253 | * @return $this |
||
| 254 | */ |
||
| 255 | 3 | public function difference($values) : Collection |
|
| 268 | /** |
||
| 269 | * Append more values to the collection |
||
| 270 | * @param iterable $source the values to add |
||
| 271 | * @return Collection |
||
| 272 | */ |
||
| 273 | 2 | public function extend($source) : Collection |
|
| 280 | /** |
||
| 281 | * Append more values to the collection |
||
| 282 | * @param iterable $source the values to add |
||
| 283 | * @return Collection |
||
| 284 | */ |
||
| 285 | 1 | public function merge($source) : Collection |
|
| 289 | /** |
||
| 290 | * Perform a shallow flatten of the collection |
||
| 291 | * @return Collection |
||
| 292 | */ |
||
| 293 | 1 | public function flatten() : Collection |
|
| 302 | /** |
||
| 303 | * Group by a key (if a callable is used - return the value to group by) |
||
| 304 | * @param string|callable $iterator the key to group by |
||
| 305 | * @return Collection |
||
| 306 | */ |
||
| 307 | 1 | public function groupBy($iterator) : Collection |
|
| 316 | /** |
||
| 317 | * Get the first X items from the collection |
||
| 318 | * @param int $count the number of items to include (defaults to 1) |
||
| 319 | * @return Collection |
||
| 320 | */ |
||
| 321 | 1 | public function first(int $count = 1) : Collection |
|
| 333 | /** |
||
| 334 | * Get the first X items from the collection |
||
| 335 | * @param int $count the number of items to include (defaults to 1) |
||
| 336 | * @return Collection |
||
| 337 | */ |
||
| 338 | 1 | public function head(int $count = 1) : Collection |
|
| 342 | /** |
||
| 343 | * Get the last X items from the collection |
||
| 344 | * @param int $count the number of items to include (defaults to 1) |
||
| 345 | * @return Collection |
||
| 346 | */ |
||
| 347 | 2 | public function last(int $count = 1) : Collection |
|
| 352 | /** |
||
| 353 | * Get the first X items from the collection |
||
| 354 | * @param int $count the number of items to include (defaults to 1) |
||
| 355 | * @return Collection |
||
| 356 | */ |
||
| 357 | 1 | public function tail(int $count = 1) : Collection |
|
| 361 | /** |
||
| 362 | * Get all but the last X items from the collection |
||
| 363 | * @param int $count the number of items to exclude (defaults to 1) |
||
| 364 | * @return Collection |
||
| 365 | */ |
||
| 366 | 1 | public function initial(int $count = 1) : Collection |
|
| 371 | /** |
||
| 372 | * Get all but the first X items from the collection |
||
| 373 | * @param int $count the number of items to exclude (defaults to 1) |
||
| 374 | * @return Collection |
||
| 375 | */ |
||
| 376 | 1 | public function rest(int $count = 1) : Collection |
|
| 381 | /** |
||
| 382 | * Execute a callable for each item in the collection (does not modify the collection) |
||
| 383 | * @param callable $iterator the callable to execute |
||
| 384 | * @return $this |
||
| 385 | */ |
||
| 386 | 1 | public function each(callable $iterator) : Collection |
|
| 393 | /** |
||
| 394 | * Execute a callable for each item in the collection (does not modify the collection) |
||
| 395 | * @param callable $iterator the callable to execute |
||
| 396 | * @return $this |
||
| 397 | */ |
||
| 398 | 1 | public function invoke(callable $iterator) : Collection |
|
| 402 | /** |
||
| 403 | * Get all the collection keys |
||
| 404 | * @return $this |
||
| 405 | */ |
||
| 406 | public function keys() : Collection |
||
| 410 | /** |
||
| 411 | * Pluck a value from each object (uses map internally) |
||
| 412 | * @param string|int $key the key to extract |
||
| 413 | * @return $this |
||
| 414 | */ |
||
| 415 | 2 | public function pluck($key) : Collection |
|
| 423 | /** |
||
| 424 | * Intersect the collection with another iterable (uses filter internally) |
||
| 425 | * @param interable $values the data to intersect with |
||
| 426 | * @return $this |
||
| 427 | */ |
||
| 428 | 1 | public function intersection($values) : Collection |
|
| 441 | /** |
||
| 442 | * Reject values on a given predicate (opposite of filter) |
||
| 443 | * @param callable $iterator the predicate |
||
| 444 | * @return $this |
||
| 445 | */ |
||
| 446 | 1 | public function reject(callable $iterator) : Collection |
|
| 452 | /** |
||
| 453 | * Shuffle the values in the collection |
||
| 454 | * @return Collection |
||
| 455 | */ |
||
| 456 | 1 | public function shuffle() : Collection |
|
| 467 | /** |
||
| 468 | * Sort the collection using a standard sorting function |
||
| 469 | * @param callable $iterator the sort function (must return -1, 0 or 1) |
||
| 470 | * @return Collection |
||
| 471 | */ |
||
| 472 | 1 | public function sortBy(callable $iterator) : Collection |
|
| 478 | /** |
||
| 479 | * Inspect the whole collection (as an array) mid-chain |
||
| 480 | * @param callable $iterator the callable to execute |
||
| 481 | * @return $this |
||
| 482 | */ |
||
| 483 | 1 | public function tap(callable $iterator) : Collection |
|
| 488 | /** |
||
| 489 | * Modify the whole collection (as an array) mid-chain |
||
| 490 | * @param callable $iterator the callable to execute |
||
| 491 | * @return Collection |
||
| 492 | */ |
||
| 493 | 1 | public function thru(callable $iterator) : Collection |
|
| 499 | /** |
||
| 500 | * Leave only unique items in the collection |
||
| 501 | * @return Collection |
||
| 502 | */ |
||
| 503 | 3 | public function unique() : Collection |
|
| 514 | /** |
||
| 515 | * Get only the values of the collection |
||
| 516 | * @return Collection |
||
| 517 | */ |
||
| 518 | 5 | public function values() : Collection |
|
| 522 | |||
| 523 | 3 | protected function whereCallback($v, $properties, $strict = true) |
|
| 633 | /** |
||
| 634 | * Filter items from the collection using key => value pairs |
||
| 635 | * @param array $properties the key => value to check for in each item |
||
| 636 | * @param boolean $strict should the comparison be strict |
||
| 637 | * @return $this |
||
| 638 | */ |
||
| 639 | public function where(array $properties, $strict = true) : Collection |
||
| 645 | /** |
||
| 646 | * Exclude all listed values from the collection (uses filter internally). |
||
| 647 | * @param iterable $values the values to exclude |
||
| 648 | * @return $this |
||
| 649 | */ |
||
| 650 | 2 | public function without($values) : Collection |
|
| 654 | /** |
||
| 655 | * Combine all the values from the collection with a key |
||
| 656 | * @param iterable $keys the keys to use |
||
| 657 | * @return Collection |
||
| 658 | */ |
||
| 659 | 2 | public function zip($keys) : Collection |
|
| 666 | /** |
||
| 667 | * Reverse the collection order |
||
| 668 | * @return Collection |
||
| 669 | */ |
||
| 670 | 2 | public function reverse() : Collection |
|
| 674 | |||
| 675 | // accessors |
||
| 676 | /** |
||
| 677 | * Do all of the items in the collection match a given criteria |
||
| 678 | * @param callable $iterator the criteria - should return true / false |
||
| 679 | * @return bool |
||
| 680 | */ |
||
| 681 | 1 | public function all(callable $iterator) : bool |
|
| 690 | /** |
||
| 691 | * Do any of the items in the collection match a given criteria |
||
| 692 | * @param callable $iterator the criteria - should return true / false |
||
| 693 | * @return bool |
||
| 694 | */ |
||
| 695 | 1 | public function any(callable $iterator) : bool |
|
| 704 | /** |
||
| 705 | * Does the collection contain a given value |
||
| 706 | * @param mixed $needle the value to check for |
||
| 707 | * @return bool |
||
| 708 | */ |
||
| 709 | 1 | public function contains($needle) : bool |
|
| 718 | /** |
||
| 719 | * Get the first element matching a given criteria (or null) |
||
| 720 | * @param callable $iterator the filter criteria |
||
| 721 | * @return mixed |
||
| 722 | */ |
||
| 723 | 1 | public function find(callable $iterator) |
|
| 732 | /** |
||
| 733 | * Get all the elements matching a given criteria (with the option to limit the number of results) |
||
| 734 | * @param callable $iterator the search criteria |
||
| 735 | * @param int|null $limit optional limit to the number of results (default to null - no limit) |
||
| 736 | * @return Collection |
||
| 737 | */ |
||
| 738 | 1 | public function findAll(callable $iterator, int $limit = null) : Collection |
|
| 751 | /** |
||
| 752 | * Get the key corresponding to a value (or false) |
||
| 753 | * @param mixed $needle the value to search for |
||
| 754 | * @return mixed |
||
| 755 | */ |
||
| 756 | 1 | public function indexOf($needle) |
|
| 760 | /** |
||
| 761 | * Get the last key corresponding to a value (or false) |
||
| 762 | * @param mixed $needle the value to search for |
||
| 763 | * @return mixed |
||
| 764 | */ |
||
| 765 | 1 | public function lastIndexOf($needle) |
|
| 775 | /** |
||
| 776 | * Get the number of elements in the collection |
||
| 777 | * @return int |
||
| 778 | */ |
||
| 779 | 1 | public function size() : int |
|
| 783 | /** |
||
| 784 | * Get the minimal item in the collection |
||
| 785 | * @return mixed |
||
| 786 | */ |
||
| 787 | 1 | public function min() |
|
| 799 | /** |
||
| 800 | * Get the maximum item in the collection |
||
| 801 | * @return mixed |
||
| 802 | */ |
||
| 803 | 1 | public function max() |
|
| 815 | /** |
||
| 816 | * Does the collection contain a given key |
||
| 817 | * @param string|int $key the key to check |
||
| 818 | * @return bool |
||
| 819 | */ |
||
| 820 | 1 | public function has($key) : bool |
|
| 824 | /** |
||
| 825 | * Reduce the collection to a single value |
||
| 826 | * @param callable $iterator the reducer (will recieve the carried value, the value, the key and the collection) |
||
| 827 | * @param mixed $initial the initial value |
||
| 828 | * @return mixed the final value |
||
| 829 | */ |
||
| 830 | 2 | public function reduce(callable $iterator, $initial = null) |
|
| 837 | /** |
||
| 838 | * Reduce the collection to a single value, starting from the last element |
||
| 839 | * @param callable $iterator the reducer (will recieve the carried value, the value, the key and the collection) |
||
| 840 | * @param mixed $initial the initial value |
||
| 841 | * @return mixed the final value |
||
| 842 | */ |
||
| 843 | 1 | public function reduceRight(callable $iterator, $initial = null) |
|
| 847 | } |
||
| 848 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: