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 |
||
24 | class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable |
||
25 | { |
||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $items; |
||
30 | |||
31 | /** |
||
32 | * Collection constructor. |
||
33 | * |
||
34 | * @param array $items |
||
35 | */ |
||
36 | 123 | public function __construct(array $items = []) |
|
41 | |||
42 | /** |
||
43 | * @return array |
||
44 | */ |
||
45 | 29 | public function toArray() |
|
49 | |||
50 | /** |
||
51 | * Set the value to the key no matter what. |
||
52 | * |
||
53 | * @param string $key |
||
54 | * @param mixed $value |
||
55 | * |
||
56 | * @return $this |
||
57 | */ |
||
58 | 23 | public function set($key, $value) |
|
64 | |||
65 | /** |
||
66 | * Get the value related to the key. |
||
67 | * |
||
68 | * @param string $key |
||
69 | * @param mixed $default |
||
70 | * |
||
71 | * @return mixed|null |
||
72 | */ |
||
73 | 3 | public function get($key, $default = null) |
|
81 | |||
82 | /** |
||
83 | * Get the item at the given index (numerically). |
||
84 | * |
||
85 | * @param int $index |
||
86 | * |
||
87 | * @return mixed|null |
||
88 | */ |
||
89 | 11 | public function atIndex($index) |
|
90 | { |
||
91 | 11 | $i = 0; |
|
92 | 11 | foreach ($this->items as $key => $value) { |
|
93 | 11 | if ($i++ == $index) { |
|
94 | 10 | return $value; |
|
95 | } |
||
96 | 7 | } |
|
97 | |||
98 | 1 | return null; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Get the key of a value if exists. |
||
103 | * |
||
104 | * @param mixed $value |
||
105 | * |
||
106 | * @return mixed|null |
||
107 | */ |
||
108 | 10 | public function keyOf($value) |
|
109 | { |
||
110 | 10 | foreach ($this->items as $key => $iValue) { |
|
111 | 10 | if ($value === $iValue) { |
|
112 | 8 | return $key; |
|
113 | } |
||
114 | 9 | } |
|
115 | |||
116 | 2 | return null; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Get the index of a value if exists (numerically). |
||
121 | * |
||
122 | * @param mixed $value |
||
123 | * |
||
124 | * @return int|null |
||
125 | */ |
||
126 | 6 | public function indexOf($value) |
|
127 | { |
||
128 | 6 | $i = 0; |
|
129 | 6 | foreach ($this->items as $key => $iValue) { |
|
130 | 6 | if ($value === $iValue) { |
|
131 | 4 | return $i; |
|
132 | } |
||
133 | 5 | $i++; |
|
134 | 5 | } |
|
135 | |||
136 | 2 | return null; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Test is the key exists. |
||
141 | * |
||
142 | * @param string $key |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | 19 | public function containsKey($key) |
|
150 | |||
151 | /** |
||
152 | * Test if this values exists. |
||
153 | * |
||
154 | * @param mixed $value |
||
155 | * |
||
156 | * @performanceCompared true |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | 1 | public function contains($value) |
|
164 | |||
165 | /** |
||
166 | * Add a new value to the collection, next numeric index will be used. |
||
167 | * |
||
168 | * @param mixed $item |
||
169 | * |
||
170 | * @return $this |
||
171 | */ |
||
172 | 6 | public function add($item) |
|
178 | |||
179 | /** |
||
180 | * Append the items at the end of the collection not regarding the keys. |
||
181 | * |
||
182 | * @param Traversable|array $values $items |
||
183 | * |
||
184 | * @return $this |
||
185 | */ |
||
186 | 4 | public function append($values) |
|
187 | { |
||
188 | 4 | if (!is_array($values) && !($values instanceof Traversable)) { |
|
189 | 1 | $this->doThrow('Invalid input type for append.', $values); |
|
190 | } |
||
191 | |||
192 | 3 | foreach ($values as $value) { |
|
193 | 3 | $this->add($value); |
|
194 | 3 | } |
|
195 | |||
196 | 3 | return $this; |
|
197 | } |
||
198 | |||
199 | /** |
||
200 | * Clear the collection of all its items. |
||
201 | * |
||
202 | * @return $this |
||
203 | */ |
||
204 | 1 | public function clear() |
|
210 | |||
211 | /** |
||
212 | * Remove the $key/value in the collection. |
||
213 | * |
||
214 | * @param string $key |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | 8 | public function remove($key) |
|
224 | |||
225 | /** |
||
226 | * Remove the $key/value in the collection and return the removed value. |
||
227 | * |
||
228 | * @param string $key |
||
229 | * |
||
230 | * @return mixed |
||
231 | */ |
||
232 | 2 | public function pull($key) |
|
242 | |||
243 | /** |
||
244 | * Get the first time and reset and rewind. |
||
245 | * |
||
246 | * @param callable|null $callback |
||
247 | * |
||
248 | * @return mixed |
||
249 | */ |
||
250 | 4 | public function first(callable $callback = null) |
|
258 | |||
259 | /** |
||
260 | * Shift an element off the beginning of the collection(in-place). |
||
261 | * |
||
262 | * @performanceCompared true |
||
263 | */ |
||
264 | 1 | public function shift() |
|
270 | |||
271 | /** |
||
272 | * Shift an element off the end of the collection(in-place). |
||
273 | * |
||
274 | * @performanceCompared true |
||
275 | */ |
||
276 | 1 | public function pop() |
|
280 | |||
281 | /** |
||
282 | * Get the last item. |
||
283 | * |
||
284 | * @param callable|null $callback |
||
285 | * |
||
286 | * @return mixed |
||
287 | */ |
||
288 | 4 | public function last(callable $callback = null) |
|
296 | |||
297 | /** |
||
298 | * Map and return a new Collection. |
||
299 | * |
||
300 | * @param callable $callback |
||
301 | * |
||
302 | * @performanceCompared true |
||
303 | * |
||
304 | * @return Collection |
||
305 | */ |
||
306 | 3 | public function map(callable $callback) |
|
316 | |||
317 | /** |
||
318 | * Map (in-place). |
||
319 | * |
||
320 | * @param callable $callback |
||
321 | * |
||
322 | * @return $this |
||
323 | */ |
||
324 | 2 | public function transform(callable $callback) |
|
333 | |||
334 | /** |
||
335 | * Filter and return a new Collection. |
||
336 | * |
||
337 | * @param callable $callback |
||
338 | * |
||
339 | * @performanceCompared true |
||
340 | * |
||
341 | * @return Collection |
||
342 | */ |
||
343 | 5 | public function filter(callable $callback) |
|
355 | |||
356 | /** |
||
357 | * Filter (in-place). |
||
358 | * |
||
359 | * @param callable $callback |
||
360 | * |
||
361 | * @return $this |
||
362 | */ |
||
363 | 4 | public function prune(callable $callback) |
|
374 | |||
375 | /** |
||
376 | * Combine and return a new Collection. |
||
377 | * It takes the keys of the current Collection and assign the values. |
||
378 | * |
||
379 | * @param Traversable|array $values |
||
380 | * @param bool $inPlace |
||
381 | * |
||
382 | * @performanceCompared true |
||
383 | * |
||
384 | * @return mixed |
||
385 | */ |
||
386 | 7 | public function combine($values, $inPlace = false) |
|
402 | |||
403 | /** |
||
404 | * Combine (in-place). |
||
405 | * |
||
406 | * @param Traversable|array $values |
||
407 | * |
||
408 | * @return $this |
||
409 | */ |
||
410 | 3 | public function replace($values) |
|
416 | |||
417 | /** |
||
418 | * Opposite of Combine |
||
419 | * It keeps the values of the current Collection and assign new keys. |
||
420 | * |
||
421 | * @param Traversable|array $keys |
||
422 | * |
||
423 | * @return Collection |
||
424 | */ |
||
425 | 5 | public function combineKeys($keys) |
|
444 | |||
445 | /** |
||
446 | * CombineKeys (in-place). |
||
447 | * |
||
448 | * @param Traversable|array $keys |
||
449 | * |
||
450 | * @return $this |
||
451 | */ |
||
452 | 1 | public function reindex($keys) |
|
458 | |||
459 | /** |
||
460 | * Reduce. |
||
461 | * |
||
462 | * @param callable $callback |
||
463 | * @param null $initial |
||
464 | * |
||
465 | * @performanceCompared true |
||
466 | * |
||
467 | * @return mixed |
||
468 | */ |
||
469 | 2 | public function reduce(callable $callback, $initial = null) |
|
479 | |||
480 | /** |
||
481 | * Run the callback on each element (passive). |
||
482 | * |
||
483 | * @param callable $callback |
||
484 | * |
||
485 | * @performanceCompared true |
||
486 | * |
||
487 | * @return $this |
||
488 | */ |
||
489 | 1 | public function each(callable $callback) |
|
498 | |||
499 | /** |
||
500 | * Flip the keys and the values and return a new Collection. |
||
501 | * |
||
502 | * @performanceCompared true |
||
503 | * |
||
504 | * @return Collection |
||
505 | */ |
||
506 | 3 | public function flip() |
|
515 | |||
516 | /** |
||
517 | * Flip (in-place). |
||
518 | * |
||
519 | * @return $this |
||
520 | */ |
||
521 | 2 | public function invert() |
|
527 | |||
528 | /** |
||
529 | * Merge the items and the collections and return a new Collection. |
||
530 | * |
||
531 | * Note that is a real merge, numerical keys are merged too.(unlike array_merge) |
||
532 | * |
||
533 | * @param Traversable|array $items |
||
534 | * @param bool $inPlace |
||
535 | * |
||
536 | * @performanceCompared true |
||
537 | * |
||
538 | * @return Collection |
||
539 | */ |
||
540 | 8 | public function merge($items, $inPlace = false) |
|
552 | |||
553 | /** |
||
554 | * Merge (in-place). |
||
555 | * |
||
556 | * @param Traversable|array $items |
||
557 | * |
||
558 | * @return $this |
||
559 | */ |
||
560 | 4 | public function coalesce($items) |
|
564 | |||
565 | /** |
||
566 | * Union the collection with Items. |
||
567 | * |
||
568 | * @param Traversable|array $items |
||
569 | * @param bool $inPlace |
||
570 | * |
||
571 | * @performanceCompared true |
||
572 | * |
||
573 | * @return Collection |
||
574 | */ |
||
575 | 8 | public function union($items, $inPlace = false) |
|
589 | |||
590 | /** |
||
591 | * Union (in-place). |
||
592 | * |
||
593 | * @param Traversable|array $items |
||
594 | * |
||
595 | * @return $this |
||
596 | */ |
||
597 | 4 | public function absorb($items) |
|
603 | |||
604 | /** |
||
605 | * Assert that the callback result is $expected for all. |
||
606 | * |
||
607 | * @param callable $callback |
||
608 | * @param bool $expected |
||
609 | * |
||
610 | * @return bool |
||
611 | */ |
||
612 | 1 | public function assert(callable $callback, $expected) |
|
623 | |||
624 | /** |
||
625 | * Return all the values. |
||
626 | * |
||
627 | * @performanceCompared true |
||
628 | * |
||
629 | * @return Collection |
||
630 | */ |
||
631 | 9 | public function values() |
|
635 | |||
636 | /** |
||
637 | * Return all the keys. |
||
638 | * |
||
639 | * @performanceCompared true |
||
640 | * |
||
641 | * @return Collection |
||
642 | */ |
||
643 | 2 | public function keys() |
|
647 | |||
648 | /** |
||
649 | * Pass the collection to the given callback and return the result. |
||
650 | * |
||
651 | * @param callable $callback |
||
652 | * |
||
653 | * @return mixed |
||
654 | */ |
||
655 | 2 | public function pipe(callable $callback) |
|
656 | { |
||
657 | 2 | return $callback($this); |
|
658 | } |
||
659 | |||
660 | /** |
||
661 | * Shuffle. (random in-place). |
||
662 | * |
||
663 | * @return $this |
||
664 | */ |
||
665 | 1 | public function shuffle() |
|
666 | { |
||
667 | 1 | shuffle($this->items); |
|
668 | |||
669 | 1 | return $this; |
|
670 | } |
||
671 | |||
672 | /** |
||
673 | * Shuffle and return a new Collection. |
||
674 | * |
||
675 | * @return Collection |
||
676 | */ |
||
677 | 1 | public function random() |
|
678 | { |
||
679 | 1 | $array = $this->items; |
|
680 | 1 | shuffle($array); |
|
681 | |||
682 | 1 | return Factory::create($array); |
|
683 | } |
||
684 | |||
685 | /** |
||
686 | * Deduplicate the collection and return a new Collection. |
||
687 | * |
||
688 | * @performanceCompared true |
||
689 | * |
||
690 | * @return Collection |
||
691 | */ |
||
692 | 1 | public function unique() |
|
693 | { |
||
694 | 1 | return Factory::create(array_unique($this->items)); |
|
695 | } |
||
696 | |||
697 | /** |
||
698 | * @param string $separator |
||
699 | * |
||
700 | * @return string |
||
701 | */ |
||
702 | 1 | public function implode($separator) |
|
703 | { |
||
704 | 1 | return implode($this->items, $separator); |
|
705 | } |
||
706 | |||
707 | /** |
||
708 | * Unique (in-place). |
||
709 | * |
||
710 | * @return $this |
||
711 | */ |
||
712 | 1 | public function distinct() |
|
713 | { |
||
714 | 1 | $this->items = array_unique($this->items); |
|
715 | |||
716 | 1 | return $this; |
|
717 | } |
||
718 | |||
719 | /** |
||
720 | * Merge the values items by items. |
||
721 | * |
||
722 | * @param Traversable|array $items |
||
723 | * |
||
724 | * @return Collection |
||
725 | */ |
||
726 | 2 | public function zip($items) |
|
727 | { |
||
728 | 2 | $collection = Factory::create($items); |
|
729 | |||
730 | 2 | return $this->map( |
|
731 | function ($value, $key, $index) use ($collection) { |
||
732 | 2 | return [$value, $collection->atIndex($index)]; |
|
733 | } |
||
734 | 2 | ); |
|
735 | } |
||
736 | |||
737 | /** |
||
738 | * Reverse the collection and return a new Collection. |
||
739 | * |
||
740 | * @performanceCompared true |
||
741 | * |
||
742 | * @return Collection |
||
743 | */ |
||
744 | 1 | public function reverse() |
|
745 | { |
||
746 | 1 | return Factory::create(array_reverse($this->items, true)); |
|
747 | } |
||
748 | |||
749 | /** |
||
750 | * Reverse (in-place). |
||
751 | * |
||
752 | * @return $this |
||
753 | */ |
||
754 | 1 | public function inverse() |
|
755 | { |
||
756 | 1 | $this->items = array_reverse($this->items); |
|
757 | |||
758 | 1 | return $this; |
|
759 | } |
||
760 | |||
761 | /** |
||
762 | * @return bool |
||
763 | */ |
||
764 | 1 | public function isEmpty() |
|
768 | |||
769 | /** |
||
770 | * Split in the collection in $count parts. |
||
771 | * |
||
772 | * @param int $count |
||
773 | * |
||
774 | * @return Collection |
||
775 | */ |
||
776 | 1 | public function split($count = 1) |
|
777 | { |
||
778 | 1 | return $this->chunk(ceil($this->count() / $count)); |
|
779 | } |
||
780 | |||
781 | /** |
||
782 | * Chunk of $size sub collection. |
||
783 | * |
||
784 | * @param int $size |
||
785 | * |
||
786 | * @performanceCompared true |
||
787 | * |
||
788 | * @return Collection |
||
789 | */ |
||
790 | 2 | public function chunk($size) |
|
791 | { |
||
792 | 2 | return Factory::create(array_chunk($this->items, $size, true)); |
|
793 | } |
||
794 | |||
795 | /** |
||
796 | * Get a slice of the collection and inject it in a new one. |
||
797 | * |
||
798 | * @param int $offset |
||
799 | * @param int|null $length |
||
800 | * |
||
801 | * @performanceCompared true |
||
802 | * |
||
803 | * @return Collection |
||
804 | */ |
||
805 | 7 | public function slice($offset, $length = null) |
|
806 | { |
||
807 | 7 | return Factory::create(array_slice($this->items, $offset, $length, true)); |
|
808 | } |
||
809 | |||
810 | /** |
||
811 | * Keep a slice of the collection (in-place). |
||
812 | * |
||
813 | * @param int $offset |
||
814 | * @param int|null $length |
||
815 | * |
||
816 | * @return $this |
||
817 | */ |
||
818 | 6 | public function keep($offset, $length = null) |
|
819 | { |
||
820 | 6 | $this->items = $this->slice($offset, $length)->toArray(); |
|
821 | |||
822 | 6 | return $this; |
|
823 | } |
||
824 | |||
825 | /** |
||
826 | * Cut a slice of the collection (in-place). |
||
827 | * |
||
828 | * @param int $offset |
||
829 | * @param int|null $length |
||
830 | * |
||
831 | * @return $this |
||
832 | */ |
||
833 | 3 | public function cut($offset, $length = null) |
|
834 | { |
||
835 | 3 | if ($length === null) { |
|
836 | 1 | $length = PHP_INT_MAX; |
|
837 | 1 | } |
|
838 | 3 | if ($offset < 0) { |
|
839 | 1 | $offset = $this->count() + $offset; |
|
840 | 1 | } |
|
841 | |||
842 | 3 | return $this->prune( |
|
843 | 3 | function ($value, $key, $index) use ($offset, $length) { |
|
844 | 3 | return !(($index >= $offset) && ($index < $offset + $length)); |
|
845 | } |
||
846 | 3 | ); |
|
847 | } |
||
848 | |||
849 | /** |
||
850 | * Compares the collection against $items and returns the values that are not present in the collection. |
||
851 | * |
||
852 | * @param Traversable|array $items |
||
853 | * |
||
854 | * @performanceCompared true |
||
855 | * |
||
856 | * @return Collection |
||
857 | */ |
||
858 | 1 | public function diff($items) |
|
859 | { |
||
860 | 1 | $items = Factory::getArrayForItems($items); |
|
861 | |||
862 | 1 | return Factory::create(array_diff($this->items, $items)); |
|
863 | } |
||
864 | |||
865 | /** |
||
866 | * Compares the collection against $items and returns the keys that are not present in the collection. |
||
867 | * |
||
868 | * @param Traversable|array $items |
||
869 | * |
||
870 | * @performanceCompared true |
||
871 | * |
||
872 | * @return Collection |
||
873 | */ |
||
874 | 1 | public function diffKeys($items) |
|
875 | { |
||
876 | 1 | $items = Factory::getArrayForItems($items); |
|
877 | |||
878 | 1 | return Factory::create(array_diff_key($this->items, $items)); |
|
879 | } |
||
880 | |||
881 | /** |
||
882 | * Compares the collection against $items and returns the values that exist in the collection. |
||
883 | * |
||
884 | * @param Traversable|array $items |
||
885 | * |
||
886 | * @performanceCompared true |
||
887 | * |
||
888 | * @return Collection |
||
889 | */ |
||
890 | 1 | public function intersect($items) |
|
891 | { |
||
892 | 1 | $items = Factory::getArrayForItems($items); |
|
893 | |||
894 | 1 | return Factory::create(array_intersect($this->items, $items)); |
|
895 | } |
||
896 | |||
897 | /** |
||
898 | * Compares the collection against $items and returns the keys that exist in the collection. |
||
899 | * |
||
900 | * @param Traversable|array $items |
||
901 | * |
||
902 | * @performanceCompared true |
||
903 | * |
||
904 | * @return Collection |
||
905 | */ |
||
906 | 1 | public function intersectKeys($items) |
|
907 | { |
||
908 | 1 | $items = Factory::getArrayForItems($items); |
|
909 | |||
910 | 1 | return Factory::create(array_intersect_key($this->items, $items)); |
|
911 | } |
||
912 | |||
913 | /** |
||
914 | * Return true if one item return true to the callback. |
||
915 | * |
||
916 | * @param callable $callback |
||
917 | * |
||
918 | * @return bool |
||
919 | */ |
||
920 | 1 | public function exists(callable $callback) |
|
921 | { |
||
922 | 1 | $index = 0; |
|
923 | 1 | foreach ($this->items as $key => $item) { |
|
924 | 1 | if ($callback($item, $key, $index++) === true) { |
|
925 | 1 | return true; |
|
926 | } |
||
927 | 1 | } |
|
928 | |||
929 | 1 | return false; |
|
930 | } |
||
931 | |||
932 | /* -- --- -- */ |
||
933 | /* -- INTERFACE COMPLIANCE -- */ |
||
934 | /* -- --- -- */ |
||
935 | |||
936 | /* -- -- */ |
||
937 | /* -- JsonSerializable -- */ |
||
938 | /* -- -- */ |
||
939 | |||
940 | /** |
||
941 | * @return array |
||
942 | */ |
||
943 | 2 | public function jsonSerialize() |
|
944 | { |
||
945 | 2 | return $this->toArray(); |
|
946 | } |
||
947 | |||
948 | /* -- -- */ |
||
949 | /* -- Array Access Methods -- */ |
||
950 | /* -- -- */ |
||
951 | |||
952 | /** |
||
953 | * {@inheritDoc} |
||
954 | */ |
||
955 | 1 | public function offsetExists($offset) |
|
959 | |||
960 | /** |
||
961 | * {@inheritDoc} |
||
962 | */ |
||
963 | 2 | public function offsetGet($offset) |
|
971 | |||
972 | /** |
||
973 | * {@inheritDoc} |
||
974 | */ |
||
975 | 1 | public function offsetUnset($offset) |
|
979 | |||
980 | /** |
||
981 | * {@inheritDoc} |
||
982 | */ |
||
983 | 2 | public function offsetSet($offset, $value) |
|
991 | |||
992 | /* -- -- */ |
||
993 | /* -- Iterator Methods -- */ |
||
994 | /* -- -- */ |
||
995 | |||
996 | /** |
||
997 | * {@inheritDoc} |
||
998 | */ |
||
999 | 13 | public function current() |
|
1003 | |||
1004 | /** |
||
1005 | * {@inheritDoc} |
||
1006 | */ |
||
1007 | 6 | public function key() |
|
1011 | |||
1012 | /** |
||
1013 | * {@inheritDoc} |
||
1014 | */ |
||
1015 | 13 | public function next() |
|
1019 | |||
1020 | /** |
||
1021 | * {@inheritDoc} |
||
1022 | */ |
||
1023 | 123 | public function rewind() |
|
1027 | |||
1028 | /** |
||
1029 | * {@inheritDoc} |
||
1030 | */ |
||
1031 | 8 | public function valid() |
|
1035 | |||
1036 | /* -- -- */ |
||
1037 | /* -- Countable Method -- */ |
||
1038 | /* -- -- */ |
||
1039 | |||
1040 | /** |
||
1041 | * {@inheritDoc} |
||
1042 | */ |
||
1043 | 17 | public function count() |
|
1047 | |||
1048 | /* -- --- -- */ |
||
1049 | /* -- OTHERS -- */ |
||
1050 | /* -- --- -- */ |
||
1051 | |||
1052 | /** |
||
1053 | * @param string $message |
||
1054 | * @param mixed $arguments |
||
1055 | * |
||
1056 | * @throws InvalidArgumentException |
||
1057 | */ |
||
1058 | 19 | protected function doThrow($message, $arguments) |
|
1063 | |||
1064 | /** |
||
1065 | * @return $this |
||
1066 | */ |
||
1067 | 2 | public function dump() |
|
1068 | { |
||
1069 | 2 | return $this; |
|
1070 | } |
||
1071 | } |
||
1072 |