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 | 189 | public function __construct($source = null) |
|
39 | |||
40 | /** |
||
41 | * Create a new instance with no items. |
||
42 | * |
||
43 | * @return static |
||
44 | */ |
||
45 | 23 | 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 $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 | 154 | public function all() |
|
101 | |||
102 | /** |
||
103 | * Get the average value of a given key. |
||
104 | * |
||
105 | * @param callable|string|null $callback |
||
106 | * @return mixed |
||
107 | */ |
||
108 | 1 | public function avg($callback = null) |
|
112 | |||
113 | /** |
||
114 | * Get the median of a given key. |
||
115 | * |
||
116 | * @param string|array|null $key |
||
117 | * @return mixed |
||
118 | */ |
||
119 | 1 | 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 | 3 | public function mode($key = null) |
|
134 | |||
135 | /** |
||
136 | * Collapse the collection of items into a single array. |
||
137 | * |
||
138 | * @return static |
||
139 | */ |
||
140 | 3 | public function collapse() |
|
152 | |||
153 | /** |
||
154 | * Determine if an item exists in the enumerable. |
||
155 | * |
||
156 | * @param mixed $key |
||
157 | * @param mixed $operator |
||
158 | * @param mixed $value |
||
159 | * @return bool |
||
160 | */ |
||
161 | 2 | public function contains($key, $operator = null, $value = null) |
|
162 | { |
||
163 | 2 | if (func_num_args() === 1 && $this->useAsCallable($key)) { |
|
164 | 2 | $placeholder = new stdClass; |
|
165 | |||
166 | 2 | return $this->first($key, $placeholder) !== $placeholder; |
|
167 | } |
||
168 | |||
169 | 2 | if (func_num_args() === 1) { |
|
170 | 2 | $needle = $key; |
|
171 | |||
172 | 2 | foreach ($this as $value) { |
|
173 | 2 | if ($value == $needle) { |
|
174 | 2 | return true; |
|
175 | } |
||
176 | } |
||
177 | |||
178 | 2 | return false; |
|
179 | } |
||
180 | |||
181 | return $this->contains($this->operatorForWhere(...func_get_args())); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Cross join the given iterables, returning all possible permutations. |
||
186 | * |
||
187 | * @param array ...$arrays |
||
188 | * @return static |
||
189 | */ |
||
190 | 1 | public function crossJoin(...$arrays) |
|
194 | |||
195 | /** |
||
196 | * Get the items that are not present in the given items. |
||
197 | * |
||
198 | * @param mixed $items |
||
199 | * @return static |
||
200 | */ |
||
201 | 3 | public function diff($items) |
|
205 | |||
206 | /** |
||
207 | * Get the items that are not present in the given items, using the callback. |
||
208 | * |
||
209 | * @param mixed $items |
||
210 | * @param callable $callback |
||
211 | * @return static |
||
212 | */ |
||
213 | 2 | public function diffUsing($items, callable $callback) |
|
217 | |||
218 | /** |
||
219 | * Get the items whose keys and values are not present in the given items. |
||
220 | * |
||
221 | * @param mixed $items |
||
222 | * @return static |
||
223 | */ |
||
224 | 2 | public function diffAssoc($items) |
|
228 | |||
229 | /** |
||
230 | * Get the items whose keys and values are not present in the given items. |
||
231 | * |
||
232 | * @param mixed $items |
||
233 | * @param callable $callback |
||
234 | * @return static |
||
235 | */ |
||
236 | 1 | public function diffAssocUsing($items, callable $callback) |
|
240 | |||
241 | /** |
||
242 | * Get the items whose keys are not present in the given items. |
||
243 | * |
||
244 | * @param mixed $items |
||
245 | * @return static |
||
246 | */ |
||
247 | 2 | public function diffKeys($items) |
|
251 | |||
252 | /** |
||
253 | * Get the items whose keys are not present in the given items. |
||
254 | * |
||
255 | * @param mixed $items |
||
256 | * @param callable $callback |
||
257 | * @return static |
||
258 | */ |
||
259 | 1 | public function diffKeysUsing($items, callable $callback) |
|
263 | |||
264 | /** |
||
265 | * Retrieve duplicate items. |
||
266 | * |
||
267 | * @param callable|null $callback |
||
268 | * @param bool $strict |
||
269 | * @return static |
||
270 | */ |
||
271 | public function duplicates($callback = null, $strict = false) |
||
272 | { |
||
273 | return $this->passthru('duplicates', func_get_args()); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Retrieve duplicate items using strict comparison. |
||
278 | * |
||
279 | * @param callable|null $callback |
||
280 | * @return static |
||
281 | */ |
||
282 | public function duplicatesStrict($callback = null) |
||
283 | { |
||
284 | return $this->passthru('duplicatesStrict', func_get_args()); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Get all items except for those with the specified keys. |
||
289 | * |
||
290 | * @param mixed $keys |
||
291 | * @return static |
||
292 | */ |
||
293 | 2 | public function except($keys) |
|
297 | |||
298 | /** |
||
299 | * Run a filter over each of the items. |
||
300 | * |
||
301 | * @param callable|null $callback |
||
302 | * @return static |
||
303 | */ |
||
304 | 10 | public function filter(callable $callback = null) |
|
320 | |||
321 | /** |
||
322 | * Get the first item from the enumerable passing the given truth test. |
||
323 | * |
||
324 | * @param callable|null $callback |
||
325 | * @param mixed $default |
||
326 | * @return mixed |
||
327 | */ |
||
328 | 6 | public function first(callable $callback = null, $default = null) |
|
329 | { |
||
330 | 6 | $iterator = $this->getIterator(); |
|
331 | |||
332 | 6 | if (is_null($callback)) { |
|
333 | 2 | if (! $iterator->valid()) { |
|
334 | return value($default); |
||
335 | } |
||
336 | |||
337 | 2 | return $iterator->current(); |
|
338 | } |
||
339 | |||
340 | 4 | foreach ($iterator as $key => $value) { |
|
341 | 4 | if ($callback($value, $key)) { |
|
342 | 4 | return $value; |
|
343 | } |
||
344 | } |
||
345 | |||
346 | 3 | return value($default); |
|
347 | } |
||
348 | |||
349 | /** |
||
350 | * Get a flattened list of the items in the collection. |
||
351 | * |
||
352 | * @param int $depth |
||
353 | * @return static |
||
354 | */ |
||
355 | 3 | public function flatten($depth = INF) |
|
371 | |||
372 | /** |
||
373 | * Flip the items in the collection. |
||
374 | * |
||
375 | * @return static |
||
376 | */ |
||
377 | 1 | public function flip() |
|
385 | |||
386 | /** |
||
387 | * Get an item by key. |
||
388 | * |
||
389 | * @param mixed $key |
||
390 | * @param mixed $default |
||
391 | * @return mixed |
||
392 | */ |
||
393 | 7 | public function get($key, $default = null) |
|
407 | |||
408 | /** |
||
409 | * Group an associative array by a field or using a callback. |
||
410 | * |
||
411 | * @param array|callable|string $groupBy |
||
412 | * @param bool $preserveKeys |
||
413 | * @return static |
||
414 | */ |
||
415 | 7 | public function groupBy($groupBy, $preserveKeys = false) |
|
419 | |||
420 | /** |
||
421 | * Key an associative array by a field or using a callback. |
||
422 | * |
||
423 | * @param callable|string $keyBy |
||
424 | * @return static |
||
425 | */ |
||
426 | 3 | public function keyBy($keyBy) |
|
442 | |||
443 | /** |
||
444 | * Determine if an item exists in the collection by key. |
||
445 | * |
||
446 | * @param mixed $key |
||
447 | * @return bool |
||
448 | */ |
||
449 | 2 | public function has($key) |
|
462 | |||
463 | /** |
||
464 | * Concatenate values of a given key as a string. |
||
465 | * |
||
466 | * @param string $value |
||
467 | * @param string $glue |
||
468 | * @return string |
||
469 | */ |
||
470 | public function implode($value, $glue = null) |
||
471 | { |
||
472 | return $this->collect()->implode(...func_get_args()); |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * Intersect the collection with the given items. |
||
477 | * |
||
478 | * @param mixed $items |
||
479 | * @return static |
||
480 | */ |
||
481 | 2 | public function intersect($items) |
|
485 | |||
486 | /** |
||
487 | * Intersect the collection with the given items by key. |
||
488 | * |
||
489 | * @param mixed $items |
||
490 | * @return static |
||
491 | */ |
||
492 | 2 | public function intersectByKeys($items) |
|
496 | |||
497 | /** |
||
498 | * Determine if the items is empty or not. |
||
499 | * |
||
500 | * @return bool |
||
501 | */ |
||
502 | 12 | public function isEmpty() |
|
506 | |||
507 | /** |
||
508 | * Join all items from the collection using a string. The final items can use a separate glue string. |
||
509 | * |
||
510 | * @param string $glue |
||
511 | * @param string $finalGlue |
||
512 | * @return string |
||
513 | */ |
||
514 | 1 | public function join($glue, $finalGlue = '') |
|
518 | |||
519 | /** |
||
520 | * Get the keys of the collection items. |
||
521 | * |
||
522 | * @return static |
||
523 | */ |
||
524 | 3 | public function keys() |
|
532 | |||
533 | /** |
||
534 | * Get the last item from the collection. |
||
535 | * |
||
536 | * @param callable|null $callback |
||
537 | * @param mixed $default |
||
538 | * @return mixed |
||
539 | */ |
||
540 | 2 | public function last(callable $callback = null, $default = null) |
|
552 | |||
553 | /** |
||
554 | * Get the values of a given key. |
||
555 | * |
||
556 | * @param string|array $value |
||
557 | * @param string|null $key |
||
558 | * @return static |
||
559 | */ |
||
560 | public function pluck($value, $key = null) |
||
561 | { |
||
562 | return new static(function () use ($value, $key) { |
||
563 | [$value, $key] = $this->explodePluckParameters($value, $key); |
||
564 | |||
565 | foreach ($this as $item) { |
||
566 | $itemValue = data_get($item, $value); |
||
567 | |||
568 | if (is_null($key)) { |
||
569 | yield $itemValue; |
||
570 | } else { |
||
571 | $itemKey = data_get($item, $key); |
||
572 | |||
573 | if (is_object($itemKey) && method_exists($itemKey, '__toString')) { |
||
574 | $itemKey = (string) $itemKey; |
||
575 | } |
||
576 | |||
577 | yield $itemKey => $itemValue; |
||
578 | } |
||
579 | } |
||
580 | }); |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * Run a map over each of the items. |
||
585 | * |
||
586 | * @param callable $callback |
||
587 | * @return static |
||
588 | */ |
||
589 | 63 | public function map(callable $callback) |
|
597 | |||
598 | /** |
||
599 | * Run a dictionary map over the items. |
||
600 | * |
||
601 | * The callback should return an associative array with a single key/value pair. |
||
602 | * |
||
603 | * @param callable $callback |
||
604 | * @return static |
||
605 | */ |
||
606 | 4 | public function mapToDictionary(callable $callback) |
|
610 | |||
611 | /** |
||
612 | * Run an associative map over each of the items. |
||
613 | * |
||
614 | * The callback should return an associative array with a single key/value pair. |
||
615 | * |
||
616 | * @param callable $callback |
||
617 | * @return static |
||
618 | */ |
||
619 | 5 | public function mapWithKeys(callable $callback) |
|
627 | |||
628 | /** |
||
629 | * Merge the collection with the given items. |
||
630 | * |
||
631 | * @param mixed $items |
||
632 | * @return static |
||
633 | */ |
||
634 | 3 | public function merge($items) |
|
638 | |||
639 | /** |
||
640 | * Recursively merge the collection with the given items. |
||
641 | * |
||
642 | * @param mixed $items |
||
643 | * @return static |
||
644 | */ |
||
645 | 3 | public function mergeRecursive($items) |
|
649 | |||
650 | /** |
||
651 | * Create a collection by using this collection for keys and another for its values. |
||
652 | * |
||
653 | * @param mixed $values |
||
654 | * @return static |
||
655 | */ |
||
656 | 2 | public function combine($values) |
|
680 | |||
681 | /** |
||
682 | * Union the collection with the given items. |
||
683 | * |
||
684 | * @param mixed $items |
||
685 | * @return static |
||
686 | */ |
||
687 | 3 | public function union($items) |
|
691 | |||
692 | /** |
||
693 | * Create a new collection consisting of every n-th element. |
||
694 | * |
||
695 | * @param int $step |
||
696 | * @param int $offset |
||
697 | * @return static |
||
698 | */ |
||
699 | 1 | public function nth($step, $offset = 0) |
|
713 | |||
714 | /** |
||
715 | * Get the items with the specified keys. |
||
716 | * |
||
717 | * @param mixed $keys |
||
718 | * @return static |
||
719 | */ |
||
720 | 1 | public function only($keys) |
|
721 | { |
||
722 | 1 | if ($keys instanceof Enumerable) { |
|
723 | $keys = $keys->all(); |
||
724 | 1 | } elseif (! is_null($keys)) { |
|
725 | 1 | $keys = is_array($keys) ? $keys : func_get_args(); |
|
726 | } |
||
727 | |||
728 | return new static(function () use ($keys) { |
||
729 | 1 | if (is_null($keys)) { |
|
730 | 1 | yield from $this; |
|
731 | } else { |
||
732 | 1 | $keys = array_flip($keys); |
|
733 | |||
734 | 1 | foreach ($this as $key => $value) { |
|
735 | 1 | if (array_key_exists($key, $keys)) { |
|
736 | 1 | yield $key => $value; |
|
737 | |||
738 | 1 | unset($keys[$key]); |
|
739 | |||
740 | 1 | if (empty($keys)) { |
|
741 | break; |
||
742 | } |
||
743 | } |
||
744 | } |
||
745 | } |
||
746 | 1 | }); |
|
747 | } |
||
748 | |||
749 | /** |
||
750 | * Push all of the given items onto the collection. |
||
751 | * |
||
752 | * @param iterable $source |
||
753 | * @return static |
||
754 | */ |
||
755 | 14 | public function concat($source) |
|
762 | |||
763 | /** |
||
764 | * Get one or a specified number of items randomly from the collection. |
||
765 | * |
||
766 | * @param int|null $number |
||
767 | * @return static|mixed |
||
768 | * |
||
769 | * @throws \InvalidArgumentException |
||
770 | */ |
||
771 | 3 | public function random($number = null) |
|
777 | |||
778 | /** |
||
779 | * Reduce the collection to a single value. |
||
780 | * |
||
781 | * @param callable $callback |
||
782 | * @param mixed $initial |
||
783 | * @return mixed |
||
784 | */ |
||
785 | 6 | public function reduce(callable $callback, $initial = null) |
|
795 | |||
796 | /** |
||
797 | * Replace the collection items with the given items. |
||
798 | * |
||
799 | * @param mixed $items |
||
800 | * @return static |
||
801 | */ |
||
802 | 3 | public function replace($items) |
|
822 | |||
823 | /** |
||
824 | * Recursively replace the collection items with the given items. |
||
825 | * |
||
826 | * @param mixed $items |
||
827 | * @return static |
||
828 | */ |
||
829 | 3 | public function replaceRecursive($items) |
|
833 | |||
834 | /** |
||
835 | * Reverse items order. |
||
836 | * |
||
837 | * @return static |
||
838 | */ |
||
839 | 1 | public function reverse() |
|
843 | |||
844 | /** |
||
845 | * Search the collection for a given value and return the corresponding key if successful. |
||
846 | * |
||
847 | * @param mixed $value |
||
848 | * @param bool $strict |
||
849 | * @return mixed |
||
850 | */ |
||
851 | 3 | public function search($value, $strict = false) |
|
867 | |||
868 | /** |
||
869 | * Shuffle the items in the collection. |
||
870 | * |
||
871 | * @param int $seed |
||
872 | * @return static |
||
873 | */ |
||
874 | 1 | public function shuffle($seed = null) |
|
878 | |||
879 | /** |
||
880 | * Skip the first {$count} items. |
||
881 | * |
||
882 | * @param int $count |
||
883 | * @return static |
||
884 | */ |
||
885 | 5 | public function skip($count) |
|
901 | |||
902 | /** |
||
903 | * Get a slice of items from the enumerable. |
||
904 | * |
||
905 | * @param int $offset |
||
906 | * @param int $length |
||
907 | * @return static |
||
908 | */ |
||
909 | 8 | public function slice($offset, $length = null) |
|
919 | |||
920 | /** |
||
921 | * Split a collection into a certain number of groups. |
||
922 | * |
||
923 | * @param int $numberOfGroups |
||
924 | * @return static |
||
925 | */ |
||
926 | 7 | public function split($numberOfGroups) |
|
930 | |||
931 | /** |
||
932 | * Chunk the collection into chunks of the given size. |
||
933 | * |
||
934 | * @param int $size |
||
935 | * @return static |
||
936 | */ |
||
937 | 3 | public function chunk($size) |
|
969 | |||
970 | /** |
||
971 | * Sort through each item with a callback. |
||
972 | * |
||
973 | * @param callable|null $callback |
||
974 | * @return static |
||
975 | */ |
||
976 | 2 | public function sort(callable $callback = null) |
|
980 | |||
981 | /** |
||
982 | * Sort the collection using the given callback. |
||
983 | * |
||
984 | * @param callable|string $callback |
||
985 | * @param int $options |
||
986 | * @param bool $descending |
||
987 | * @return static |
||
988 | */ |
||
989 | 2 | public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
|
993 | |||
994 | /** |
||
995 | * Sort the collection in descending order using the given callback. |
||
996 | * |
||
997 | * @param callable|string $callback |
||
998 | * @param int $options |
||
999 | * @return static |
||
1000 | */ |
||
1001 | 1 | public function sortByDesc($callback, $options = SORT_REGULAR) |
|
1005 | |||
1006 | /** |
||
1007 | * Sort the collection keys. |
||
1008 | * |
||
1009 | * @param int $options |
||
1010 | * @param bool $descending |
||
1011 | * @return static |
||
1012 | */ |
||
1013 | 1 | public function sortKeys($options = SORT_REGULAR, $descending = false) |
|
1017 | |||
1018 | /** |
||
1019 | * Sort the collection keys in descending order. |
||
1020 | * |
||
1021 | * @param int $options |
||
1022 | * @return static |
||
1023 | */ |
||
1024 | 1 | public function sortKeysDesc($options = SORT_REGULAR) |
|
1028 | |||
1029 | /** |
||
1030 | * Take the first or last {$limit} items. |
||
1031 | * |
||
1032 | * @param int $limit |
||
1033 | * @return static |
||
1034 | */ |
||
1035 | 5 | public function take($limit) |
|
1057 | |||
1058 | /** |
||
1059 | * Pass each item in the collection to the given callback, lazily. |
||
1060 | * |
||
1061 | * @param callable $callback |
||
1062 | * @return static |
||
1063 | */ |
||
1064 | public function tapEach(callable $callback) |
||
1074 | |||
1075 | /** |
||
1076 | * Reset the keys on the underlying array. |
||
1077 | * |
||
1078 | * @return static |
||
1079 | */ |
||
1080 | 33 | public function values() |
|
1088 | |||
1089 | /** |
||
1090 | * Zip the collection together with one or more arrays. |
||
1091 | * |
||
1092 | * e.g. new LazyCollection([1, 2, 3])->zip([4, 5, 6]); |
||
1093 | * => [[1, 4], [2, 5], [3, 6]] |
||
1094 | * |
||
1095 | * @param mixed ...$items |
||
1096 | * @return static |
||
1097 | */ |
||
1098 | 1 | public function zip($items) |
|
1114 | |||
1115 | /** |
||
1116 | * Pad collection to the specified length with a value. |
||
1117 | * |
||
1118 | * @param int $size |
||
1119 | * @param mixed $value |
||
1120 | * @return static |
||
1121 | */ |
||
1122 | 1 | public function pad($size, $value) |
|
1142 | |||
1143 | /** |
||
1144 | * Get the values iterator. |
||
1145 | * |
||
1146 | * @return \Traversable |
||
1147 | */ |
||
1148 | 159 | public function getIterator() |
|
1152 | |||
1153 | /** |
||
1154 | * Count the number of items in the collection. |
||
1155 | * |
||
1156 | * @return int |
||
1157 | */ |
||
1158 | 9 | public function count() |
|
1166 | |||
1167 | /** |
||
1168 | * Make an iterator from the given source. |
||
1169 | * |
||
1170 | * @param mixed $source |
||
1171 | * @return \Traversable |
||
1172 | */ |
||
1173 | 159 | protected function makeIterator($source) |
|
1185 | |||
1186 | /** |
||
1187 | * Explode the "value" and "key" arguments passed to "pluck". |
||
1188 | * |
||
1189 | * @param string|array $value |
||
1190 | * @param string|array|null $key |
||
1191 | * @return array |
||
1192 | */ |
||
1193 | protected function explodePluckParameters($value, $key) |
||
1194 | { |
||
1195 | $value = is_string($value) ? explode('.', $value) : $value; |
||
1196 | |||
1197 | $key = is_null($key) || is_array($key) ? $key : explode('.', $key); |
||
1198 | |||
1199 | return [$value, $key]; |
||
1200 | } |
||
1201 | |||
1202 | /** |
||
1203 | * Pass this lazy collection through a method on the collection class. |
||
1204 | * |
||
1205 | * @param string $method |
||
1206 | * @param array $params |
||
1207 | * @return static |
||
1208 | */ |
||
1209 | 59 | protected function passthru($method, array $params) |
|
1215 | } |
||
1216 |
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.