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 |
||
11 | class Collection implements ArrayAccess, Enumerable |
||
12 | { |
||
13 | use EnumeratesValues, Macroable; |
||
14 | |||
15 | /** |
||
16 | * The items contained in the collection. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $items = []; |
||
21 | |||
22 | /** |
||
23 | * Create a new collection. |
||
24 | * |
||
25 | * @param mixed $items |
||
26 | * @return void |
||
|
|||
27 | */ |
||
28 | public function __construct($items = []) |
||
32 | |||
33 | /** |
||
34 | * Create a new collection by invoking the callback a given amount of times. |
||
35 | * |
||
36 | * @param int $number |
||
37 | * @param callable $callback |
||
38 | * @return static |
||
39 | */ |
||
40 | public static function times($number, callable $callback = null) |
||
52 | |||
53 | /** |
||
54 | * Get all of the items in the collection. |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | public function all() |
||
62 | |||
63 | /** |
||
64 | * Get a lazy collection for the items in this collection. |
||
65 | * |
||
66 | * @return \IlluminateAgnostic\Arr\Support\LazyCollection |
||
67 | */ |
||
68 | public function lazy() |
||
72 | 238 | ||
73 | 238 | /** |
|
74 | * Get the average value of a given key. |
||
75 | * |
||
76 | * @param callable|string|null $callback |
||
77 | * @return mixed |
||
78 | */ |
||
79 | public function avg($callback = null) |
||
93 | |||
94 | 7 | /** |
|
95 | 2 | * Get the median of a given key. |
|
96 | 7 | * |
|
97 | * @param string|array|null $key |
||
98 | * @return mixed |
||
99 | */ |
||
100 | public function median($key = null) |
||
123 | 1 | ||
124 | 1 | /** |
|
125 | * Get the mode of a given key. |
||
126 | * |
||
127 | 1 | * @param string|array|null $key |
|
128 | * @return array|null |
||
129 | */ |
||
130 | public function mode($key = null) |
||
152 | |||
153 | 4 | /** |
|
154 | 4 | * Collapse the collection of items into a single array. |
|
155 | * |
||
156 | 4 | * @return static |
|
157 | 4 | */ |
|
158 | public function collapse() |
||
162 | |||
163 | /** |
||
164 | * Determine if an item exists in the collection. |
||
165 | * |
||
166 | * @param mixed $key |
||
167 | 3 | * @param mixed $operator |
|
168 | * @param mixed $value |
||
169 | 3 | * @return bool |
|
170 | */ |
||
171 | public function contains($key, $operator = null, $value = null) |
||
185 | 6 | ||
186 | /** |
||
187 | 6 | * Cross join with the given lists, returning all possible permutations. |
|
188 | 1 | * |
|
189 | * @param mixed ...$lists |
||
190 | * @return static |
||
191 | 5 | */ |
|
192 | public function crossJoin(...$lists) |
||
198 | 3 | ||
199 | 3 | /** |
|
200 | * Get the items in the collection that are not present in the given items. |
||
201 | * |
||
202 | * @param mixed $items |
||
203 | * @return static |
||
204 | */ |
||
205 | public function diff($items) |
||
209 | |||
210 | 4 | /** |
|
211 | 1 | * Get the items in the collection that are not present in the given items. |
|
212 | * |
||
213 | * @param mixed $items |
||
214 | 3 | * @param callable $callback |
|
215 | * @return static |
||
216 | 3 | */ |
|
217 | public function diffUsing($items, callable $callback) |
||
221 | |||
222 | 3 | /** |
|
223 | * Get the items in the collection whose keys and values are not present in the given items. |
||
224 | 3 | * |
|
225 | * @param mixed $items |
||
226 | * @return static |
||
227 | 3 | */ |
|
228 | 3 | public function diffAssoc($items) |
|
232 | |||
233 | /** |
||
234 | * Get the items in the collection whose keys and values are not present in the given items. |
||
235 | * |
||
236 | 3 | * @param mixed $items |
|
237 | * @param callable $callback |
||
238 | 3 | * @return static |
|
239 | */ |
||
240 | public function diffAssocUsing($items, callable $callback) |
||
244 | |||
245 | /** |
||
246 | * Get the items in the collection whose keys are not present in the given items. |
||
247 | * |
||
248 | * @param mixed $items |
||
249 | 1 | * @return static |
|
250 | */ |
||
251 | 1 | public function diffKeys($items) |
|
255 | |||
256 | /** |
||
257 | * Get the items in the collection whose keys are not present in the given items. |
||
258 | * |
||
259 | * @param mixed $items |
||
260 | * @param callable $callback |
||
261 | * @return static |
||
262 | 4 | */ |
|
263 | public function diffKeysUsing($items, callable $callback) |
||
267 | |||
268 | 4 | /** |
|
269 | * Retrieve duplicate items from the collection. |
||
270 | * |
||
271 | 2 | * @param callable|null $callback |
|
272 | * @param bool $strict |
||
273 | * @return static |
||
274 | 3 | */ |
|
275 | public function duplicates($callback = null, $strict = false) |
||
295 | |||
296 | 1 | /** |
|
297 | * Retrieve duplicate items from the collection using strict comparison. |
||
298 | * |
||
299 | * @param callable|null $callback |
||
300 | * @return static |
||
301 | */ |
||
302 | public function duplicatesStrict($callback = null) |
||
306 | |||
307 | 1 | /** |
|
308 | 1 | * Get the comparison function to detect duplicates. |
|
309 | * |
||
310 | * @param bool $strict |
||
311 | * @return \Closure |
||
312 | */ |
||
313 | protected function duplicateComparator($strict) |
||
325 | |||
326 | /** |
||
327 | * Get all items except for those with the specified keys. |
||
328 | * |
||
329 | * @param \IlluminateAgnostic\Arr\Support\Collection|mixed $keys |
||
330 | * @return static |
||
331 | */ |
||
332 | public function except($keys) |
||
342 | |||
343 | /** |
||
344 | * Run a filter over each of the items. |
||
345 | * |
||
346 | * @param callable|null $callback |
||
347 | 3 | * @return static |
|
348 | */ |
||
349 | 3 | public function filter(callable $callback = null) |
|
357 | |||
358 | /** |
||
359 | 2 | * Get the first item from the collection passing the given truth test. |
|
360 | * |
||
361 | 2 | * @param callable|null $callback |
|
362 | * @param mixed $default |
||
363 | * @return mixed |
||
364 | */ |
||
365 | public function first(callable $callback = null, $default = null) |
||
369 | |||
370 | 2 | /** |
|
371 | * Get a flattened array of the items in the collection. |
||
372 | 2 | * |
|
373 | * @param int $depth |
||
374 | * @return static |
||
375 | */ |
||
376 | public function flatten($depth = INF) |
||
380 | |||
381 | /** |
||
382 | 1 | * Flip the items in the collection. |
|
383 | * |
||
384 | 1 | * @return static |
|
385 | */ |
||
386 | public function flip() |
||
390 | |||
391 | /** |
||
392 | * Remove an item from the collection by key. |
||
393 | 2 | * |
|
394 | * @param string|array $keys |
||
395 | 2 | * @return $this |
|
396 | */ |
||
397 | public function forget($keys) |
||
405 | 1 | ||
406 | /** |
||
407 | 1 | * Get an item from the collection by key. |
|
408 | * |
||
409 | * @param mixed $key |
||
410 | * @param mixed $default |
||
411 | * @return mixed |
||
412 | */ |
||
413 | public function get($key, $default = null) |
||
421 | |||
422 | /** |
||
423 | * Group an associative array by a field or using a callback. |
||
424 | 7 | * |
|
425 | * @param array|callable|string $groupBy |
||
426 | * @param bool $preserveKeys |
||
427 | * @return static |
||
428 | */ |
||
429 | public function groupBy($groupBy, $preserveKeys = false) |
||
467 | |||
468 | /** |
||
469 | * Key an associative array by a field or using a callback. |
||
470 | * |
||
471 | * @param callable|string $keyBy |
||
472 | * @return static |
||
473 | 2 | */ |
|
474 | public function keyBy($keyBy) |
||
492 | 32 | ||
493 | 32 | /** |
|
494 | * Determine if an item exists in the collection by key. |
||
495 | * |
||
496 | 1 | * @param mixed $key |
|
497 | * @return bool |
||
498 | */ |
||
499 | public function has($key) |
||
511 | 10 | ||
512 | 4 | /** |
|
513 | * Concatenate values of a given key as a string. |
||
514 | * |
||
515 | 6 | * @param string $value |
|
516 | * @param string $glue |
||
517 | * @return string |
||
518 | */ |
||
519 | public function implode($value, $glue = null) |
||
529 | |||
530 | /** |
||
531 | * Intersect the collection with the given items. |
||
532 | * |
||
533 | * @param mixed $items |
||
534 | * @return static |
||
535 | */ |
||
536 | public function intersect($items) |
||
540 | |||
541 | /** |
||
542 | * Intersect the collection with the given items by key. |
||
543 | * |
||
544 | * @param mixed $items |
||
545 | * @return static |
||
546 | */ |
||
547 | public function intersectByKeys($items) |
||
553 | |||
554 | /** |
||
555 | * Determine if the collection is empty or not. |
||
556 | * |
||
557 | * @return bool |
||
558 | */ |
||
559 | public function isEmpty() |
||
563 | |||
564 | 2 | /** |
|
565 | * Join all items from the collection using a string. The final items can use a separate glue string. |
||
566 | * |
||
567 | * @param string $glue |
||
568 | * @param string $finalGlue |
||
569 | * @return string |
||
570 | */ |
||
571 | public function join($glue, $finalGlue = '') |
||
593 | |||
594 | /** |
||
595 | * Get the keys of the collection items. |
||
596 | * |
||
597 | * @return static |
||
598 | */ |
||
599 | public function keys() |
||
603 | 1 | ||
604 | /** |
||
605 | 1 | * Get the last item from the collection. |
|
606 | * |
||
607 | * @param callable|null $callback |
||
608 | 9 | * @param mixed $default |
|
609 | 6 | * @return mixed |
|
610 | */ |
||
611 | 6 | public function last(callable $callback = null, $default = null) |
|
615 | 9 | ||
616 | /** |
||
617 | * Get the values of a given key. |
||
618 | 9 | * |
|
619 | 9 | * @param string|array $value |
|
620 | * @param string|null $key |
||
621 | 9 | * @return static |
|
622 | 1 | */ |
|
623 | public function pluck($value, $key = null) |
||
627 | 9 | ||
628 | 9 | /** |
|
629 | 6 | * Run a map over each of the items. |
|
630 | 6 | * |
|
631 | 6 | * @param callable $callback |
|
632 | 6 | * @return static |
|
633 | 6 | */ |
|
634 | 6 | public function map(callable $callback) |
|
642 | |||
643 | /** |
||
644 | * Run a dictionary map over the items. |
||
645 | * |
||
646 | * The callback should return an associative array with a single key/value pair. |
||
647 | * |
||
648 | 1 | * @param callable $callback |
|
649 | * @return static |
||
650 | 1 | */ |
|
651 | public function mapToDictionary(callable $callback) |
||
671 | |||
672 | /** |
||
673 | * Run an associative map over each of the items. |
||
674 | * |
||
675 | * The callback should return an associative array with a single key/value pair. |
||
676 | * |
||
677 | 1 | * @param callable $callback |
|
678 | * @return static |
||
679 | 1 | */ |
|
680 | public function mapWithKeys(callable $callback) |
||
694 | |||
695 | /** |
||
696 | * Merge the collection with the given items. |
||
697 | * |
||
698 | * @param mixed $items |
||
699 | * @return static |
||
700 | */ |
||
701 | 1 | public function merge($items) |
|
705 | 1 | ||
706 | /** |
||
707 | * Recursively merge the collection with the given items. |
||
708 | * |
||
709 | * @param mixed $items |
||
710 | * @return static |
||
711 | */ |
||
712 | public function mergeRecursive($items) |
||
716 | 2 | ||
717 | /** |
||
718 | 2 | * Create a collection by using this collection for keys and another for its values. |
|
719 | * |
||
720 | * @param mixed $values |
||
721 | 2 | * @return static |
|
722 | 2 | */ |
|
723 | public function combine($values) |
||
727 | |||
728 | /** |
||
729 | * Union the collection with the given items. |
||
730 | * |
||
731 | * @param mixed $items |
||
732 | 1 | * @return static |
|
733 | */ |
||
734 | 1 | public function union($items) |
|
738 | |||
739 | /** |
||
740 | * Create a new collection consisting of every n-th element. |
||
741 | * |
||
742 | * @param int $step |
||
743 | 1 | * @param int $offset |
|
744 | * @return static |
||
745 | */ |
||
746 | 1 | public function nth($step, $offset = 0) |
|
762 | |||
763 | /** |
||
764 | * Get the items with the specified keys. |
||
765 | * |
||
766 | * @param mixed $keys |
||
767 | * @return static |
||
768 | */ |
||
769 | public function only($keys) |
||
783 | 3 | ||
784 | /** |
||
785 | * Get and remove the last item from the collection. |
||
786 | * |
||
787 | * @return mixed |
||
788 | */ |
||
789 | public function pop() |
||
793 | 1 | ||
794 | /** |
||
795 | * Push an item onto the beginning of the collection. |
||
796 | * |
||
797 | * @param mixed $value |
||
798 | * @param mixed $key |
||
799 | * @return $this |
||
800 | */ |
||
801 | public function prepend($value, $key = null) |
||
807 | |||
808 | 2 | /** |
|
809 | * Push an item onto the end of the collection. |
||
810 | * |
||
811 | * @param mixed $value |
||
812 | * @return $this |
||
813 | */ |
||
814 | public function push($value) |
||
820 | 6 | ||
821 | 5 | /** |
|
822 | * Push all of the given items onto the collection. |
||
823 | * |
||
824 | 1 | * @param iterable $source |
|
825 | * @return static |
||
826 | */ |
||
827 | public function concat($source) |
||
837 | 1 | ||
838 | /** |
||
839 | 1 | * Get and remove an item from the collection. |
|
840 | * |
||
841 | * @param mixed $key |
||
842 | 10 | * @param mixed $default |
|
843 | * @return mixed |
||
844 | 10 | */ |
|
845 | public function pull($key, $default = null) |
||
849 | 10 | ||
850 | 8 | /** |
|
851 | * Put an item in the collection by key. |
||
852 | * |
||
853 | 10 | * @param mixed $key |
|
854 | 10 | * @param mixed $value |
|
855 | * @return $this |
||
856 | 10 | */ |
|
857 | 10 | public function put($key, $value) |
|
863 | |||
864 | 10 | /** |
|
865 | * Get one or a specified number of items randomly from the collection. |
||
866 | 10 | * |
|
867 | 1 | * @param int|null $number |
|
868 | * @return static|mixed |
||
869 | * |
||
870 | 10 | * @throws \InvalidArgumentException |
|
871 | */ |
||
872 | public function random($number = null) |
||
880 | |||
881 | 3 | /** |
|
882 | * Reduce the collection to a single value. |
||
883 | 3 | * |
|
884 | * @param callable $callback |
||
885 | 3 | * @param mixed $initial |
|
886 | 3 | * @return mixed |
|
887 | */ |
||
888 | 3 | public function reduce(callable $callback, $initial = null) |
|
892 | 3 | ||
893 | /** |
||
894 | * Replace the collection items with the given items. |
||
895 | 3 | * |
|
896 | * @param mixed $items |
||
897 | * @return static |
||
898 | */ |
||
899 | public function replace($items) |
||
903 | |||
904 | 2 | /** |
|
905 | * Recursively replace the collection items with the given items. |
||
906 | 2 | * |
|
907 | * @param mixed $items |
||
908 | 2 | * @return static |
|
909 | 2 | */ |
|
910 | 2 | public function replaceRecursive($items) |
|
914 | 2 | ||
915 | /** |
||
916 | * Reverse items order. |
||
917 | * |
||
918 | * @return static |
||
919 | */ |
||
920 | public function reverse() |
||
924 | 2 | ||
925 | /** |
||
926 | 2 | * Search the collection for a given value and return the corresponding key if successful. |
|
927 | * |
||
928 | 2 | * @param mixed $value |
|
929 | 1 | * @param bool $strict |
|
930 | * @return mixed |
||
931 | */ |
||
932 | 2 | public function search($value, $strict = false) |
|
946 | |||
947 | /** |
||
948 | * Get and remove the first item from the collection. |
||
949 | * |
||
950 | * @return mixed |
||
951 | */ |
||
952 | 2 | public function shift() |
|
956 | |||
957 | /** |
||
958 | * Shuffle the items in the collection. |
||
959 | * |
||
960 | * @param int $seed |
||
961 | * @return static |
||
962 | */ |
||
963 | public function shuffle($seed = null) |
||
967 | |||
968 | /** |
||
969 | * Skip the first {$count} items. |
||
970 | * |
||
971 | * @param int $count |
||
972 | * @return static |
||
973 | */ |
||
974 | 5 | public function skip($count) |
|
978 | |||
979 | /** |
||
980 | * Slice the underlying collection array. |
||
981 | * |
||
982 | * @param int $offset |
||
983 | * @param int $length |
||
984 | * @return static |
||
985 | 49 | */ |
|
986 | public function slice($offset, $length = null) |
||
990 | |||
991 | /** |
||
992 | * Split a collection into a certain number of groups. |
||
993 | * |
||
994 | * @param int $numberOfGroups |
||
995 | * @return static |
||
996 | */ |
||
997 | 1 | public function split($numberOfGroups) |
|
1027 | 6 | ||
1028 | /** |
||
1029 | * Chunk the collection into chunks of the given size. |
||
1030 | * |
||
1031 | * @param int $size |
||
1032 | * @return static |
||
1033 | */ |
||
1034 | public function chunk($size) |
||
1048 | |||
1049 | 10 | /** |
|
1050 | * Sort through each item with a callback. |
||
1051 | 10 | * |
|
1052 | * @param callable|null $callback |
||
1053 | * @return static |
||
1054 | */ |
||
1055 | public function sort(callable $callback = null) |
||
1065 | |||
1066 | 26 | /** |
|
1067 | * Sort the collection using the given callback. |
||
1068 | * |
||
1069 | * @param callable|string $callback |
||
1070 | * @param int $options |
||
1071 | * @param bool $descending |
||
1072 | * @return static |
||
1073 | */ |
||
1074 | public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
||
1099 | 4 | ||
1100 | /** |
||
1101 | 4 | * Sort the collection in descending order using the given callback. |
|
1102 | * |
||
1103 | 4 | * @param callable|string $callback |
|
1104 | 4 | * @param int $options |
|
1105 | * @return static |
||
1106 | */ |
||
1107 | 4 | public function sortByDesc($callback, $options = SORT_REGULAR) |
|
1111 | |||
1112 | /** |
||
1113 | * Sort the collection keys. |
||
1114 | * |
||
1115 | * @param int $options |
||
1116 | * @param bool $descending |
||
1117 | * @return static |
||
1118 | */ |
||
1119 | public function sortKeys($options = SORT_REGULAR, $descending = false) |
||
1127 | |||
1128 | /** |
||
1129 | * Sort the collection keys in descending order. |
||
1130 | * |
||
1131 | * @param int $options |
||
1132 | * @return static |
||
1133 | */ |
||
1134 | public function sortKeysDesc($options = SORT_REGULAR) |
||
1138 | 5 | ||
1139 | /** |
||
1140 | 5 | * Splice a portion of the underlying collection array. |
|
1141 | 5 | * |
|
1142 | * @param int $offset |
||
1143 | 5 | * @param int|null $length |
|
1144 | 5 | * @param mixed $replacement |
|
1145 | * @return static |
||
1146 | */ |
||
1147 | public function splice($offset, $length = null, $replacement = []) |
||
1155 | |||
1156 | /** |
||
1157 | 1 | * Take the first or last {$limit} items. |
|
1158 | * |
||
1159 | 1 | * @param int $limit |
|
1160 | * @return static |
||
1161 | */ |
||
1162 | public function take($limit) |
||
1170 | |||
1171 | 1 | /** |
|
1172 | 1 | * Transform each item in the collection using a callback. |
|
1173 | * |
||
1174 | * @param callable $callback |
||
1175 | * @return $this |
||
1176 | */ |
||
1177 | public function transform(callable $callback) |
||
1183 | 1 | ||
1184 | /** |
||
1185 | * Reset the keys on the underlying array. |
||
1186 | 1 | * |
|
1187 | * @return static |
||
1188 | 1 | */ |
|
1189 | public function values() |
||
1193 | |||
1194 | /** |
||
1195 | * Zip the collection together with one or more arrays. |
||
1196 | * |
||
1197 | * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); |
||
1198 | * => [[1, 4], [2, 5], [3, 6]] |
||
1199 | * |
||
1200 | 3 | * @param mixed ...$items |
|
1201 | * @return static |
||
1202 | 3 | */ |
|
1203 | public function zip($items) |
||
1215 | |||
1216 | /** |
||
1217 | * Pad collection to the specified length with a value. |
||
1218 | * |
||
1219 | * @param int $size |
||
1220 | * @param mixed $value |
||
1221 | * @return static |
||
1222 | 3 | */ |
|
1223 | public function pad($size, $value) |
||
1227 | |||
1228 | /** |
||
1229 | * Get an iterator for the items. |
||
1230 | * |
||
1231 | * @return \ArrayIterator |
||
1232 | */ |
||
1233 | 1 | public function getIterator() |
|
1237 | |||
1238 | 1 | /** |
|
1239 | * Count the number of items in the collection. |
||
1240 | 1 | * |
|
1241 | * @return int |
||
1242 | 1 | */ |
|
1243 | 1 | public function count() |
|
1247 | |||
1248 | /** |
||
1249 | * Add an item to the collection. |
||
1250 | * |
||
1251 | * @param mixed $item |
||
1252 | * @return $this |
||
1253 | 1 | */ |
|
1254 | public function add($item) |
||
1260 | 1 | ||
1261 | 1 | /** |
|
1262 | * Get a base Support collection instance from this collection. |
||
1263 | * |
||
1264 | 1 | * @return \IlluminateAgnostic\Arr\Support\Collection |
|
1265 | */ |
||
1266 | public function toBase() |
||
1270 | |||
1271 | /** |
||
1272 | * Determine if an item exists at an offset. |
||
1273 | * |
||
1274 | * @param mixed $key |
||
1275 | * @return bool |
||
1276 | 1 | */ |
|
1277 | public function offsetExists($key) |
||
1281 | |||
1282 | 1 | /** |
|
1283 | 1 | * Get an item at a given offset. |
|
1284 | * |
||
1285 | * @param mixed $key |
||
1286 | 1 | * @return mixed |
|
1287 | */ |
||
1288 | 1 | public function offsetGet($key) |
|
1292 | |||
1293 | /** |
||
1294 | * Set the item at a given offset. |
||
1295 | * |
||
1296 | * @param mixed $key |
||
1297 | * @param mixed $value |
||
1298 | 1 | * @return void |
|
1299 | */ |
||
1300 | 1 | public function offsetSet($key, $value) |
|
1308 | |||
1309 | /** |
||
1310 | * Unset the item at a given offset. |
||
1311 | * |
||
1312 | * @param string $key |
||
1313 | 7 | * @return void |
|
1314 | */ |
||
1315 | 7 | public function offsetUnset($key) |
|
1319 | } |
||
1320 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.