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 | 51 | public static function from($input) : Collection |
|
40 | /** |
||
41 | * Create an instance |
||
42 | * @param mixed $input Anything iterable |
||
43 | */ |
||
44 | 55 | public function __construct($input = []) |
|
79 | |||
80 | /** |
||
81 | * Applies all pending operations |
||
82 | * @return $this |
||
83 | */ |
||
84 | 44 | public function squash() : Collection |
|
93 | /** |
||
94 | * Get an actual array from the collection |
||
95 | * @return array |
||
96 | */ |
||
97 | 43 | 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 | 27 | 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 | 8 | 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 | 5 | 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 | 3 | public function values() : Collection |
|
522 | /** |
||
523 | * Filter items from the collection using key => value pairs |
||
524 | * @param array $properties the key => value to check for in each item |
||
525 | * @param boolean $strict should the comparison be strict |
||
526 | * @return $this |
||
527 | */ |
||
528 | public function where(array $properties, $strict = true) : Collection |
||
565 | /** |
||
566 | * Exclude all listed values from the collection (uses filter internally). |
||
567 | * @param iterable $values the values to exclude |
||
568 | * @return $this |
||
569 | */ |
||
570 | 2 | public function without($values) : Collection |
|
574 | /** |
||
575 | * Combine all the values from the collection with a key |
||
576 | * @param iterable $keys the keys to use |
||
577 | * @return Collection |
||
578 | */ |
||
579 | 2 | public function zip($keys) : Collection |
|
586 | /** |
||
587 | * Reverse the collection order |
||
588 | * @return Collection |
||
589 | */ |
||
590 | 2 | public function reverse() : Collection |
|
594 | |||
595 | // accessors |
||
596 | /** |
||
597 | * Do all of the items in the collection match a given criteria |
||
598 | * @param callable $iterator the criteria - should return true / false |
||
599 | * @return bool |
||
600 | */ |
||
601 | 1 | public function all(callable $iterator) : bool |
|
610 | /** |
||
611 | * Do any of the items in the collection match a given criteria |
||
612 | * @param callable $iterator the criteria - should return true / false |
||
613 | * @return bool |
||
614 | */ |
||
615 | 1 | public function any(callable $iterator) : bool |
|
624 | /** |
||
625 | * Does the collection contain a given value |
||
626 | * @param mixed $needle the value to check for |
||
627 | * @return bool |
||
628 | */ |
||
629 | 1 | public function contains($needle) : bool |
|
638 | /** |
||
639 | * Get the first element matching a given criteria (or null) |
||
640 | * @param callable $iterator the filter criteria |
||
641 | * @return mixed |
||
642 | */ |
||
643 | 1 | public function find(callable $iterator) |
|
652 | /** |
||
653 | * Get all the elements matching a given criteria (with the option to limit the number of results) |
||
654 | * @param callable $iterator the search criteria |
||
655 | * @param int|null $limit optional limit to the number of results (default to null - no limit) |
||
656 | * @return Collection |
||
657 | */ |
||
658 | 1 | public function findAll(callable $iterator, int $limit = null) : Collection |
|
671 | /** |
||
672 | * Get the key corresponding to a value (or false) |
||
673 | * @param mixed $needle the value to search for |
||
674 | * @return mixed |
||
675 | */ |
||
676 | 1 | public function indexOf($needle) |
|
680 | /** |
||
681 | * Get the last key corresponding to a value (or false) |
||
682 | * @param mixed $needle the value to search for |
||
683 | * @return mixed |
||
684 | */ |
||
685 | 1 | public function lastIndexOf($needle) |
|
695 | /** |
||
696 | * Get the number of elements in the collection |
||
697 | * @return int |
||
698 | */ |
||
699 | 1 | public function size() : int |
|
703 | /** |
||
704 | * Get the minimal item in the collection |
||
705 | * @return mixed |
||
706 | */ |
||
707 | 1 | public function min() |
|
719 | /** |
||
720 | * Get the maximum item in the collection |
||
721 | * @return mixed |
||
722 | */ |
||
723 | 1 | public function max() |
|
735 | /** |
||
736 | * Does the collection contain a given key |
||
737 | * @param string|int $key the key to check |
||
738 | * @return bool |
||
739 | */ |
||
740 | 1 | public function has($key) : bool |
|
744 | /** |
||
745 | * Reduce the collection to a single value |
||
746 | * @param callable $iterator the reducer (will recieve the carried value, the value, the key and the collection) |
||
747 | * @param mixed $initial the initial value |
||
748 | * @return mixed the final value |
||
749 | */ |
||
750 | 2 | public function reduce(callable $iterator, $initial = null) |
|
757 | /** |
||
758 | * Reduce the collection to a single value, starting from the last element |
||
759 | * @param callable $iterator the reducer (will recieve the carried value, the value, the key and the collection) |
||
760 | * @param mixed $initial the initial value |
||
761 | * @return mixed the final value |
||
762 | */ |
||
763 | 1 | public function reduceRight(callable $iterator, $initial = null) |
|
767 | } |
||
768 |
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: