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 |
||
25 | class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable |
||
26 | { |
||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $items; |
||
31 | |||
32 | /** |
||
33 | * Collection constructor. |
||
34 | * |
||
35 | * @param array $items |
||
36 | */ |
||
37 | 148 | public function __construct(array $items = []) |
|
42 | |||
43 | /** |
||
44 | * @return array |
||
45 | */ |
||
46 | 52 | public function toArray() |
|
50 | |||
51 | /** |
||
52 | * Set the value to the key no matter what. |
||
53 | * |
||
54 | * @param string $key |
||
55 | * @param mixed $value |
||
56 | * |
||
57 | * @return $this |
||
58 | */ |
||
59 | 46 | public function set($key, $value) |
|
65 | |||
66 | /** |
||
67 | * Get the value related to the key. |
||
68 | * |
||
69 | * @param string $key |
||
70 | * @param mixed $default |
||
71 | * |
||
72 | * @return mixed|null |
||
73 | */ |
||
74 | 3 | public function get($key, $default = null) |
|
82 | |||
83 | /** |
||
84 | * Get the item at the given index (numerically). |
||
85 | * |
||
86 | * @param int $index |
||
87 | * |
||
88 | * @return mixed|null |
||
89 | */ |
||
90 | 11 | public function atIndex($index) |
|
101 | |||
102 | /** |
||
103 | * Get the key of a value if exists. |
||
104 | * |
||
105 | * @param mixed $value |
||
106 | * |
||
107 | * @return mixed|null |
||
108 | */ |
||
109 | 10 | public function keyOf($value) |
|
119 | |||
120 | /** |
||
121 | * Get the index of a value if exists (numerically). |
||
122 | * |
||
123 | * @param mixed $value |
||
124 | * |
||
125 | * @return int|null |
||
126 | */ |
||
127 | 6 | public function indexOf($value) |
|
139 | |||
140 | /** |
||
141 | * Test is the key exists. |
||
142 | * |
||
143 | * @param string $key |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | 42 | public function containsKey($key) |
|
151 | |||
152 | /** |
||
153 | * Test if this values exists. |
||
154 | * |
||
155 | * @param mixed $value |
||
156 | * |
||
157 | * @performanceCompared true |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | 1 | public function contains($value) |
|
165 | |||
166 | /** |
||
167 | * Add a new value to the collection, next numeric index will be used. |
||
168 | * |
||
169 | * @param mixed $item |
||
170 | * |
||
171 | * @return $this |
||
172 | */ |
||
173 | 29 | public function add($item) |
|
179 | |||
180 | /** |
||
181 | * Append the items at the end of the collection not regarding the keys. |
||
182 | * |
||
183 | * @param Traversable|array $values $items |
||
184 | * |
||
185 | * @return $this |
||
186 | */ |
||
187 | 27 | public function append($values) |
|
199 | |||
200 | /** |
||
201 | * Clear the collection of all its items. |
||
202 | * |
||
203 | * @return $this |
||
204 | */ |
||
205 | 1 | public function clear() |
|
211 | |||
212 | /** |
||
213 | * Remove the $key/value in the collection. |
||
214 | * |
||
215 | * @param string $key |
||
216 | * |
||
217 | * @return $this |
||
218 | */ |
||
219 | 8 | public function remove($key) |
|
225 | |||
226 | /** |
||
227 | * Remove the $key/value in the collection and return the removed value. |
||
228 | * |
||
229 | * @param string $key |
||
230 | * |
||
231 | * @return mixed |
||
232 | */ |
||
233 | 2 | public function pull($key) |
|
243 | |||
244 | /** |
||
245 | * Get the first time and reset and rewind. |
||
246 | * |
||
247 | * @param callable|null $callback |
||
248 | * |
||
249 | * @return mixed |
||
250 | */ |
||
251 | 27 | public function first(callable $callback = null) |
|
259 | |||
260 | /** |
||
261 | * Shift an element off the beginning of the collection(in-place). |
||
262 | * |
||
263 | * @performanceCompared true |
||
264 | */ |
||
265 | 1 | public function shift() |
|
271 | |||
272 | /** |
||
273 | * Shift an element off the end of the collection(in-place). |
||
274 | * |
||
275 | * @performanceCompared true |
||
276 | */ |
||
277 | 1 | public function pop() |
|
281 | |||
282 | /** |
||
283 | * Get the last item. |
||
284 | * |
||
285 | * @param callable|null $callback |
||
286 | * |
||
287 | * @return mixed |
||
288 | */ |
||
289 | 27 | public function last(callable $callback = null) |
|
297 | |||
298 | /** |
||
299 | * Map and return a new Collection. |
||
300 | * |
||
301 | * @param callable $callback |
||
302 | * |
||
303 | * @performanceCompared true |
||
304 | * |
||
305 | * @return Collection |
||
306 | */ |
||
307 | 26 | public function map(callable $callback) |
|
317 | |||
318 | /** |
||
319 | * Map (in-place). |
||
320 | * |
||
321 | * @param callable $callback |
||
322 | * |
||
323 | * @return $this |
||
324 | */ |
||
325 | 2 | public function transform(callable $callback) |
|
334 | |||
335 | /** |
||
336 | * Filter and return a new Collection. |
||
337 | * |
||
338 | * @param callable $callback |
||
339 | * |
||
340 | * @performanceCompared true |
||
341 | * |
||
342 | * @return Collection |
||
343 | */ |
||
344 | 5 | public function filter(callable $callback) |
|
356 | |||
357 | /** |
||
358 | * Filter (in-place). |
||
359 | * |
||
360 | * @param callable $callback |
||
361 | * |
||
362 | * @return $this |
||
363 | */ |
||
364 | 4 | public function prune(callable $callback) |
|
375 | |||
376 | /** |
||
377 | * Combine and return a new Collection. |
||
378 | * It takes the keys of the current Collection and assign the values. |
||
379 | * |
||
380 | * @param Traversable|array $values |
||
381 | * @param bool $inPlace |
||
382 | * |
||
383 | * @performanceCompared true |
||
384 | * |
||
385 | * @return mixed |
||
386 | */ |
||
387 | 7 | public function combine($values, $inPlace = false) |
|
403 | |||
404 | /** |
||
405 | * Combine (in-place). |
||
406 | * |
||
407 | * @param Traversable|array $values |
||
408 | * |
||
409 | * @return $this |
||
410 | */ |
||
411 | 3 | public function replace($values) |
|
417 | |||
418 | /** |
||
419 | * Opposite of Combine |
||
420 | * It keeps the values of the current Collection and assign new keys. |
||
421 | * |
||
422 | * @param Traversable|array $keys |
||
423 | * |
||
424 | * @return Collection |
||
425 | */ |
||
426 | 5 | public function combineKeys($keys) |
|
445 | |||
446 | /** |
||
447 | * CombineKeys (in-place). |
||
448 | * |
||
449 | * @param Traversable|array $keys |
||
450 | * |
||
451 | * @return $this |
||
452 | */ |
||
453 | 1 | public function reindex($keys) |
|
459 | |||
460 | /** |
||
461 | * Reduce. |
||
462 | * |
||
463 | * @param callable $callback |
||
464 | * @param null $initial |
||
465 | * |
||
466 | * @performanceCompared true |
||
467 | * |
||
468 | * @return mixed |
||
469 | */ |
||
470 | 25 | public function reduce(callable $callback, $initial = null) |
|
480 | |||
481 | /** |
||
482 | * Run the callback on each element (passive). |
||
483 | * |
||
484 | * @param callable $callback |
||
485 | * |
||
486 | * @performanceCompared true |
||
487 | * |
||
488 | * @return $this |
||
489 | */ |
||
490 | 1 | public function each(callable $callback) |
|
499 | |||
500 | /** |
||
501 | * Flip the keys and the values and return a new Collection. |
||
502 | * |
||
503 | * @performanceCompared true |
||
504 | * |
||
505 | * @return Collection |
||
506 | */ |
||
507 | 3 | public function flip() |
|
516 | |||
517 | /** |
||
518 | * Flip (in-place). |
||
519 | * |
||
520 | * @return $this |
||
521 | */ |
||
522 | 2 | public function invert() |
|
528 | |||
529 | /** |
||
530 | * Merge the items and the collections and return a new Collection. |
||
531 | * |
||
532 | * Note that is a real merge, numerical keys are merged too.(unlike array_merge) |
||
533 | * |
||
534 | * @param Traversable|array $items |
||
535 | * @param bool $inPlace |
||
536 | * |
||
537 | * @performanceCompared true |
||
538 | * |
||
539 | * @return Collection |
||
540 | */ |
||
541 | 8 | public function merge($items, $inPlace = false) |
|
553 | |||
554 | /** |
||
555 | * Merge (in-place). |
||
556 | * |
||
557 | * @param Traversable|array $items |
||
558 | * |
||
559 | * @return $this |
||
560 | */ |
||
561 | 4 | public function coalesce($items) |
|
565 | |||
566 | /** |
||
567 | * Union the collection with Items. |
||
568 | * |
||
569 | * @param Traversable|array $items |
||
570 | * @param bool $inPlace |
||
571 | * |
||
572 | * @performanceCompared true |
||
573 | * |
||
574 | * @return Collection |
||
575 | */ |
||
576 | 8 | public function union($items, $inPlace = false) |
|
590 | |||
591 | /** |
||
592 | * Union (in-place). |
||
593 | * |
||
594 | * @param Traversable|array $items |
||
595 | * |
||
596 | * @return $this |
||
597 | */ |
||
598 | 4 | public function absorb($items) |
|
604 | |||
605 | /** |
||
606 | * Assert that the callback result is $expected for all. |
||
607 | * |
||
608 | * @param callable $callback |
||
609 | * @param bool $expected |
||
610 | * |
||
611 | * @return bool |
||
612 | */ |
||
613 | 26 | public function assert(callable $callback, $expected) |
|
624 | |||
625 | /** |
||
626 | * Return all the values. |
||
627 | * |
||
628 | * @performanceCompared true |
||
629 | * |
||
630 | * @return Collection |
||
631 | */ |
||
632 | 9 | public function values() |
|
636 | |||
637 | /** |
||
638 | * Return all the keys. |
||
639 | * |
||
640 | * @performanceCompared true |
||
641 | * |
||
642 | * @return Collection |
||
643 | */ |
||
644 | 2 | public function keys() |
|
648 | |||
649 | /** |
||
650 | * Pass the collection to the given callback and return the result. |
||
651 | * |
||
652 | * @param callable $callback |
||
653 | * |
||
654 | * @return mixed |
||
655 | */ |
||
656 | 2 | public function pipe(callable $callback) |
|
660 | |||
661 | /** |
||
662 | * Shuffle. (random in-place). |
||
663 | * |
||
664 | * @return $this |
||
665 | */ |
||
666 | 1 | public function shuffle() |
|
672 | |||
673 | /** |
||
674 | * Shuffle and return a new Collection. |
||
675 | * |
||
676 | * @return Collection |
||
677 | */ |
||
678 | 1 | public function random() |
|
685 | |||
686 | /** |
||
687 | * Deduplicate the collection and return a new Collection. |
||
688 | * |
||
689 | * @performanceCompared true |
||
690 | * |
||
691 | * @return Collection |
||
692 | */ |
||
693 | 1 | public function unique() |
|
697 | |||
698 | /** |
||
699 | * @param string $separator |
||
700 | * |
||
701 | * @return string |
||
702 | */ |
||
703 | 24 | public function implode($separator) |
|
707 | |||
708 | /** |
||
709 | * Unique (in-place). |
||
710 | * |
||
711 | * @return $this |
||
712 | */ |
||
713 | 1 | public function distinct() |
|
719 | |||
720 | /** |
||
721 | * Merge the values items by items. |
||
722 | * |
||
723 | * @param Traversable|array $items |
||
724 | * |
||
725 | * @return Collection |
||
726 | */ |
||
727 | 2 | public function zip($items) |
|
737 | |||
738 | /** |
||
739 | * Reverse the collection and return a new Collection. |
||
740 | * |
||
741 | * @performanceCompared true |
||
742 | * |
||
743 | * @return Collection |
||
744 | */ |
||
745 | 1 | public function reverse() |
|
749 | |||
750 | /** |
||
751 | * Reverse (in-place). |
||
752 | * |
||
753 | * @return $this |
||
754 | */ |
||
755 | 8 | public function inverse() |
|
761 | |||
762 | /** |
||
763 | * @return bool |
||
764 | */ |
||
765 | 1 | public function isEmpty() |
|
769 | |||
770 | /** |
||
771 | * Split in the collection in $count parts. |
||
772 | * |
||
773 | * @param int $count |
||
774 | * |
||
775 | * @return Collection |
||
776 | */ |
||
777 | 1 | public function split($count = 1) |
|
781 | |||
782 | /** |
||
783 | * Chunk of $size sub collection. |
||
784 | * |
||
785 | * @param int $size |
||
786 | * |
||
787 | * @performanceCompared true |
||
788 | * |
||
789 | * @return Collection |
||
790 | */ |
||
791 | 2 | public function chunk($size) |
|
795 | |||
796 | /** |
||
797 | * Get a slice of the collection and inject it in a new one. |
||
798 | * |
||
799 | * @param int $offset |
||
800 | * @param int|null $length |
||
801 | * |
||
802 | * @performanceCompared true |
||
803 | * |
||
804 | * @return Collection |
||
805 | */ |
||
806 | 30 | public function slice($offset, $length = null) |
|
810 | |||
811 | /** |
||
812 | * Keep a slice of the collection (in-place). |
||
813 | * |
||
814 | * @param int $offset |
||
815 | * @param int|null $length |
||
816 | * |
||
817 | * @return $this |
||
818 | */ |
||
819 | 6 | public function keep($offset, $length = null) |
|
825 | |||
826 | /** |
||
827 | * Cut a slice of the collection (in-place). |
||
828 | * |
||
829 | * @param int $offset |
||
830 | * @param int|null $length |
||
831 | * |
||
832 | * @return $this |
||
833 | */ |
||
834 | 3 | public function cut($offset, $length = null) |
|
849 | |||
850 | /** |
||
851 | * Compares the collection against $items and returns the values that are not present in the collection. |
||
852 | * |
||
853 | * @param Traversable|array $items |
||
854 | * |
||
855 | * @performanceCompared true |
||
856 | * |
||
857 | * @return Collection |
||
858 | */ |
||
859 | 1 | public function diff($items) |
|
865 | |||
866 | /** |
||
867 | * Compares the collection against $items and returns the keys that are not present in the collection. |
||
868 | * |
||
869 | * @param Traversable|array $items |
||
870 | * |
||
871 | * @performanceCompared true |
||
872 | * |
||
873 | * @return Collection |
||
874 | */ |
||
875 | 1 | public function diffKeys($items) |
|
881 | |||
882 | /** |
||
883 | * Compares the collection against $items and returns the values that exist in the collection. |
||
884 | * |
||
885 | * @param Traversable|array $items |
||
886 | * |
||
887 | * @performanceCompared true |
||
888 | * |
||
889 | * @return Collection |
||
890 | */ |
||
891 | 1 | public function intersect($items) |
|
897 | |||
898 | /** |
||
899 | * Compares the collection against $items and returns the keys that exist in the collection. |
||
900 | * |
||
901 | * @param Traversable|array $items |
||
902 | * |
||
903 | * @performanceCompared true |
||
904 | * |
||
905 | * @return Collection |
||
906 | */ |
||
907 | 1 | public function intersectKeys($items) |
|
913 | |||
914 | /** |
||
915 | * Return true if one item return true to the callback. |
||
916 | * |
||
917 | * @param callable $callback |
||
918 | * |
||
919 | * @return bool |
||
920 | */ |
||
921 | 1 | public function exists(callable $callback) |
|
932 | |||
933 | /* -- --- -- */ |
||
934 | /* -- INTERFACE COMPLIANCE -- */ |
||
935 | /* -- --- -- */ |
||
936 | |||
937 | /* -- -- */ |
||
938 | /* -- JsonSerializable -- */ |
||
939 | /* -- -- */ |
||
940 | |||
941 | /** |
||
942 | * @return array |
||
943 | */ |
||
944 | 2 | public function jsonSerialize() |
|
948 | |||
949 | /* -- -- */ |
||
950 | /* -- Array Access Methods -- */ |
||
951 | /* -- -- */ |
||
952 | |||
953 | /** |
||
954 | * {@inheritDoc} |
||
955 | */ |
||
956 | 1 | public function offsetExists($offset) |
|
960 | |||
961 | /** |
||
962 | * {@inheritDoc} |
||
963 | */ |
||
964 | 2 | public function offsetGet($offset) |
|
972 | |||
973 | /** |
||
974 | * {@inheritDoc} |
||
975 | */ |
||
976 | 1 | public function offsetUnset($offset) |
|
980 | |||
981 | /** |
||
982 | * {@inheritDoc} |
||
983 | */ |
||
984 | 2 | public function offsetSet($offset, $value) |
|
992 | |||
993 | /* -- -- */ |
||
994 | /* -- Iterator Methods -- */ |
||
995 | /* -- -- */ |
||
996 | |||
997 | /** |
||
998 | * {@inheritDoc} |
||
999 | */ |
||
1000 | 36 | public function current() |
|
1004 | |||
1005 | /** |
||
1006 | * {@inheritDoc} |
||
1007 | */ |
||
1008 | 6 | public function key() |
|
1012 | |||
1013 | /** |
||
1014 | * {@inheritDoc} |
||
1015 | */ |
||
1016 | 36 | public function next() |
|
1020 | |||
1021 | /** |
||
1022 | * {@inheritDoc} |
||
1023 | */ |
||
1024 | 148 | public function rewind() |
|
1028 | |||
1029 | /** |
||
1030 | * {@inheritDoc} |
||
1031 | */ |
||
1032 | 31 | public function valid() |
|
1036 | |||
1037 | /* -- -- */ |
||
1038 | /* -- Countable Method -- */ |
||
1039 | /* -- -- */ |
||
1040 | |||
1041 | /** |
||
1042 | * {@inheritDoc} |
||
1043 | */ |
||
1044 | 17 | public function count() |
|
1048 | |||
1049 | /* -- --- -- */ |
||
1050 | /* -- OTHERS -- */ |
||
1051 | /* -- --- -- */ |
||
1052 | |||
1053 | /** |
||
1054 | * @param string $message |
||
1055 | * @param mixed $arguments |
||
1056 | * |
||
1057 | * @throws InvalidArgumentException |
||
1058 | */ |
||
1059 | 21 | protected function doThrow($message, $arguments) |
|
1064 | |||
1065 | /** |
||
1066 | * @return $this |
||
1067 | */ |
||
1068 | 2 | public function dump() |
|
1072 | |||
1073 | /** |
||
1074 | * @param array ...$params |
||
1075 | * |
||
1076 | * @throws InvalidArgumentException |
||
1077 | * |
||
1078 | * @return Collection |
||
1079 | */ |
||
1080 | 25 | public function __invoke(...$params) |
|
1089 | } |
||
1090 |