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', 'some', 'sortBy', 'sortByDesc', 'sum', 'unique', |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * Create a new collection. |
||
66 | * |
||
67 | * @param mixed $items |
||
68 | * @return void |
||
|
|||
69 | */ |
||
70 | 238 | 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 | 116 | 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) |
|
160 | |||
161 | /** |
||
162 | * Alias for the "avg" method. |
||
163 | * |
||
164 | * @param callable|string|null $callback |
||
165 | * @return mixed |
||
166 | */ |
||
167 | 3 | public function average($callback = null) |
|
171 | |||
172 | /** |
||
173 | * Get the median of a given key. |
||
174 | * |
||
175 | * @param string|array|null $key |
||
176 | * @return mixed |
||
177 | */ |
||
178 | 6 | public function median($key = null) |
|
201 | |||
202 | /** |
||
203 | * Get the mode of a given key. |
||
204 | * |
||
205 | * @param string|array|null $key |
||
206 | * @return array|null |
||
207 | */ |
||
208 | 4 | public function mode($key = null) |
|
230 | |||
231 | /** |
||
232 | * Collapse the collection of items into a single array. |
||
233 | * |
||
234 | * @return static |
||
235 | */ |
||
236 | 3 | public function collapse() |
|
240 | |||
241 | /** |
||
242 | * Alias for the "contains" method. |
||
243 | * |
||
244 | * @param mixed $key |
||
245 | * @param mixed $operator |
||
246 | * @param mixed $value |
||
247 | * @return bool |
||
248 | */ |
||
249 | 1 | public function some($key, $operator = null, $value = null) |
|
253 | |||
254 | /** |
||
255 | * Determine if an item exists in the collection. |
||
256 | * |
||
257 | * @param mixed $key |
||
258 | * @param mixed $operator |
||
259 | * @param mixed $value |
||
260 | * @return bool |
||
261 | */ |
||
262 | 4 | public function contains($key, $operator = null, $value = null) |
|
276 | |||
277 | /** |
||
278 | * Determine if an item exists in the collection using strict comparison. |
||
279 | * |
||
280 | * @param mixed $key |
||
281 | * @param mixed $value |
||
282 | * @return bool |
||
283 | */ |
||
284 | 1 | public function containsStrict($key, $value = null) |
|
298 | |||
299 | /** |
||
300 | * Cross join with the given lists, returning all possible permutations. |
||
301 | * |
||
302 | * @param mixed ...$lists |
||
303 | * @return static |
||
304 | */ |
||
305 | 1 | public function crossJoin(...$lists) |
|
311 | |||
312 | /** |
||
313 | * Dump the collection and end the script. |
||
314 | * |
||
315 | * @param mixed ...$args |
||
316 | * @return void |
||
317 | */ |
||
318 | public function dd(...$args) |
||
324 | |||
325 | /** |
||
326 | * Dump the collection. |
||
327 | * |
||
328 | * @return $this |
||
329 | */ |
||
330 | public function dump() |
||
340 | |||
341 | /** |
||
342 | * Get the items in the collection that are not present in the given items. |
||
343 | * |
||
344 | * @param mixed $items |
||
345 | * @return static |
||
346 | */ |
||
347 | 3 | public function diff($items) |
|
351 | |||
352 | /** |
||
353 | * Get the items in the collection that are not present in the given items. |
||
354 | * |
||
355 | * @param mixed $items |
||
356 | * @param callable $callback |
||
357 | * @return static |
||
358 | */ |
||
359 | 2 | public function diffUsing($items, callable $callback) |
|
363 | |||
364 | /** |
||
365 | * Get the items in the collection whose keys and values are not present in the given items. |
||
366 | * |
||
367 | * @param mixed $items |
||
368 | * @return static |
||
369 | */ |
||
370 | 2 | public function diffAssoc($items) |
|
374 | |||
375 | /** |
||
376 | * Get the items in the collection whose keys and values are not present in the given items. |
||
377 | * |
||
378 | * @param mixed $items |
||
379 | * @param callable $callback |
||
380 | * @return static |
||
381 | */ |
||
382 | 1 | public function diffAssocUsing($items, callable $callback) |
|
386 | |||
387 | /** |
||
388 | * Get the items in the collection whose keys are not present in the given items. |
||
389 | * |
||
390 | * @param mixed $items |
||
391 | * @return static |
||
392 | */ |
||
393 | 2 | public function diffKeys($items) |
|
397 | |||
398 | /** |
||
399 | * Get the items in the collection whose keys are not present in the given items. |
||
400 | * |
||
401 | * @param mixed $items |
||
402 | * @param callable $callback |
||
403 | * @return static |
||
404 | */ |
||
405 | 1 | public function diffKeysUsing($items, callable $callback) |
|
409 | |||
410 | /** |
||
411 | * Execute a callback over each item. |
||
412 | * |
||
413 | * @param callable $callback |
||
414 | * @return $this |
||
415 | */ |
||
416 | 7 | public function each(callable $callback) |
|
426 | |||
427 | /** |
||
428 | * Execute a callback over each nested chunk of items. |
||
429 | * |
||
430 | * @param callable $callback |
||
431 | * @return static |
||
432 | */ |
||
433 | 1 | public function eachSpread(callable $callback) |
|
441 | |||
442 | /** |
||
443 | * Determine if all items in the collection pass the given test. |
||
444 | * |
||
445 | * @param string|callable $key |
||
446 | * @param mixed $operator |
||
447 | * @param mixed $value |
||
448 | * @return bool |
||
449 | */ |
||
450 | 1 | public function every($key, $operator = null, $value = null) |
|
466 | |||
467 | /** |
||
468 | * Get all items except for those with the specified keys. |
||
469 | * |
||
470 | * @param \IlluminateAgnostic\Arr\Support\Collection|mixed $keys |
||
471 | * @return static |
||
472 | */ |
||
473 | 2 | public function except($keys) |
|
483 | |||
484 | /** |
||
485 | * Run a filter over each of the items. |
||
486 | * |
||
487 | * @param callable|null $callback |
||
488 | * @return static |
||
489 | */ |
||
490 | 32 | public function filter(callable $callback = null) |
|
498 | |||
499 | /** |
||
500 | * Apply the callback if the value is truthy. |
||
501 | * |
||
502 | * @param bool $value |
||
503 | * @param callable $callback |
||
504 | * @param callable $default |
||
505 | * @return static|mixed |
||
506 | */ |
||
507 | 12 | public function when($value, callable $callback, callable $default = null) |
|
517 | |||
518 | /** |
||
519 | * Apply the callback if the collection is empty. |
||
520 | * |
||
521 | * @param callable $callback |
||
522 | * @param callable $default |
||
523 | * @return static|mixed |
||
524 | */ |
||
525 | 4 | public function whenEmpty(callable $callback, callable $default = null) |
|
529 | |||
530 | /** |
||
531 | * Apply the callback if the collection is not empty. |
||
532 | * |
||
533 | * @param callable $callback |
||
534 | * @param callable $default |
||
535 | * @return static|mixed |
||
536 | */ |
||
537 | 4 | public function whenNotEmpty(callable $callback, callable $default = null) |
|
541 | |||
542 | /** |
||
543 | * Apply the callback if the value is falsy. |
||
544 | * |
||
545 | * @param bool $value |
||
546 | * @param callable $callback |
||
547 | * @param callable $default |
||
548 | * @return static|mixed |
||
549 | */ |
||
550 | 2 | public function unless($value, callable $callback, callable $default = null) |
|
554 | |||
555 | /** |
||
556 | * Apply the callback unless the collection is empty. |
||
557 | * |
||
558 | * @param callable $callback |
||
559 | * @param callable $default |
||
560 | * @return static|mixed |
||
561 | */ |
||
562 | 2 | public function unlessEmpty(callable $callback, callable $default = null) |
|
566 | |||
567 | /** |
||
568 | * Apply the callback unless the collection is not empty. |
||
569 | * |
||
570 | * @param callable $callback |
||
571 | * @param callable $default |
||
572 | * @return static|mixed |
||
573 | */ |
||
574 | 2 | public function unlessNotEmpty(callable $callback, callable $default = null) |
|
578 | |||
579 | /** |
||
580 | * Filter items by the given key value pair. |
||
581 | * |
||
582 | * @param string $key |
||
583 | * @param mixed $operator |
||
584 | * @param mixed $value |
||
585 | * @return static |
||
586 | */ |
||
587 | 3 | public function where($key, $operator = null, $value = null) |
|
591 | |||
592 | /** |
||
593 | * Get an operator checker callback. |
||
594 | * |
||
595 | * @param string $key |
||
596 | * @param string $operator |
||
597 | * @param mixed $value |
||
598 | * @return \Closure |
||
599 | */ |
||
600 | 9 | protected function operatorForWhere($key, $operator = null, $value = null) |
|
640 | |||
641 | /** |
||
642 | * Filter items by the given key value pair using strict comparison. |
||
643 | * |
||
644 | * @param string $key |
||
645 | * @param mixed $value |
||
646 | * @return static |
||
647 | */ |
||
648 | 1 | public function whereStrict($key, $value) |
|
652 | |||
653 | /** |
||
654 | * Filter items by the given key value pair. |
||
655 | * |
||
656 | * @param string $key |
||
657 | * @param mixed $values |
||
658 | * @param bool $strict |
||
659 | * @return static |
||
660 | */ |
||
661 | 2 | View Code Duplication | public function whereIn($key, $values, $strict = false) |
669 | |||
670 | /** |
||
671 | * Filter items by the given key value pair using strict comparison. |
||
672 | * |
||
673 | * @param string $key |
||
674 | * @param mixed $values |
||
675 | * @return static |
||
676 | */ |
||
677 | 1 | public function whereInStrict($key, $values) |
|
681 | |||
682 | /** |
||
683 | * Filter items such that the value of the given key is between the given values. |
||
684 | * |
||
685 | * @param string $key |
||
686 | * @param array $values |
||
687 | * @return static |
||
688 | */ |
||
689 | 1 | public function whereBetween($key, $values) |
|
693 | |||
694 | /** |
||
695 | * Filter items such that the value of the given key is not between the given values. |
||
696 | * |
||
697 | * @param string $key |
||
698 | * @param array $values |
||
699 | * @return static |
||
700 | */ |
||
701 | 1 | public function whereNotBetween($key, $values) |
|
707 | |||
708 | /** |
||
709 | * Filter items by the given key value pair. |
||
710 | * |
||
711 | * @param string $key |
||
712 | * @param mixed $values |
||
713 | * @param bool $strict |
||
714 | * @return static |
||
715 | */ |
||
716 | 2 | View Code Duplication | public function whereNotIn($key, $values, $strict = false) |
724 | |||
725 | /** |
||
726 | * Filter items by the given key value pair using strict comparison. |
||
727 | * |
||
728 | * @param string $key |
||
729 | * @param mixed $values |
||
730 | * @return static |
||
731 | */ |
||
732 | 1 | public function whereNotInStrict($key, $values) |
|
736 | |||
737 | /** |
||
738 | * Filter the items, removing any items that don't match the given type. |
||
739 | * |
||
740 | * @param string $type |
||
741 | * @return static |
||
742 | */ |
||
743 | 1 | public function whereInstanceOf($type) |
|
749 | |||
750 | /** |
||
751 | * Get the first item from the collection. |
||
752 | * |
||
753 | * @param callable|null $callback |
||
754 | * @param mixed $default |
||
755 | * @return mixed |
||
756 | */ |
||
757 | 13 | public function first(callable $callback = null, $default = null) |
|
761 | |||
762 | /** |
||
763 | * Get the first item by the given key value pair. |
||
764 | * |
||
765 | * @param string $key |
||
766 | * @param mixed $operator |
||
767 | * @param mixed $value |
||
768 | * @return mixed |
||
769 | */ |
||
770 | 1 | public function firstWhere($key, $operator = null, $value = null) |
|
774 | |||
775 | /** |
||
776 | * Get a flattened array of the items in the collection. |
||
777 | * |
||
778 | * @param int $depth |
||
779 | * @return static |
||
780 | */ |
||
781 | 3 | public function flatten($depth = INF) |
|
785 | |||
786 | /** |
||
787 | * Flip the items in the collection. |
||
788 | * |
||
789 | * @return static |
||
790 | */ |
||
791 | 1 | public function flip() |
|
795 | |||
796 | /** |
||
797 | * Remove an item from the collection by key. |
||
798 | * |
||
799 | * @param string|array $keys |
||
800 | * @return $this |
||
801 | */ |
||
802 | 2 | public function forget($keys) |
|
810 | |||
811 | /** |
||
812 | * Get an item from the collection by key. |
||
813 | * |
||
814 | * @param mixed $key |
||
815 | * @param mixed $default |
||
816 | * @return mixed |
||
817 | */ |
||
818 | 6 | public function get($key, $default = null) |
|
826 | |||
827 | /** |
||
828 | * Group an associative array by a field or using a callback. |
||
829 | * |
||
830 | * @param array|callable|string $groupBy |
||
831 | * @param bool $preserveKeys |
||
832 | * @return static |
||
833 | */ |
||
834 | 10 | public function groupBy($groupBy, $preserveKeys = false) |
|
872 | |||
873 | /** |
||
874 | * Key an associative array by a field or using a callback. |
||
875 | * |
||
876 | * @param callable|string $keyBy |
||
877 | * @return static |
||
878 | */ |
||
879 | 3 | public function keyBy($keyBy) |
|
897 | |||
898 | /** |
||
899 | * Determine if an item exists in the collection by key. |
||
900 | * |
||
901 | * @param mixed $key |
||
902 | * @return bool |
||
903 | */ |
||
904 | 2 | public function has($key) |
|
916 | |||
917 | /** |
||
918 | * Concatenate values of a given key as a string. |
||
919 | * |
||
920 | * @param string $value |
||
921 | * @param string $glue |
||
922 | * @return string |
||
923 | */ |
||
924 | 2 | public function implode($value, $glue = null) |
|
934 | |||
935 | /** |
||
936 | * Intersect the collection with the given items. |
||
937 | * |
||
938 | * @param mixed $items |
||
939 | * @return static |
||
940 | */ |
||
941 | 2 | public function intersect($items) |
|
945 | |||
946 | /** |
||
947 | * Intersect the collection with the given items by key. |
||
948 | * |
||
949 | * @param mixed $items |
||
950 | * @return static |
||
951 | */ |
||
952 | 2 | public function intersectByKeys($items) |
|
958 | |||
959 | /** |
||
960 | * Determine if the collection is empty or not. |
||
961 | * |
||
962 | * @return bool |
||
963 | */ |
||
964 | 19 | public function isEmpty() |
|
968 | |||
969 | /** |
||
970 | * Determine if the collection is not empty. |
||
971 | * |
||
972 | * @return bool |
||
973 | */ |
||
974 | 5 | public function isNotEmpty() |
|
978 | |||
979 | /** |
||
980 | * Determine if the given value is callable, but not a string. |
||
981 | * |
||
982 | * @param mixed $value |
||
983 | * @return bool |
||
984 | */ |
||
985 | 49 | protected function useAsCallable($value) |
|
989 | |||
990 | /** |
||
991 | * Join all items from the collection using a string. The final items can use a separate glue string. |
||
992 | * |
||
993 | * @param string $glue |
||
994 | * @param string $finalGlue |
||
995 | * @return string |
||
996 | */ |
||
997 | 1 | public function join($glue, $finalGlue = '') |
|
1019 | |||
1020 | /** |
||
1021 | * Get the keys of the collection items. |
||
1022 | * |
||
1023 | * @return static |
||
1024 | */ |
||
1025 | 6 | public function keys() |
|
1029 | |||
1030 | /** |
||
1031 | * Get the last item from the collection. |
||
1032 | * |
||
1033 | * @param callable|null $callback |
||
1034 | * @param mixed $default |
||
1035 | * @return mixed |
||
1036 | */ |
||
1037 | 8 | public function last(callable $callback = null, $default = null) |
|
1041 | |||
1042 | /** |
||
1043 | * Get the values of a given key. |
||
1044 | * |
||
1045 | * @param string|array $value |
||
1046 | * @param string|null $key |
||
1047 | * @return static |
||
1048 | */ |
||
1049 | 10 | public function pluck($value, $key = null) |
|
1053 | |||
1054 | /** |
||
1055 | * Run a map over each of the items. |
||
1056 | * |
||
1057 | * @param callable $callback |
||
1058 | * @return static |
||
1059 | */ |
||
1060 | 26 | public function map(callable $callback) |
|
1068 | |||
1069 | /** |
||
1070 | * Run a map over each nested chunk of items. |
||
1071 | * |
||
1072 | * @param callable $callback |
||
1073 | * @return static |
||
1074 | */ |
||
1075 | 1 | public function mapSpread(callable $callback) |
|
1083 | |||
1084 | /** |
||
1085 | * Run a dictionary map over the items. |
||
1086 | * |
||
1087 | * The callback should return an associative array with a single key/value pair. |
||
1088 | * |
||
1089 | * @param callable $callback |
||
1090 | * @return static |
||
1091 | */ |
||
1092 | 4 | public function mapToDictionary(callable $callback) |
|
1112 | |||
1113 | /** |
||
1114 | * Run a grouping map over the items. |
||
1115 | * |
||
1116 | * The callback should return an associative array with a single key/value pair. |
||
1117 | * |
||
1118 | * @param callable $callback |
||
1119 | * @return static |
||
1120 | */ |
||
1121 | 2 | public function mapToGroups(callable $callback) |
|
1127 | |||
1128 | /** |
||
1129 | * Run an associative map over each of the items. |
||
1130 | * |
||
1131 | * The callback should return an associative array with a single key/value pair. |
||
1132 | * |
||
1133 | * @param callable $callback |
||
1134 | * @return static |
||
1135 | */ |
||
1136 | 5 | public function mapWithKeys(callable $callback) |
|
1150 | |||
1151 | /** |
||
1152 | * Map a collection and flatten the result by a single level. |
||
1153 | * |
||
1154 | * @param callable $callback |
||
1155 | * @return static |
||
1156 | */ |
||
1157 | 1 | public function flatMap(callable $callback) |
|
1161 | |||
1162 | /** |
||
1163 | * Map the values into a new class. |
||
1164 | * |
||
1165 | * @param string $class |
||
1166 | * @return static |
||
1167 | */ |
||
1168 | 1 | public function mapInto($class) |
|
1174 | |||
1175 | /** |
||
1176 | * Get the max value of a given key. |
||
1177 | * |
||
1178 | * @param callable|string|null $callback |
||
1179 | * @return mixed |
||
1180 | */ |
||
1181 | 1 | View Code Duplication | public function max($callback = null) |
1193 | |||
1194 | /** |
||
1195 | * Merge the collection with the given items. |
||
1196 | * |
||
1197 | * @param mixed $items |
||
1198 | * @return static |
||
1199 | */ |
||
1200 | 3 | public function merge($items) |
|
1204 | |||
1205 | /** |
||
1206 | * Create a collection by using this collection for keys and another for its values. |
||
1207 | * |
||
1208 | * @param mixed $values |
||
1209 | * @return static |
||
1210 | */ |
||
1211 | 2 | public function combine($values) |
|
1215 | |||
1216 | /** |
||
1217 | * Union the collection with the given items. |
||
1218 | * |
||
1219 | * @param mixed $items |
||
1220 | * @return static |
||
1221 | */ |
||
1222 | 3 | public function union($items) |
|
1226 | |||
1227 | /** |
||
1228 | * Get the min value of a given key. |
||
1229 | * |
||
1230 | * @param callable|string|null $callback |
||
1231 | * @return mixed |
||
1232 | */ |
||
1233 | 1 | View Code Duplication | public function min($callback = null) |
1245 | |||
1246 | /** |
||
1247 | * Create a new collection consisting of every n-th element. |
||
1248 | * |
||
1249 | * @param int $step |
||
1250 | * @param int $offset |
||
1251 | * @return static |
||
1252 | */ |
||
1253 | 1 | public function nth($step, $offset = 0) |
|
1269 | |||
1270 | /** |
||
1271 | * Get the items with the specified keys. |
||
1272 | * |
||
1273 | * @param mixed $keys |
||
1274 | * @return static |
||
1275 | */ |
||
1276 | 1 | public function only($keys) |
|
1290 | |||
1291 | /** |
||
1292 | * "Paginate" the collection by slicing it into a smaller collection. |
||
1293 | * |
||
1294 | * @param int $page |
||
1295 | * @param int $perPage |
||
1296 | * @return static |
||
1297 | */ |
||
1298 | 1 | public function forPage($page, $perPage) |
|
1304 | |||
1305 | /** |
||
1306 | * Partition the collection into two arrays using the given callback or key. |
||
1307 | * |
||
1308 | * @param callable|string $key |
||
1309 | * @param mixed $operator |
||
1310 | * @param mixed $value |
||
1311 | * @return static |
||
1312 | */ |
||
1313 | 7 | public function partition($key, $operator = null, $value = null) |
|
1327 | |||
1328 | /** |
||
1329 | * Pass the collection to the given callback and return the result. |
||
1330 | * |
||
1331 | * @param callable $callback |
||
1332 | * @return mixed |
||
1333 | */ |
||
1334 | 1 | public function pipe(callable $callback) |
|
1338 | |||
1339 | /** |
||
1340 | * Get and remove the last item from the collection. |
||
1341 | * |
||
1342 | * @return mixed |
||
1343 | */ |
||
1344 | 2 | public function pop() |
|
1348 | |||
1349 | /** |
||
1350 | * Push an item onto the beginning of the collection. |
||
1351 | * |
||
1352 | * @param mixed $value |
||
1353 | * @param mixed $key |
||
1354 | * @return $this |
||
1355 | */ |
||
1356 | 1 | public function prepend($value, $key = null) |
|
1362 | |||
1363 | /** |
||
1364 | * Push an item onto the end of the collection. |
||
1365 | * |
||
1366 | * @param mixed $value |
||
1367 | * @return $this |
||
1368 | */ |
||
1369 | 21 | public function push($value) |
|
1375 | |||
1376 | /** |
||
1377 | * Push all of the given items onto the collection. |
||
1378 | * |
||
1379 | * @param iterable $source |
||
1380 | * @return static |
||
1381 | */ |
||
1382 | 2 | public function concat($source) |
|
1392 | |||
1393 | /** |
||
1394 | * Get and remove an item from the collection. |
||
1395 | * |
||
1396 | * @param mixed $key |
||
1397 | * @param mixed $default |
||
1398 | * @return mixed |
||
1399 | */ |
||
1400 | 3 | public function pull($key, $default = null) |
|
1404 | |||
1405 | /** |
||
1406 | * Put an item in the collection by key. |
||
1407 | * |
||
1408 | * @param mixed $key |
||
1409 | * @param mixed $value |
||
1410 | * @return $this |
||
1411 | */ |
||
1412 | 3 | public function put($key, $value) |
|
1418 | |||
1419 | /** |
||
1420 | * Get one or a specified number of items randomly from the collection. |
||
1421 | * |
||
1422 | * @param int|null $number |
||
1423 | * @return static|mixed |
||
1424 | * |
||
1425 | * @throws \InvalidArgumentException |
||
1426 | */ |
||
1427 | 3 | public function random($number = null) |
|
1435 | |||
1436 | /** |
||
1437 | * Reduce the collection to a single value. |
||
1438 | * |
||
1439 | * @param callable $callback |
||
1440 | * @param mixed $initial |
||
1441 | * @return mixed |
||
1442 | */ |
||
1443 | 5 | public function reduce(callable $callback, $initial = null) |
|
1447 | |||
1448 | /** |
||
1449 | * Create a collection of all elements that do not pass a given truth test. |
||
1450 | * |
||
1451 | * @param callable|mixed $callback |
||
1452 | * @return static |
||
1453 | */ |
||
1454 | 9 | public function reject($callback = true) |
|
1464 | |||
1465 | /** |
||
1466 | * Reverse items order. |
||
1467 | * |
||
1468 | * @return static |
||
1469 | */ |
||
1470 | 1 | public function reverse() |
|
1474 | |||
1475 | /** |
||
1476 | * Search the collection for a given value and return the corresponding key if successful. |
||
1477 | * |
||
1478 | * @param mixed $value |
||
1479 | * @param bool $strict |
||
1480 | * @return mixed |
||
1481 | */ |
||
1482 | 3 | public function search($value, $strict = false) |
|
1496 | |||
1497 | /** |
||
1498 | * Get and remove the first item from the collection. |
||
1499 | * |
||
1500 | * @return mixed |
||
1501 | */ |
||
1502 | 1 | public function shift() |
|
1506 | |||
1507 | /** |
||
1508 | * Shuffle the items in the collection. |
||
1509 | * |
||
1510 | * @param int $seed |
||
1511 | * @return static |
||
1512 | */ |
||
1513 | 1 | public function shuffle($seed = null) |
|
1517 | |||
1518 | /** |
||
1519 | * Slice the underlying collection array. |
||
1520 | * |
||
1521 | * @param int $offset |
||
1522 | * @param int $length |
||
1523 | * @return static |
||
1524 | */ |
||
1525 | 10 | public function slice($offset, $length = null) |
|
1529 | |||
1530 | /** |
||
1531 | * Split a collection into a certain number of groups. |
||
1532 | * |
||
1533 | * @param int $numberOfGroups |
||
1534 | * @return static |
||
1535 | */ |
||
1536 | 7 | public function split($numberOfGroups) |
|
1566 | |||
1567 | /** |
||
1568 | * Chunk the underlying collection array. |
||
1569 | * |
||
1570 | * @param int $size |
||
1571 | * @return static |
||
1572 | */ |
||
1573 | 3 | public function chunk($size) |
|
1587 | |||
1588 | /** |
||
1589 | * Sort through each item with a callback. |
||
1590 | * |
||
1591 | * @param callable|null $callback |
||
1592 | * @return static |
||
1593 | */ |
||
1594 | 11 | public function sort(callable $callback = null) |
|
1604 | |||
1605 | /** |
||
1606 | * Sort the collection using the given callback. |
||
1607 | * |
||
1608 | * @param callable|string $callback |
||
1609 | * @param int $options |
||
1610 | * @param bool $descending |
||
1611 | * @return static |
||
1612 | */ |
||
1613 | 5 | public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
|
1638 | |||
1639 | /** |
||
1640 | * Sort the collection in descending order using the given callback. |
||
1641 | * |
||
1642 | * @param callable|string $callback |
||
1643 | * @param int $options |
||
1644 | * @return static |
||
1645 | */ |
||
1646 | 1 | public function sortByDesc($callback, $options = SORT_REGULAR) |
|
1650 | |||
1651 | /** |
||
1652 | * Sort the collection keys. |
||
1653 | * |
||
1654 | * @param int $options |
||
1655 | * @param bool $descending |
||
1656 | * @return static |
||
1657 | */ |
||
1658 | 2 | public function sortKeys($options = SORT_REGULAR, $descending = false) |
|
1666 | |||
1667 | /** |
||
1668 | * Sort the collection keys in descending order. |
||
1669 | * |
||
1670 | * @param int $options |
||
1671 | * @return static |
||
1672 | */ |
||
1673 | public function sortKeysDesc($options = SORT_REGULAR) |
||
1677 | |||
1678 | /** |
||
1679 | * Splice a portion of the underlying collection array. |
||
1680 | * |
||
1681 | * @param int $offset |
||
1682 | * @param int|null $length |
||
1683 | * @param mixed $replacement |
||
1684 | * @return static |
||
1685 | */ |
||
1686 | 1 | public function splice($offset, $length = null, $replacement = []) |
|
1694 | |||
1695 | /** |
||
1696 | * Get the sum of the given values. |
||
1697 | * |
||
1698 | * @param callable|string|null $callback |
||
1699 | * @return mixed |
||
1700 | */ |
||
1701 | 8 | public function sum($callback = null) |
|
1713 | |||
1714 | /** |
||
1715 | * Take the first or last {$limit} items. |
||
1716 | * |
||
1717 | * @param int $limit |
||
1718 | * @return static |
||
1719 | */ |
||
1720 | 2 | public function take($limit) |
|
1728 | |||
1729 | /** |
||
1730 | * Pass the collection to the given callback and then return it. |
||
1731 | * |
||
1732 | * @param callable $callback |
||
1733 | * @return $this |
||
1734 | */ |
||
1735 | 1 | public function tap(callable $callback) |
|
1741 | |||
1742 | /** |
||
1743 | * Transform each item in the collection using a callback. |
||
1744 | * |
||
1745 | * @param callable $callback |
||
1746 | * @return $this |
||
1747 | */ |
||
1748 | 1 | public function transform(callable $callback) |
|
1754 | |||
1755 | /** |
||
1756 | * Return only unique items from the collection array. |
||
1757 | * |
||
1758 | * @param string|callable|null $key |
||
1759 | * @param bool $strict |
||
1760 | * @return static |
||
1761 | */ |
||
1762 | 5 | public function unique($key = null, $strict = false) |
|
1776 | |||
1777 | /** |
||
1778 | * Return only unique items from the collection array using strict comparison. |
||
1779 | * |
||
1780 | * @param string|callable|null $key |
||
1781 | * @return static |
||
1782 | */ |
||
1783 | 1 | public function uniqueStrict($key = null) |
|
1787 | |||
1788 | /** |
||
1789 | * Reset the keys on the underlying array. |
||
1790 | * |
||
1791 | * @return static |
||
1792 | */ |
||
1793 | 39 | public function values() |
|
1797 | |||
1798 | /** |
||
1799 | * Get a value retrieving callback. |
||
1800 | * |
||
1801 | * @param string $value |
||
1802 | * @return callable |
||
1803 | */ |
||
1804 | 38 | protected function valueRetriever($value) |
|
1814 | |||
1815 | /** |
||
1816 | * Zip the collection together with one or more arrays. |
||
1817 | * |
||
1818 | * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); |
||
1819 | * => [[1, 4], [2, 5], [3, 6]] |
||
1820 | * |
||
1821 | * @param mixed ...$items |
||
1822 | * @return static |
||
1823 | */ |
||
1824 | 1 | public function zip($items) |
|
1836 | |||
1837 | /** |
||
1838 | * Pad collection to the specified length with a value. |
||
1839 | * |
||
1840 | * @param int $size |
||
1841 | * @param mixed $value |
||
1842 | * @return static |
||
1843 | */ |
||
1844 | 1 | public function pad($size, $value) |
|
1848 | |||
1849 | /** |
||
1850 | * Get the collection of items as a plain array. |
||
1851 | * |
||
1852 | * @return array |
||
1853 | */ |
||
1854 | 60 | public function toArray() |
|
1860 | |||
1861 | /** |
||
1862 | * Convert the object into something JSON serializable. |
||
1863 | * |
||
1864 | * @return array |
||
1865 | */ |
||
1866 | 2 | public function jsonSerialize() |
|
1880 | |||
1881 | /** |
||
1882 | * Get the collection of items as JSON. |
||
1883 | * |
||
1884 | * @param int $options |
||
1885 | * @return string |
||
1886 | */ |
||
1887 | 2 | public function toJson($options = 0) |
|
1891 | |||
1892 | /** |
||
1893 | * Get an iterator for the items. |
||
1894 | * |
||
1895 | * @return \ArrayIterator |
||
1896 | */ |
||
1897 | 5 | public function getIterator() |
|
1901 | |||
1902 | /** |
||
1903 | * Get a CachingIterator instance. |
||
1904 | * |
||
1905 | * @param int $flags |
||
1906 | * @return \CachingIterator |
||
1907 | */ |
||
1908 | 1 | public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING) |
|
1912 | |||
1913 | /** |
||
1914 | * Count the number of items in the collection. |
||
1915 | * |
||
1916 | * @return int |
||
1917 | */ |
||
1918 | 29 | public function count() |
|
1922 | |||
1923 | /** |
||
1924 | * Count the number of items in the collection using a given truth test. |
||
1925 | * |
||
1926 | * @param callable|null $callback |
||
1927 | * @return static |
||
1928 | */ |
||
1929 | 2 | public function countBy($callback = null) |
|
1941 | |||
1942 | /** |
||
1943 | * Add an item to the collection. |
||
1944 | * |
||
1945 | * @param mixed $item |
||
1946 | * @return $this |
||
1947 | */ |
||
1948 | public function add($item) |
||
1954 | |||
1955 | /** |
||
1956 | * Get a base Support collection instance from this collection. |
||
1957 | * |
||
1958 | * @return \IlluminateAgnostic\Arr\Support\Collection |
||
1959 | */ |
||
1960 | public function toBase() |
||
1964 | |||
1965 | /** |
||
1966 | * Determine if an item exists at an offset. |
||
1967 | * |
||
1968 | * @param mixed $key |
||
1969 | * @return bool |
||
1970 | */ |
||
1971 | 17 | public function offsetExists($key) |
|
1975 | |||
1976 | /** |
||
1977 | * Get an item at a given offset. |
||
1978 | * |
||
1979 | * @param mixed $key |
||
1980 | * @return mixed |
||
1981 | */ |
||
1982 | 18 | public function offsetGet($key) |
|
1986 | |||
1987 | /** |
||
1988 | * Set the item at a given offset. |
||
1989 | * |
||
1990 | * @param mixed $key |
||
1991 | * @param mixed $value |
||
1992 | * @return void |
||
1993 | */ |
||
1994 | 47 | public function offsetSet($key, $value) |
|
2002 | |||
2003 | /** |
||
2004 | * Unset the item at a given offset. |
||
2005 | * |
||
2006 | * @param string $key |
||
2007 | * @return void |
||
2008 | */ |
||
2009 | 4 | public function offsetUnset($key) |
|
2013 | |||
2014 | /** |
||
2015 | * Convert the collection to its string representation. |
||
2016 | * |
||
2017 | * @return string |
||
2018 | */ |
||
2019 | 1 | public function __toString() |
|
2023 | |||
2024 | /** |
||
2025 | * Results array of items from Collection or Arrayable. |
||
2026 | * |
||
2027 | * @param mixed $items |
||
2028 | * @return array |
||
2029 | */ |
||
2030 | 238 | protected function getArrayableItems($items) |
|
2048 | |||
2049 | /** |
||
2050 | * Add a method to the list of proxied methods. |
||
2051 | * |
||
2052 | * @param string $method |
||
2053 | * @return void |
||
2054 | */ |
||
2055 | 1 | public static function proxy($method) |
|
2059 | |||
2060 | /** |
||
2061 | * Dynamically access collection proxies. |
||
2062 | * |
||
2063 | * @param string $key |
||
2064 | * @return mixed |
||
2065 | * |
||
2066 | * @throws \Exception |
||
2067 | */ |
||
2068 | 16 | public function __get($key) |
|
2076 | } |
||
2077 |
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.