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 |
||
40 | class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable |
||
41 | { |
||
42 | use Macroable; |
||
43 | |||
44 | /** |
||
45 | * The items contained in the collection. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $items = []; |
||
50 | |||
51 | /** |
||
52 | * The methods that can be proxied. |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected static $proxies = [ |
||
57 | 'average', 'avg', 'contains', 'each', 'every', 'filter', 'first', |
||
58 | 'flatMap', 'groupBy', 'keyBy', 'map', 'max', 'min', 'partition', |
||
59 | 'reject', 'some', 'sortBy', 'sortByDesc', 'sum', 'unique', |
||
60 | ]; |
||
61 | |||
62 | /** |
||
63 | * Create a new collection. |
||
64 | * |
||
65 | * @param mixed $items |
||
66 | * @return void |
||
|
|||
67 | */ |
||
68 | public function __construct($items = []) |
||
72 | 238 | ||
73 | 238 | /** |
|
74 | * Create a new collection instance if the value isn't one already. |
||
75 | * |
||
76 | * @param mixed $items |
||
77 | * @return static |
||
78 | */ |
||
79 | public static function make($items = []) |
||
83 | 10 | ||
84 | /** |
||
85 | * Wrap the given value in a collection if applicable. |
||
86 | * |
||
87 | * @param mixed $value |
||
88 | * @return static |
||
89 | */ |
||
90 | public static function wrap($value) |
||
96 | 7 | ||
97 | /** |
||
98 | * Get the underlying items from the given collection if applicable. |
||
99 | * |
||
100 | * @param array|static $value |
||
101 | * @return array |
||
102 | */ |
||
103 | public static function unwrap($value) |
||
107 | 3 | ||
108 | /** |
||
109 | * Create a new collection by invoking the callback a given amount of times. |
||
110 | * |
||
111 | * @param int $number |
||
112 | * @param callable $callback |
||
113 | * @return static |
||
114 | */ |
||
115 | public static function times($number, callable $callback = null) |
||
127 | 1 | ||
128 | /** |
||
129 | * Get all of the items in the collection. |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | public function all() |
||
137 | 116 | ||
138 | /** |
||
139 | * Get the average value of a given key. |
||
140 | * |
||
141 | * @param callable|string|null $callback |
||
142 | * @return mixed |
||
143 | */ |
||
144 | public function avg($callback = null) |
||
158 | |||
159 | 1 | /** |
|
160 | * Alias for the "avg" method. |
||
161 | * |
||
162 | * @param callable|string|null $callback |
||
163 | * @return mixed |
||
164 | */ |
||
165 | public function average($callback = null) |
||
169 | 3 | ||
170 | /** |
||
171 | * Get the median of a given key. |
||
172 | * |
||
173 | * @param string|array|null $key |
||
174 | * @return mixed |
||
175 | */ |
||
176 | public function median($key = null) |
||
199 | 3 | ||
200 | /** |
||
201 | * Get the mode of a given key. |
||
202 | * |
||
203 | * @param string|array|null $key |
||
204 | * @return array|null |
||
205 | */ |
||
206 | public function mode($key = null) |
||
228 | 3 | ||
229 | /** |
||
230 | * Collapse the collection of items into a single array. |
||
231 | * |
||
232 | * @return static |
||
233 | */ |
||
234 | public function collapse() |
||
238 | 3 | ||
239 | /** |
||
240 | * Alias for the "contains" method. |
||
241 | * |
||
242 | * @param mixed $key |
||
243 | * @param mixed $operator |
||
244 | * @param mixed $value |
||
245 | * @return bool |
||
246 | */ |
||
247 | public function some($key, $operator = null, $value = null) |
||
251 | 1 | ||
252 | /** |
||
253 | * Determine if an item exists in the collection. |
||
254 | * |
||
255 | * @param mixed $key |
||
256 | * @param mixed $operator |
||
257 | * @param mixed $value |
||
258 | * @return bool |
||
259 | */ |
||
260 | public function contains($key, $operator = null, $value = null) |
||
274 | 3 | ||
275 | /** |
||
276 | * Determine if an item exists in the collection using strict comparison. |
||
277 | * |
||
278 | * @param mixed $key |
||
279 | * @param mixed $value |
||
280 | * @return bool |
||
281 | */ |
||
282 | public function containsStrict($key, $value = null) |
||
296 | 1 | ||
297 | /** |
||
298 | * Cross join with the given lists, returning all possible permutations. |
||
299 | * |
||
300 | * @param mixed ...$lists |
||
301 | * @return static |
||
302 | */ |
||
303 | public function crossJoin(...$lists) |
||
309 | |||
310 | /** |
||
311 | * Dump the collection and end the script. |
||
312 | * |
||
313 | * @param mixed ...$args |
||
314 | * @return void |
||
315 | */ |
||
316 | public function dd(...$args) |
||
322 | |||
323 | /** |
||
324 | * Dump the collection. |
||
325 | * |
||
326 | * @return $this |
||
327 | */ |
||
328 | public function dump() |
||
338 | |||
339 | /** |
||
340 | * Get the items in the collection that are not present in the given items. |
||
341 | * |
||
342 | * @param mixed $items |
||
343 | * @return static |
||
344 | */ |
||
345 | public function diff($items) |
||
349 | 3 | ||
350 | /** |
||
351 | * Get the items in the collection that are not present in the given items. |
||
352 | * |
||
353 | * @param mixed $items |
||
354 | * @param callable $callback |
||
355 | * @return static |
||
356 | */ |
||
357 | public function diffUsing($items, callable $callback) |
||
361 | 2 | ||
362 | /** |
||
363 | * Get the items in the collection whose keys and values are not present in the given items. |
||
364 | * |
||
365 | * @param mixed $items |
||
366 | * @return static |
||
367 | */ |
||
368 | public function diffAssoc($items) |
||
372 | 2 | ||
373 | /** |
||
374 | * Get the items in the collection whose keys and values are not present in the given items. |
||
375 | * |
||
376 | * @param mixed $items |
||
377 | * @param callable $callback |
||
378 | * @return static |
||
379 | */ |
||
380 | public function diffAssocUsing($items, callable $callback) |
||
384 | 1 | ||
385 | /** |
||
386 | * Get the items in the collection whose keys are not present in the given items. |
||
387 | * |
||
388 | * @param mixed $items |
||
389 | * @return static |
||
390 | */ |
||
391 | public function diffKeys($items) |
||
395 | 2 | ||
396 | /** |
||
397 | * Get the items in the collection whose keys are not present in the given items. |
||
398 | * |
||
399 | * @param mixed $items |
||
400 | * @param callable $callback |
||
401 | * @return static |
||
402 | */ |
||
403 | public function diffKeysUsing($items, callable $callback) |
||
407 | 1 | ||
408 | /** |
||
409 | * Retrieve duplicate items from the collection. |
||
410 | * |
||
411 | * @param callable|null $callback |
||
412 | * @param bool $strict |
||
413 | * @return static |
||
414 | */ |
||
415 | public function duplicates($callback = null, $strict = false) |
||
435 | |||
436 | 1 | /** |
|
437 | * Retrieve duplicate items from the collection using strict comparison. |
||
438 | 1 | * |
|
439 | 1 | * @param callable|null $callback |
|
440 | * @return static |
||
441 | */ |
||
442 | public function duplicatesStrict($callback = null) |
||
446 | |||
447 | /** |
||
448 | * Get the comparison function to detect duplicates. |
||
449 | * |
||
450 | 1 | * @param bool $strict |
|
451 | * @return \Closure |
||
452 | 1 | */ |
|
453 | 1 | protected function duplicateComparator($strict) |
|
465 | |||
466 | /** |
||
467 | * Execute a callback over each item. |
||
468 | * |
||
469 | * @param callable $callback |
||
470 | * @return $this |
||
471 | */ |
||
472 | public function each(callable $callback) |
||
482 | |||
483 | /** |
||
484 | * Execute a callback over each nested chunk of items. |
||
485 | * |
||
486 | * @param callable $callback |
||
487 | * @return static |
||
488 | */ |
||
489 | public function eachSpread(callable $callback) |
||
497 | |||
498 | /** |
||
499 | * Determine if all items in the collection pass the given test. |
||
500 | * |
||
501 | * @param string|callable $key |
||
502 | * @param mixed $operator |
||
503 | * @param mixed $value |
||
504 | * @return bool |
||
505 | */ |
||
506 | public function every($key, $operator = null, $value = null) |
||
522 | |||
523 | /** |
||
524 | * Get all items except for those with the specified keys. |
||
525 | 4 | * |
|
526 | * @param \IlluminateAgnostic\Str\Support\Collection|mixed $keys |
||
527 | 4 | * @return static |
|
528 | */ |
||
529 | public function except($keys) |
||
539 | 4 | ||
540 | /** |
||
541 | * Run a filter over each of the items. |
||
542 | * |
||
543 | * @param callable|null $callback |
||
544 | * @return static |
||
545 | */ |
||
546 | public function filter(callable $callback = null) |
||
554 | |||
555 | /** |
||
556 | * Apply the callback if the value is truthy. |
||
557 | * |
||
558 | * @param bool $value |
||
559 | * @param callable $callback |
||
560 | * @param callable $default |
||
561 | * @return static|mixed |
||
562 | 2 | */ |
|
563 | public function when($value, callable $callback, callable $default = null) |
||
573 | |||
574 | 2 | /** |
|
575 | * Apply the callback if the collection is empty. |
||
576 | 2 | * |
|
577 | * @param callable $callback |
||
578 | * @param callable $default |
||
579 | * @return static|mixed |
||
580 | */ |
||
581 | public function whenEmpty(callable $callback, callable $default = null) |
||
585 | |||
586 | /** |
||
587 | 3 | * Apply the callback if the collection is not empty. |
|
588 | * |
||
589 | 3 | * @param callable $callback |
|
590 | * @param callable $default |
||
591 | * @return static|mixed |
||
592 | */ |
||
593 | public function whenNotEmpty(callable $callback, callable $default = null) |
||
597 | |||
598 | /** |
||
599 | * Apply the callback if the value is falsy. |
||
600 | 9 | * |
|
601 | * @param bool $value |
||
602 | 9 | * @param callable $callback |
|
603 | 1 | * @param callable $default |
|
604 | * @return static|mixed |
||
605 | 1 | */ |
|
606 | public function unless($value, callable $callback, callable $default = null) |
||
610 | |||
611 | 6 | /** |
|
612 | * Apply the callback unless the collection is empty. |
||
613 | * |
||
614 | * @param callable $callback |
||
615 | 9 | * @param callable $default |
|
616 | * @return static|mixed |
||
617 | */ |
||
618 | 9 | public function unlessEmpty(callable $callback, callable $default = null) |
|
622 | 1 | ||
623 | /** |
||
624 | * Apply the callback unless the collection is not empty. |
||
625 | 9 | * |
|
626 | * @param callable $callback |
||
627 | 9 | * @param callable $default |
|
628 | 9 | * @return static|mixed |
|
629 | 6 | */ |
|
630 | 6 | public function unlessNotEmpty(callable $callback, callable $default = null) |
|
634 | 6 | ||
635 | 3 | /** |
|
636 | 1 | * Filter items by the given key value pair. |
|
637 | * |
||
638 | 9 | * @param string $key |
|
639 | * @param mixed $operator |
||
640 | * @param mixed $value |
||
641 | * @return static |
||
642 | */ |
||
643 | public function where($key, $operator = null, $value = null) |
||
647 | |||
648 | 1 | /** |
|
649 | * Get an operator checker callback. |
||
650 | 1 | * |
|
651 | * @param string $key |
||
652 | * @param string $operator |
||
653 | * @param mixed $value |
||
654 | * @return \Closure |
||
655 | */ |
||
656 | protected function operatorForWhere($key, $operator = null, $value = null) |
||
696 | |||
697 | /** |
||
698 | * Filter items by the given key value pair using strict comparison. |
||
699 | * |
||
700 | * @param string $key |
||
701 | 1 | * @param mixed $value |
|
702 | * @return static |
||
703 | */ |
||
704 | 1 | public function whereStrict($key, $value) |
|
708 | |||
709 | /** |
||
710 | * Filter items by the given key value pair. |
||
711 | * |
||
712 | * @param string $key |
||
713 | * @param mixed $values |
||
714 | * @param bool $strict |
||
715 | * @return static |
||
716 | 2 | */ |
|
717 | View Code Duplication | public function whereIn($key, $values, $strict = false) |
|
725 | |||
726 | /** |
||
727 | * Filter items by the given key value pair using strict comparison. |
||
728 | * |
||
729 | * @param string $key |
||
730 | * @param mixed $values |
||
731 | * @return static |
||
732 | 1 | */ |
|
733 | public function whereInStrict($key, $values) |
||
737 | |||
738 | /** |
||
739 | * Filter items such that the value of the given key is between the given values. |
||
740 | * |
||
741 | * @param string $key |
||
742 | * @param array $values |
||
743 | 1 | * @return static |
|
744 | */ |
||
745 | public function whereBetween($key, $values) |
||
749 | |||
750 | /** |
||
751 | * Filter items such that the value of the given key is not between the given values. |
||
752 | * |
||
753 | * @param string $key |
||
754 | * @param array $values |
||
755 | * @return static |
||
756 | */ |
||
757 | 13 | public function whereNotBetween($key, $values) |
|
763 | |||
764 | /** |
||
765 | * Filter items by the given key value pair. |
||
766 | * |
||
767 | * @param string $key |
||
768 | * @param mixed $values |
||
769 | * @param bool $strict |
||
770 | 1 | * @return static |
|
771 | */ |
||
772 | 1 | View Code Duplication | public function whereNotIn($key, $values, $strict = false) |
780 | |||
781 | 3 | /** |
|
782 | * Filter items by the given key value pair using strict comparison. |
||
783 | 3 | * |
|
784 | * @param string $key |
||
785 | * @param mixed $values |
||
786 | * @return static |
||
787 | */ |
||
788 | public function whereNotInStrict($key, $values) |
||
792 | |||
793 | 1 | /** |
|
794 | * Filter the items, removing any items that don't match the given type. |
||
795 | * |
||
796 | * @param string $type |
||
797 | * @return static |
||
798 | */ |
||
799 | public function whereInstanceOf($type) |
||
805 | 2 | ||
806 | /** |
||
807 | * Get the first item from the collection passing the given truth test. |
||
808 | 2 | * |
|
809 | * @param callable|null $callback |
||
810 | * @param mixed $default |
||
811 | * @return mixed |
||
812 | */ |
||
813 | public function first(callable $callback = null, $default = null) |
||
817 | |||
818 | 6 | /** |
|
819 | * Get the first item by the given key value pair. |
||
820 | 6 | * |
|
821 | 5 | * @param string $key |
|
822 | * @param mixed $operator |
||
823 | * @param mixed $value |
||
824 | 1 | * @return mixed |
|
825 | */ |
||
826 | public function firstWhere($key, $operator = null, $value = null) |
||
830 | |||
831 | /** |
||
832 | * Get a flattened array of the items in the collection. |
||
833 | * |
||
834 | 10 | * @param int $depth |
|
835 | * @return static |
||
836 | 10 | */ |
|
837 | 1 | public function flatten($depth = INF) |
|
841 | |||
842 | 10 | /** |
|
843 | * Flip the items in the collection. |
||
844 | 10 | * |
|
845 | * @return static |
||
846 | 10 | */ |
|
847 | 10 | public function flip() |
|
851 | |||
852 | /** |
||
853 | 10 | * Remove an item from the collection by key. |
|
854 | 10 | * |
|
855 | * @param string|array $keys |
||
856 | 10 | * @return $this |
|
857 | 10 | */ |
|
858 | public function forget($keys) |
||
866 | 10 | ||
867 | 1 | /** |
|
868 | * Get an item from the collection by key. |
||
869 | * |
||
870 | 10 | * @param mixed $key |
|
871 | * @param mixed $default |
||
872 | * @return mixed |
||
873 | */ |
||
874 | public function get($key, $default = null) |
||
882 | |||
883 | 3 | /** |
|
884 | * Group an associative array by a field or using a callback. |
||
885 | 3 | * |
|
886 | 3 | * @param array|callable|string $groupBy |
|
887 | * @param bool $preserveKeys |
||
888 | 3 | * @return static |
|
889 | */ |
||
890 | public function groupBy($groupBy, $preserveKeys = false) |
||
928 | 2 | ||
929 | 1 | /** |
|
930 | * Key an associative array by a field or using a callback. |
||
931 | * |
||
932 | 2 | * @param callable|string $keyBy |
|
933 | * @return static |
||
934 | */ |
||
935 | public function keyBy($keyBy) |
||
953 | |||
954 | 2 | /** |
|
955 | 2 | * Determine if an item exists in the collection by key. |
|
956 | * |
||
957 | * @param mixed $key |
||
958 | * @return bool |
||
959 | */ |
||
960 | public function has($key) |
||
972 | |||
973 | /** |
||
974 | 5 | * Concatenate values of a given key as a string. |
|
975 | * |
||
976 | 5 | * @param string $value |
|
977 | * @param string $glue |
||
978 | * @return string |
||
979 | */ |
||
980 | public function implode($value, $glue = null) |
||
990 | |||
991 | /** |
||
992 | * Intersect the collection with the given items. |
||
993 | * |
||
994 | * @param mixed $items |
||
995 | * @return static |
||
996 | */ |
||
997 | 1 | public function intersect($items) |
|
1001 | |||
1002 | /** |
||
1003 | 1 | * Intersect the collection with the given items by key. |
|
1004 | * |
||
1005 | 1 | * @param mixed $items |
|
1006 | 1 | * @return static |
|
1007 | */ |
||
1008 | public function intersectByKeys($items) |
||
1014 | |||
1015 | 1 | /** |
|
1016 | * Determine if the collection is empty or not. |
||
1017 | 1 | * |
|
1018 | * @return bool |
||
1019 | */ |
||
1020 | public function isEmpty() |
||
1024 | |||
1025 | 6 | /** |
|
1026 | * Determine if the collection is not empty. |
||
1027 | 6 | * |
|
1028 | * @return bool |
||
1029 | */ |
||
1030 | public function isNotEmpty() |
||
1034 | |||
1035 | /** |
||
1036 | * Determine if the given value is callable, but not a string. |
||
1037 | 8 | * |
|
1038 | * @param mixed $value |
||
1039 | 8 | * @return bool |
|
1040 | */ |
||
1041 | protected function useAsCallable($value) |
||
1045 | |||
1046 | /** |
||
1047 | * Join all items from the collection using a string. The final items can use a separate glue string. |
||
1048 | * |
||
1049 | 10 | * @param string $glue |
|
1050 | * @param string $finalGlue |
||
1051 | 10 | * @return string |
|
1052 | */ |
||
1053 | public function join($glue, $finalGlue = '') |
||
1075 | 1 | ||
1076 | /** |
||
1077 | * Get the keys of the collection items. |
||
1078 | 1 | * |
|
1079 | * @return static |
||
1080 | 1 | */ |
|
1081 | 1 | public function keys() |
|
1085 | |||
1086 | /** |
||
1087 | * Get the last item from the collection. |
||
1088 | * |
||
1089 | * @param callable|null $callback |
||
1090 | * @param mixed $default |
||
1091 | * @return mixed |
||
1092 | 4 | */ |
|
1093 | public function last(callable $callback = null, $default = null) |
||
1097 | 4 | ||
1098 | /** |
||
1099 | 4 | * Get the values of a given key. |
|
1100 | * |
||
1101 | 4 | * @param string|array $value |
|
1102 | * @param string|null $key |
||
1103 | 4 | * @return static |
|
1104 | 4 | */ |
|
1105 | public function pluck($value, $key = null) |
||
1109 | |||
1110 | 4 | /** |
|
1111 | * Run a map over each of the items. |
||
1112 | * |
||
1113 | * @param callable $callback |
||
1114 | * @return static |
||
1115 | */ |
||
1116 | public function map(callable $callback) |
||
1124 | |||
1125 | 2 | /** |
|
1126 | * Run a map over each nested chunk of items. |
||
1127 | * |
||
1128 | * @param callable $callback |
||
1129 | * @return static |
||
1130 | */ |
||
1131 | public function mapSpread(callable $callback) |
||
1139 | |||
1140 | 5 | /** |
|
1141 | 5 | * Run a dictionary map over the items. |
|
1142 | * |
||
1143 | 5 | * The callback should return an associative array with a single key/value pair. |
|
1144 | 5 | * |
|
1145 | * @param callable $callback |
||
1146 | * @return static |
||
1147 | */ |
||
1148 | 5 | public function mapToDictionary(callable $callback) |
|
1168 | 1 | ||
1169 | /** |
||
1170 | * Run a grouping map over the items. |
||
1171 | 1 | * |
|
1172 | 1 | * The callback should return an associative array with a single key/value pair. |
|
1173 | * |
||
1174 | * @param callable $callback |
||
1175 | * @return static |
||
1176 | */ |
||
1177 | public function mapToGroups(callable $callback) |
||
1183 | 1 | ||
1184 | /** |
||
1185 | * Run an associative map over each of the items. |
||
1186 | 1 | * |
|
1187 | * The callback should return an associative array with a single key/value pair. |
||
1188 | 1 | * |
|
1189 | * @param callable $callback |
||
1190 | 1 | * @return static |
|
1191 | 1 | */ |
|
1192 | public function mapWithKeys(callable $callback) |
||
1206 | |||
1207 | /** |
||
1208 | * Map a collection and flatten the result by a single level. |
||
1209 | * |
||
1210 | * @param callable $callback |
||
1211 | 2 | * @return static |
|
1212 | */ |
||
1213 | 2 | public function flatMap(callable $callback) |
|
1217 | |||
1218 | /** |
||
1219 | * Map the values into a new class. |
||
1220 | * |
||
1221 | * @param string $class |
||
1222 | 3 | * @return static |
|
1223 | */ |
||
1224 | 3 | public function mapInto($class) |
|
1230 | |||
1231 | /** |
||
1232 | * Get the max value of a given key. |
||
1233 | 1 | * |
|
1234 | * @param callable|string|null $callback |
||
1235 | 1 | * @return mixed |
|
1236 | */ |
||
1237 | View Code Duplication | public function max($callback = null) |
|
1249 | |||
1250 | /** |
||
1251 | * Merge the collection with the given items. |
||
1252 | * |
||
1253 | 1 | * @param mixed $items |
|
1254 | * @return static |
||
1255 | 1 | */ |
|
1256 | public function merge($items) |
||
1260 | 1 | ||
1261 | 1 | /** |
|
1262 | * Recursively merge the collection with the given items. |
||
1263 | * |
||
1264 | 1 | * @param mixed $items |
|
1265 | * @return static |
||
1266 | */ |
||
1267 | 1 | public function mergeRecursive($items) |
|
1271 | |||
1272 | /** |
||
1273 | * Create a collection by using this collection for keys and another for its values. |
||
1274 | * |
||
1275 | * @param mixed $values |
||
1276 | 1 | * @return static |
|
1277 | */ |
||
1278 | 1 | public function combine($values) |
|
1282 | 1 | ||
1283 | 1 | /** |
|
1284 | * Union the collection with the given items. |
||
1285 | * |
||
1286 | 1 | * @param mixed $items |
|
1287 | * @return static |
||
1288 | 1 | */ |
|
1289 | public function union($items) |
||
1293 | |||
1294 | /** |
||
1295 | * Get the min value of a given key. |
||
1296 | * |
||
1297 | * @param callable|string|null $callback |
||
1298 | 1 | * @return mixed |
|
1299 | */ |
||
1300 | 1 | View Code Duplication | public function min($callback = null) |
1312 | |||
1313 | 7 | /** |
|
1314 | * Create a new collection consisting of every n-th element. |
||
1315 | 7 | * |
|
1316 | * @param int $step |
||
1317 | 7 | * @param int $offset |
|
1318 | 6 | * @return static |
|
1319 | 7 | */ |
|
1320 | public function nth($step, $offset = 0) |
||
1336 | 1 | ||
1337 | /** |
||
1338 | * Get the items with the specified keys. |
||
1339 | * |
||
1340 | * @param mixed $keys |
||
1341 | * @return static |
||
1342 | */ |
||
1343 | public function only($keys) |
||
1357 | |||
1358 | 1 | /** |
|
1359 | * "Paginate" the collection by slicing it into a smaller collection. |
||
1360 | 1 | * |
|
1361 | * @param int $page |
||
1362 | * @param int $perPage |
||
1363 | * @return static |
||
1364 | */ |
||
1365 | public function forPage($page, $perPage) |
||
1371 | 21 | ||
1372 | /** |
||
1373 | 21 | * Partition the collection into two arrays using the given callback or key. |
|
1374 | * |
||
1375 | * @param callable|string $key |
||
1376 | * @param mixed $operator |
||
1377 | * @param mixed $value |
||
1378 | * @return static |
||
1379 | */ |
||
1380 | public function partition($key, $operator = null, $value = null) |
||
1394 | |||
1395 | /** |
||
1396 | * Pass the collection to the given callback and return the result. |
||
1397 | * |
||
1398 | * @param callable $callback |
||
1399 | * @return mixed |
||
1400 | 3 | */ |
|
1401 | public function pipe(callable $callback) |
||
1405 | |||
1406 | /** |
||
1407 | * Get and remove the last item from the collection. |
||
1408 | * |
||
1409 | * @return mixed |
||
1410 | */ |
||
1411 | public function pop() |
||
1415 | |||
1416 | 3 | /** |
|
1417 | * Push an item onto the beginning of the collection. |
||
1418 | * |
||
1419 | * @param mixed $value |
||
1420 | * @param mixed $key |
||
1421 | * @return $this |
||
1422 | */ |
||
1423 | public function prepend($value, $key = null) |
||
1429 | 3 | ||
1430 | 1 | /** |
|
1431 | * Push an item onto the end of the collection. |
||
1432 | * |
||
1433 | 3 | * @param mixed $value |
|
1434 | * @return $this |
||
1435 | */ |
||
1436 | public function push($value) |
||
1442 | |||
1443 | 5 | /** |
|
1444 | * Push all of the given items onto the collection. |
||
1445 | 5 | * |
|
1446 | * @param iterable $source |
||
1447 | * @return static |
||
1448 | */ |
||
1449 | public function concat($source) |
||
1459 | 9 | ||
1460 | 8 | /** |
|
1461 | 9 | * Get and remove an item from the collection. |
|
1462 | 9 | * |
|
1463 | * @param mixed $key |
||
1464 | * @param mixed $default |
||
1465 | * @return mixed |
||
1466 | */ |
||
1467 | public function pull($key, $default = null) |
||
1471 | |||
1472 | 1 | /** |
|
1473 | * Put an item in the collection by key. |
||
1474 | * |
||
1475 | * @param mixed $key |
||
1476 | * @param mixed $value |
||
1477 | * @return $this |
||
1478 | */ |
||
1479 | public function put($key, $value) |
||
1485 | 3 | ||
1486 | /** |
||
1487 | * Get one or a specified number of items randomly from the collection. |
||
1488 | 2 | * |
|
1489 | 2 | * @param int|null $number |
|
1490 | 1 | * @return static|mixed |
|
1491 | * |
||
1492 | * @throws \InvalidArgumentException |
||
1493 | */ |
||
1494 | 1 | public function random($number = null) |
|
1502 | 1 | ||
1503 | /** |
||
1504 | 1 | * Reduce the collection to a single value. |
|
1505 | * |
||
1506 | * @param callable $callback |
||
1507 | * @param mixed $initial |
||
1508 | * @return mixed |
||
1509 | */ |
||
1510 | public function reduce(callable $callback, $initial = null) |
||
1514 | |||
1515 | 1 | /** |
|
1516 | * Create a collection of all elements that do not pass a given truth test. |
||
1517 | * |
||
1518 | * @param callable|mixed $callback |
||
1519 | * @return static |
||
1520 | */ |
||
1521 | public function reject($callback = true) |
||
1531 | |||
1532 | /** |
||
1533 | * Replace the collection items with the given items. |
||
1534 | * |
||
1535 | * @param mixed $items |
||
1536 | 7 | * @return static |
|
1537 | */ |
||
1538 | 7 | public function replace($items) |
|
1542 | 6 | ||
1543 | /** |
||
1544 | 6 | * Recursively replace the collection items with the given items. |
|
1545 | * |
||
1546 | 6 | * @param mixed $items |
|
1547 | * @return static |
||
1548 | 6 | */ |
|
1549 | public function replaceRecursive($items) |
||
1553 | 6 | ||
1554 | 5 | /** |
|
1555 | * Reverse items order. |
||
1556 | * |
||
1557 | 6 | * @return static |
|
1558 | 6 | */ |
|
1559 | public function reverse() |
||
1563 | |||
1564 | 6 | /** |
|
1565 | * Search the collection for a given value and return the corresponding key if successful. |
||
1566 | * |
||
1567 | * @param mixed $value |
||
1568 | * @param bool $strict |
||
1569 | * @return mixed |
||
1570 | */ |
||
1571 | public function search($value, $strict = false) |
||
1585 | 1 | ||
1586 | /** |
||
1587 | * Get and remove the first item from the collection. |
||
1588 | * |
||
1589 | * @return mixed |
||
1590 | */ |
||
1591 | public function shift() |
||
1595 | |||
1596 | 11 | /** |
|
1597 | * Shuffle the items in the collection. |
||
1598 | 11 | * |
|
1599 | 1 | * @param int $seed |
|
1600 | 10 | * @return static |
|
1601 | */ |
||
1602 | 11 | public function shuffle($seed = null) |
|
1606 | |||
1607 | /** |
||
1608 | * Slice the underlying collection array. |
||
1609 | * |
||
1610 | * @param int $offset |
||
1611 | * @param int $length |
||
1612 | * @return static |
||
1613 | 5 | */ |
|
1614 | public function slice($offset, $length = null) |
||
1618 | |||
1619 | /** |
||
1620 | * Split a collection into a certain number of groups. |
||
1621 | * |
||
1622 | 5 | * @param int $numberOfGroups |
|
1623 | 5 | * @return static |
|
1624 | */ |
||
1625 | public function split($numberOfGroups) |
||
1655 | |||
1656 | /** |
||
1657 | * Chunk the underlying collection array. |
||
1658 | 2 | * |
|
1659 | * @param int $size |
||
1660 | 2 | * @return static |
|
1661 | */ |
||
1662 | 2 | public function chunk($size) |
|
1676 | |||
1677 | /** |
||
1678 | * Sort through each item with a callback. |
||
1679 | * |
||
1680 | * @param callable|null $callback |
||
1681 | * @return static |
||
1682 | */ |
||
1683 | public function sort(callable $callback = null) |
||
1693 | |||
1694 | /** |
||
1695 | * Sort the collection using the given callback. |
||
1696 | * |
||
1697 | * @param callable|string $callback |
||
1698 | * @param int $options |
||
1699 | * @param bool $descending |
||
1700 | * @return static |
||
1701 | 8 | */ |
|
1702 | public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
||
1727 | |||
1728 | /** |
||
1729 | * Sort the collection in descending order using the given callback. |
||
1730 | * |
||
1731 | * @param callable|string $callback |
||
1732 | * @param int $options |
||
1733 | * @return static |
||
1734 | */ |
||
1735 | 1 | public function sortByDesc($callback, $options = SORT_REGULAR) |
|
1739 | 1 | ||
1740 | /** |
||
1741 | * Sort the collection keys. |
||
1742 | * |
||
1743 | * @param int $options |
||
1744 | * @param bool $descending |
||
1745 | * @return static |
||
1746 | */ |
||
1747 | public function sortKeys($options = SORT_REGULAR, $descending = false) |
||
1755 | |||
1756 | /** |
||
1757 | * Sort the collection keys in descending order. |
||
1758 | * |
||
1759 | * @param int $options |
||
1760 | * @return static |
||
1761 | */ |
||
1762 | 5 | public function sortKeysDesc($options = SORT_REGULAR) |
|
1766 | 5 | ||
1767 | /** |
||
1768 | * Splice a portion of the underlying collection array. |
||
1769 | 5 | * |
|
1770 | 5 | * @param int $offset |
|
1771 | * @param int|null $length |
||
1772 | * @param mixed $replacement |
||
1773 | 5 | * @return static |
|
1774 | 5 | */ |
|
1775 | public function splice($offset, $length = null, $replacement = []) |
||
1783 | 1 | ||
1784 | /** |
||
1785 | 1 | * Get the sum of the given values. |
|
1786 | * |
||
1787 | * @param callable|string|null $callback |
||
1788 | * @return mixed |
||
1789 | */ |
||
1790 | public function sum($callback = null) |
||
1802 | |||
1803 | /** |
||
1804 | 38 | * Take the first or last {$limit} items. |
|
1805 | * |
||
1806 | 38 | * @param int $limit |
|
1807 | 25 | * @return static |
|
1808 | */ |
||
1809 | public function take($limit) |
||
1817 | |||
1818 | /** |
||
1819 | * Pass the collection to the given callback and then return it. |
||
1820 | * |
||
1821 | * @param callable $callback |
||
1822 | * @return $this |
||
1823 | */ |
||
1824 | 1 | public function tap(callable $callback) |
|
1830 | |||
1831 | 1 | /** |
|
1832 | 1 | * Transform each item in the collection using a callback. |
|
1833 | * |
||
1834 | 1 | * @param callable $callback |
|
1835 | * @return $this |
||
1836 | */ |
||
1837 | public function transform(callable $callback) |
||
1843 | |||
1844 | 1 | /** |
|
1845 | * Return only unique items from the collection array. |
||
1846 | 1 | * |
|
1847 | * @param string|callable|null $key |
||
1848 | * @param bool $strict |
||
1849 | * @return static |
||
1850 | */ |
||
1851 | public function unique($key = null, $strict = false) |
||
1865 | |||
1866 | 2 | /** |
|
1867 | * Return only unique items from the collection array using strict comparison. |
||
1868 | * |
||
1869 | 2 | * @param string|callable|null $key |
|
1870 | 2 | * @return static |
|
1871 | 2 | */ |
|
1872 | 1 | public function uniqueStrict($key = null) |
|
1876 | |||
1877 | 1 | /** |
|
1878 | 2 | * Reset the keys on the underlying array. |
|
1879 | * |
||
1880 | * @return static |
||
1881 | */ |
||
1882 | public function values() |
||
1886 | |||
1887 | 2 | /** |
|
1888 | * Get a value retrieving callback. |
||
1889 | 2 | * |
|
1890 | * @param callable|string|null $value |
||
1891 | * @return callable |
||
1892 | */ |
||
1893 | protected function valueRetriever($value) |
||
1903 | |||
1904 | /** |
||
1905 | * Zip the collection together with one or more arrays. |
||
1906 | * |
||
1907 | * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); |
||
1908 | 1 | * => [[1, 4], [2, 5], [3, 6]] |
|
1909 | * |
||
1910 | 1 | * @param mixed ...$items |
|
1911 | * @return static |
||
1912 | */ |
||
1913 | public function zip($items) |
||
1925 | |||
1926 | /** |
||
1927 | * Pad collection to the specified length with a value. |
||
1928 | * |
||
1929 | 2 | * @param int $size |
|
1930 | * @param mixed $value |
||
1931 | 2 | * @return static |
|
1932 | */ |
||
1933 | 1 | public function pad($size, $value) |
|
1937 | |||
1938 | 2 | /** |
|
1939 | 2 | * Get the collection of items as a plain array. |
|
1940 | * |
||
1941 | * @return array |
||
1942 | */ |
||
1943 | public function toArray() |
||
1949 | |||
1950 | /** |
||
1951 | * Convert the object into something JSON serializable. |
||
1952 | * |
||
1953 | * @return array |
||
1954 | */ |
||
1955 | public function jsonSerialize() |
||
1969 | |||
1970 | /** |
||
1971 | 17 | * Get the collection of items as JSON. |
|
1972 | * |
||
1973 | 17 | * @param int $options |
|
1974 | * @return string |
||
1975 | */ |
||
1976 | public function toJson($options = 0) |
||
1980 | |||
1981 | /** |
||
1982 | 18 | * Get an iterator for the items. |
|
1983 | * |
||
1984 | 18 | * @return \ArrayIterator |
|
1985 | */ |
||
1986 | public function getIterator() |
||
1990 | |||
1991 | /** |
||
1992 | * Get a CachingIterator instance. |
||
1993 | * |
||
1994 | 47 | * @param int $flags |
|
1995 | * @return \CachingIterator |
||
1996 | 47 | */ |
|
1997 | 32 | public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING) |
|
2001 | 47 | ||
2002 | /** |
||
2003 | * Count the number of items in the collection. |
||
2004 | * |
||
2005 | * @return int |
||
2006 | */ |
||
2007 | public function count() |
||
2011 | 4 | ||
2012 | 4 | /** |
|
2013 | * Count the number of items in the collection using a given truth test. |
||
2014 | * |
||
2015 | * @param callable|null $callback |
||
2016 | * @return static |
||
2017 | */ |
||
2018 | public function countBy($callback = null) |
||
2030 | 238 | ||
2031 | /** |
||
2032 | 238 | * Add an item to the collection. |
|
2033 | 232 | * |
|
2034 | 37 | * @param mixed $item |
|
2035 | 22 | * @return $this |
|
2036 | 16 | */ |
|
2037 | 1 | public function add($item) |
|
2043 | 2 | ||
2044 | /** |
||
2045 | * Get a base Support collection instance from this collection. |
||
2046 | 13 | * |
|
2047 | * @return \IlluminateAgnostic\Str\Support\Collection |
||
2048 | */ |
||
2049 | public function toBase() |
||
2053 | |||
2054 | /** |
||
2055 | 1 | * Determine if an item exists at an offset. |
|
2056 | * |
||
2057 | 1 | * @param mixed $key |
|
2058 | 1 | * @return bool |
|
2059 | */ |
||
2060 | public function offsetExists($key) |
||
2064 | |||
2065 | /** |
||
2066 | * Get an item at a given offset. |
||
2067 | * |
||
2068 | 16 | * @param mixed $key |
|
2069 | * @return mixed |
||
2070 | 16 | */ |
|
2071 | 1 | public function offsetGet($key) |
|
2075 | |||
2076 | /** |
||
2077 | * Set the item at a given offset. |
||
2078 | * |
||
2079 | * @param mixed $key |
||
2080 | * @param mixed $value |
||
2081 | * @return void |
||
2082 | */ |
||
2083 | public function offsetSet($key, $value) |
||
2091 | |||
2092 | /** |
||
2093 | * Unset the item at a given offset. |
||
2094 | * |
||
2095 | * @param string $key |
||
2096 | * @return void |
||
2097 | */ |
||
2098 | public function offsetUnset($key) |
||
2102 | |||
2103 | /** |
||
2104 | * Convert the collection to its string representation. |
||
2105 | * |
||
2106 | * @return string |
||
2107 | */ |
||
2108 | public function __toString() |
||
2112 | |||
2113 | /** |
||
2114 | * Results array of items from Collection or Arrayable. |
||
2115 | * |
||
2116 | * @param mixed $items |
||
2117 | * @return array |
||
2118 | */ |
||
2119 | protected function getArrayableItems($items) |
||
2137 | |||
2138 | /** |
||
2139 | * Add a method to the list of proxied methods. |
||
2140 | * |
||
2141 | * @param string $method |
||
2142 | * @return void |
||
2143 | */ |
||
2144 | public static function proxy($method) |
||
2148 | |||
2149 | /** |
||
2150 | * Dynamically access collection proxies. |
||
2151 | * |
||
2152 | * @param string $key |
||
2153 | * @return mixed |
||
2154 | * |
||
2155 | * @throws \Exception |
||
2156 | */ |
||
2157 | public function __get($key) |
||
2165 | } |
||
2166 |
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.