Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
19 | class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable |
||
20 | { |
||
21 | use Macroable; |
||
22 | |||
23 | /** |
||
24 | * The items contained in the collection. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $items = []; |
||
29 | |||
30 | /** |
||
31 | * The methods that can be proxied. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected static $proxies = [ |
||
36 | 'average', 'avg', 'contains', 'each', 'every', 'filter', 'first', 'flatMap', |
||
37 | 'keyBy', 'map', 'partition', 'reject', 'sortBy', 'sortByDesc', 'sum', 'unique', |
||
38 | ]; |
||
39 | |||
40 | /** |
||
41 | * Create a new collection. |
||
42 | * |
||
43 | * @param mixed $items |
||
44 | * @return void |
||
|
|||
45 | */ |
||
46 | 215 | public function __construct($items = []) |
|
50 | |||
51 | /** |
||
52 | * Create a new collection instance if the value isn't one already. |
||
53 | * |
||
54 | * @param mixed $items |
||
55 | * @return static |
||
56 | */ |
||
57 | 10 | public static function make($items = []) |
|
61 | |||
62 | /** |
||
63 | * Wrap the given value in a collection if applicable. |
||
64 | * |
||
65 | * @param mixed $value |
||
66 | * @return static |
||
67 | */ |
||
68 | 7 | public static function wrap($value) |
|
74 | |||
75 | /** |
||
76 | * Get the underlying items from the given collection if applicable. |
||
77 | * |
||
78 | * @param array|static $value |
||
79 | * @return array |
||
80 | */ |
||
81 | 3 | public static function unwrap($value) |
|
85 | |||
86 | /** |
||
87 | * Create a new collection by invoking the callback a given amount of times. |
||
88 | * |
||
89 | * @param int $number |
||
90 | * @param callable $callback |
||
91 | * @return static |
||
92 | */ |
||
93 | 1 | public static function times($number, callable $callback = null) |
|
105 | |||
106 | /** |
||
107 | * Get all of the items in the collection. |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | 109 | public function all() |
|
115 | |||
116 | /** |
||
117 | * Get the average value of a given key. |
||
118 | * |
||
119 | * @param callable|string|null $callback |
||
120 | * @return mixed |
||
121 | */ |
||
122 | 4 | public function avg($callback = null) |
|
128 | |||
129 | /** |
||
130 | * Alias for the "avg" method. |
||
131 | * |
||
132 | * @param callable|string|null $callback |
||
133 | * @return mixed |
||
134 | */ |
||
135 | 3 | public function average($callback = null) |
|
139 | |||
140 | /** |
||
141 | * Get the median of a given key. |
||
142 | * |
||
143 | * @param null $key |
||
144 | * @return mixed |
||
145 | */ |
||
146 | 5 | public function median($key = null) |
|
167 | |||
168 | /** |
||
169 | * Get the mode of a given key. |
||
170 | * |
||
171 | * @param mixed $key |
||
172 | * @return array|null |
||
173 | */ |
||
174 | 4 | public function mode($key = null) |
|
198 | |||
199 | /** |
||
200 | * Collapse the collection of items into a single array. |
||
201 | * |
||
202 | * @return static |
||
203 | */ |
||
204 | 3 | public function collapse() |
|
208 | |||
209 | /** |
||
210 | * Determine if an item exists in the collection. |
||
211 | * |
||
212 | * @param mixed $key |
||
213 | * @param mixed $operator |
||
214 | * @param mixed $value |
||
215 | * @return bool |
||
216 | */ |
||
217 | 3 | public function contains($key, $operator = null, $value = null) |
|
231 | |||
232 | /** |
||
233 | * Determine if an item exists in the collection using strict comparison. |
||
234 | * |
||
235 | * @param mixed $key |
||
236 | * @param mixed $value |
||
237 | * @return bool |
||
238 | */ |
||
239 | 1 | public function containsStrict($key, $value = null) |
|
253 | |||
254 | /** |
||
255 | * Cross join with the given lists, returning all possible permutations. |
||
256 | * |
||
257 | * @param mixed ...$lists |
||
258 | * @return static |
||
259 | */ |
||
260 | 1 | public function crossJoin(...$lists) |
|
266 | |||
267 | /** |
||
268 | * Dump the collection and end the script. |
||
269 | * |
||
270 | * @return void |
||
271 | */ |
||
272 | public function dd(...$args) |
||
280 | |||
281 | /** |
||
282 | * Dump the collection. |
||
283 | * |
||
284 | * @return $this |
||
285 | */ |
||
286 | public function dump() |
||
296 | |||
297 | /** |
||
298 | * Get the items in the collection that are not present in the given items. |
||
299 | * |
||
300 | * @param mixed $items |
||
301 | * @return static |
||
302 | */ |
||
303 | 3 | public function diff($items) |
|
307 | |||
308 | /** |
||
309 | * Get the items in the collection that are not present in the given items. |
||
310 | * |
||
311 | * @param mixed $items |
||
312 | * @param callable $callback |
||
313 | * @return static |
||
314 | */ |
||
315 | 2 | public function diffUsing($items, callable $callback) |
|
319 | |||
320 | /** |
||
321 | * Get the items in the collection whose keys and values are not present in the given items. |
||
322 | * |
||
323 | * @param mixed $items |
||
324 | * @return static |
||
325 | */ |
||
326 | 2 | public function diffAssoc($items) |
|
330 | |||
331 | /** |
||
332 | * Get the items in the collection whose keys and values are not present in the given items. |
||
333 | * |
||
334 | * @param mixed $items |
||
335 | * @param callable $callback |
||
336 | * @return static |
||
337 | */ |
||
338 | 1 | public function diffAssocUsing($items, callable $callback) |
|
342 | |||
343 | /** |
||
344 | * Get the items in the collection whose keys are not present in the given items. |
||
345 | * |
||
346 | * @param mixed $items |
||
347 | * @return static |
||
348 | */ |
||
349 | 2 | public function diffKeys($items) |
|
353 | |||
354 | /** |
||
355 | * Get the items in the collection whose keys are not present in the given items. |
||
356 | * |
||
357 | * @param mixed $items |
||
358 | * @param callable $callback |
||
359 | * @return static |
||
360 | */ |
||
361 | 1 | public function diffKeysUsing($items, callable $callback) |
|
365 | |||
366 | /** |
||
367 | * Execute a callback over each item. |
||
368 | * |
||
369 | * @param callable $callback |
||
370 | * @return $this |
||
371 | */ |
||
372 | 7 | public function each(callable $callback) |
|
382 | |||
383 | /** |
||
384 | * Execute a callback over each nested chunk of items. |
||
385 | * |
||
386 | * @param callable $callback |
||
387 | * @return static |
||
388 | */ |
||
389 | public function eachSpread(callable $callback) |
||
397 | |||
398 | /** |
||
399 | * Determine if all items in the collection pass the given test. |
||
400 | * |
||
401 | * @param string|callable $key |
||
402 | * @param mixed $operator |
||
403 | * @param mixed $value |
||
404 | * @return bool |
||
405 | */ |
||
406 | 1 | public function every($key, $operator = null, $value = null) |
|
422 | |||
423 | /** |
||
424 | * Get all items except for those with the specified keys. |
||
425 | * |
||
426 | * @param \IlluminateAgnostic\Arr\Support\Collection|mixed $keys |
||
427 | * @return static |
||
428 | */ |
||
429 | 2 | public function except($keys) |
|
439 | |||
440 | /** |
||
441 | * Run a filter over each of the items. |
||
442 | * |
||
443 | * @param callable|null $callback |
||
444 | * @return static |
||
445 | */ |
||
446 | 22 | public function filter(callable $callback = null) |
|
454 | |||
455 | /** |
||
456 | * Apply the callback if the value is truthy. |
||
457 | * |
||
458 | * @param bool $value |
||
459 | * @param callable $callback |
||
460 | * @param callable $default |
||
461 | * @return mixed |
||
462 | */ |
||
463 | 4 | public function when($value, callable $callback, callable $default = null) |
|
473 | |||
474 | /** |
||
475 | * Apply the callback if the value is falsy. |
||
476 | * |
||
477 | * @param bool $value |
||
478 | * @param callable $callback |
||
479 | * @param callable $default |
||
480 | * @return mixed |
||
481 | */ |
||
482 | 2 | public function unless($value, callable $callback, callable $default = null) |
|
486 | |||
487 | /** |
||
488 | * Filter items by the given key value pair. |
||
489 | * |
||
490 | * @param string $key |
||
491 | * @param mixed $operator |
||
492 | * @param mixed $value |
||
493 | * @return static |
||
494 | */ |
||
495 | 2 | public function where($key, $operator, $value = null) |
|
499 | |||
500 | /** |
||
501 | * Get an operator checker callback. |
||
502 | * |
||
503 | * @param string $key |
||
504 | * @param string $operator |
||
505 | * @param mixed $value |
||
506 | * @return \Closure |
||
507 | */ |
||
508 | 7 | protected function operatorForWhere($key, $operator, $value = null) |
|
542 | |||
543 | /** |
||
544 | * Filter items by the given key value pair using strict comparison. |
||
545 | * |
||
546 | * @param string $key |
||
547 | * @param mixed $value |
||
548 | * @return static |
||
549 | */ |
||
550 | 1 | public function whereStrict($key, $value) |
|
554 | |||
555 | /** |
||
556 | * Filter items by the given key value pair. |
||
557 | * |
||
558 | * @param string $key |
||
559 | * @param mixed $values |
||
560 | * @param bool $strict |
||
561 | * @return static |
||
562 | */ |
||
563 | 2 | View Code Duplication | public function whereIn($key, $values, $strict = false) |
571 | |||
572 | /** |
||
573 | * Filter items by the given key value pair using strict comparison. |
||
574 | * |
||
575 | * @param string $key |
||
576 | * @param mixed $values |
||
577 | * @return static |
||
578 | */ |
||
579 | 1 | public function whereInStrict($key, $values) |
|
583 | |||
584 | /** |
||
585 | * Filter items by the given key value pair. |
||
586 | * |
||
587 | * @param string $key |
||
588 | * @param mixed $values |
||
589 | * @param bool $strict |
||
590 | * @return static |
||
591 | */ |
||
592 | 2 | View Code Duplication | public function whereNotIn($key, $values, $strict = false) |
600 | |||
601 | /** |
||
602 | * Filter items by the given key value pair using strict comparison. |
||
603 | * |
||
604 | * @param string $key |
||
605 | * @param mixed $values |
||
606 | * @return static |
||
607 | */ |
||
608 | 1 | public function whereNotInStrict($key, $values) |
|
612 | |||
613 | /** |
||
614 | * Filter the items, removing any items that don't match the given type. |
||
615 | * |
||
616 | * @param string $type |
||
617 | * @return static |
||
618 | */ |
||
619 | public function whereInstanceOf($type) |
||
625 | |||
626 | /** |
||
627 | * Get the first item from the collection. |
||
628 | * |
||
629 | * @param callable|null $callback |
||
630 | * @param mixed $default |
||
631 | * @return mixed |
||
632 | */ |
||
633 | 11 | public function first(callable $callback = null, $default = null) |
|
637 | |||
638 | /** |
||
639 | * Get the first item by the given key value pair. |
||
640 | * |
||
641 | * @param string $key |
||
642 | * @param mixed $operator |
||
643 | * @param mixed $value |
||
644 | * @return static |
||
645 | */ |
||
646 | 1 | public function firstWhere($key, $operator, $value = null) |
|
650 | |||
651 | /** |
||
652 | * Get a flattened array of the items in the collection. |
||
653 | * |
||
654 | * @param int $depth |
||
655 | * @return static |
||
656 | */ |
||
657 | 3 | public function flatten($depth = INF) |
|
661 | |||
662 | /** |
||
663 | * Flip the items in the collection. |
||
664 | * |
||
665 | * @return static |
||
666 | */ |
||
667 | 1 | public function flip() |
|
671 | |||
672 | /** |
||
673 | * Remove an item from the collection by key. |
||
674 | * |
||
675 | * @param string|array $keys |
||
676 | * @return $this |
||
677 | */ |
||
678 | 2 | public function forget($keys) |
|
686 | |||
687 | /** |
||
688 | * Get an item from the collection by key. |
||
689 | * |
||
690 | * @param mixed $key |
||
691 | * @param mixed $default |
||
692 | * @return mixed |
||
693 | */ |
||
694 | 5 | public function get($key, $default = null) |
|
702 | |||
703 | /** |
||
704 | * Group an associative array by a field or using a callback. |
||
705 | * |
||
706 | * @param callable|string $groupBy |
||
707 | * @param bool $preserveKeys |
||
708 | * @return static |
||
709 | */ |
||
710 | 7 | public function groupBy($groupBy, $preserveKeys = false) |
|
748 | |||
749 | /** |
||
750 | * Key an associative array by a field or using a callback. |
||
751 | * |
||
752 | * @param callable|string $keyBy |
||
753 | * @return static |
||
754 | */ |
||
755 | 3 | public function keyBy($keyBy) |
|
773 | |||
774 | /** |
||
775 | * Determine if an item exists in the collection by key. |
||
776 | * |
||
777 | * @param mixed $key |
||
778 | * @return bool |
||
779 | */ |
||
780 | 2 | public function has($key) |
|
792 | |||
793 | /** |
||
794 | * Concatenate values of a given key as a string. |
||
795 | * |
||
796 | * @param string $value |
||
797 | * @param string $glue |
||
798 | * @return string |
||
799 | */ |
||
800 | 1 | public function implode($value, $glue = null) |
|
810 | |||
811 | /** |
||
812 | * Intersect the collection with the given items. |
||
813 | * |
||
814 | * @param mixed $items |
||
815 | * @return static |
||
816 | */ |
||
817 | 2 | public function intersect($items) |
|
821 | |||
822 | /** |
||
823 | * Intersect the collection with the given items by key. |
||
824 | * |
||
825 | * @param mixed $items |
||
826 | * @return static |
||
827 | */ |
||
828 | 2 | public function intersectByKeys($items) |
|
834 | |||
835 | /** |
||
836 | * Determine if the collection is empty or not. |
||
837 | * |
||
838 | * @return bool |
||
839 | */ |
||
840 | 7 | public function isEmpty() |
|
844 | |||
845 | /** |
||
846 | * Determine if the collection is not empty. |
||
847 | * |
||
848 | * @return bool |
||
849 | */ |
||
850 | 1 | public function isNotEmpty() |
|
854 | |||
855 | /** |
||
856 | * Determine if the given value is callable, but not a string. |
||
857 | * |
||
858 | * @param mixed $value |
||
859 | * @return bool |
||
860 | */ |
||
861 | 40 | protected function useAsCallable($value) |
|
865 | |||
866 | /** |
||
867 | * Get the keys of the collection items. |
||
868 | * |
||
869 | * @return static |
||
870 | */ |
||
871 | 6 | public function keys() |
|
875 | |||
876 | /** |
||
877 | * Get the last item from the collection. |
||
878 | * |
||
879 | * @param callable|null $callback |
||
880 | * @param mixed $default |
||
881 | * @return mixed |
||
882 | */ |
||
883 | 7 | public function last(callable $callback = null, $default = null) |
|
887 | |||
888 | /** |
||
889 | * Get the values of a given key. |
||
890 | * |
||
891 | * @param string|array $value |
||
892 | * @param string|null $key |
||
893 | * @return static |
||
894 | */ |
||
895 | 9 | public function pluck($value, $key = null) |
|
899 | |||
900 | /** |
||
901 | * Run a map over each of the items. |
||
902 | * |
||
903 | * @param callable $callback |
||
904 | * @return static |
||
905 | */ |
||
906 | 16 | public function map(callable $callback) |
|
914 | |||
915 | /** |
||
916 | * Run a map over each nested chunk of items. |
||
917 | * |
||
918 | * @param callable $callback |
||
919 | * @return static |
||
920 | */ |
||
921 | public function mapSpread(callable $callback) |
||
929 | |||
930 | /** |
||
931 | * Run a dictionary map over the items. |
||
932 | * |
||
933 | * The callback should return an associative array with a single key/value pair. |
||
934 | * |
||
935 | * @param callable $callback |
||
936 | * @return static |
||
937 | */ |
||
938 | 4 | public function mapToDictionary(callable $callback) |
|
958 | |||
959 | /** |
||
960 | * Run a grouping map over the items. |
||
961 | * |
||
962 | * The callback should return an associative array with a single key/value pair. |
||
963 | * |
||
964 | * @param callable $callback |
||
965 | * @return static |
||
966 | */ |
||
967 | 2 | public function mapToGroups(callable $callback) |
|
973 | |||
974 | /** |
||
975 | * Run an associative map over each of the items. |
||
976 | * |
||
977 | * The callback should return an associative array with a single key/value pair. |
||
978 | * |
||
979 | * @param callable $callback |
||
980 | * @return static |
||
981 | */ |
||
982 | 4 | public function mapWithKeys(callable $callback) |
|
996 | |||
997 | /** |
||
998 | * Map a collection and flatten the result by a single level. |
||
999 | * |
||
1000 | * @param callable $callback |
||
1001 | * @return static |
||
1002 | */ |
||
1003 | 1 | public function flatMap(callable $callback) |
|
1007 | |||
1008 | /** |
||
1009 | * Map the values into a new class. |
||
1010 | * |
||
1011 | * @param string $class |
||
1012 | * @return static |
||
1013 | */ |
||
1014 | public function mapInto($class) |
||
1020 | |||
1021 | /** |
||
1022 | * Get the max value of a given key. |
||
1023 | * |
||
1024 | * @param callable|string|null $callback |
||
1025 | * @return mixed |
||
1026 | */ |
||
1027 | 1 | View Code Duplication | public function max($callback = null) |
1039 | |||
1040 | /** |
||
1041 | * Merge the collection with the given items. |
||
1042 | * |
||
1043 | * @param mixed $items |
||
1044 | * @return static |
||
1045 | */ |
||
1046 | 3 | public function merge($items) |
|
1050 | |||
1051 | /** |
||
1052 | * Create a collection by using this collection for keys and another for its values. |
||
1053 | * |
||
1054 | * @param mixed $values |
||
1055 | * @return static |
||
1056 | */ |
||
1057 | 2 | public function combine($values) |
|
1061 | |||
1062 | /** |
||
1063 | * Union the collection with the given items. |
||
1064 | * |
||
1065 | * @param mixed $items |
||
1066 | * @return static |
||
1067 | */ |
||
1068 | 3 | public function union($items) |
|
1072 | |||
1073 | /** |
||
1074 | * Get the min value of a given key. |
||
1075 | * |
||
1076 | * @param callable|string|null $callback |
||
1077 | * @return mixed |
||
1078 | */ |
||
1079 | 1 | View Code Duplication | public function min($callback = null) |
1091 | |||
1092 | /** |
||
1093 | * Create a new collection consisting of every n-th element. |
||
1094 | * |
||
1095 | * @param int $step |
||
1096 | * @param int $offset |
||
1097 | * @return static |
||
1098 | */ |
||
1099 | 1 | public function nth($step, $offset = 0) |
|
1115 | |||
1116 | /** |
||
1117 | * Get the items with the specified keys. |
||
1118 | * |
||
1119 | * @param mixed $keys |
||
1120 | * @return static |
||
1121 | */ |
||
1122 | 1 | public function only($keys) |
|
1136 | |||
1137 | /** |
||
1138 | * "Paginate" the collection by slicing it into a smaller collection. |
||
1139 | * |
||
1140 | * @param int $page |
||
1141 | * @param int $perPage |
||
1142 | * @return static |
||
1143 | */ |
||
1144 | 1 | public function forPage($page, $perPage) |
|
1150 | |||
1151 | /** |
||
1152 | * Partition the collection into two arrays using the given callback or key. |
||
1153 | * |
||
1154 | * @param callable|string $key |
||
1155 | * @param mixed $operator |
||
1156 | * @param mixed $value |
||
1157 | * @return static |
||
1158 | */ |
||
1159 | 7 | public function partition($key, $operator = null, $value = null) |
|
1173 | |||
1174 | /** |
||
1175 | * Pass the collection to the given callback and return the result. |
||
1176 | * |
||
1177 | * @param callable $callback |
||
1178 | * @return mixed |
||
1179 | */ |
||
1180 | 1 | public function pipe(callable $callback) |
|
1184 | |||
1185 | /** |
||
1186 | * Get and remove the last item from the collection. |
||
1187 | * |
||
1188 | * @return mixed |
||
1189 | */ |
||
1190 | 1 | public function pop() |
|
1194 | |||
1195 | /** |
||
1196 | * Push an item onto the beginning of the collection. |
||
1197 | * |
||
1198 | * @param mixed $value |
||
1199 | * @param mixed $key |
||
1200 | * @return $this |
||
1201 | */ |
||
1202 | 1 | public function prepend($value, $key = null) |
|
1208 | |||
1209 | /** |
||
1210 | * Push an item onto the end of the collection. |
||
1211 | * |
||
1212 | * @param mixed $value |
||
1213 | * @return $this |
||
1214 | */ |
||
1215 | 7 | public function push($value) |
|
1221 | |||
1222 | /** |
||
1223 | * Push all of the given items onto the collection. |
||
1224 | * |
||
1225 | * @param \Traversable $source |
||
1226 | * @return $this |
||
1227 | */ |
||
1228 | 2 | public function concat($source) |
|
1238 | |||
1239 | /** |
||
1240 | * Get and remove an item from the collection. |
||
1241 | * |
||
1242 | * @param mixed $key |
||
1243 | * @param mixed $default |
||
1244 | * @return mixed |
||
1245 | */ |
||
1246 | 3 | public function pull($key, $default = null) |
|
1250 | |||
1251 | /** |
||
1252 | * Put an item in the collection by key. |
||
1253 | * |
||
1254 | * @param mixed $key |
||
1255 | * @param mixed $value |
||
1256 | * @return $this |
||
1257 | */ |
||
1258 | 3 | public function put($key, $value) |
|
1264 | |||
1265 | /** |
||
1266 | * Get one or a specified number of items randomly from the collection. |
||
1267 | * |
||
1268 | * @param int|null $number |
||
1269 | * @return mixed |
||
1270 | * |
||
1271 | * @throws \InvalidArgumentException |
||
1272 | */ |
||
1273 | 3 | public function random($number = null) |
|
1281 | |||
1282 | /** |
||
1283 | * Reduce the collection to a single value. |
||
1284 | * |
||
1285 | * @param callable $callback |
||
1286 | * @param mixed $initial |
||
1287 | * @return mixed |
||
1288 | */ |
||
1289 | 6 | public function reduce(callable $callback, $initial = null) |
|
1293 | |||
1294 | /** |
||
1295 | * Create a collection of all elements that do not pass a given truth test. |
||
1296 | * |
||
1297 | * @param callable|mixed $callback |
||
1298 | * @return static |
||
1299 | */ |
||
1300 | 8 | public function reject($callback) |
|
1312 | |||
1313 | /** |
||
1314 | * Reverse items order. |
||
1315 | * |
||
1316 | * @return static |
||
1317 | */ |
||
1318 | 1 | public function reverse() |
|
1322 | |||
1323 | /** |
||
1324 | * Search the collection for a given value and return the corresponding key if successful. |
||
1325 | * |
||
1326 | * @param mixed $value |
||
1327 | * @param bool $strict |
||
1328 | * @return mixed |
||
1329 | */ |
||
1330 | 2 | public function search($value, $strict = false) |
|
1344 | |||
1345 | /** |
||
1346 | * Get and remove the first item from the collection. |
||
1347 | * |
||
1348 | * @return mixed |
||
1349 | */ |
||
1350 | 1 | public function shift() |
|
1354 | |||
1355 | /** |
||
1356 | * Shuffle the items in the collection. |
||
1357 | * |
||
1358 | * @param int $seed |
||
1359 | * @return static |
||
1360 | */ |
||
1361 | 1 | public function shuffle($seed = null) |
|
1377 | |||
1378 | /** |
||
1379 | * Slice the underlying collection array. |
||
1380 | * |
||
1381 | * @param int $offset |
||
1382 | * @param int $length |
||
1383 | * @return static |
||
1384 | */ |
||
1385 | 10 | public function slice($offset, $length = null) |
|
1389 | |||
1390 | /** |
||
1391 | * Split a collection into a certain number of groups. |
||
1392 | * |
||
1393 | * @param int $numberOfGroups |
||
1394 | * @return static |
||
1395 | */ |
||
1396 | 4 | public function split($numberOfGroups) |
|
1406 | |||
1407 | /** |
||
1408 | * Chunk the underlying collection array. |
||
1409 | * |
||
1410 | * @param int $size |
||
1411 | * @return static |
||
1412 | */ |
||
1413 | 6 | public function chunk($size) |
|
1427 | |||
1428 | /** |
||
1429 | * Sort through each item with a callback. |
||
1430 | * |
||
1431 | * @param callable|null $callback |
||
1432 | * @return static |
||
1433 | */ |
||
1434 | 9 | public function sort(callable $callback = null) |
|
1444 | |||
1445 | /** |
||
1446 | * Sort the collection using the given callback. |
||
1447 | * |
||
1448 | * @param callable|string $callback |
||
1449 | * @param int $options |
||
1450 | * @param bool $descending |
||
1451 | * @return static |
||
1452 | */ |
||
1453 | 5 | public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
|
1478 | |||
1479 | /** |
||
1480 | * Sort the collection in descending order using the given callback. |
||
1481 | * |
||
1482 | * @param callable|string $callback |
||
1483 | * @param int $options |
||
1484 | * @return static |
||
1485 | */ |
||
1486 | 1 | public function sortByDesc($callback, $options = SORT_REGULAR) |
|
1490 | |||
1491 | /** |
||
1492 | * Sort the collection keys. |
||
1493 | * |
||
1494 | * @param int $options |
||
1495 | * @param bool $descending |
||
1496 | * @return static |
||
1497 | */ |
||
1498 | 2 | public function sortKeys($options = SORT_REGULAR, $descending = false) |
|
1506 | |||
1507 | /** |
||
1508 | * Sort the collection keys in descending order. |
||
1509 | * |
||
1510 | * @param int $options |
||
1511 | * @return static |
||
1512 | */ |
||
1513 | public function sortKeysDesc($options = SORT_REGULAR) |
||
1517 | |||
1518 | /** |
||
1519 | * Splice a portion of the underlying collection array. |
||
1520 | * |
||
1521 | * @param int $offset |
||
1522 | * @param int|null $length |
||
1523 | * @param mixed $replacement |
||
1524 | * @return static |
||
1525 | */ |
||
1526 | 1 | public function splice($offset, $length = null, $replacement = []) |
|
1534 | |||
1535 | /** |
||
1536 | * Get the sum of the given values. |
||
1537 | * |
||
1538 | * @param callable|string|null $callback |
||
1539 | * @return mixed |
||
1540 | */ |
||
1541 | 8 | public function sum($callback = null) |
|
1553 | |||
1554 | /** |
||
1555 | * Take the first or last {$limit} items. |
||
1556 | * |
||
1557 | * @param int $limit |
||
1558 | * @return static |
||
1559 | */ |
||
1560 | 2 | public function take($limit) |
|
1568 | |||
1569 | /** |
||
1570 | * Pass the collection to the given callback and then return it. |
||
1571 | * |
||
1572 | * @param callable $callback |
||
1573 | * @return $this |
||
1574 | */ |
||
1575 | 1 | public function tap(callable $callback) |
|
1581 | |||
1582 | /** |
||
1583 | * Transform each item in the collection using a callback. |
||
1584 | * |
||
1585 | * @param callable $callback |
||
1586 | * @return $this |
||
1587 | */ |
||
1588 | 1 | public function transform(callable $callback) |
|
1594 | |||
1595 | /** |
||
1596 | * Return only unique items from the collection array. |
||
1597 | * |
||
1598 | * @param string|callable|null $key |
||
1599 | * @param bool $strict |
||
1600 | * @return static |
||
1601 | */ |
||
1602 | 5 | public function unique($key = null, $strict = false) |
|
1616 | |||
1617 | /** |
||
1618 | * Return only unique items from the collection array using strict comparison. |
||
1619 | * |
||
1620 | * @param string|callable|null $key |
||
1621 | * @return static |
||
1622 | */ |
||
1623 | 1 | public function uniqueStrict($key = null) |
|
1627 | |||
1628 | /** |
||
1629 | * Reset the keys on the underlying array. |
||
1630 | * |
||
1631 | * @return static |
||
1632 | */ |
||
1633 | 32 | public function values() |
|
1637 | |||
1638 | /** |
||
1639 | * Get a value retrieving callback. |
||
1640 | * |
||
1641 | * @param string $value |
||
1642 | * @return callable |
||
1643 | */ |
||
1644 | 32 | protected function valueRetriever($value) |
|
1654 | |||
1655 | /** |
||
1656 | * Zip the collection together with one or more arrays. |
||
1657 | * |
||
1658 | * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); |
||
1659 | * => [[1, 4], [2, 5], [3, 6]] |
||
1660 | * |
||
1661 | * @param mixed ...$items |
||
1662 | * @return static |
||
1663 | */ |
||
1664 | public function zip($items) |
||
1676 | |||
1677 | /** |
||
1678 | * Pad collection to the specified length with a value. |
||
1679 | * |
||
1680 | * @param int $size |
||
1681 | * @param mixed $value |
||
1682 | * @return static |
||
1683 | */ |
||
1684 | 1 | public function pad($size, $value) |
|
1688 | |||
1689 | /** |
||
1690 | * Get the collection of items as a plain array. |
||
1691 | * |
||
1692 | * @return array |
||
1693 | */ |
||
1694 | public function toArray() |
||
1700 | |||
1701 | /** |
||
1702 | * Convert the object into something JSON serializable. |
||
1703 | * |
||
1704 | * @return array |
||
1705 | */ |
||
1706 | public function jsonSerialize() |
||
1720 | |||
1721 | /** |
||
1722 | * Get the collection of items as JSON. |
||
1723 | * |
||
1724 | * @param int $options |
||
1725 | * @return string |
||
1726 | */ |
||
1727 | 2 | public function toJson($options = 0) |
|
1731 | |||
1732 | /** |
||
1733 | * Get an iterator for the items. |
||
1734 | * |
||
1735 | * @return \ArrayIterator |
||
1736 | */ |
||
1737 | 5 | public function getIterator() |
|
1741 | |||
1742 | /** |
||
1743 | * Get a CachingIterator instance. |
||
1744 | * |
||
1745 | * @param int $flags |
||
1746 | * @return \CachingIterator |
||
1747 | */ |
||
1748 | 1 | public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING) |
|
1752 | |||
1753 | /** |
||
1754 | * Count the number of items in the collection. |
||
1755 | * |
||
1756 | * @return int |
||
1757 | */ |
||
1758 | 22 | public function count() |
|
1762 | |||
1763 | /** |
||
1764 | * Get a base Support collection instance from this collection. |
||
1765 | * |
||
1766 | * @return \IlluminateAgnostic\Arr\Support\Collection |
||
1767 | */ |
||
1768 | public function toBase() |
||
1772 | |||
1773 | /** |
||
1774 | * Determine if an item exists at an offset. |
||
1775 | * |
||
1776 | * @param mixed $key |
||
1777 | * @return bool |
||
1778 | */ |
||
1779 | 16 | public function offsetExists($key) |
|
1783 | |||
1784 | /** |
||
1785 | * Get an item at a given offset. |
||
1786 | * |
||
1787 | * @param mixed $key |
||
1788 | * @return mixed |
||
1789 | */ |
||
1790 | 17 | public function offsetGet($key) |
|
1794 | |||
1795 | /** |
||
1796 | * Set the item at a given offset. |
||
1797 | * |
||
1798 | * @param mixed $key |
||
1799 | * @param mixed $value |
||
1800 | * @return void |
||
1801 | */ |
||
1802 | 30 | public function offsetSet($key, $value) |
|
1810 | |||
1811 | /** |
||
1812 | * Unset the item at a given offset. |
||
1813 | * |
||
1814 | * @param string $key |
||
1815 | * @return void |
||
1816 | */ |
||
1817 | 4 | public function offsetUnset($key) |
|
1821 | |||
1822 | /** |
||
1823 | * Convert the collection to its string representation. |
||
1824 | * |
||
1825 | * @return string |
||
1826 | */ |
||
1827 | 1 | public function __toString() |
|
1831 | |||
1832 | /** |
||
1833 | * Results array of items from Collection or Arrayable. |
||
1834 | * |
||
1835 | * @param mixed $items |
||
1836 | * @return array |
||
1837 | */ |
||
1838 | 215 | protected function getArrayableItems($items) |
|
1856 | |||
1857 | /** |
||
1858 | * Add a method to the list of proxied methods. |
||
1859 | * |
||
1860 | * @param string $method |
||
1861 | * @return void |
||
1862 | */ |
||
1863 | 1 | public static function proxy($method) |
|
1867 | |||
1868 | /** |
||
1869 | * Dynamically access collection proxies. |
||
1870 | * |
||
1871 | * @param string $key |
||
1872 | * @return mixed |
||
1873 | * |
||
1874 | * @throws \Exception |
||
1875 | */ |
||
1876 | 12 | public function __get($key) |
|
1884 | } |
||
1885 |
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.