Complex classes like LazyCollection 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 LazyCollection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class LazyCollection implements Enumerable |
||
13 | { |
||
14 | use EnumeratesValues, Macroable; |
||
15 | |||
16 | /** |
||
17 | * The source from which to generate items. |
||
18 | * |
||
19 | * @var callable|static |
||
20 | */ |
||
21 | public $source; |
||
22 | |||
23 | /** |
||
24 | * Create a new lazy collection instance. |
||
25 | * |
||
26 | * @param mixed $source |
||
27 | * @return void |
||
|
|||
28 | */ |
||
29 | 236 | public function __construct($source = null) |
|
39 | |||
40 | /** |
||
41 | * Create a new instance with no items. |
||
42 | * |
||
43 | * @return static |
||
44 | */ |
||
45 | 28 | public static function empty() |
|
49 | |||
50 | /** |
||
51 | * Create a new instance by invoking the callback a given amount of times. |
||
52 | * |
||
53 | * @param int $number |
||
54 | * @param callable|null $callback |
||
55 | * @return static |
||
56 | */ |
||
57 | 1 | public static function times($number, callable $callback = null) |
|
71 | |||
72 | /** |
||
73 | * Create an enumerable with the given range. |
||
74 | * |
||
75 | * @param int $from |
||
76 | * @param int $to |
||
77 | * @return static |
||
78 | */ |
||
79 | public static function range($from, $to) |
||
87 | |||
88 | /** |
||
89 | * Get all items in the enumerable. |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | 195 | public function all() |
|
101 | |||
102 | /** |
||
103 | * Eager load all items into a new lazy collection backed by an array. |
||
104 | * |
||
105 | * @return static |
||
106 | */ |
||
107 | public function eager() |
||
111 | |||
112 | /** |
||
113 | * Cache values as they're enumerated. |
||
114 | * |
||
115 | * @return static |
||
116 | */ |
||
117 | public function remember() |
||
149 | |||
150 | /** |
||
151 | * Get the average value of a given key. |
||
152 | * |
||
153 | * @param callable|string|null $callback |
||
154 | * @return mixed |
||
155 | */ |
||
156 | 1 | public function avg($callback = null) |
|
160 | |||
161 | /** |
||
162 | * Get the median of a given key. |
||
163 | * |
||
164 | * @param string|array|null $key |
||
165 | * @return mixed |
||
166 | */ |
||
167 | 6 | public function median($key = null) |
|
171 | |||
172 | /** |
||
173 | * Get the mode of a given key. |
||
174 | * |
||
175 | * @param string|array|null $key |
||
176 | * @return array|null |
||
177 | */ |
||
178 | 4 | public function mode($key = null) |
|
182 | |||
183 | /** |
||
184 | * Collapse the collection of items into a single array. |
||
185 | * |
||
186 | * @return static |
||
187 | */ |
||
188 | 3 | public function collapse() |
|
200 | |||
201 | /** |
||
202 | * Determine if an item exists in the enumerable. |
||
203 | * |
||
204 | * @param mixed $key |
||
205 | * @param mixed $operator |
||
206 | * @param mixed $value |
||
207 | * @return bool |
||
208 | */ |
||
209 | 4 | public function contains($key, $operator = null, $value = null) |
|
231 | |||
232 | /** |
||
233 | * Cross join the given iterables, returning all possible permutations. |
||
234 | * |
||
235 | * @param array ...$arrays |
||
236 | * @return static |
||
237 | */ |
||
238 | 1 | public function crossJoin(...$arrays) |
|
242 | |||
243 | /** |
||
244 | * Get the items that are not present in the given items. |
||
245 | * |
||
246 | * @param mixed $items |
||
247 | * @return static |
||
248 | */ |
||
249 | 3 | public function diff($items) |
|
253 | |||
254 | /** |
||
255 | * Get the items that are not present in the given items, using the callback. |
||
256 | * |
||
257 | * @param mixed $items |
||
258 | * @param callable $callback |
||
259 | * @return static |
||
260 | */ |
||
261 | 2 | public function diffUsing($items, callable $callback) |
|
265 | |||
266 | /** |
||
267 | * Get the items whose keys and values are not present in the given items. |
||
268 | * |
||
269 | * @param mixed $items |
||
270 | * @return static |
||
271 | */ |
||
272 | 2 | public function diffAssoc($items) |
|
276 | |||
277 | /** |
||
278 | * Get the items whose keys and values are not present in the given items, using the callback. |
||
279 | * |
||
280 | * @param mixed $items |
||
281 | * @param callable $callback |
||
282 | * @return static |
||
283 | */ |
||
284 | 1 | public function diffAssocUsing($items, callable $callback) |
|
288 | |||
289 | /** |
||
290 | * Get the items whose keys are not present in the given items. |
||
291 | * |
||
292 | * @param mixed $items |
||
293 | * @return static |
||
294 | */ |
||
295 | 2 | public function diffKeys($items) |
|
299 | |||
300 | /** |
||
301 | * Get the items whose keys are not present in the given items, using the callback. |
||
302 | * |
||
303 | * @param mixed $items |
||
304 | * @param callable $callback |
||
305 | * @return static |
||
306 | */ |
||
307 | 1 | public function diffKeysUsing($items, callable $callback) |
|
311 | |||
312 | /** |
||
313 | * Retrieve duplicate items. |
||
314 | * |
||
315 | * @param callable|null $callback |
||
316 | * @param bool $strict |
||
317 | * @return static |
||
318 | */ |
||
319 | 3 | public function duplicates($callback = null, $strict = false) |
|
323 | |||
324 | /** |
||
325 | * Retrieve duplicate items using strict comparison. |
||
326 | * |
||
327 | * @param callable|null $callback |
||
328 | * @return static |
||
329 | */ |
||
330 | 1 | public function duplicatesStrict($callback = null) |
|
334 | |||
335 | /** |
||
336 | * Get all items except for those with the specified keys. |
||
337 | * |
||
338 | * @param mixed $keys |
||
339 | * @return static |
||
340 | */ |
||
341 | 2 | public function except($keys) |
|
345 | |||
346 | /** |
||
347 | * Run a filter over each of the items. |
||
348 | * |
||
349 | * @param callable|null $callback |
||
350 | * @return static |
||
351 | */ |
||
352 | 26 | public function filter(callable $callback = null) |
|
368 | |||
369 | /** |
||
370 | * Get the first item from the enumerable passing the given truth test. |
||
371 | * |
||
372 | * @param callable|null $callback |
||
373 | * @param mixed $default |
||
374 | * @return mixed |
||
375 | */ |
||
376 | 10 | public function first(callable $callback = null, $default = null) |
|
396 | |||
397 | /** |
||
398 | * Get a flattened list of the items in the collection. |
||
399 | * |
||
400 | * @param int $depth |
||
401 | * @return static |
||
402 | */ |
||
403 | 3 | public function flatten($depth = INF) |
|
419 | |||
420 | /** |
||
421 | * Flip the items in the collection. |
||
422 | * |
||
423 | * @return static |
||
424 | */ |
||
425 | 1 | public function flip() |
|
433 | |||
434 | /** |
||
435 | * Get an item by key. |
||
436 | * |
||
437 | * @param mixed $key |
||
438 | * @param mixed $default |
||
439 | * @return mixed |
||
440 | */ |
||
441 | 7 | public function get($key, $default = null) |
|
455 | |||
456 | /** |
||
457 | * Group an associative array by a field or using a callback. |
||
458 | * |
||
459 | * @param array|callable|string $groupBy |
||
460 | * @param bool $preserveKeys |
||
461 | * @return static |
||
462 | */ |
||
463 | 10 | 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) |
|
490 | |||
491 | /** |
||
492 | * Determine if an item exists in the collection by key. |
||
493 | * |
||
494 | * @param mixed $key |
||
495 | * @return bool |
||
496 | */ |
||
497 | 2 | public function has($key) |
|
510 | |||
511 | /** |
||
512 | * Concatenate values of a given key as a string. |
||
513 | * |
||
514 | * @param string $value |
||
515 | * @param string|null $glue |
||
516 | * @return string |
||
517 | */ |
||
518 | 1 | public function implode($value, $glue = null) |
|
522 | |||
523 | /** |
||
524 | * Intersect the collection with the given items. |
||
525 | * |
||
526 | * @param mixed $items |
||
527 | * @return static |
||
528 | */ |
||
529 | 2 | public function intersect($items) |
|
533 | |||
534 | /** |
||
535 | * Intersect the collection with the given items by key. |
||
536 | * |
||
537 | * @param mixed $items |
||
538 | * @return static |
||
539 | */ |
||
540 | 2 | public function intersectByKeys($items) |
|
544 | |||
545 | /** |
||
546 | * Determine if the items is empty or not. |
||
547 | * |
||
548 | * @return bool |
||
549 | */ |
||
550 | 12 | public function isEmpty() |
|
554 | |||
555 | /** |
||
556 | * Join all items from the collection using a string. The final items can use a separate glue string. |
||
557 | * |
||
558 | * @param string $glue |
||
559 | * @param string $finalGlue |
||
560 | * @return string |
||
561 | */ |
||
562 | 1 | public function join($glue, $finalGlue = '') |
|
566 | |||
567 | /** |
||
568 | * Get the keys of the collection items. |
||
569 | * |
||
570 | * @return static |
||
571 | */ |
||
572 | 3 | public function keys() |
|
580 | |||
581 | /** |
||
582 | * Get the last item from the collection. |
||
583 | * |
||
584 | * @param callable|null $callback |
||
585 | * @param mixed $default |
||
586 | * @return mixed |
||
587 | */ |
||
588 | 4 | public function last(callable $callback = null, $default = null) |
|
600 | |||
601 | /** |
||
602 | * Get the values of a given key. |
||
603 | * |
||
604 | * @param string|array $value |
||
605 | * @param string|null $key |
||
606 | * @return static |
||
607 | */ |
||
608 | 4 | public function pluck($value, $key = null) |
|
630 | |||
631 | /** |
||
632 | * Run a map over each of the items. |
||
633 | * |
||
634 | * @param callable $callback |
||
635 | * @return static |
||
636 | */ |
||
637 | 68 | public function map(callable $callback) |
|
645 | |||
646 | /** |
||
647 | * Run a dictionary map over the items. |
||
648 | * |
||
649 | * The callback should return an associative array with a single key/value pair. |
||
650 | * |
||
651 | * @param callable $callback |
||
652 | * @return static |
||
653 | */ |
||
654 | 4 | public function mapToDictionary(callable $callback) |
|
658 | |||
659 | /** |
||
660 | * Run an associative map over each of the items. |
||
661 | * |
||
662 | * The callback should return an associative array with a single key/value pair. |
||
663 | * |
||
664 | * @param callable $callback |
||
665 | * @return static |
||
666 | */ |
||
667 | 5 | public function mapWithKeys(callable $callback) |
|
675 | |||
676 | /** |
||
677 | * Merge the collection with the given items. |
||
678 | * |
||
679 | * @param mixed $items |
||
680 | * @return static |
||
681 | */ |
||
682 | 3 | public function merge($items) |
|
686 | |||
687 | /** |
||
688 | * Recursively merge the collection with the given items. |
||
689 | * |
||
690 | * @param mixed $items |
||
691 | * @return static |
||
692 | */ |
||
693 | 3 | public function mergeRecursive($items) |
|
697 | |||
698 | /** |
||
699 | * Create a collection by using this collection for keys and another for its values. |
||
700 | * |
||
701 | * @param mixed $values |
||
702 | * @return static |
||
703 | */ |
||
704 | 2 | public function combine($values) |
|
728 | |||
729 | /** |
||
730 | * Union the collection with the given items. |
||
731 | * |
||
732 | * @param mixed $items |
||
733 | * @return static |
||
734 | */ |
||
735 | 3 | public function union($items) |
|
739 | |||
740 | /** |
||
741 | * Create a new collection consisting of every n-th element. |
||
742 | * |
||
743 | * @param int $step |
||
744 | * @param int $offset |
||
745 | * @return static |
||
746 | */ |
||
747 | 1 | public function nth($step, $offset = 0) |
|
761 | |||
762 | /** |
||
763 | * Get the items with the specified keys. |
||
764 | * |
||
765 | * @param mixed $keys |
||
766 | * @return static |
||
767 | */ |
||
768 | 1 | public function only($keys) |
|
796 | |||
797 | /** |
||
798 | * Push all of the given items onto the collection. |
||
799 | * |
||
800 | * @param iterable $source |
||
801 | * @return static |
||
802 | */ |
||
803 | 15 | public function concat($source) |
|
810 | |||
811 | /** |
||
812 | * Get one or a specified number of items randomly from the collection. |
||
813 | * |
||
814 | * @param int|null $number |
||
815 | * @return static|mixed |
||
816 | * |
||
817 | * @throws \InvalidArgumentException |
||
818 | */ |
||
819 | 3 | public function random($number = null) |
|
825 | |||
826 | /** |
||
827 | * Reduce the collection to a single value. |
||
828 | * |
||
829 | * @param callable $callback |
||
830 | * @param mixed $initial |
||
831 | * @return mixed |
||
832 | */ |
||
833 | 7 | public function reduce(callable $callback, $initial = null) |
|
843 | |||
844 | /** |
||
845 | * Replace the collection items with the given items. |
||
846 | * |
||
847 | * @param mixed $items |
||
848 | * @return static |
||
849 | */ |
||
850 | 3 | public function replace($items) |
|
870 | |||
871 | /** |
||
872 | * Recursively replace the collection items with the given items. |
||
873 | * |
||
874 | * @param mixed $items |
||
875 | * @return static |
||
876 | */ |
||
877 | 3 | public function replaceRecursive($items) |
|
881 | |||
882 | /** |
||
883 | * Reverse items order. |
||
884 | * |
||
885 | * @return static |
||
886 | */ |
||
887 | 1 | public function reverse() |
|
891 | |||
892 | /** |
||
893 | * Search the collection for a given value and return the corresponding key if successful. |
||
894 | * |
||
895 | * @param mixed $value |
||
896 | * @param bool $strict |
||
897 | * @return mixed |
||
898 | */ |
||
899 | 3 | public function search($value, $strict = false) |
|
915 | |||
916 | /** |
||
917 | * Shuffle the items in the collection. |
||
918 | * |
||
919 | * @param int|null $seed |
||
920 | * @return static |
||
921 | */ |
||
922 | 1 | public function shuffle($seed = null) |
|
926 | |||
927 | /** |
||
928 | * Skip the first {$count} items. |
||
929 | * |
||
930 | * @param int $count |
||
931 | * @return static |
||
932 | */ |
||
933 | 5 | public function skip($count) |
|
949 | |||
950 | /** |
||
951 | * Get a slice of items from the enumerable. |
||
952 | * |
||
953 | * @param int $offset |
||
954 | * @param int|null $length |
||
955 | * @return static |
||
956 | */ |
||
957 | 8 | public function slice($offset, $length = null) |
|
967 | |||
968 | /** |
||
969 | * Split a collection into a certain number of groups. |
||
970 | * |
||
971 | * @param int $numberOfGroups |
||
972 | * @return static |
||
973 | */ |
||
974 | 7 | public function split($numberOfGroups) |
|
978 | |||
979 | /** |
||
980 | * Chunk the collection into chunks of the given size. |
||
981 | * |
||
982 | * @param int $size |
||
983 | * @return static |
||
984 | */ |
||
985 | 3 | public function chunk($size) |
|
1017 | |||
1018 | /** |
||
1019 | * Sort through each item with a callback. |
||
1020 | * |
||
1021 | * @param callable|null|int $callback |
||
1022 | * @return static |
||
1023 | */ |
||
1024 | 2 | public function sort($callback = null) |
|
1028 | |||
1029 | /** |
||
1030 | * Sort items in descending order. |
||
1031 | * |
||
1032 | * @param int $options |
||
1033 | * @return static |
||
1034 | */ |
||
1035 | 1 | public function sortDesc($options = SORT_REGULAR) |
|
1039 | |||
1040 | /** |
||
1041 | * Sort the collection using the given callback. |
||
1042 | * |
||
1043 | * @param callable|string $callback |
||
1044 | * @param int $options |
||
1045 | * @param bool $descending |
||
1046 | * @return static |
||
1047 | */ |
||
1048 | 4 | public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
|
1052 | |||
1053 | /** |
||
1054 | * Sort the collection in descending order using the given callback. |
||
1055 | * |
||
1056 | * @param callable|string $callback |
||
1057 | * @param int $options |
||
1058 | * @return static |
||
1059 | */ |
||
1060 | 1 | public function sortByDesc($callback, $options = SORT_REGULAR) |
|
1064 | |||
1065 | /** |
||
1066 | * Sort the collection keys. |
||
1067 | * |
||
1068 | * @param int $options |
||
1069 | * @param bool $descending |
||
1070 | * @return static |
||
1071 | */ |
||
1072 | 1 | public function sortKeys($options = SORT_REGULAR, $descending = false) |
|
1076 | |||
1077 | /** |
||
1078 | * Sort the collection keys in descending order. |
||
1079 | * |
||
1080 | * @param int $options |
||
1081 | * @return static |
||
1082 | */ |
||
1083 | 1 | public function sortKeysDesc($options = SORT_REGULAR) |
|
1087 | |||
1088 | /** |
||
1089 | * Take the first or last {$limit} items. |
||
1090 | * |
||
1091 | * @param int $limit |
||
1092 | * @return static |
||
1093 | */ |
||
1094 | 5 | public function take($limit) |
|
1116 | |||
1117 | /** |
||
1118 | * Pass each item in the collection to the given callback, lazily. |
||
1119 | * |
||
1120 | * @param callable $callback |
||
1121 | * @return static |
||
1122 | */ |
||
1123 | public function tapEach(callable $callback) |
||
1133 | |||
1134 | /** |
||
1135 | * Reset the keys on the underlying array. |
||
1136 | * |
||
1137 | * @return static |
||
1138 | */ |
||
1139 | 47 | public function values() |
|
1147 | |||
1148 | /** |
||
1149 | * Zip the collection together with one or more arrays. |
||
1150 | * |
||
1151 | * e.g. new LazyCollection([1, 2, 3])->zip([4, 5, 6]); |
||
1152 | * => [[1, 4], [2, 5], [3, 6]] |
||
1153 | * |
||
1154 | * @param mixed ...$items |
||
1155 | * @return static |
||
1156 | */ |
||
1157 | 1 | public function zip($items) |
|
1173 | |||
1174 | /** |
||
1175 | * Pad collection to the specified length with a value. |
||
1176 | * |
||
1177 | * @param int $size |
||
1178 | * @param mixed $value |
||
1179 | * @return static |
||
1180 | */ |
||
1181 | 1 | public function pad($size, $value) |
|
1201 | |||
1202 | /** |
||
1203 | * Get the values iterator. |
||
1204 | * |
||
1205 | * @return \Traversable |
||
1206 | */ |
||
1207 | 200 | public function getIterator() |
|
1211 | |||
1212 | /** |
||
1213 | * Count the number of items in the collection. |
||
1214 | * |
||
1215 | * @return int |
||
1216 | */ |
||
1217 | 9 | public function count() |
|
1225 | |||
1226 | /** |
||
1227 | * Make an iterator from the given source. |
||
1228 | * |
||
1229 | * @param mixed $source |
||
1230 | * @return \Traversable |
||
1231 | */ |
||
1232 | 200 | protected function makeIterator($source) |
|
1244 | |||
1245 | /** |
||
1246 | * Explode the "value" and "key" arguments passed to "pluck". |
||
1247 | * |
||
1248 | * @param string|array $value |
||
1249 | * @param string|array|null $key |
||
1250 | * @return array |
||
1251 | */ |
||
1252 | 4 | protected function explodePluckParameters($value, $key) |
|
1260 | |||
1261 | /** |
||
1262 | * Pass this lazy collection through a method on the collection class. |
||
1263 | * |
||
1264 | * @param string $method |
||
1265 | * @param array $params |
||
1266 | * @return static |
||
1267 | */ |
||
1268 | 69 | protected function passthru($method, array $params) |
|
1274 | } |
||
1275 |
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.