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 |
||
42 | class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable |
||
43 | { |
||
44 | use Macroable; |
||
45 | |||
46 | /** |
||
47 | * The items contained in the collection. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $items = []; |
||
52 | |||
53 | /** |
||
54 | * The methods that can be proxied. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected static $proxies = [ |
||
59 | 'average', 'avg', 'contains', 'each', 'every', 'filter', 'first', |
||
60 | 'flatMap', 'groupBy', 'keyBy', 'map', 'max', 'min', 'partition', |
||
61 | 'reject', 'sortBy', 'sortByDesc', 'sum', 'unique', |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * Create a new collection. |
||
66 | * |
||
67 | * @param mixed $items |
||
68 | * @return void |
||
|
|||
69 | */ |
||
70 | 216 | public function __construct($items = []) |
|
74 | |||
75 | /** |
||
76 | * Create a new collection instance if the value isn't one already. |
||
77 | * |
||
78 | * @param mixed $items |
||
79 | * @return static |
||
80 | */ |
||
81 | 10 | public static function make($items = []) |
|
85 | |||
86 | /** |
||
87 | * Wrap the given value in a collection if applicable. |
||
88 | * |
||
89 | * @param mixed $value |
||
90 | * @return static |
||
91 | */ |
||
92 | 7 | public static function wrap($value) |
|
98 | |||
99 | /** |
||
100 | * Get the underlying items from the given collection if applicable. |
||
101 | * |
||
102 | * @param array|static $value |
||
103 | * @return array |
||
104 | */ |
||
105 | 3 | public static function unwrap($value) |
|
109 | |||
110 | /** |
||
111 | * Create a new collection by invoking the callback a given amount of times. |
||
112 | * |
||
113 | * @param int $number |
||
114 | * @param callable $callback |
||
115 | * @return static |
||
116 | */ |
||
117 | 1 | public static function times($number, callable $callback = null) |
|
129 | |||
130 | /** |
||
131 | * Get all of the items in the collection. |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | 109 | public function all() |
|
139 | |||
140 | /** |
||
141 | * Get the average value of a given key. |
||
142 | * |
||
143 | * @param callable|string|null $callback |
||
144 | * @return mixed |
||
145 | */ |
||
146 | 4 | public function avg($callback = null) |
|
152 | |||
153 | /** |
||
154 | * Alias for the "avg" method. |
||
155 | * |
||
156 | * @param callable|string|null $callback |
||
157 | * @return mixed |
||
158 | */ |
||
159 | 3 | public function average($callback = null) |
|
163 | |||
164 | /** |
||
165 | * Get the median of a given key. |
||
166 | * |
||
167 | * @param null $key |
||
168 | * @return mixed |
||
169 | */ |
||
170 | 5 | public function median($key = null) |
|
191 | |||
192 | /** |
||
193 | * Get the mode of a given key. |
||
194 | * |
||
195 | * @param mixed $key |
||
196 | * @return array|null |
||
197 | */ |
||
198 | 4 | public function mode($key = null) |
|
222 | |||
223 | /** |
||
224 | * Collapse the collection of items into a single array. |
||
225 | * |
||
226 | * @return static |
||
227 | */ |
||
228 | 3 | public function collapse() |
|
232 | |||
233 | /** |
||
234 | * Determine if an item exists in the collection. |
||
235 | * |
||
236 | * @param mixed $key |
||
237 | * @param mixed $operator |
||
238 | * @param mixed $value |
||
239 | * @return bool |
||
240 | */ |
||
241 | 3 | public function contains($key, $operator = null, $value = null) |
|
255 | |||
256 | /** |
||
257 | * Determine if an item exists in the collection using strict comparison. |
||
258 | * |
||
259 | * @param mixed $key |
||
260 | * @param mixed $value |
||
261 | * @return bool |
||
262 | */ |
||
263 | 1 | public function containsStrict($key, $value = null) |
|
277 | |||
278 | /** |
||
279 | * Cross join with the given lists, returning all possible permutations. |
||
280 | * |
||
281 | * @param mixed ...$lists |
||
282 | * @return static |
||
283 | */ |
||
284 | 1 | public function crossJoin(...$lists) |
|
290 | |||
291 | /** |
||
292 | * Dump the collection and end the script. |
||
293 | * |
||
294 | * @return void |
||
295 | */ |
||
296 | public function dd(...$args) |
||
302 | |||
303 | /** |
||
304 | * Dump the collection. |
||
305 | * |
||
306 | * @return $this |
||
307 | */ |
||
308 | public function dump() |
||
318 | |||
319 | /** |
||
320 | * Get the items in the collection that are not present in the given items. |
||
321 | * |
||
322 | * @param mixed $items |
||
323 | * @return static |
||
324 | */ |
||
325 | 3 | public function diff($items) |
|
329 | |||
330 | /** |
||
331 | * Get the items in the collection that are not present in the given items. |
||
332 | * |
||
333 | * @param mixed $items |
||
334 | * @param callable $callback |
||
335 | * @return static |
||
336 | */ |
||
337 | 2 | public function diffUsing($items, callable $callback) |
|
341 | |||
342 | /** |
||
343 | * Get the items in the collection whose keys and values are not present in the given items. |
||
344 | * |
||
345 | * @param mixed $items |
||
346 | * @return static |
||
347 | */ |
||
348 | 2 | public function diffAssoc($items) |
|
352 | |||
353 | /** |
||
354 | * Get the items in the collection whose keys and values are not present in the given items. |
||
355 | * |
||
356 | * @param mixed $items |
||
357 | * @param callable $callback |
||
358 | * @return static |
||
359 | */ |
||
360 | 1 | public function diffAssocUsing($items, callable $callback) |
|
364 | |||
365 | /** |
||
366 | * Get the items in the collection whose keys are not present in the given items. |
||
367 | * |
||
368 | * @param mixed $items |
||
369 | * @return static |
||
370 | */ |
||
371 | 2 | public function diffKeys($items) |
|
375 | |||
376 | /** |
||
377 | * Get the items in the collection whose keys are not present in the given items. |
||
378 | * |
||
379 | * @param mixed $items |
||
380 | * @param callable $callback |
||
381 | * @return static |
||
382 | */ |
||
383 | 1 | public function diffKeysUsing($items, callable $callback) |
|
387 | |||
388 | /** |
||
389 | * Execute a callback over each item. |
||
390 | * |
||
391 | * @param callable $callback |
||
392 | * @return $this |
||
393 | */ |
||
394 | 7 | public function each(callable $callback) |
|
404 | |||
405 | /** |
||
406 | * Execute a callback over each nested chunk of items. |
||
407 | * |
||
408 | * @param callable $callback |
||
409 | * @return static |
||
410 | */ |
||
411 | 1 | public function eachSpread(callable $callback) |
|
419 | |||
420 | /** |
||
421 | * Determine if all items in the collection pass the given test. |
||
422 | * |
||
423 | * @param string|callable $key |
||
424 | * @param mixed $operator |
||
425 | * @param mixed $value |
||
426 | * @return bool |
||
427 | */ |
||
428 | 1 | public function every($key, $operator = null, $value = null) |
|
444 | |||
445 | /** |
||
446 | * Get all items except for those with the specified keys. |
||
447 | * |
||
448 | * @param \IlluminateAgnostic\Str\Support\Collection|mixed $keys |
||
449 | * @return static |
||
450 | */ |
||
451 | 2 | public function except($keys) |
|
461 | |||
462 | /** |
||
463 | * Run a filter over each of the items. |
||
464 | * |
||
465 | * @param callable|null $callback |
||
466 | * @return static |
||
467 | */ |
||
468 | 22 | public function filter(callable $callback = null) |
|
476 | |||
477 | /** |
||
478 | * Apply the callback if the value is truthy. |
||
479 | * |
||
480 | * @param bool $value |
||
481 | * @param callable $callback |
||
482 | * @param callable $default |
||
483 | * @return static|mixed |
||
484 | */ |
||
485 | 4 | public function when($value, callable $callback, callable $default = null) |
|
495 | |||
496 | /** |
||
497 | * Apply the callback if the value is falsy. |
||
498 | * |
||
499 | * @param bool $value |
||
500 | * @param callable $callback |
||
501 | * @param callable $default |
||
502 | * @return static|mixed |
||
503 | */ |
||
504 | 2 | public function unless($value, callable $callback, callable $default = null) |
|
508 | |||
509 | /** |
||
510 | * Filter items by the given key value pair. |
||
511 | * |
||
512 | * @param string $key |
||
513 | * @param mixed $operator |
||
514 | * @param mixed $value |
||
515 | * @return static |
||
516 | */ |
||
517 | 2 | public function where($key, $operator, $value = null) |
|
521 | |||
522 | /** |
||
523 | * Get an operator checker callback. |
||
524 | * |
||
525 | * @param string $key |
||
526 | * @param string $operator |
||
527 | * @param mixed $value |
||
528 | * @return \Closure |
||
529 | */ |
||
530 | 7 | protected function operatorForWhere($key, $operator, $value = null) |
|
564 | |||
565 | /** |
||
566 | * Filter items by the given key value pair using strict comparison. |
||
567 | * |
||
568 | * @param string $key |
||
569 | * @param mixed $value |
||
570 | * @return static |
||
571 | */ |
||
572 | 1 | public function whereStrict($key, $value) |
|
576 | |||
577 | /** |
||
578 | * Filter items by the given key value pair. |
||
579 | * |
||
580 | * @param string $key |
||
581 | * @param mixed $values |
||
582 | * @param bool $strict |
||
583 | * @return static |
||
584 | */ |
||
585 | 2 | View Code Duplication | public function whereIn($key, $values, $strict = false) |
593 | |||
594 | /** |
||
595 | * Filter items by the given key value pair using strict comparison. |
||
596 | * |
||
597 | * @param string $key |
||
598 | * @param mixed $values |
||
599 | * @return static |
||
600 | */ |
||
601 | 1 | public function whereInStrict($key, $values) |
|
605 | |||
606 | /** |
||
607 | * Filter items by the given key value pair. |
||
608 | * |
||
609 | * @param string $key |
||
610 | * @param mixed $values |
||
611 | * @param bool $strict |
||
612 | * @return static |
||
613 | */ |
||
614 | 2 | View Code Duplication | public function whereNotIn($key, $values, $strict = false) |
622 | |||
623 | /** |
||
624 | * Filter items by the given key value pair using strict comparison. |
||
625 | * |
||
626 | * @param string $key |
||
627 | * @param mixed $values |
||
628 | * @return static |
||
629 | */ |
||
630 | 1 | public function whereNotInStrict($key, $values) |
|
634 | |||
635 | /** |
||
636 | * Filter the items, removing any items that don't match the given type. |
||
637 | * |
||
638 | * @param string $type |
||
639 | * @return static |
||
640 | */ |
||
641 | 1 | public function whereInstanceOf($type) |
|
647 | |||
648 | /** |
||
649 | * Get the first item from the collection. |
||
650 | * |
||
651 | * @param callable|null $callback |
||
652 | * @param mixed $default |
||
653 | * @return mixed |
||
654 | */ |
||
655 | 11 | public function first(callable $callback = null, $default = null) |
|
659 | |||
660 | /** |
||
661 | * Get the first item by the given key value pair. |
||
662 | * |
||
663 | * @param string $key |
||
664 | * @param mixed $operator |
||
665 | * @param mixed $value |
||
666 | * @return static |
||
667 | */ |
||
668 | 1 | public function firstWhere($key, $operator, $value = null) |
|
672 | |||
673 | /** |
||
674 | * Get a flattened array of the items in the collection. |
||
675 | * |
||
676 | * @param int $depth |
||
677 | * @return static |
||
678 | */ |
||
679 | 3 | public function flatten($depth = INF) |
|
683 | |||
684 | /** |
||
685 | * Flip the items in the collection. |
||
686 | * |
||
687 | * @return static |
||
688 | */ |
||
689 | 1 | public function flip() |
|
693 | |||
694 | /** |
||
695 | * Remove an item from the collection by key. |
||
696 | * |
||
697 | * @param string|array $keys |
||
698 | * @return $this |
||
699 | */ |
||
700 | 2 | public function forget($keys) |
|
708 | |||
709 | /** |
||
710 | * Get an item from the collection by key. |
||
711 | * |
||
712 | * @param mixed $key |
||
713 | * @param mixed $default |
||
714 | * @return mixed |
||
715 | */ |
||
716 | 5 | public function get($key, $default = null) |
|
724 | |||
725 | /** |
||
726 | * Group an associative array by a field or using a callback. |
||
727 | * |
||
728 | * @param callable|string $groupBy |
||
729 | * @param bool $preserveKeys |
||
730 | * @return static |
||
731 | */ |
||
732 | 8 | public function groupBy($groupBy, $preserveKeys = false) |
|
770 | |||
771 | /** |
||
772 | * Key an associative array by a field or using a callback. |
||
773 | * |
||
774 | * @param callable|string $keyBy |
||
775 | * @return static |
||
776 | */ |
||
777 | 3 | public function keyBy($keyBy) |
|
795 | |||
796 | /** |
||
797 | * Determine if an item exists in the collection by key. |
||
798 | * |
||
799 | * @param mixed $key |
||
800 | * @return bool |
||
801 | */ |
||
802 | 2 | public function has($key) |
|
814 | |||
815 | /** |
||
816 | * Concatenate values of a given key as a string. |
||
817 | * |
||
818 | * @param string $value |
||
819 | * @param string $glue |
||
820 | * @return string |
||
821 | */ |
||
822 | 1 | public function implode($value, $glue = null) |
|
832 | |||
833 | /** |
||
834 | * Intersect the collection with the given items. |
||
835 | * |
||
836 | * @param mixed $items |
||
837 | * @return static |
||
838 | */ |
||
839 | 2 | public function intersect($items) |
|
843 | |||
844 | /** |
||
845 | * Intersect the collection with the given items by key. |
||
846 | * |
||
847 | * @param mixed $items |
||
848 | * @return static |
||
849 | */ |
||
850 | 2 | public function intersectByKeys($items) |
|
856 | |||
857 | /** |
||
858 | * Determine if the collection is empty or not. |
||
859 | * |
||
860 | * @return bool |
||
861 | */ |
||
862 | 7 | public function isEmpty() |
|
866 | |||
867 | /** |
||
868 | * Determine if the collection is not empty. |
||
869 | * |
||
870 | * @return bool |
||
871 | */ |
||
872 | 1 | public function isNotEmpty() |
|
876 | |||
877 | /** |
||
878 | * Determine if the given value is callable, but not a string. |
||
879 | * |
||
880 | * @param mixed $value |
||
881 | * @return bool |
||
882 | */ |
||
883 | 41 | protected function useAsCallable($value) |
|
887 | |||
888 | /** |
||
889 | * Get the keys of the collection items. |
||
890 | * |
||
891 | * @return static |
||
892 | */ |
||
893 | 6 | public function keys() |
|
897 | |||
898 | /** |
||
899 | * Get the last item from the collection. |
||
900 | * |
||
901 | * @param callable|null $callback |
||
902 | * @param mixed $default |
||
903 | * @return mixed |
||
904 | */ |
||
905 | 7 | public function last(callable $callback = null, $default = null) |
|
909 | |||
910 | /** |
||
911 | * Get the values of a given key. |
||
912 | * |
||
913 | * @param string|array $value |
||
914 | * @param string|null $key |
||
915 | * @return static |
||
916 | */ |
||
917 | 9 | public function pluck($value, $key = null) |
|
921 | |||
922 | /** |
||
923 | * Run a map over each of the items. |
||
924 | * |
||
925 | * @param callable $callback |
||
926 | * @return static |
||
927 | */ |
||
928 | 16 | public function map(callable $callback) |
|
936 | |||
937 | /** |
||
938 | * Run a map over each nested chunk of items. |
||
939 | * |
||
940 | * @param callable $callback |
||
941 | * @return static |
||
942 | */ |
||
943 | 1 | public function mapSpread(callable $callback) |
|
951 | |||
952 | /** |
||
953 | * Run a dictionary map over the items. |
||
954 | * |
||
955 | * The callback should return an associative array with a single key/value pair. |
||
956 | * |
||
957 | * @param callable $callback |
||
958 | * @return static |
||
959 | */ |
||
960 | 4 | public function mapToDictionary(callable $callback) |
|
980 | |||
981 | /** |
||
982 | * Run a grouping map over the items. |
||
983 | * |
||
984 | * The callback should return an associative array with a single key/value pair. |
||
985 | * |
||
986 | * @param callable $callback |
||
987 | * @return static |
||
988 | */ |
||
989 | 2 | public function mapToGroups(callable $callback) |
|
995 | |||
996 | /** |
||
997 | * Run an associative map over each of the items. |
||
998 | * |
||
999 | * The callback should return an associative array with a single key/value pair. |
||
1000 | * |
||
1001 | * @param callable $callback |
||
1002 | * @return static |
||
1003 | */ |
||
1004 | 4 | public function mapWithKeys(callable $callback) |
|
1018 | |||
1019 | /** |
||
1020 | * Map a collection and flatten the result by a single level. |
||
1021 | * |
||
1022 | * @param callable $callback |
||
1023 | * @return static |
||
1024 | */ |
||
1025 | 1 | public function flatMap(callable $callback) |
|
1029 | |||
1030 | /** |
||
1031 | * Map the values into a new class. |
||
1032 | * |
||
1033 | * @param string $class |
||
1034 | * @return static |
||
1035 | */ |
||
1036 | 1 | public function mapInto($class) |
|
1042 | |||
1043 | /** |
||
1044 | * Get the max value of a given key. |
||
1045 | * |
||
1046 | * @param callable|string|null $callback |
||
1047 | * @return mixed |
||
1048 | */ |
||
1049 | 1 | View Code Duplication | public function max($callback = null) |
1061 | |||
1062 | /** |
||
1063 | * Merge the collection with the given items. |
||
1064 | * |
||
1065 | * @param mixed $items |
||
1066 | * @return static |
||
1067 | */ |
||
1068 | 3 | public function merge($items) |
|
1072 | |||
1073 | /** |
||
1074 | * Create a collection by using this collection for keys and another for its values. |
||
1075 | * |
||
1076 | * @param mixed $values |
||
1077 | * @return static |
||
1078 | */ |
||
1079 | 2 | public function combine($values) |
|
1083 | |||
1084 | /** |
||
1085 | * Union the collection with the given items. |
||
1086 | * |
||
1087 | * @param mixed $items |
||
1088 | * @return static |
||
1089 | */ |
||
1090 | 3 | public function union($items) |
|
1094 | |||
1095 | /** |
||
1096 | * Get the min value of a given key. |
||
1097 | * |
||
1098 | * @param callable|string|null $callback |
||
1099 | * @return mixed |
||
1100 | */ |
||
1101 | 1 | View Code Duplication | public function min($callback = null) |
1113 | |||
1114 | /** |
||
1115 | * Create a new collection consisting of every n-th element. |
||
1116 | * |
||
1117 | * @param int $step |
||
1118 | * @param int $offset |
||
1119 | * @return static |
||
1120 | */ |
||
1121 | 1 | public function nth($step, $offset = 0) |
|
1137 | |||
1138 | /** |
||
1139 | * Get the items with the specified keys. |
||
1140 | * |
||
1141 | * @param mixed $keys |
||
1142 | * @return static |
||
1143 | */ |
||
1144 | 1 | public function only($keys) |
|
1158 | |||
1159 | /** |
||
1160 | * "Paginate" the collection by slicing it into a smaller collection. |
||
1161 | * |
||
1162 | * @param int $page |
||
1163 | * @param int $perPage |
||
1164 | * @return static |
||
1165 | */ |
||
1166 | 1 | public function forPage($page, $perPage) |
|
1172 | |||
1173 | /** |
||
1174 | * Partition the collection into two arrays using the given callback or key. |
||
1175 | * |
||
1176 | * @param callable|string $key |
||
1177 | * @param mixed $operator |
||
1178 | * @param mixed $value |
||
1179 | * @return static |
||
1180 | */ |
||
1181 | 7 | public function partition($key, $operator = null, $value = null) |
|
1195 | |||
1196 | /** |
||
1197 | * Pass the collection to the given callback and return the result. |
||
1198 | * |
||
1199 | * @param callable $callback |
||
1200 | * @return mixed |
||
1201 | */ |
||
1202 | 1 | public function pipe(callable $callback) |
|
1206 | |||
1207 | /** |
||
1208 | * Get and remove the last item from the collection. |
||
1209 | * |
||
1210 | * @return mixed |
||
1211 | */ |
||
1212 | 1 | public function pop() |
|
1216 | |||
1217 | /** |
||
1218 | * Push an item onto the beginning of the collection. |
||
1219 | * |
||
1220 | * @param mixed $value |
||
1221 | * @param mixed $key |
||
1222 | * @return $this |
||
1223 | */ |
||
1224 | 1 | public function prepend($value, $key = null) |
|
1230 | |||
1231 | /** |
||
1232 | * Push an item onto the end of the collection. |
||
1233 | * |
||
1234 | * @param mixed $value |
||
1235 | * @return $this |
||
1236 | */ |
||
1237 | 7 | public function push($value) |
|
1243 | |||
1244 | /** |
||
1245 | * Push all of the given items onto the collection. |
||
1246 | * |
||
1247 | * @param \Traversable|array $source |
||
1248 | * @return static |
||
1249 | */ |
||
1250 | 2 | public function concat($source) |
|
1260 | |||
1261 | /** |
||
1262 | * Get and remove an item from the collection. |
||
1263 | * |
||
1264 | * @param mixed $key |
||
1265 | * @param mixed $default |
||
1266 | * @return mixed |
||
1267 | */ |
||
1268 | 3 | public function pull($key, $default = null) |
|
1272 | |||
1273 | /** |
||
1274 | * Put an item in the collection by key. |
||
1275 | * |
||
1276 | * @param mixed $key |
||
1277 | * @param mixed $value |
||
1278 | * @return $this |
||
1279 | */ |
||
1280 | 3 | public function put($key, $value) |
|
1286 | |||
1287 | /** |
||
1288 | * Get one or a specified number of items randomly from the collection. |
||
1289 | * |
||
1290 | * @param int|null $number |
||
1291 | * @return static|mixed |
||
1292 | * |
||
1293 | * @throws \InvalidArgumentException |
||
1294 | */ |
||
1295 | 3 | public function random($number = null) |
|
1303 | |||
1304 | /** |
||
1305 | * Reduce the collection to a single value. |
||
1306 | * |
||
1307 | * @param callable $callback |
||
1308 | * @param mixed $initial |
||
1309 | * @return mixed |
||
1310 | */ |
||
1311 | 6 | public function reduce(callable $callback, $initial = null) |
|
1315 | |||
1316 | /** |
||
1317 | * Create a collection of all elements that do not pass a given truth test. |
||
1318 | * |
||
1319 | * @param callable|mixed $callback |
||
1320 | * @return static |
||
1321 | */ |
||
1322 | 8 | public function reject($callback) |
|
1334 | |||
1335 | /** |
||
1336 | * Reverse items order. |
||
1337 | * |
||
1338 | * @return static |
||
1339 | */ |
||
1340 | 1 | public function reverse() |
|
1344 | |||
1345 | /** |
||
1346 | * Search the collection for a given value and return the corresponding key if successful. |
||
1347 | * |
||
1348 | * @param mixed $value |
||
1349 | * @param bool $strict |
||
1350 | * @return mixed |
||
1351 | */ |
||
1352 | 2 | public function search($value, $strict = false) |
|
1366 | |||
1367 | /** |
||
1368 | * Get and remove the first item from the collection. |
||
1369 | * |
||
1370 | * @return mixed |
||
1371 | */ |
||
1372 | 1 | public function shift() |
|
1376 | |||
1377 | /** |
||
1378 | * Shuffle the items in the collection. |
||
1379 | * |
||
1380 | * @param int $seed |
||
1381 | * @return static |
||
1382 | */ |
||
1383 | 1 | public function shuffle($seed = null) |
|
1387 | |||
1388 | /** |
||
1389 | * Slice the underlying collection array. |
||
1390 | * |
||
1391 | * @param int $offset |
||
1392 | * @param int $length |
||
1393 | * @return static |
||
1394 | */ |
||
1395 | 10 | public function slice($offset, $length = null) |
|
1399 | |||
1400 | /** |
||
1401 | * Split a collection into a certain number of groups. |
||
1402 | * |
||
1403 | * @param int $numberOfGroups |
||
1404 | * @return static |
||
1405 | */ |
||
1406 | 4 | public function split($numberOfGroups) |
|
1416 | |||
1417 | /** |
||
1418 | * Chunk the underlying collection array. |
||
1419 | * |
||
1420 | * @param int $size |
||
1421 | * @return static |
||
1422 | */ |
||
1423 | 6 | public function chunk($size) |
|
1437 | |||
1438 | /** |
||
1439 | * Sort through each item with a callback. |
||
1440 | * |
||
1441 | * @param callable|null $callback |
||
1442 | * @return static |
||
1443 | */ |
||
1444 | 9 | public function sort(callable $callback = null) |
|
1454 | |||
1455 | /** |
||
1456 | * Sort the collection using the given callback. |
||
1457 | * |
||
1458 | * @param callable|string $callback |
||
1459 | * @param int $options |
||
1460 | * @param bool $descending |
||
1461 | * @return static |
||
1462 | */ |
||
1463 | 5 | public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
|
1488 | |||
1489 | /** |
||
1490 | * Sort the collection in descending order using the given callback. |
||
1491 | * |
||
1492 | * @param callable|string $callback |
||
1493 | * @param int $options |
||
1494 | * @return static |
||
1495 | */ |
||
1496 | 1 | public function sortByDesc($callback, $options = SORT_REGULAR) |
|
1500 | |||
1501 | /** |
||
1502 | * Sort the collection keys. |
||
1503 | * |
||
1504 | * @param int $options |
||
1505 | * @param bool $descending |
||
1506 | * @return static |
||
1507 | */ |
||
1508 | 2 | public function sortKeys($options = SORT_REGULAR, $descending = false) |
|
1516 | |||
1517 | /** |
||
1518 | * Sort the collection keys in descending order. |
||
1519 | * |
||
1520 | * @param int $options |
||
1521 | * @return static |
||
1522 | */ |
||
1523 | public function sortKeysDesc($options = SORT_REGULAR) |
||
1527 | |||
1528 | /** |
||
1529 | * Splice a portion of the underlying collection array. |
||
1530 | * |
||
1531 | * @param int $offset |
||
1532 | * @param int|null $length |
||
1533 | * @param mixed $replacement |
||
1534 | * @return static |
||
1535 | */ |
||
1536 | 1 | public function splice($offset, $length = null, $replacement = []) |
|
1544 | |||
1545 | /** |
||
1546 | * Get the sum of the given values. |
||
1547 | * |
||
1548 | * @param callable|string|null $callback |
||
1549 | * @return mixed |
||
1550 | */ |
||
1551 | 8 | public function sum($callback = null) |
|
1563 | |||
1564 | /** |
||
1565 | * Take the first or last {$limit} items. |
||
1566 | * |
||
1567 | * @param int $limit |
||
1568 | * @return static |
||
1569 | */ |
||
1570 | 2 | public function take($limit) |
|
1578 | |||
1579 | /** |
||
1580 | * Pass the collection to the given callback and then return it. |
||
1581 | * |
||
1582 | * @param callable $callback |
||
1583 | * @return $this |
||
1584 | */ |
||
1585 | 1 | public function tap(callable $callback) |
|
1591 | |||
1592 | /** |
||
1593 | * Transform each item in the collection using a callback. |
||
1594 | * |
||
1595 | * @param callable $callback |
||
1596 | * @return $this |
||
1597 | */ |
||
1598 | 1 | public function transform(callable $callback) |
|
1604 | |||
1605 | /** |
||
1606 | * Return only unique items from the collection array. |
||
1607 | * |
||
1608 | * @param string|callable|null $key |
||
1609 | * @param bool $strict |
||
1610 | * @return static |
||
1611 | */ |
||
1612 | 5 | public function unique($key = null, $strict = false) |
|
1626 | |||
1627 | /** |
||
1628 | * Return only unique items from the collection array using strict comparison. |
||
1629 | * |
||
1630 | * @param string|callable|null $key |
||
1631 | * @return static |
||
1632 | */ |
||
1633 | 1 | public function uniqueStrict($key = null) |
|
1637 | |||
1638 | /** |
||
1639 | * Reset the keys on the underlying array. |
||
1640 | * |
||
1641 | * @return static |
||
1642 | */ |
||
1643 | 32 | public function values() |
|
1647 | |||
1648 | /** |
||
1649 | * Get a value retrieving callback. |
||
1650 | * |
||
1651 | * @param string $value |
||
1652 | * @return callable |
||
1653 | */ |
||
1654 | 33 | protected function valueRetriever($value) |
|
1664 | |||
1665 | /** |
||
1666 | * Zip the collection together with one or more arrays. |
||
1667 | * |
||
1668 | * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); |
||
1669 | * => [[1, 4], [2, 5], [3, 6]] |
||
1670 | * |
||
1671 | * @param mixed ...$items |
||
1672 | * @return static |
||
1673 | */ |
||
1674 | 1 | public function zip($items) |
|
1686 | |||
1687 | /** |
||
1688 | * Pad collection to the specified length with a value. |
||
1689 | * |
||
1690 | * @param int $size |
||
1691 | * @param mixed $value |
||
1692 | * @return static |
||
1693 | */ |
||
1694 | 1 | public function pad($size, $value) |
|
1698 | |||
1699 | /** |
||
1700 | * Get the collection of items as a plain array. |
||
1701 | * |
||
1702 | * @return array |
||
1703 | */ |
||
1704 | 49 | public function toArray() |
|
1710 | |||
1711 | /** |
||
1712 | * Convert the object into something JSON serializable. |
||
1713 | * |
||
1714 | * @return array |
||
1715 | */ |
||
1716 | 2 | public function jsonSerialize() |
|
1730 | |||
1731 | /** |
||
1732 | * Get the collection of items as JSON. |
||
1733 | * |
||
1734 | * @param int $options |
||
1735 | * @return string |
||
1736 | */ |
||
1737 | 2 | public function toJson($options = 0) |
|
1741 | |||
1742 | /** |
||
1743 | * Get an iterator for the items. |
||
1744 | * |
||
1745 | * @return \ArrayIterator |
||
1746 | */ |
||
1747 | 5 | public function getIterator() |
|
1751 | |||
1752 | /** |
||
1753 | * Get a CachingIterator instance. |
||
1754 | * |
||
1755 | * @param int $flags |
||
1756 | * @return \CachingIterator |
||
1757 | */ |
||
1758 | 1 | public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING) |
|
1762 | |||
1763 | /** |
||
1764 | * Count the number of items in the collection. |
||
1765 | * |
||
1766 | * @return int |
||
1767 | */ |
||
1768 | 22 | public function count() |
|
1772 | |||
1773 | /** |
||
1774 | * Get a base Support collection instance from this collection. |
||
1775 | * |
||
1776 | * @return \IlluminateAgnostic\Str\Support\Collection |
||
1777 | */ |
||
1778 | public function toBase() |
||
1782 | |||
1783 | /** |
||
1784 | * Determine if an item exists at an offset. |
||
1785 | * |
||
1786 | * @param mixed $key |
||
1787 | * @return bool |
||
1788 | */ |
||
1789 | 16 | public function offsetExists($key) |
|
1793 | |||
1794 | /** |
||
1795 | * Get an item at a given offset. |
||
1796 | * |
||
1797 | * @param mixed $key |
||
1798 | * @return mixed |
||
1799 | */ |
||
1800 | 18 | public function offsetGet($key) |
|
1804 | |||
1805 | /** |
||
1806 | * Set the item at a given offset. |
||
1807 | * |
||
1808 | * @param mixed $key |
||
1809 | * @param mixed $value |
||
1810 | * @return void |
||
1811 | */ |
||
1812 | 31 | public function offsetSet($key, $value) |
|
1820 | |||
1821 | /** |
||
1822 | * Unset the item at a given offset. |
||
1823 | * |
||
1824 | * @param string $key |
||
1825 | * @return void |
||
1826 | */ |
||
1827 | 4 | public function offsetUnset($key) |
|
1831 | |||
1832 | /** |
||
1833 | * Convert the collection to its string representation. |
||
1834 | * |
||
1835 | * @return string |
||
1836 | */ |
||
1837 | 1 | public function __toString() |
|
1841 | |||
1842 | /** |
||
1843 | * Results array of items from Collection or Arrayable. |
||
1844 | * |
||
1845 | * @param mixed $items |
||
1846 | * @return array |
||
1847 | */ |
||
1848 | 216 | protected function getArrayableItems($items) |
|
1866 | |||
1867 | /** |
||
1868 | * Add a method to the list of proxied methods. |
||
1869 | * |
||
1870 | * @param string $method |
||
1871 | * @return void |
||
1872 | */ |
||
1873 | 1 | public static function proxy($method) |
|
1877 | |||
1878 | /** |
||
1879 | * Dynamically access collection proxies. |
||
1880 | * |
||
1881 | * @param string $key |
||
1882 | * @return mixed |
||
1883 | * |
||
1884 | * @throws \Exception |
||
1885 | */ |
||
1886 | 15 | public function __get($key) |
|
1894 | } |
||
1895 |
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.