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 | |||
11 | 4 | protected static function rangeGenerator($low, $high, $step = 1) |
|
18 | /** |
||
19 | * Create a collection based on a range generator |
||
20 | * @param int|float $low start value |
||
21 | * @param int|float $high end value |
||
22 | * @param int|float $step increment |
||
23 | * @return Collection |
||
24 | */ |
||
25 | 4 | public static function range($low, $high, $step = 1) : Collection |
|
29 | /** |
||
30 | * A static alias of the __constructor |
||
31 | * @param mixed $input Anything iterable |
||
32 | * @return Collection |
||
33 | */ |
||
34 | 50 | public static function from($input) : Collection |
|
38 | /** |
||
39 | * Create an instance |
||
40 | * @param mixed $input Anything iterable |
||
41 | */ |
||
42 | 54 | public function __construct($input = []) |
|
75 | |||
76 | /** |
||
77 | * Applies all pending operations |
||
78 | * @return $this |
||
79 | */ |
||
80 | 45 | public function squash() : Collection |
|
87 | /** |
||
88 | * Get an actual array from the collection |
||
89 | * @return array |
||
90 | */ |
||
91 | 43 | public function toArray() : array |
|
96 | /** |
||
97 | * Gets the first value in the collection or null if empty |
||
98 | * @return mixed |
||
99 | */ |
||
100 | 3 | public function value() |
|
107 | |||
108 | // iterator |
||
109 | 51 | public function key() |
|
157 | |||
158 | // array access |
||
159 | 1 | public function offsetGet($offset) |
|
175 | /** |
||
176 | * Get the collection length |
||
177 | * @return int |
||
178 | */ |
||
179 | 1 | public function count() |
|
184 | |||
185 | // mutators |
||
186 | /** |
||
187 | * Filter values from the collection based on a predicate. The callback will receive the value, key and collection |
||
188 | * @param callable $iterator the predicate |
||
189 | * @return $this |
||
190 | */ |
||
191 | 8 | public function filter(callable $iterator) : Collection |
|
196 | /** |
||
197 | * Pass all values of the collection through a mutator callable, which will receive the value, key and collection |
||
198 | * @param callable $iterator the mutator |
||
199 | * @return $this |
||
200 | */ |
||
201 | 4 | public function map(callable $iterator) : Collection |
|
206 | /** |
||
207 | * Clone the current collection and return it. |
||
208 | * @return Collection |
||
209 | */ |
||
210 | public function clone() : Collection |
||
214 | /** |
||
215 | * Remove all falsy values from the collection (uses filter internally). |
||
216 | * @return $this |
||
217 | */ |
||
218 | 1 | public function compact() : Collection |
|
224 | /** |
||
225 | * Exclude all listed values from the collection (uses filter internally). |
||
226 | * @param iterable $values the values to exclude |
||
227 | * @return $this |
||
228 | */ |
||
229 | 3 | public function difference($values) : Collection |
|
242 | /** |
||
243 | * Append more values to the collection |
||
244 | * @param iterable $source the values to add |
||
245 | * @return Collection |
||
246 | */ |
||
247 | 2 | public function extend($source) : Collection |
|
254 | /** |
||
255 | * Append more values to the collection |
||
256 | * @param iterable $source the values to add |
||
257 | * @return Collection |
||
258 | */ |
||
259 | 1 | public function merge($source) : Collection |
|
263 | /** |
||
264 | * Perform a shallow flatten of the collection |
||
265 | * @return Collection |
||
266 | */ |
||
267 | 1 | public function flatten() : Collection |
|
276 | /** |
||
277 | * Group by a key (if a callable is used - return the value to group by) |
||
278 | * @param string|callable $iterator the key to group by |
||
279 | * @return Collection |
||
280 | */ |
||
281 | 1 | public function groupBy($iterator) : Collection |
|
290 | /** |
||
291 | * Get the first X items from the collection |
||
292 | * @param int $count the number of items to include (defaults to 1) |
||
293 | * @return Collection |
||
294 | */ |
||
295 | 1 | public function first(int $count = 1) : Collection |
|
307 | /** |
||
308 | * Get the first X items from the collection |
||
309 | * @param int $count the number of items to include (defaults to 1) |
||
310 | * @return Collection |
||
311 | */ |
||
312 | 1 | public function head(int $count = 1) : Collection |
|
316 | /** |
||
317 | * Get the last X items from the collection |
||
318 | * @param int $count the number of items to include (defaults to 1) |
||
319 | * @return Collection |
||
320 | */ |
||
321 | 2 | public function last(int $count = 1) : Collection |
|
326 | /** |
||
327 | * Get the first X items from the collection |
||
328 | * @param int $count the number of items to include (defaults to 1) |
||
329 | * @return Collection |
||
330 | */ |
||
331 | 1 | public function tail(int $count = 1) : Collection |
|
335 | /** |
||
336 | * Get all but the last X items from the collection |
||
337 | * @param int $count the number of items to exclude (defaults to 1) |
||
338 | * @return Collection |
||
339 | */ |
||
340 | 1 | public function initial(int $count = 1) : Collection |
|
345 | /** |
||
346 | * Get all but the first X items from the collection |
||
347 | * @param int $count the number of items to exclude (defaults to 1) |
||
348 | * @return Collection |
||
349 | */ |
||
350 | 1 | public function rest(int $count = 1) : Collection |
|
355 | /** |
||
356 | * Execute a callable for each item in the collection (does not modify the collection) |
||
357 | * @param callable $iterator the callable to execute |
||
358 | * @return $this |
||
359 | */ |
||
360 | 1 | public function each(callable $iterator) : Collection |
|
367 | /** |
||
368 | * Execute a callable for each item in the collection (does not modify the collection) |
||
369 | * @param callable $iterator the callable to execute |
||
370 | * @return $this |
||
371 | */ |
||
372 | 1 | public function invoke(callable $iterator) : Collection |
|
376 | /** |
||
377 | * Get all the collection keys |
||
378 | * @return $this |
||
379 | */ |
||
380 | public function keys() : Collection |
||
384 | /** |
||
385 | * Pluck a value from each object (uses map internally) |
||
386 | * @param string|int $key the key to extract |
||
387 | * @return $this |
||
388 | */ |
||
389 | 1 | public function pluck($key) : Collection |
|
397 | /** |
||
398 | * Intersect the collection with another iterable (uses filter internally) |
||
399 | * @param interable $values the data to intersect with |
||
400 | * @return $this |
||
401 | */ |
||
402 | 1 | public function intersection($values) : Collection |
|
415 | /** |
||
416 | * Reject values on a given predicate (opposite of filter) |
||
417 | * @param callable $iterator the predicate |
||
418 | * @return $this |
||
419 | */ |
||
420 | 1 | public function reject(callable $iterator) : Collection |
|
426 | /** |
||
427 | * Shuffle the values in the collection |
||
428 | * @return Collection |
||
429 | */ |
||
430 | 1 | public function shuffle() : Collection |
|
441 | /** |
||
442 | * Sort the collection using a standard sorting function |
||
443 | * @param callable $iterator the sort function (must return -1, 0 or 1) |
||
444 | * @return Collection |
||
445 | */ |
||
446 | 1 | public function sortBy(callable $iterator) : Collection |
|
452 | /** |
||
453 | * Inspect the whole collection (as an array) mid-chain |
||
454 | * @param callable $iterator the callable to execute |
||
455 | * @return $this |
||
456 | */ |
||
457 | 1 | public function tap(callable $iterator) : Collection |
|
462 | /** |
||
463 | * Modify the whole collection (as an array) mid-chain |
||
464 | * @param callable $iterator the callable to execute |
||
465 | * @return Collection |
||
466 | */ |
||
467 | 1 | public function thru(callable $iterator) : Collection |
|
473 | /** |
||
474 | * Leave only unique items in the collection |
||
475 | * @return Collection |
||
476 | */ |
||
477 | 3 | public function unique() : Collection |
|
488 | /** |
||
489 | * Get only the values of the collection |
||
490 | * @return Collection |
||
491 | */ |
||
492 | 3 | public function values() : Collection |
|
496 | /** |
||
497 | * Filter items from the collection using key => value pairs |
||
498 | * @param array $properties the key => value to check for in each item |
||
499 | * @param boolean $strict should the comparison be strict |
||
500 | * @return $this |
||
501 | */ |
||
502 | public function where(array $properties, $strict = true) : Collection |
||
514 | /** |
||
515 | * Exclude all listed values from the collection (uses filter internally). |
||
516 | * @param iterable $values the values to exclude |
||
517 | * @return $this |
||
518 | */ |
||
519 | 2 | public function without($values) : Collection |
|
523 | /** |
||
524 | * Combine all the values from the collection with a key |
||
525 | * @param iterable $keys the keys to use |
||
526 | * @return Collection |
||
527 | */ |
||
528 | 2 | public function zip($keys) : Collection |
|
535 | /** |
||
536 | * Reverse the collection order |
||
537 | * @return Collection |
||
538 | */ |
||
539 | 1 | public function reverse() : Collection |
|
543 | |||
544 | // accessors |
||
545 | /** |
||
546 | * Do all of the items in the collection match a given criteria |
||
547 | * @param callable $iterator the criteria - should return true / false |
||
548 | * @return bool |
||
549 | */ |
||
550 | 1 | public function all(callable $iterator) : bool |
|
559 | /** |
||
560 | * Do any of the items in the collection match a given criteria |
||
561 | * @param callable $iterator the criteria - should return true / false |
||
562 | * @return bool |
||
563 | */ |
||
564 | 1 | public function any(callable $iterator) : bool |
|
573 | /** |
||
574 | * Does the collection contain a given value |
||
575 | * @param mixed $needle the value to check for |
||
576 | * @return bool |
||
577 | */ |
||
578 | 1 | public function contains($needle) : bool |
|
587 | /** |
||
588 | * Get the first element matching a given criteria (or null) |
||
589 | * @param callable $iterator the filter criteria |
||
590 | * @return mixed |
||
591 | */ |
||
592 | 1 | public function find(callable $iterator) |
|
601 | /** |
||
602 | * Get all the elements matching a given criteria (with the option to limit the number of results) |
||
603 | * @param callable $iterator the search criteria |
||
604 | * @param int|null $limit optional limit to the number of results (default to null - no limit) |
||
605 | * @return Collection |
||
606 | */ |
||
607 | 1 | public function findAll(callable $iterator, int $limit = null) : Collection |
|
620 | /** |
||
621 | * Get the key corresponding to a value (or false) |
||
622 | * @param mixed $needle the value to search for |
||
623 | * @return mixed |
||
624 | */ |
||
625 | 1 | public function indexOf($needle) |
|
629 | /** |
||
630 | * Get the last key corresponding to a value (or false) |
||
631 | * @param mixed $needle the value to search for |
||
632 | * @return mixed |
||
633 | */ |
||
634 | 1 | public function lastIndexOf($needle) |
|
644 | /** |
||
645 | * Get the number of elements in the collection |
||
646 | * @return int |
||
647 | */ |
||
648 | 1 | public function size() : int |
|
652 | /** |
||
653 | * Get the minimal item in the collection |
||
654 | * @return mixed |
||
655 | */ |
||
656 | 1 | public function min() |
|
668 | /** |
||
669 | * Get the maximum item in the collection |
||
670 | * @return mixed |
||
671 | */ |
||
672 | 1 | public function max() |
|
684 | /** |
||
685 | * Does the collection contain a given key |
||
686 | * @param string|int $key the key to check |
||
687 | * @return bool |
||
688 | */ |
||
689 | 1 | public function has($key) : bool |
|
693 | /** |
||
694 | * Reduce the collection to a single value |
||
695 | * @param callable $iterator the reducer |
||
696 | * @param mixed $initial the initial value |
||
697 | * @return mixed the final value |
||
698 | */ |
||
699 | 1 | public function reduce(callable $iterator, $initial = null) |
|
703 | /** |
||
704 | * Reduce the collection to a single value, starting from the last element |
||
705 | * @param callable $iterator the reducer |
||
706 | * @param mixed $initial the initial value |
||
707 | * @return mixed the final value |
||
708 | */ |
||
709 | 1 | public function reduceRight(callable $iterator, $initial = null) |
|
713 | } |
||
714 |