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 |
||
26 | class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable |
||
27 | { |
||
28 | /** @var array */ |
||
29 | protected $items; |
||
30 | |||
31 | /** |
||
32 | * Collection constructor. |
||
33 | * |
||
34 | * @param array $items |
||
35 | */ |
||
36 | 93 | public function __construct(array $items = []) |
|
41 | |||
42 | /** |
||
43 | * Generate a collection from an array of items. |
||
44 | * I created this method so that it's possible to extend a collection more easily. |
||
45 | * |
||
46 | * @param mixed $items |
||
47 | * |
||
48 | * @return Collection |
||
49 | */ |
||
50 | 30 | public static function factory($items = null) |
|
57 | |||
58 | /** |
||
59 | * Get collection as an array |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | 47 | public function toArray() |
|
67 | |||
68 | /** |
||
69 | * Determine if collection has a given key |
||
70 | * |
||
71 | * @param mixed $key The key to look for |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | 47 | public function has($key) |
|
79 | |||
80 | /** |
||
81 | * Does collection have item at position? |
||
82 | * |
||
83 | * Determine if collection has an item at a particular position (indexed from one). |
||
84 | * Position can be positive and start from the beginning or it can be negative and |
||
85 | * start from the end. |
||
86 | * |
||
87 | * @param int $position |
||
88 | * |
||
89 | * @return bool |
||
90 | */ |
||
91 | 2 | public function hasValueAt($position) |
|
100 | |||
101 | /** |
||
102 | * Get key at given position |
||
103 | * |
||
104 | * Returns the key at the given position, starting from one. Position can be positive (start from beginning) or |
||
105 | * negative (start from the end). |
||
106 | * |
||
107 | * If the position does not exist, a RuntimeException is thrown. |
||
108 | * |
||
109 | * @param int $position |
||
110 | * |
||
111 | * @return string |
||
112 | * |
||
113 | * @throws RuntimeException |
||
114 | */ |
||
115 | 6 | public function getKeyAt($position) |
|
129 | |||
130 | /** |
||
131 | * Get value at given position |
||
132 | * |
||
133 | * Returns the value at the given position, starting from one. Position can be positive (start from beginning) or |
||
134 | * negative (start from the end). |
||
135 | * |
||
136 | * If the position does not exist, a RuntimeException is thrown. |
||
137 | * |
||
138 | * @param int $position |
||
139 | * |
||
140 | * @return mixed |
||
141 | * |
||
142 | * @throws RuntimeException |
||
143 | */ |
||
144 | 2 | public function getValueAt($position) |
|
148 | |||
149 | /** |
||
150 | * Get the key of the first item found matching $item |
||
151 | * |
||
152 | * @todo should this throw an exception if not found? |
||
153 | * |
||
154 | * @param mixed|callable $item |
||
155 | * |
||
156 | * @return mixed|null |
||
157 | */ |
||
158 | 2 | public function keyOf($item) |
|
173 | |||
174 | /** |
||
175 | * Get the offset (index) of the first item found that matches $item |
||
176 | * |
||
177 | * @todo should this throw an exception if not found? |
||
178 | * |
||
179 | * @param mixed|callable $item |
||
180 | * |
||
181 | * @return int|null |
||
182 | */ |
||
183 | 2 | public function indexOf($item) |
|
201 | |||
202 | /** |
||
203 | * Get item by key, with an optional default return value |
||
204 | * |
||
205 | * @param mixed $key |
||
206 | * @param mixed $default |
||
207 | * |
||
208 | * @return mixed |
||
209 | */ |
||
210 | 8 | public function get($key, $default = null) |
|
218 | |||
219 | /** |
||
220 | * Add an item with no regard to key |
||
221 | * |
||
222 | * @param mixed $value |
||
223 | * |
||
224 | * @return $this |
||
225 | */ |
||
226 | 8 | public function add($value) |
|
232 | |||
233 | /** |
||
234 | * Set an item at a given key |
||
235 | * |
||
236 | * @param mixed $key |
||
237 | * @param mixed $value |
||
238 | * @param bool $overwrite If false, do not overwrite existing key |
||
239 | * |
||
240 | * @return $this |
||
241 | */ |
||
242 | 14 | public function set($key, $value, $overwrite = true) |
|
250 | |||
251 | /** |
||
252 | * Delete an item by key |
||
253 | * |
||
254 | * @param mixed $key |
||
255 | * |
||
256 | * @return $this |
||
257 | */ |
||
258 | 3 | public function delete($key) |
|
264 | |||
265 | /** |
||
266 | * Clear the collection of all its items. |
||
267 | * |
||
268 | * @return $this |
||
269 | */ |
||
270 | 2 | public function clear() |
|
276 | |||
277 | /** |
||
278 | * Determine if collection contains given value |
||
279 | * |
||
280 | * @param mixed|callable $val |
||
281 | * @param mixed $key |
||
282 | * |
||
283 | * @return bool |
||
284 | */ |
||
285 | 4 | public function contains($val, $key = null) |
|
303 | |||
304 | /** |
||
305 | * Fetch item from collection by key and remove it from collection |
||
306 | * |
||
307 | * @param mixed $key |
||
308 | * |
||
309 | * @return mixed |
||
310 | */ |
||
311 | 1 | public function pull($key) |
|
319 | |||
320 | /** |
||
321 | * Join collection items using a delimiter |
||
322 | * |
||
323 | * @param string $delim |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | 1 | public function join($delim = '') |
|
331 | |||
332 | /** |
||
333 | * Determine if collection has any items |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | 4 | public function isEmpty() |
|
341 | |||
342 | /** |
||
343 | * Get a collection of only this collection's values (without its keys) |
||
344 | * |
||
345 | * @return Collection |
||
346 | */ |
||
347 | 1 | public function values() |
|
351 | |||
352 | /** |
||
353 | * Get a collection of only this collection's keys |
||
354 | * |
||
355 | * @return Collection |
||
356 | */ |
||
357 | 1 | public function keys() |
|
361 | |||
362 | /** |
||
363 | * Get a collection with order reversed |
||
364 | * |
||
365 | * @return Collection |
||
366 | */ |
||
367 | 6 | public function reverse() |
|
371 | |||
372 | /** |
||
373 | * Get a collection with keys and values flipped |
||
374 | * |
||
375 | * @return Collection |
||
376 | */ |
||
377 | 1 | public function flip() |
|
385 | |||
386 | /** |
||
387 | * Shuffle the order of this collection's values |
||
388 | * |
||
389 | * @return Collection |
||
390 | */ |
||
391 | 1 | public function shuffle() |
|
396 | |||
397 | /** |
||
398 | * Get a random value from the collection |
||
399 | * |
||
400 | * @todo might be useful to add a $count param to specify you want $count random items... |
||
401 | * |
||
402 | * @return mixed |
||
403 | */ |
||
404 | 1 | public function random() |
|
408 | |||
409 | /** |
||
410 | * Sort the collection (using values) |
||
411 | * |
||
412 | * @param callable $alg |
||
413 | * |
||
414 | * @return $this |
||
415 | */ |
||
416 | 2 | public function sort(callable $alg = null) |
|
426 | |||
427 | /** |
||
428 | * Sort the collection (using keys) |
||
429 | * |
||
430 | * @param callable $alg |
||
431 | * |
||
432 | * @return $this |
||
433 | */ |
||
434 | 2 | public function ksort(callable $alg = null) |
|
444 | |||
445 | /** |
||
446 | * Append items to collection without regard to keys |
||
447 | * |
||
448 | * @param array|Traversable $items |
||
449 | * |
||
450 | * @return $this |
||
451 | */ |
||
452 | 2 | public function append($items) |
|
464 | |||
465 | /** |
||
466 | * Return first item or first item where callback returns true |
||
467 | * |
||
468 | * @param callable|null $callback |
||
469 | * |
||
470 | * @return mixed|null |
||
471 | */ |
||
472 | 7 | public function first(callable $callback = null) |
|
483 | |||
484 | /** |
||
485 | * Return last item or last item where callback returns true |
||
486 | * |
||
487 | * @param callable|null $callback |
||
488 | * |
||
489 | * @return mixed|null |
||
490 | */ |
||
491 | 3 | public function last(callable $callback = null) |
|
495 | |||
496 | /** |
||
497 | * Map collection |
||
498 | * |
||
499 | * Create a new collection using the results of a callback function on each item in this collection. |
||
500 | * |
||
501 | * @param callable $callback |
||
502 | * |
||
503 | * @return Collection |
||
504 | */ |
||
505 | 3 | public function map(callable $callback) |
|
516 | |||
517 | /** |
||
518 | * Combine collection with another traversable/collection |
||
519 | * |
||
520 | * Using this collection's keys, and the incoming collection's values, a new collection is created and returned. |
||
521 | * |
||
522 | * @param array|Traversable $items |
||
523 | * |
||
524 | * @return Collection |
||
525 | */ |
||
526 | 5 | public function combine($items) |
|
539 | |||
540 | /** |
||
541 | * Get a new collection with only distinct values |
||
542 | * |
||
543 | * @return Collection |
||
544 | */ |
||
545 | 1 | public function distinct() |
|
556 | |||
557 | /** |
||
558 | * Remove all duplicate values from collection in-place |
||
559 | * |
||
560 | * @return Collection |
||
561 | */ |
||
562 | 1 | public function deduplicate() |
|
568 | |||
569 | /** |
||
570 | * Return a new collection with only filtered keys/values |
||
571 | * |
||
572 | * The callback accepts value, key, index and should return true if the item should be added to the returned |
||
573 | * collection |
||
574 | * |
||
575 | * @param callable $callback |
||
576 | * |
||
577 | * @return Collection |
||
578 | */ |
||
579 | 2 | public function filter(callable $callback = null) |
|
597 | |||
598 | /** |
||
599 | * Fold collection into a single value |
||
600 | * |
||
601 | * Loop through collection calling a callback function and passing the result to the next iteration, eventually |
||
602 | * returning a single value. |
||
603 | * |
||
604 | * @param callable $callback |
||
605 | * @param mixed $initial |
||
606 | * |
||
607 | * @return null |
||
608 | */ |
||
609 | 1 | public function fold(callable $callback, $initial = null) |
|
619 | |||
620 | /** |
||
621 | * Return a merge of this collection and $items |
||
622 | * |
||
623 | * @param array|Traversable $items |
||
624 | * |
||
625 | * @return Collection |
||
626 | */ |
||
627 | 3 | public function merge($items) |
|
640 | |||
641 | /** |
||
642 | * Create a new collection with a union of this collection and $items |
||
643 | * |
||
644 | * This method is similar to merge, except that existing items will not be overwritten. |
||
645 | * |
||
646 | * @param $items |
||
647 | */ |
||
648 | 2 | public function union($items) |
|
661 | |||
662 | /** |
||
663 | * Call callback for each item in collection, passively |
||
664 | * If at any point the callback returns false, iteration stops. |
||
665 | * |
||
666 | * @param callable $callback |
||
667 | * |
||
668 | * @return $this |
||
669 | */ |
||
670 | 2 | public function each(callable $callback) |
|
681 | |||
682 | /** |
||
683 | * Assert callback returns $expected value for each item in collection. |
||
684 | * |
||
685 | * @todo This can be used to easily make methods like all($callback) and none($callback). |
||
686 | * |
||
687 | * @param callable $callback |
||
688 | * @param bool $expected |
||
689 | * |
||
690 | * @return bool |
||
691 | */ |
||
692 | 1 | public function assert(callable $callback, $expected = true) |
|
703 | |||
704 | /** |
||
705 | * Pipe collection through a callback |
||
706 | * |
||
707 | * @param callable $callback |
||
708 | * |
||
709 | * @return mixed |
||
710 | */ |
||
711 | 1 | public function pipe(callable $callback) |
|
715 | |||
716 | /** |
||
717 | * Get new collection in chunks of $size |
||
718 | * |
||
719 | * Creates a new collection of arrays of $size length. The remainder items will be placed at the end. |
||
720 | * |
||
721 | * @param int $size |
||
722 | * |
||
723 | * @return Collection |
||
724 | */ |
||
725 | 2 | public function chunk($size) |
|
729 | |||
730 | /** |
||
731 | * Get a new collection of $count chunks |
||
732 | * |
||
733 | * @todo It might be useful to have a method that spreads remainder items more evenly so you don't end up with the |
||
734 | * last item containing only one or two items. |
||
735 | * |
||
736 | * @param int $count |
||
737 | * |
||
738 | * @return Collection |
||
739 | */ |
||
740 | 1 | public function split($count = 1) |
|
744 | |||
745 | /** |
||
746 | * Get a slice of this collection. |
||
747 | * |
||
748 | * @param int $offset |
||
749 | * @param int|null $length |
||
750 | * |
||
751 | * @return Collection |
||
752 | */ |
||
753 | 1 | public function slice($offset, $length = null) |
|
757 | |||
758 | /** |
||
759 | * Get collection with only differing items |
||
760 | * |
||
761 | * @param array|Traversable $items |
||
762 | * |
||
763 | * @return Collection |
||
764 | */ |
||
765 | 1 | public function diff($items) |
|
769 | |||
770 | /** |
||
771 | * Get collection with only differing items (by key) |
||
772 | * |
||
773 | * @param array|Traversable $items |
||
774 | * |
||
775 | * @return Collection |
||
776 | */ |
||
777 | 1 | public function kdiff($items) |
|
781 | |||
782 | /** |
||
783 | * Get collection with only intersecting items |
||
784 | * |
||
785 | * @param array|Traversable $items |
||
786 | * |
||
787 | * @return Collection |
||
788 | */ |
||
789 | 1 | public function intersect($items) |
|
793 | |||
794 | /** |
||
795 | * Get collection with only intersecting items (by key) |
||
796 | * |
||
797 | * @param array|Traversable $items |
||
798 | * |
||
799 | * @return Collection |
||
800 | */ |
||
801 | 1 | public function kintersect($items) |
|
805 | |||
806 | /** |
||
807 | * Remove last item in collection and return it |
||
808 | * |
||
809 | * @return mixed |
||
810 | */ |
||
811 | 1 | public function pop() |
|
815 | |||
816 | /** |
||
817 | * Remove first item in collection and return it (and re-index if numerically indexed) |
||
818 | * |
||
819 | * @return mixed |
||
820 | */ |
||
821 | 2 | public function shift() |
|
825 | |||
826 | /** |
||
827 | * Add item to the end of the collection |
||
828 | * |
||
829 | * @note This method is no different than add() but I included it for consistency's sake since I have the others |
||
830 | * |
||
831 | * @param mixed $item |
||
832 | * |
||
833 | * @return $this |
||
834 | */ |
||
835 | 1 | public function push($item) |
|
839 | |||
840 | /** |
||
841 | * Add item to the beginning of the collection (and re-index if a numerically indexed collection) |
||
842 | * |
||
843 | * @param mixed $item |
||
844 | * |
||
845 | * @return $this |
||
846 | */ |
||
847 | 5 | public function unshift($item) |
|
853 | |||
854 | /** |
||
855 | * Get new collection padded to specified $size with $value |
||
856 | * |
||
857 | * Using $value, pad the collection to specified $size. If $size is smaller or equal to the size of the collection, |
||
858 | * then no padding takes place. If $size is positive, padding is added to the end, while if negative, padding will |
||
859 | * be added to the beginning. |
||
860 | * |
||
861 | * @param int $size |
||
862 | * @param mixed $value |
||
863 | * |
||
864 | * @return Collection |
||
865 | */ |
||
866 | 3 | public function pad($size, $value = null) |
|
879 | |||
880 | /** |
||
881 | * Partition collection into two collections using a callback |
||
882 | * |
||
883 | * Iterates over each element in the collection with a callback. Items where callback returns true are placed in one |
||
884 | * collection and the rest in another. Finally, the two collections are placed in an array and returned for easy use |
||
885 | * with the list() function. ( list($a, $b) = $col->partition(function($val, $key, $index) {}) ) |
||
886 | * |
||
887 | * @param callable $callback |
||
888 | * |
||
889 | * @return array<Collection> |
||
890 | */ |
||
891 | 2 | public function partition(callable $callback) |
|
907 | |||
908 | /** ++++ ++++ **/ |
||
909 | /** ++ Interface Compliance ++ **/ |
||
910 | /** ++++ ++++ **/ |
||
911 | |||
912 | /** |
||
913 | * @return array |
||
914 | */ |
||
915 | 1 | public function jsonSerialize() |
|
919 | |||
920 | /** ++++ ++++ **/ |
||
921 | /** ++ Array Access Methods ++ **/ |
||
922 | /** ++++ ++++ **/ |
||
923 | |||
924 | /** |
||
925 | * {@inheritDoc} |
||
926 | */ |
||
927 | 1 | public function offsetExists($offset) |
|
931 | |||
932 | /** |
||
933 | * {@inheritDoc} |
||
934 | */ |
||
935 | 2 | public function offsetGet($offset) |
|
943 | |||
944 | /** |
||
945 | * {@inheritDoc} |
||
946 | */ |
||
947 | 1 | public function offsetUnset($offset) |
|
951 | |||
952 | /** |
||
953 | * {@inheritDoc} |
||
954 | */ |
||
955 | 1 | public function offsetSet($offset, $value) |
|
963 | |||
964 | /** ++++ ++++ **/ |
||
965 | /** ++ Iterator Methods ++ **/ |
||
966 | /** ++++ ++++ **/ |
||
967 | |||
968 | /** |
||
969 | * {@inheritDoc} |
||
970 | */ |
||
971 | 35 | public function current() |
|
975 | |||
976 | /** |
||
977 | * {@inheritDoc} |
||
978 | */ |
||
979 | 35 | public function key() |
|
983 | |||
984 | /** |
||
985 | * {@inheritDoc} |
||
986 | */ |
||
987 | 34 | public function next() |
|
991 | |||
992 | /** |
||
993 | * {@inheritDoc} |
||
994 | * |
||
995 | * @todo Should this return $this? |
||
996 | */ |
||
997 | 93 | public function rewind() |
|
1001 | |||
1002 | /** |
||
1003 | * {@inheritDoc} |
||
1004 | */ |
||
1005 | 36 | public function valid() |
|
1009 | |||
1010 | /** ++++ ++++ **/ |
||
1011 | /** ++ Countable Method ++ **/ |
||
1012 | /** ++++ ++++ **/ |
||
1013 | |||
1014 | /** |
||
1015 | * {@inheritDoc} |
||
1016 | */ |
||
1017 | 10 | public function count() |
|
1021 | } |