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 |
||
35 | class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable |
||
36 | { |
||
37 | /** @var array The items for this collection */ |
||
38 | protected $items; |
||
39 | |||
40 | /** |
||
41 | * Collection constructor. |
||
42 | * |
||
43 | * @param array $items |
||
44 | */ |
||
45 | 100 | public function __construct(array $items = []) |
|
50 | |||
51 | /** |
||
52 | * Generate a collection from an array of items. |
||
53 | * I created this method so that it's possible to extend a collection more easily. |
||
54 | * |
||
55 | * @param mixed $items |
||
56 | * |
||
57 | * @return Collection |
||
58 | */ |
||
59 | 37 | public static function factory($items = null) |
|
66 | |||
67 | /** |
||
68 | * Get collection as an array |
||
69 | * |
||
70 | * @return array |
||
71 | */ |
||
72 | 50 | public function toArray() |
|
76 | |||
77 | /** |
||
78 | * Determine if collection has a given key |
||
79 | * |
||
80 | * @param mixed $key The key to look for |
||
81 | * |
||
82 | * @return bool |
||
83 | */ |
||
84 | 50 | public function has($key) |
|
88 | |||
89 | /** |
||
90 | * Does collection have item at position? |
||
91 | * |
||
92 | * Determine if collection has an item at a particular position (indexed from one). |
||
93 | * Position can be positive and start from the beginning or it can be negative and |
||
94 | * start from the end. |
||
95 | * |
||
96 | * @param int $position |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | 2 | public function hasValueAt($position) |
|
109 | |||
110 | /** |
||
111 | * Get key at given position |
||
112 | * |
||
113 | * Returns the key at the given position, starting from one. Position can be positive (start from beginning) or |
||
114 | * negative (start from the end). |
||
115 | * |
||
116 | * If the position does not exist, a RuntimeException is thrown. |
||
117 | * |
||
118 | * @param int $position |
||
119 | * |
||
120 | * @return string |
||
121 | * |
||
122 | * @throws RuntimeException |
||
123 | */ |
||
124 | 6 | public function getKeyAt($position) |
|
138 | |||
139 | /** |
||
140 | * Get value at given position |
||
141 | * |
||
142 | * Returns the value at the given position, starting from one. Position can be positive (start from beginning) or |
||
143 | * negative (start from the end). |
||
144 | * |
||
145 | * If the position does not exist, a RuntimeException is thrown. |
||
146 | * |
||
147 | * @param int $position |
||
148 | * |
||
149 | * @return mixed |
||
150 | * |
||
151 | * @throws RuntimeException |
||
152 | */ |
||
153 | 2 | public function getValueAt($position) |
|
157 | |||
158 | /** |
||
159 | * Get the key of the first item found matching $item |
||
160 | * |
||
161 | * @param mixed|callable $item |
||
162 | * |
||
163 | * @return mixed|null |
||
164 | */ |
||
165 | 3 | public function keyOf($item) |
|
180 | |||
181 | /** |
||
182 | * Get the offset (index) of the first item found that matches $item |
||
183 | * |
||
184 | * @param mixed|callable $item |
||
185 | * |
||
186 | * @return int|null |
||
187 | */ |
||
188 | 3 | public function indexOf($item) |
|
206 | |||
207 | /** |
||
208 | * Get item by key, with an optional default return value |
||
209 | * |
||
210 | * @param mixed $key |
||
211 | * @param mixed $default |
||
212 | * |
||
213 | * @return mixed |
||
214 | */ |
||
215 | 8 | public function get($key, $default = null) |
|
223 | |||
224 | /** |
||
225 | * Add an item with no regard to key |
||
226 | * |
||
227 | * @param mixed $value |
||
228 | * |
||
229 | * @return $this |
||
230 | */ |
||
231 | 8 | public function add($value) |
|
237 | |||
238 | /** |
||
239 | * Set an item at a given key |
||
240 | * |
||
241 | * @param mixed $key |
||
242 | * @param mixed $value |
||
243 | * @param bool $overwrite If false, do not overwrite existing key |
||
244 | * |
||
245 | * @return $this |
||
246 | */ |
||
247 | 14 | public function set($key, $value, $overwrite = true) |
|
255 | |||
256 | /** |
||
257 | * Delete an item by key |
||
258 | * |
||
259 | * @param mixed $key |
||
260 | * |
||
261 | * @return $this |
||
262 | */ |
||
263 | 3 | public function delete($key) |
|
269 | |||
270 | /** |
||
271 | * Clear the collection of all its items. |
||
272 | * |
||
273 | * @return $this |
||
274 | */ |
||
275 | 2 | public function clear() |
|
281 | |||
282 | /** |
||
283 | * Determine if collection contains given value |
||
284 | * |
||
285 | * @param mixed|callable $val |
||
286 | * @param mixed $key |
||
287 | * |
||
288 | * @return bool |
||
289 | */ |
||
290 | 4 | public function contains($val, $key = null) |
|
308 | |||
309 | /** |
||
310 | * Fetch item from collection by key and remove it from collection |
||
311 | * |
||
312 | * @param mixed $key |
||
313 | * |
||
314 | * @return mixed |
||
315 | */ |
||
316 | 1 | public function pull($key) |
|
324 | |||
325 | /** |
||
326 | * Join collection items using a delimiter |
||
327 | * |
||
328 | * @param string $delim |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | 1 | public function join($delim = '') |
|
336 | |||
337 | /** |
||
338 | * Determine if collection has any items |
||
339 | * |
||
340 | * @return bool |
||
341 | */ |
||
342 | 5 | public function isEmpty() |
|
346 | |||
347 | /** |
||
348 | * Get a collection of only this collection's values (without its keys) |
||
349 | * |
||
350 | * @return Collection |
||
351 | */ |
||
352 | 1 | public function values() |
|
356 | |||
357 | /** |
||
358 | * Get a collection of only this collection's keys |
||
359 | * |
||
360 | * @return Collection |
||
361 | */ |
||
362 | 1 | public function keys() |
|
366 | |||
367 | /** |
||
368 | * Get a collection with order reversed |
||
369 | * |
||
370 | * @return Collection |
||
371 | */ |
||
372 | 6 | public function reverse() |
|
376 | |||
377 | /** |
||
378 | * Get a collection with keys and values flipped |
||
379 | * |
||
380 | * @return Collection |
||
381 | */ |
||
382 | 1 | public function flip() |
|
390 | |||
391 | /** |
||
392 | * Shuffle the order of this collection's values |
||
393 | * |
||
394 | * @return Collection |
||
395 | */ |
||
396 | 1 | public function shuffle() |
|
401 | |||
402 | /** |
||
403 | * Get a random value from the collection |
||
404 | * |
||
405 | * @return mixed |
||
406 | */ |
||
407 | 1 | public function random() |
|
411 | |||
412 | /** |
||
413 | * Sort the collection (using values) |
||
414 | * |
||
415 | * @param callable $alg |
||
416 | * |
||
417 | * @return $this |
||
418 | */ |
||
419 | 2 | public function sort(callable $alg = null) |
|
429 | |||
430 | /** |
||
431 | * Sort the collection (using keys) |
||
432 | * |
||
433 | * @param callable $alg |
||
434 | * |
||
435 | * @return $this |
||
436 | */ |
||
437 | 2 | public function ksort(callable $alg = null) |
|
447 | |||
448 | /** |
||
449 | * Append items to collection without regard to keys |
||
450 | * |
||
451 | * @param array|Traversable $items |
||
452 | * |
||
453 | * @return $this |
||
454 | */ |
||
455 | 2 | public function append($items) |
|
467 | |||
468 | /** |
||
469 | * Return first item or first item where callback returns true |
||
470 | * |
||
471 | * @param callable|null $callback |
||
472 | * |
||
473 | * @return mixed|null |
||
474 | */ |
||
475 | 8 | public function first(callable $callback = null) |
|
486 | |||
487 | /** |
||
488 | * Return last item or last item where callback returns true |
||
489 | * |
||
490 | * @param callable|null $callback |
||
491 | * |
||
492 | * @return mixed|null |
||
493 | */ |
||
494 | 3 | public function last(callable $callback = null) |
|
498 | |||
499 | /** |
||
500 | * Map collection |
||
501 | * |
||
502 | * Create a new collection using the results of a callback function on each item in this collection. |
||
503 | * |
||
504 | * @param callable $callback |
||
505 | * |
||
506 | * @return Collection |
||
507 | */ |
||
508 | 3 | public function map(callable $callback) |
|
519 | |||
520 | /** |
||
521 | * Combine collection with another traversable/collection |
||
522 | * |
||
523 | * Using this collection's keys, and the incoming collection's values, a new collection is created and returned. |
||
524 | * |
||
525 | * @param array|Traversable $items |
||
526 | * |
||
527 | * @return Collection |
||
528 | */ |
||
529 | 5 | public function combine($items) |
|
542 | |||
543 | /** |
||
544 | * Get a new collection with only distinct values |
||
545 | * |
||
546 | * @return Collection |
||
547 | */ |
||
548 | 1 | public function distinct() |
|
559 | |||
560 | /** |
||
561 | * Remove all duplicate values from collection in-place |
||
562 | * |
||
563 | * @return Collection |
||
564 | */ |
||
565 | 1 | public function deduplicate() |
|
571 | |||
572 | /** |
||
573 | * Return a new collection with only filtered keys/values |
||
574 | * |
||
575 | * The callback accepts value, key, index and should return true if the item should be added to the returned |
||
576 | * collection |
||
577 | * |
||
578 | * @param callable $callback |
||
579 | * |
||
580 | * @return Collection |
||
581 | */ |
||
582 | 2 | public function filter(callable $callback = null) |
|
600 | |||
601 | /** |
||
602 | * Fold collection into a single value |
||
603 | * |
||
604 | * Loop through collection calling a callback function and passing the result to the next iteration, eventually |
||
605 | * returning a single value. |
||
606 | * |
||
607 | * @param callable $callback |
||
608 | * @param mixed $initial |
||
609 | * |
||
610 | * @return null |
||
611 | */ |
||
612 | 1 | public function fold(callable $callback, $initial = null) |
|
622 | |||
623 | /** |
||
624 | * Return a merge of this collection and $items |
||
625 | * |
||
626 | * @param array|Traversable $items |
||
627 | * |
||
628 | * @return Collection |
||
629 | */ |
||
630 | 3 | public function merge($items) |
|
643 | |||
644 | /** |
||
645 | * Create a new collection with a union of this collection and $items |
||
646 | * |
||
647 | * This method is similar to merge, except that existing items will not be overwritten. |
||
648 | * |
||
649 | * @param $items |
||
650 | */ |
||
651 | 2 | public function union($items) |
|
664 | |||
665 | /** |
||
666 | * Call callback for each item in collection, passively |
||
667 | * If at any point the callback returns false, iteration stops. |
||
668 | * |
||
669 | * @param callable $callback |
||
670 | * |
||
671 | * @return $this |
||
672 | */ |
||
673 | 2 | public function each(callable $callback) |
|
684 | |||
685 | /** |
||
686 | * Assert callback returns $expected value for each item in collection. |
||
687 | * |
||
688 | * @param callable $callback |
||
689 | * @param bool $expected |
||
690 | * |
||
691 | * @return bool |
||
692 | */ |
||
693 | 2 | public function assert(callable $callback, $expected = true) |
|
704 | |||
705 | /** |
||
706 | * Pipe collection through a callback |
||
707 | * |
||
708 | * @param callable $callback |
||
709 | * |
||
710 | * @return mixed |
||
711 | */ |
||
712 | 1 | public function pipe(callable $callback) |
|
716 | |||
717 | /** |
||
718 | * Get new collection in chunks of $size |
||
719 | * |
||
720 | * Creates a new collection of arrays of $size length. The remainder items will be placed at the end. |
||
721 | * |
||
722 | * @param int $size |
||
723 | * |
||
724 | * @return Collection |
||
725 | */ |
||
726 | 2 | public function chunk($size) |
|
730 | |||
731 | /** |
||
732 | * Get a new collection of $count chunks |
||
733 | * |
||
734 | * @param int $count |
||
735 | * |
||
736 | * @return Collection |
||
737 | */ |
||
738 | 1 | public function split($count = 1) |
|
742 | |||
743 | /** |
||
744 | * Get a slice of this collection. |
||
745 | * |
||
746 | * @param int $offset |
||
747 | * @param int|null $length |
||
748 | * |
||
749 | * @return Collection |
||
750 | */ |
||
751 | 1 | public function slice($offset, $length = null) |
|
755 | |||
756 | /** |
||
757 | * Get collection with only differing items |
||
758 | * |
||
759 | * @param array|Traversable $items |
||
760 | * |
||
761 | * @return Collection |
||
762 | */ |
||
763 | 1 | public function diff($items) |
|
767 | |||
768 | /** |
||
769 | * Get collection with only differing items (by key) |
||
770 | * |
||
771 | * @param array|Traversable $items |
||
772 | * |
||
773 | * @return Collection |
||
774 | */ |
||
775 | 2 | public function kdiff($items) |
|
779 | |||
780 | /** |
||
781 | * Get collection with only intersecting items |
||
782 | * |
||
783 | * @param array|Traversable $items |
||
784 | * |
||
785 | * @return Collection |
||
786 | */ |
||
787 | 1 | public function intersect($items) |
|
791 | |||
792 | /** |
||
793 | * Get collection with only intersecting items (by key) |
||
794 | * |
||
795 | * @param array|Traversable $items |
||
796 | * |
||
797 | * @return Collection |
||
798 | */ |
||
799 | 1 | public function kintersect($items) |
|
803 | |||
804 | /** |
||
805 | * Remove last item in collection and return it |
||
806 | * |
||
807 | * @return mixed |
||
808 | */ |
||
809 | 1 | public function pop() |
|
813 | |||
814 | /** |
||
815 | * Remove first item in collection and return it (and re-index if numerically indexed) |
||
816 | * |
||
817 | * @return mixed |
||
818 | */ |
||
819 | 2 | public function shift() |
|
823 | |||
824 | /** |
||
825 | * Add item to the end of the collection |
||
826 | * |
||
827 | * @note This method is no different than add() but I included it for consistency's sake since I have the others |
||
828 | * |
||
829 | * @param mixed $item |
||
830 | * |
||
831 | * @return $this |
||
832 | */ |
||
833 | 1 | public function push($item) |
|
837 | |||
838 | /** |
||
839 | * Add item to the beginning of the collection (and re-index if a numerically indexed collection) |
||
840 | * |
||
841 | * @param mixed $item |
||
842 | * |
||
843 | * @return $this |
||
844 | */ |
||
845 | 5 | public function unshift($item) |
|
851 | |||
852 | /** |
||
853 | * Get new collection padded to specified $size with $value |
||
854 | * |
||
855 | * Using $value, pad the collection to specified $size. If $size is smaller or equal to the size of the collection, |
||
856 | * then no padding takes place. If $size is positive, padding is added to the end, while if negative, padding will |
||
857 | * be added to the beginning. |
||
858 | * |
||
859 | * @param int $size |
||
860 | * @param mixed $value |
||
861 | * |
||
862 | * @return Collection |
||
863 | */ |
||
864 | 3 | public function pad($size, $value = null) |
|
877 | |||
878 | /** |
||
879 | * Partition collection into two collections using a callback |
||
880 | * |
||
881 | * Iterates over each element in the collection with a callback. Items where callback returns true are placed in one |
||
882 | * collection and the rest in another. Finally, the two collections are placed in an array and returned for easy use |
||
883 | * with the list() function. ( list($a, $b) = $col->partition(function($val, $key, $index) {}) ) |
||
884 | * |
||
885 | * @param callable $callback |
||
886 | * |
||
887 | * @return array<Collection> |
||
888 | */ |
||
889 | 2 | public function partition(callable $callback) |
|
905 | |||
906 | /** |
||
907 | * Get column values by key |
||
908 | * |
||
909 | * This method expects the collection's data to be tabular in nature (two-dimensional and for the rows to have |
||
910 | * consistently named keys). If the data is not structured this way, it will do the best it can but it is not meant |
||
911 | * for unstructured, non-tabular data so don't expect consistent results. |
||
912 | * |
||
913 | * @param string|int $column The key of the column you want to get |
||
914 | * |
||
915 | * @return Collection |
||
916 | */ |
||
917 | 3 | public function getColumn($column) |
|
921 | |||
922 | /** |
||
923 | * Is collection tabular? |
||
924 | * |
||
925 | * Returns true if the data in the collection is tabular in nature, meaning it is at least two-dimensional and each |
||
926 | * row contains the same number of values with the same keys. |
||
927 | * |
||
928 | * @return bool |
||
929 | */ |
||
930 | 1 | public function isTabular() |
|
942 | |||
943 | /** ++++ ++++ **/ |
||
944 | /** ++ Interface Compliance ++ **/ |
||
945 | /** ++++ ++++ **/ |
||
946 | |||
947 | /** |
||
948 | * JSON serialize |
||
949 | * |
||
950 | * @return array |
||
951 | */ |
||
952 | 1 | public function jsonSerialize() |
|
956 | |||
957 | /** ++++ ++++ **/ |
||
958 | /** ++ Array Access Methods ++ **/ |
||
959 | /** ++++ ++++ **/ |
||
960 | |||
961 | /** |
||
962 | * Does offset exist? |
||
963 | * |
||
964 | * @param mixed $offset |
||
965 | * |
||
966 | * @return bool |
||
967 | */ |
||
968 | 1 | public function offsetExists($offset) |
|
972 | |||
973 | /** |
||
974 | * Get item at offset |
||
975 | * |
||
976 | * @param mixed $offset |
||
977 | * |
||
978 | * @return mixed |
||
979 | */ |
||
980 | 2 | public function offsetGet($offset) |
|
988 | |||
989 | /** |
||
990 | * Unset item at offset |
||
991 | * |
||
992 | * @param mixed $offset |
||
993 | * |
||
994 | * @return void |
||
995 | */ |
||
996 | 1 | public function offsetUnset($offset) |
|
1000 | |||
1001 | /** |
||
1002 | * Set item at offset |
||
1003 | * |
||
1004 | * @param mixed $offset |
||
1005 | * @param mixed $value |
||
1006 | * |
||
1007 | * @return $this |
||
1008 | */ |
||
1009 | 1 | public function offsetSet($offset, $value) |
|
1017 | |||
1018 | /** ++++ ++++ **/ |
||
1019 | /** ++ Iterator Methods ++ **/ |
||
1020 | /** ++++ ++++ **/ |
||
1021 | |||
1022 | /** |
||
1023 | * {@inheritDoc} |
||
1024 | */ |
||
1025 | 38 | public function current() |
|
1029 | |||
1030 | /** |
||
1031 | * {@inheritDoc} |
||
1032 | */ |
||
1033 | 38 | public function key() |
|
1037 | |||
1038 | /** |
||
1039 | * {@inheritDoc} |
||
1040 | */ |
||
1041 | 37 | public function next() |
|
1045 | |||
1046 | /** |
||
1047 | * {@inheritDoc} |
||
1048 | */ |
||
1049 | 100 | public function rewind() |
|
1053 | |||
1054 | /** |
||
1055 | * {@inheritDoc} |
||
1056 | */ |
||
1057 | 39 | public function valid() |
|
1061 | |||
1062 | /** ++++ ++++ **/ |
||
1063 | /** ++ Countable Method ++ **/ |
||
1064 | /** ++++ ++++ **/ |
||
1065 | |||
1066 | /** |
||
1067 | * {@inheritDoc} |
||
1068 | */ |
||
1069 | 11 | public function count() |
|
1073 | } |