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 | 352 | 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|null $callback |
||
38 | * @return static |
||
39 | */ |
||
40 | 1 | 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 | 224 | public function all() |
|
62 | |||
63 | /** |
||
64 | * Get a lazy collection for the items in this collection. |
||
65 | * |
||
66 | * @return \IlluminateAgnostic\Str\Support\LazyCollection |
||
67 | */ |
||
68 | 1 | public function lazy() |
|
72 | |||
73 | /** |
||
74 | * Get the average value of a given key. |
||
75 | * |
||
76 | * @param callable|string|null $callback |
||
77 | * @return mixed |
||
78 | */ |
||
79 | 8 | public function avg($callback = null) |
|
93 | |||
94 | /** |
||
95 | * Get the median of a given key. |
||
96 | * |
||
97 | * @param string|array|null $key |
||
98 | * @return mixed |
||
99 | */ |
||
100 | 12 | public function median($key = null) |
|
123 | |||
124 | /** |
||
125 | * Get the mode of a given key. |
||
126 | * |
||
127 | * @param string|array|null $key |
||
128 | * @return array|null |
||
129 | */ |
||
130 | 8 | public function mode($key = null) |
|
152 | |||
153 | /** |
||
154 | * Collapse the collection of items into a single array. |
||
155 | * |
||
156 | * @return static |
||
157 | */ |
||
158 | 3 | public function collapse() |
|
162 | |||
163 | /** |
||
164 | * Determine if an item exists in the collection. |
||
165 | * |
||
166 | * @param mixed $key |
||
167 | * @param mixed $operator |
||
168 | * @param mixed $value |
||
169 | * @return bool |
||
170 | */ |
||
171 | 5 | public function contains($key, $operator = null, $value = null) |
|
185 | |||
186 | /** |
||
187 | * Cross join with the given lists, returning all possible permutations. |
||
188 | * |
||
189 | * @param mixed ...$lists |
||
190 | * @return static |
||
191 | */ |
||
192 | 2 | public function crossJoin(...$lists) |
|
198 | |||
199 | /** |
||
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 | 6 | public function diff($items) |
|
209 | |||
210 | /** |
||
211 | * Get the items in the collection that are not present in the given items, using the callback. |
||
212 | * |
||
213 | * @param mixed $items |
||
214 | * @param callable $callback |
||
215 | * @return static |
||
216 | */ |
||
217 | 4 | public function diffUsing($items, callable $callback) |
|
221 | |||
222 | /** |
||
223 | * Get the items in the collection whose keys and values are not present in the given items. |
||
224 | * |
||
225 | * @param mixed $items |
||
226 | * @return static |
||
227 | */ |
||
228 | 4 | public function diffAssoc($items) |
|
232 | |||
233 | /** |
||
234 | * Get the items in the collection whose keys and values are not present in the given items, using the callback. |
||
235 | * |
||
236 | * @param mixed $items |
||
237 | * @param callable $callback |
||
238 | * @return static |
||
239 | */ |
||
240 | 2 | 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 | * @return static |
||
250 | */ |
||
251 | 4 | public function diffKeys($items) |
|
255 | |||
256 | /** |
||
257 | * Get the items in the collection whose keys are not present in the given items, using the callback. |
||
258 | * |
||
259 | * @param mixed $items |
||
260 | * @param callable $callback |
||
261 | * @return static |
||
262 | */ |
||
263 | 2 | public function diffKeysUsing($items, callable $callback) |
|
267 | |||
268 | /** |
||
269 | * Retrieve duplicate items from the collection. |
||
270 | * |
||
271 | * @param callable|null $callback |
||
272 | * @param bool $strict |
||
273 | * @return static |
||
274 | */ |
||
275 | 8 | public function duplicates($callback = null, $strict = false) |
|
295 | |||
296 | /** |
||
297 | * Retrieve duplicate items from the collection using strict comparison. |
||
298 | * |
||
299 | * @param callable|null $callback |
||
300 | * @return static |
||
301 | */ |
||
302 | 2 | public function duplicatesStrict($callback = null) |
|
306 | |||
307 | /** |
||
308 | * Get the comparison function to detect duplicates. |
||
309 | * |
||
310 | * @param bool $strict |
||
311 | * @return \Closure |
||
312 | */ |
||
313 | 8 | protected function duplicateComparator($strict) |
|
325 | |||
326 | /** |
||
327 | * Get all items except for those with the specified keys. |
||
328 | * |
||
329 | * @param \IlluminateAgnostic\Str\Support\Collection|mixed $keys |
||
330 | * @return static |
||
331 | */ |
||
332 | 4 | public function except($keys) |
|
342 | |||
343 | /** |
||
344 | * Run a filter over each of the items. |
||
345 | * |
||
346 | * @param callable|null $callback |
||
347 | * @return static |
||
348 | */ |
||
349 | 54 | public function filter(callable $callback = null) |
|
357 | |||
358 | /** |
||
359 | * Get the first item from the collection passing the given truth test. |
||
360 | * |
||
361 | * @param callable|null $callback |
||
362 | * @param mixed $default |
||
363 | * @return mixed |
||
364 | */ |
||
365 | 25 | public function first(callable $callback = null, $default = null) |
|
369 | |||
370 | /** |
||
371 | * Get a flattened array of the items in the collection. |
||
372 | * |
||
373 | * @param int $depth |
||
374 | * @return static |
||
375 | */ |
||
376 | 3 | public function flatten($depth = INF) |
|
380 | |||
381 | /** |
||
382 | * Flip the items in the collection. |
||
383 | * |
||
384 | * @return static |
||
385 | */ |
||
386 | 1 | public function flip() |
|
390 | |||
391 | /** |
||
392 | * Remove an item from the collection by key. |
||
393 | * |
||
394 | * @param string|array $keys |
||
395 | * @return $this |
||
396 | */ |
||
397 | 2 | public function forget($keys) |
|
405 | |||
406 | /** |
||
407 | * Get an item from the collection by key. |
||
408 | * |
||
409 | * @param mixed $key |
||
410 | * @param mixed $default |
||
411 | * @return mixed |
||
412 | */ |
||
413 | 17 | public function get($key, $default = null) |
|
421 | |||
422 | /** |
||
423 | * Group an associative array by a field or using a callback. |
||
424 | * |
||
425 | * @param array|callable|string $groupBy |
||
426 | * @param bool $preserveKeys |
||
427 | * @return static |
||
428 | */ |
||
429 | 20 | 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 | */ |
||
474 | 4 | public function keyBy($keyBy) |
|
492 | |||
493 | /** |
||
494 | * Determine if an item exists in the collection by key. |
||
495 | * |
||
496 | * @param mixed $key |
||
497 | * @return bool |
||
498 | */ |
||
499 | 2 | public function has($key) |
|
511 | |||
512 | /** |
||
513 | * Concatenate values of a given key as a string. |
||
514 | * |
||
515 | * @param string $value |
||
516 | * @param string|null $glue |
||
517 | * @return string |
||
518 | */ |
||
519 | 4 | 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 | 4 | 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 | 4 | public function intersectByKeys($items) |
|
553 | |||
554 | /** |
||
555 | * Determine if the collection is empty or not. |
||
556 | * |
||
557 | * @return bool |
||
558 | */ |
||
559 | 34 | public function isEmpty() |
|
563 | |||
564 | /** |
||
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 | 2 | public function join($glue, $finalGlue = '') |
|
593 | |||
594 | /** |
||
595 | * Get the keys of the collection items. |
||
596 | * |
||
597 | * @return static |
||
598 | */ |
||
599 | 9 | public function keys() |
|
603 | |||
604 | /** |
||
605 | * Get the last item from the collection. |
||
606 | * |
||
607 | * @param callable|null $callback |
||
608 | * @param mixed $default |
||
609 | * @return mixed |
||
610 | */ |
||
611 | 12 | public function last(callable $callback = null, $default = null) |
|
615 | |||
616 | /** |
||
617 | * Get the values of a given key. |
||
618 | * |
||
619 | * @param string|array $value |
||
620 | * @param string|null $key |
||
621 | * @return static |
||
622 | */ |
||
623 | 16 | public function pluck($value, $key = null) |
|
627 | |||
628 | /** |
||
629 | * Run a map over each of the items. |
||
630 | * |
||
631 | * @param callable $callback |
||
632 | * @return static |
||
633 | */ |
||
634 | 103 | 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 | * @param callable $callback |
||
649 | * @return static |
||
650 | */ |
||
651 | 8 | 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 | * @param callable $callback |
||
678 | * @return static |
||
679 | */ |
||
680 | 5 | 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 | 6 | public function merge($items) |
|
705 | |||
706 | /** |
||
707 | * Recursively merge the collection with the given items. |
||
708 | * |
||
709 | * @param mixed $items |
||
710 | * @return static |
||
711 | */ |
||
712 | 6 | public function mergeRecursive($items) |
|
716 | |||
717 | /** |
||
718 | * Create a collection by using this collection for keys and another for its values. |
||
719 | * |
||
720 | * @param mixed $values |
||
721 | * @return static |
||
722 | */ |
||
723 | 2 | public function combine($values) |
|
727 | |||
728 | /** |
||
729 | * Union the collection with the given items. |
||
730 | * |
||
731 | * @param mixed $items |
||
732 | * @return static |
||
733 | */ |
||
734 | 6 | public function union($items) |
|
738 | |||
739 | /** |
||
740 | * Create a new collection consisting of every n-th element. |
||
741 | * |
||
742 | * @param int $step |
||
743 | * @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 | 1 | public function only($keys) |
|
783 | |||
784 | /** |
||
785 | * Get and remove the last item from the collection. |
||
786 | * |
||
787 | * @return mixed |
||
788 | */ |
||
789 | 3 | public function pop() |
|
793 | |||
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 | 2 | public function prepend($value, $key = null) |
|
807 | |||
808 | /** |
||
809 | * Push one or more items onto the end of the collection. |
||
810 | * |
||
811 | * @param mixed $values [optional] |
||
812 | * @return $this |
||
813 | */ |
||
814 | 29 | public function push(...$values) |
|
822 | |||
823 | /** |
||
824 | * Push all of the given items onto the collection. |
||
825 | * |
||
826 | * @param iterable $source |
||
827 | * @return static |
||
828 | */ |
||
829 | 15 | public function concat($source) |
|
839 | |||
840 | /** |
||
841 | * Get and remove an item from the collection. |
||
842 | * |
||
843 | * @param mixed $key |
||
844 | * @param mixed $default |
||
845 | * @return mixed |
||
846 | */ |
||
847 | 3 | public function pull($key, $default = null) |
|
851 | |||
852 | /** |
||
853 | * Put an item in the collection by key. |
||
854 | * |
||
855 | * @param mixed $key |
||
856 | * @param mixed $value |
||
857 | * @return $this |
||
858 | */ |
||
859 | 3 | public function put($key, $value) |
|
865 | |||
866 | /** |
||
867 | * Get one or a specified number of items randomly from the collection. |
||
868 | * |
||
869 | * @param int|null $number |
||
870 | * @return static|mixed |
||
871 | * |
||
872 | * @throws \InvalidArgumentException |
||
873 | */ |
||
874 | 6 | public function random($number = null) |
|
882 | |||
883 | /** |
||
884 | * Reduce the collection to a single value. |
||
885 | * |
||
886 | * @param callable $callback |
||
887 | * @param mixed $initial |
||
888 | * @return mixed |
||
889 | */ |
||
890 | 15 | public function reduce(callable $callback, $initial = null) |
|
894 | |||
895 | /** |
||
896 | * Replace the collection items with the given items. |
||
897 | * |
||
898 | * @param mixed $items |
||
899 | * @return static |
||
900 | */ |
||
901 | 3 | public function replace($items) |
|
905 | |||
906 | /** |
||
907 | * Recursively replace the collection items with the given items. |
||
908 | * |
||
909 | * @param mixed $items |
||
910 | * @return static |
||
911 | */ |
||
912 | 6 | public function replaceRecursive($items) |
|
916 | |||
917 | /** |
||
918 | * Reverse items order. |
||
919 | * |
||
920 | * @return static |
||
921 | */ |
||
922 | 2 | public function reverse() |
|
926 | |||
927 | /** |
||
928 | * Search the collection for a given value and return the corresponding key if successful. |
||
929 | * |
||
930 | * @param mixed $value |
||
931 | * @param bool $strict |
||
932 | * @return mixed |
||
933 | */ |
||
934 | 3 | public function search($value, $strict = false) |
|
948 | |||
949 | /** |
||
950 | * Get and remove the first item from the collection. |
||
951 | * |
||
952 | * @return mixed |
||
953 | */ |
||
954 | 9 | public function shift() |
|
958 | |||
959 | /** |
||
960 | * Shuffle the items in the collection. |
||
961 | * |
||
962 | * @param int|null $seed |
||
963 | * @return static |
||
964 | */ |
||
965 | 1 | public function shuffle($seed = null) |
|
969 | |||
970 | /** |
||
971 | * Skip the first {$count} items. |
||
972 | * |
||
973 | * @param int $count |
||
974 | * @return static |
||
975 | */ |
||
976 | 1 | public function skip($count) |
|
980 | |||
981 | /** |
||
982 | * Slice the underlying collection array. |
||
983 | * |
||
984 | * @param int $offset |
||
985 | * @param int|null $length |
||
986 | * @return static |
||
987 | */ |
||
988 | 16 | public function slice($offset, $length = null) |
|
992 | |||
993 | /** |
||
994 | * Split a collection into a certain number of groups. |
||
995 | * |
||
996 | * @param int $numberOfGroups |
||
997 | * @return static |
||
998 | */ |
||
999 | 14 | public function split($numberOfGroups) |
|
1029 | |||
1030 | /** |
||
1031 | * Chunk the collection into chunks of the given size. |
||
1032 | * |
||
1033 | * @param int $size |
||
1034 | * @return static |
||
1035 | */ |
||
1036 | 3 | public function chunk($size) |
|
1050 | |||
1051 | /** |
||
1052 | * Sort through each item with a callback. |
||
1053 | * |
||
1054 | * @param callable|null $callback |
||
1055 | * @return static |
||
1056 | */ |
||
1057 | 22 | public function sort($callback = null) |
|
1067 | |||
1068 | /** |
||
1069 | * Sort items in descending order. |
||
1070 | * |
||
1071 | * @param int $options |
||
1072 | * @return static |
||
1073 | */ |
||
1074 | 2 | public function sortDesc($options = SORT_REGULAR) |
|
1082 | |||
1083 | /** |
||
1084 | * Sort the collection using the given callback. |
||
1085 | * |
||
1086 | * @param callable|string $callback |
||
1087 | * @param int $options |
||
1088 | * @param bool $descending |
||
1089 | * @return static |
||
1090 | */ |
||
1091 | 9 | public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
|
1116 | |||
1117 | /** |
||
1118 | * Sort the collection in descending order using the given callback. |
||
1119 | * |
||
1120 | * @param callable|string $callback |
||
1121 | * @param int $options |
||
1122 | * @return static |
||
1123 | */ |
||
1124 | 2 | public function sortByDesc($callback, $options = SORT_REGULAR) |
|
1128 | |||
1129 | /** |
||
1130 | * Sort the collection keys. |
||
1131 | * |
||
1132 | * @param int $options |
||
1133 | * @param bool $descending |
||
1134 | * @return static |
||
1135 | */ |
||
1136 | 4 | public function sortKeys($options = SORT_REGULAR, $descending = false) |
|
1144 | |||
1145 | /** |
||
1146 | * Sort the collection keys in descending order. |
||
1147 | * |
||
1148 | * @param int $options |
||
1149 | * @return static |
||
1150 | */ |
||
1151 | 2 | public function sortKeysDesc($options = SORT_REGULAR) |
|
1155 | |||
1156 | /** |
||
1157 | * Splice a portion of the underlying collection array. |
||
1158 | * |
||
1159 | * @param int $offset |
||
1160 | * @param int|null $length |
||
1161 | * @param mixed $replacement |
||
1162 | * @return static |
||
1163 | */ |
||
1164 | 1 | public function splice($offset, $length = null, $replacement = []) |
|
1172 | |||
1173 | /** |
||
1174 | * Take the first or last {$limit} items. |
||
1175 | * |
||
1176 | * @param int $limit |
||
1177 | * @return static |
||
1178 | */ |
||
1179 | 3 | public function take($limit) |
|
1187 | |||
1188 | /** |
||
1189 | * Transform each item in the collection using a callback. |
||
1190 | * |
||
1191 | * @param callable $callback |
||
1192 | * @return $this |
||
1193 | */ |
||
1194 | 1 | public function transform(callable $callback) |
|
1200 | |||
1201 | /** |
||
1202 | * Reset the keys on the underlying array. |
||
1203 | * |
||
1204 | * @return static |
||
1205 | */ |
||
1206 | 53 | public function values() |
|
1210 | |||
1211 | /** |
||
1212 | * Zip the collection together with one or more arrays. |
||
1213 | * |
||
1214 | * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); |
||
1215 | * => [[1, 4], [2, 5], [3, 6]] |
||
1216 | * |
||
1217 | * @param mixed ...$items |
||
1218 | * @return static |
||
1219 | */ |
||
1220 | 1 | public function zip($items) |
|
1232 | |||
1233 | /** |
||
1234 | * Pad collection to the specified length with a value. |
||
1235 | * |
||
1236 | * @param int $size |
||
1237 | * @param mixed $value |
||
1238 | * @return static |
||
1239 | */ |
||
1240 | 2 | public function pad($size, $value) |
|
1244 | |||
1245 | /** |
||
1246 | * Get an iterator for the items. |
||
1247 | * |
||
1248 | * @return \ArrayIterator |
||
1249 | */ |
||
1250 | 99 | public function getIterator() |
|
1254 | |||
1255 | /** |
||
1256 | * Count the number of items in the collection. |
||
1257 | * |
||
1258 | * @return int |
||
1259 | */ |
||
1260 | 49 | public function count() |
|
1264 | |||
1265 | /** |
||
1266 | * Add an item to the collection. |
||
1267 | * |
||
1268 | * @param mixed $item |
||
1269 | * @return $this |
||
1270 | */ |
||
1271 | 1 | public function add($item) |
|
1277 | |||
1278 | /** |
||
1279 | * Get a base Support collection instance from this collection. |
||
1280 | * |
||
1281 | * @return \IlluminateAgnostic\Str\Support\Collection |
||
1282 | */ |
||
1283 | public function toBase() |
||
1287 | |||
1288 | /** |
||
1289 | * Determine if an item exists at an offset. |
||
1290 | * |
||
1291 | * @param mixed $key |
||
1292 | * @return bool |
||
1293 | */ |
||
1294 | 29 | public function offsetExists($key) |
|
1298 | |||
1299 | /** |
||
1300 | * Get an item at a given offset. |
||
1301 | * |
||
1302 | * @param mixed $key |
||
1303 | * @return mixed |
||
1304 | */ |
||
1305 | 9 | public function offsetGet($key) |
|
1309 | |||
1310 | /** |
||
1311 | * Set the item at a given offset. |
||
1312 | * |
||
1313 | * @param mixed $key |
||
1314 | * @param mixed $value |
||
1315 | * @return void |
||
1316 | */ |
||
1317 | 43 | public function offsetSet($key, $value) |
|
1325 | |||
1326 | /** |
||
1327 | * Unset the item at a given offset. |
||
1328 | * |
||
1329 | * @param string $key |
||
1330 | * @return void |
||
1331 | */ |
||
1332 | 4 | public function offsetUnset($key) |
|
1336 | } |
||
1337 |
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.