Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Collection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Collection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable |
||
20 | { |
||
21 | /** |
||
22 | * The registered string extensions. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected static $extensions = []; |
||
27 | |||
28 | /** |
||
29 | * The items contained in the collection. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $items = []; |
||
34 | |||
35 | /** |
||
36 | * Create a new collection. |
||
37 | * |
||
38 | * @param callable|\Closure|array|Traversable\Iterator|self|IteratorAggregate|JsonSerializable $items |
||
39 | */ |
||
40 | 154 | public function __construct($items = []) |
|
44 | |||
45 | /** |
||
46 | * Convert the collection to its string representation. |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | 1 | public function __toString() |
|
54 | |||
55 | /** |
||
56 | * Dynamically handle calls to the class. |
||
57 | * |
||
58 | * @param string $method |
||
59 | * @param array $parameters |
||
60 | * |
||
61 | * @throws \BadMethodCallException |
||
62 | * |
||
63 | * @return mixed |
||
64 | */ |
||
65 | 1 | View Code Duplication | public static function __callStatic($method, $parameters) |
77 | |||
78 | /** |
||
79 | * Dynamically handle calls to the class. |
||
80 | * |
||
81 | * @param string $method |
||
82 | * @param array $parameters |
||
83 | * |
||
84 | * @throws \BadMethodCallException |
||
85 | * |
||
86 | * @return mixed |
||
87 | */ |
||
88 | 2 | View Code Duplication | public function __call($method, $parameters) |
100 | |||
101 | /** |
||
102 | * Static alias of normal constructor. |
||
103 | * |
||
104 | * @param callable|\Closure|array|Traversable\Iterator|self|IteratorAggregate|JsonSerializable $items |
||
105 | * |
||
106 | * @return $this |
||
107 | */ |
||
108 | 5 | public static function from($items = []): Collection |
|
112 | |||
113 | /** |
||
114 | * Get the average value of a given key. |
||
115 | * |
||
116 | * @param callable|string|null $callback |
||
117 | * |
||
118 | * @return int|float|null |
||
119 | */ |
||
120 | 4 | public function average($callback = null) |
|
126 | |||
127 | /** |
||
128 | * Get the median of a given key. |
||
129 | * |
||
130 | * @param null $key |
||
131 | * |
||
132 | * @return mixed|null |
||
133 | */ |
||
134 | 5 | public function median($key = null) |
|
155 | |||
156 | /** |
||
157 | * Get the mode of a given key. |
||
158 | * |
||
159 | * @param string|int|null $key |
||
160 | * |
||
161 | * @return array|null |
||
162 | */ |
||
163 | 4 | public function mode($key = null) |
|
185 | |||
186 | /** |
||
187 | * Get the first item from the collection. |
||
188 | * |
||
189 | * @param callable|null $callback |
||
190 | * @param mixed $default |
||
191 | * |
||
192 | * @return mixed |
||
193 | */ |
||
194 | 9 | public function first(callable $callback = null, $default = null) |
|
198 | |||
199 | /** |
||
200 | * Get a flattened array of the items in the collection. |
||
201 | * |
||
202 | * @param int|float|string $depth |
||
203 | * |
||
204 | * @return static |
||
205 | */ |
||
206 | 3 | public function flatten($depth = INF): Collection |
|
210 | |||
211 | /** |
||
212 | * Flip the items in the collection. |
||
213 | * |
||
214 | * @return static |
||
215 | */ |
||
216 | 1 | public function flip(): Collection |
|
220 | |||
221 | /** |
||
222 | * Get all items except for those with the specified keys. |
||
223 | * |
||
224 | * @param mixed $keys |
||
225 | * |
||
226 | * @return static |
||
227 | */ |
||
228 | 1 | public function except($keys): Collection |
|
234 | |||
235 | /** |
||
236 | * Run a filter over each of the items. |
||
237 | * |
||
238 | * @param callable|null $callback |
||
239 | * |
||
240 | * @return static |
||
241 | */ |
||
242 | 13 | public function filter(callable $callback = null): Collection |
|
250 | |||
251 | /** |
||
252 | * Get all of the items in the collection. |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | 84 | public function all(): array |
|
260 | |||
261 | /** |
||
262 | * Merge the collection with the given items. |
||
263 | * |
||
264 | * @param callable|Closure|array|Traversable\Iterator|self|IteratorAggregate|JsonSerializable $items |
||
265 | * |
||
266 | * @return static |
||
267 | */ |
||
268 | 3 | public function merge($items): Collection |
|
272 | |||
273 | /** |
||
274 | * Returns a new collection with $value added as last element. If $key is not provided |
||
275 | * it will be next integer index. |
||
276 | * |
||
277 | * @param mixed $value |
||
278 | * @param mixed $key |
||
279 | * |
||
280 | * @return static |
||
281 | */ |
||
282 | 1 | public function append($value, $key = null): Collection |
|
294 | |||
295 | /** |
||
296 | * Reset the keys on the underlying array. |
||
297 | * |
||
298 | * @return static |
||
299 | */ |
||
300 | 24 | public function values(): Collection |
|
304 | |||
305 | /** |
||
306 | * Determine if an item exists in the collection. |
||
307 | * |
||
308 | * @param mixed $key |
||
309 | * @param mixed $value |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | 2 | View Code Duplication | public function contains($key, $value = null): bool |
327 | |||
328 | /** |
||
329 | * Determine if an item exists in the collection using strict comparison. |
||
330 | * |
||
331 | * @param mixed $key |
||
332 | * @param mixed $value |
||
333 | * |
||
334 | * @return bool |
||
335 | */ |
||
336 | 1 | View Code Duplication | public function containsStrict($key, $value = null): bool |
350 | |||
351 | /** |
||
352 | * Get the items in the collection that are not present in the given items. |
||
353 | * |
||
354 | * @param mixed $items |
||
355 | * |
||
356 | * @return static |
||
357 | */ |
||
358 | 2 | public function diff($items): Collection |
|
362 | |||
363 | /** |
||
364 | * Get the items in the collection whose keys are not present in the given items. |
||
365 | * |
||
366 | * @param mixed $items |
||
367 | * |
||
368 | * @return static |
||
369 | */ |
||
370 | 1 | public function diffKeys($items): Collection |
|
374 | |||
375 | /** |
||
376 | * Execute a callback over each item. |
||
377 | * |
||
378 | * @param callable $callback |
||
379 | * |
||
380 | * @return $this |
||
381 | */ |
||
382 | 4 | public function each(callable $callback): Collection |
|
392 | |||
393 | /** |
||
394 | * Create a new collection consisting of every n-th element. |
||
395 | * |
||
396 | * @param int $step |
||
397 | * @param int $offset |
||
398 | * |
||
399 | * @return static |
||
400 | */ |
||
401 | 1 | public function every($step, $offset = 0): Collection |
|
416 | |||
417 | /** |
||
418 | * Filter items by the given key value pair. |
||
419 | * |
||
420 | * @param string $key |
||
421 | * @param mixed $operator |
||
422 | * @param mixed $value |
||
423 | * |
||
424 | * @return static |
||
425 | */ |
||
426 | 2 | public function where(string $key, $operator, $value = null): Collection |
|
435 | |||
436 | /** |
||
437 | * Filter items by the given key value pair using strict comparison. |
||
438 | * |
||
439 | * @param string $key |
||
440 | * @param mixed $value |
||
441 | * |
||
442 | * @return static |
||
443 | */ |
||
444 | 1 | public function whereStrict(string $key, $value): Collection |
|
448 | |||
449 | /** |
||
450 | * Filter items by the given key value pair. |
||
451 | * |
||
452 | * @param string $key |
||
453 | * @param mixed $values |
||
454 | * @param bool $strict |
||
455 | * |
||
456 | * @return static |
||
457 | */ |
||
458 | 2 | public function whereIn(string $key, $values, bool $strict = false) |
|
466 | |||
467 | /** |
||
468 | * Filter items by the given key value pair using strict comparison. |
||
469 | * |
||
470 | * @param string $key |
||
471 | * @param mixed $values |
||
472 | * |
||
473 | * @return static |
||
474 | */ |
||
475 | 1 | public function whereInStrict(string $key, $values): Collection |
|
479 | |||
480 | /** |
||
481 | * Remove an item from the collection by key. |
||
482 | * |
||
483 | * @param string|array $keys |
||
484 | * |
||
485 | * @return $this |
||
486 | */ |
||
487 | 2 | public function forget($keys): Collection |
|
495 | |||
496 | /** |
||
497 | * Get an item from the collection by key. |
||
498 | * |
||
499 | * @param mixed $key |
||
500 | * @param mixed $default |
||
501 | * |
||
502 | * @return mixed |
||
503 | */ |
||
504 | 4 | public function get($key, $default = null) |
|
512 | |||
513 | /** |
||
514 | * Group an associative array by a field or using a callback. |
||
515 | * |
||
516 | * @param callable|string $groupBy |
||
517 | * @param bool $preserveKeys |
||
518 | * |
||
519 | * @return static |
||
520 | */ |
||
521 | 6 | public function groupBy($groupBy, bool $preserveKeys = false): Collection |
|
544 | |||
545 | /** |
||
546 | * Key an associative array by a field or using a callback. |
||
547 | * |
||
548 | * @param callable|string $keyBy |
||
549 | * |
||
550 | * @return static |
||
551 | */ |
||
552 | 2 | public function keyBy($keyBy): Collection |
|
562 | |||
563 | /** |
||
564 | * Determine if an item exists in the collection by key. |
||
565 | * |
||
566 | * @param mixed $key |
||
567 | * |
||
568 | * @return bool |
||
569 | */ |
||
570 | public function has($key): bool |
||
574 | |||
575 | /** |
||
576 | * Concatenate values of a given key as a string. |
||
577 | * |
||
578 | * @param string $value |
||
579 | * @param string|null $glue |
||
580 | * |
||
581 | * @return string |
||
582 | */ |
||
583 | 1 | public function implode(string $value, string $glue = null): string |
|
593 | |||
594 | /** |
||
595 | * Intersect the collection with the given items. |
||
596 | * |
||
597 | * @param mixed $items |
||
598 | * |
||
599 | * @return static |
||
600 | */ |
||
601 | 2 | public function intersect($items): Collection |
|
605 | |||
606 | /** |
||
607 | * Determine if the collection is empty or not. |
||
608 | * |
||
609 | * @return bool |
||
610 | */ |
||
611 | 5 | public function isEmpty(): bool |
|
615 | |||
616 | /** |
||
617 | * Get the last item from the collection. |
||
618 | * |
||
619 | * @param callable|null $callback |
||
620 | * @param mixed $default |
||
621 | * |
||
622 | * @return mixed |
||
623 | */ |
||
624 | 7 | public function last(callable $callback = null, $default = null) |
|
628 | |||
629 | /** |
||
630 | * Get the values of a given key. |
||
631 | * |
||
632 | * @param string $value |
||
633 | * @param string|null $key |
||
634 | * |
||
635 | * @return static |
||
636 | */ |
||
637 | 8 | public function pluck(string $value, $key = null): Collection |
|
656 | |||
657 | /** |
||
658 | * Run a map over each of the items. |
||
659 | * |
||
660 | * @param callable $callback |
||
661 | * |
||
662 | * @return static |
||
663 | */ |
||
664 | 8 | public function map(callable $callback): Collection |
|
671 | |||
672 | /** |
||
673 | * Run an associative map over each of the items. |
||
674 | * |
||
675 | * The callback should return an associative array with a single key/value pair. |
||
676 | * |
||
677 | * @param callable $callback |
||
678 | * |
||
679 | * @return static |
||
680 | */ |
||
681 | 1 | public function mapWithKeys(callable $callback): Collection |
|
685 | |||
686 | /** |
||
687 | * Map a collection and flatten the result by a single level. |
||
688 | * |
||
689 | * @param callable $callback |
||
690 | * |
||
691 | * @return static |
||
692 | */ |
||
693 | 2 | public function flatMap(callable $callback): Collection |
|
697 | |||
698 | /** |
||
699 | * Get the max value of a given key. |
||
700 | * |
||
701 | * @param callable|string|null $callback |
||
702 | * |
||
703 | * @return mixed |
||
704 | */ |
||
705 | 1 | View Code Duplication | public function max($callback = null) |
715 | |||
716 | /** |
||
717 | * Create a collection by using this collection for keys and another for its values. |
||
718 | * |
||
719 | * @param mixed $values |
||
720 | * |
||
721 | * @return static |
||
722 | */ |
||
723 | 2 | public function combine($values): Collection |
|
727 | |||
728 | /** |
||
729 | * Union the collection with the given items. |
||
730 | * |
||
731 | * @param mixed $items |
||
732 | * |
||
733 | * @return static |
||
734 | */ |
||
735 | 3 | public function union($items): Collection |
|
739 | |||
740 | /** |
||
741 | * Get the min value of a given key. |
||
742 | * |
||
743 | * @param callable|string|null $callback |
||
744 | * |
||
745 | * @return mixed |
||
746 | */ |
||
747 | 1 | View Code Duplication | public function min($callback = null) |
757 | |||
758 | /** |
||
759 | * Get the items with the specified keys. |
||
760 | * |
||
761 | * @param mixed $keys |
||
762 | * |
||
763 | * @return static |
||
764 | */ |
||
765 | 1 | public function only($keys): Collection |
|
771 | |||
772 | /** |
||
773 | * "Paginate" the collection by slicing it into a smaller collection. |
||
774 | * |
||
775 | * @param int $page |
||
776 | * @param int $perPage |
||
777 | * |
||
778 | * @return static |
||
779 | */ |
||
780 | 1 | public function forPage(int $page, int $perPage): Collection |
|
784 | |||
785 | /** |
||
786 | * Pass the collection to the given callback and return the result. |
||
787 | * |
||
788 | * @param callable $callback |
||
789 | * |
||
790 | * @return mixed |
||
791 | */ |
||
792 | 1 | public function pipe(callable $callback) |
|
796 | |||
797 | /** |
||
798 | * Get and remove the last item from the collection. |
||
799 | * |
||
800 | * @return mixed |
||
801 | */ |
||
802 | 1 | public function pop() |
|
806 | |||
807 | /** |
||
808 | * Push an item onto the beginning of the collection. |
||
809 | * |
||
810 | * @param mixed $value |
||
811 | * @param mixed $key |
||
812 | * |
||
813 | * @return $this |
||
814 | */ |
||
815 | 1 | public function prepend($value, $key = null): Collection |
|
821 | |||
822 | /** |
||
823 | * Push an item onto the end of the collection. |
||
824 | * |
||
825 | * @param mixed $value |
||
826 | * |
||
827 | * @return $this |
||
828 | */ |
||
829 | public function push($value): Collection |
||
835 | |||
836 | /** |
||
837 | * Get and remove an item from the collection. |
||
838 | * |
||
839 | * @param mixed $key |
||
840 | * @param mixed $default |
||
841 | * |
||
842 | * @return mixed |
||
843 | */ |
||
844 | 3 | public function pull($key, $default = null) |
|
848 | |||
849 | /** |
||
850 | * Put an item in the collection by key. |
||
851 | * |
||
852 | * @param mixed $key |
||
853 | * @param mixed $value |
||
854 | * |
||
855 | * @return $this |
||
856 | */ |
||
857 | public function put($key, $value): Collection |
||
863 | |||
864 | /** |
||
865 | * Get one or more items randomly from the collection. |
||
866 | * |
||
867 | * @param int $amount |
||
868 | * |
||
869 | * @throws \InvalidArgumentException |
||
870 | * |
||
871 | * @return mixed |
||
872 | */ |
||
873 | 3 | public function random($amount = 1) |
|
889 | |||
890 | /** |
||
891 | * Reduce the collection to a single value. |
||
892 | * |
||
893 | * @param callable $callback |
||
894 | * @param mixed $initial |
||
895 | * |
||
896 | * @return mixed |
||
897 | */ |
||
898 | 6 | public function reduce(callable $callback, $initial = null) |
|
902 | |||
903 | /** |
||
904 | * Create a collection of all elements that do not pass a given truth test. |
||
905 | * |
||
906 | * @param mixed $callback |
||
907 | * |
||
908 | * @return static |
||
909 | */ |
||
910 | 3 | public function reject($callback): Collection |
|
922 | |||
923 | /** |
||
924 | * Reverse items order. |
||
925 | * |
||
926 | * @return static |
||
927 | */ |
||
928 | 1 | public function reverse(): Collection |
|
932 | |||
933 | /** |
||
934 | * Search the collection for a given value and return the corresponding key if successful. |
||
935 | * |
||
936 | * @param mixed $value |
||
937 | * @param bool $strict |
||
938 | * |
||
939 | * @return false|int|string |
||
940 | */ |
||
941 | 2 | public function search($value, $strict = false) |
|
954 | |||
955 | /** |
||
956 | * Get and remove the first item from the collection. |
||
957 | * |
||
958 | * @return mixed |
||
959 | */ |
||
960 | 1 | public function shift() |
|
964 | |||
965 | /** |
||
966 | * Shuffle the items in the collection. |
||
967 | * |
||
968 | * @param null|int $seed |
||
969 | * |
||
970 | * @return static |
||
971 | */ |
||
972 | public function shuffle(int $seed = null): Collection |
||
986 | |||
987 | /** |
||
988 | * Slice the underlying collection array. |
||
989 | * |
||
990 | * @param int $offset |
||
991 | * @param null|int $length |
||
992 | * |
||
993 | * @return static |
||
994 | */ |
||
995 | 9 | public function slice(int $offset, int $length = null): Collection |
|
999 | |||
1000 | /** |
||
1001 | * Split a collection into a certain number of groups. |
||
1002 | * |
||
1003 | * @param int $numberOfGroups |
||
1004 | * |
||
1005 | * @return static |
||
1006 | */ |
||
1007 | 4 | public function split(int $numberOfGroups): Collection |
|
1017 | |||
1018 | /** |
||
1019 | * Chunk the underlying collection array. |
||
1020 | * |
||
1021 | * @param int|float $size |
||
1022 | * |
||
1023 | * @return static |
||
1024 | */ |
||
1025 | 4 | public function chunk($size): Collection |
|
1035 | |||
1036 | /** |
||
1037 | * Sort through each item with a callback. |
||
1038 | * |
||
1039 | * @see http://stackoverflow.com/questions/4353739/preserve-key-order-stable-sort-when-sorting-with-phps-uasort |
||
1040 | * @see http://en.wikipedia.org/wiki/Schwartzian_transform |
||
1041 | * |
||
1042 | * @param callable|null $callback |
||
1043 | * |
||
1044 | * @return static |
||
1045 | */ |
||
1046 | 9 | public function sort(callable $callback = null): Collection |
|
1056 | |||
1057 | /** |
||
1058 | * Sort an array in reverse order and maintain index association. |
||
1059 | * |
||
1060 | * @param int $option |
||
1061 | * |
||
1062 | * @return static |
||
1063 | */ |
||
1064 | 3 | View Code Duplication | public function arsort(int $option = SORT_REGULAR): Collection |
1092 | |||
1093 | /** |
||
1094 | * Sort an array and maintain index association. |
||
1095 | * |
||
1096 | * @param int $option |
||
1097 | * |
||
1098 | * @return static |
||
1099 | */ |
||
1100 | 3 | View Code Duplication | public function asort(int $option = SORT_REGULAR): Collection |
1128 | |||
1129 | /** |
||
1130 | * Sort an array using a case insensitive "natural order" algorithm. |
||
1131 | * |
||
1132 | * @return static |
||
1133 | */ |
||
1134 | 1 | View Code Duplication | public function natcasesort(): Collection |
1155 | |||
1156 | /** |
||
1157 | * Sort an array using a "natural order" algorithm. |
||
1158 | * |
||
1159 | * @return static |
||
1160 | */ |
||
1161 | 1 | View Code Duplication | public function natsort(): Collection |
1182 | |||
1183 | /** |
||
1184 | * Sort an array with a user-defined comparison function and maintain index association. |
||
1185 | * |
||
1186 | * @param callable $callback |
||
1187 | * |
||
1188 | * @return static |
||
1189 | */ |
||
1190 | 1 | View Code Duplication | public function uasort(callable $callback): Collection |
1211 | |||
1212 | /** |
||
1213 | * Sort an array by keys using a user-defined comparison function. |
||
1214 | * |
||
1215 | * @param callable $callback |
||
1216 | * |
||
1217 | * @return static |
||
1218 | */ |
||
1219 | 1 | public function uksort(callable $callback): Collection |
|
1232 | |||
1233 | /** |
||
1234 | * Sort an array by values using a user-defined comparison function. |
||
1235 | * |
||
1236 | * @param callable $callback |
||
1237 | * |
||
1238 | * @return static |
||
1239 | */ |
||
1240 | 1 | View Code Duplication | public function usort(callable $callback): Collection |
1261 | |||
1262 | /** |
||
1263 | * Sort the collection using the given callback. |
||
1264 | * |
||
1265 | * @param callable|string $callback |
||
1266 | * @param int $options |
||
1267 | * @param bool $descending |
||
1268 | * |
||
1269 | * @return static |
||
1270 | */ |
||
1271 | 3 | public function sortBy($callback, int $options = SORT_REGULAR, bool $descending = false): Collection |
|
1294 | |||
1295 | /** |
||
1296 | * Sort the collection in descending order using the given callback. |
||
1297 | * |
||
1298 | * @param callable|string $callback |
||
1299 | * @param int $options |
||
1300 | * |
||
1301 | * @return static |
||
1302 | */ |
||
1303 | 1 | public function sortByDesc($callback, int $options = SORT_REGULAR): Collection |
|
1307 | |||
1308 | /** |
||
1309 | * Splice a portion of the underlying collection array. |
||
1310 | * |
||
1311 | * @param int $offset |
||
1312 | * @param int|null $length |
||
1313 | * @param mixed $replacement |
||
1314 | * |
||
1315 | * @return static |
||
1316 | */ |
||
1317 | 1 | public function splice(int $offset, $length = null, $replacement = []): Collection |
|
1325 | |||
1326 | /** |
||
1327 | * Get the sum of the given values. |
||
1328 | * |
||
1329 | * @param callable|string|null $callback |
||
1330 | * |
||
1331 | * @return mixed |
||
1332 | */ |
||
1333 | 8 | public function sum($callback = null) |
|
1344 | |||
1345 | /** |
||
1346 | * Take the first or last {$limit} items. |
||
1347 | * |
||
1348 | * @param int $limit |
||
1349 | * |
||
1350 | * @return static |
||
1351 | */ |
||
1352 | 2 | public function take(int $limit): Collection |
|
1360 | |||
1361 | /** |
||
1362 | * Transform each item in the collection using a callback. |
||
1363 | * |
||
1364 | * @param callable $callback |
||
1365 | * |
||
1366 | * @return $this |
||
1367 | */ |
||
1368 | 1 | public function transform(callable $callback): Collection |
|
1374 | |||
1375 | /** |
||
1376 | * Return only unique items from the collection array. |
||
1377 | * |
||
1378 | * @param string|callable|null $key |
||
1379 | * @param bool $strict |
||
1380 | * |
||
1381 | * @return static |
||
1382 | */ |
||
1383 | 4 | public function unique($key = null, bool $strict = false): Collection |
|
1400 | |||
1401 | /** |
||
1402 | * Return only unique items from the collection array using strict comparison. |
||
1403 | * |
||
1404 | * @param string|callable|null $key |
||
1405 | * |
||
1406 | * @return static |
||
1407 | */ |
||
1408 | 1 | public function uniqueStrict($key = null): Collection |
|
1412 | |||
1413 | /** |
||
1414 | * Zip the collection together with one or more arrays. |
||
1415 | * |
||
1416 | * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); |
||
1417 | * => [[1, 4], [2, 5], [3, 6]] |
||
1418 | * |
||
1419 | * @param mixed ...$items |
||
1420 | * |
||
1421 | * @return static |
||
1422 | */ |
||
1423 | 1 | public function zip($items): Collection |
|
1434 | |||
1435 | /** |
||
1436 | * Collapse the collection of items into a single array. |
||
1437 | * |
||
1438 | * @return static |
||
1439 | */ |
||
1440 | 4 | public function collapse(): Collection |
|
1456 | |||
1457 | /** |
||
1458 | * Get the keys of the collection items. |
||
1459 | * |
||
1460 | * @return static |
||
1461 | */ |
||
1462 | 4 | public function keys(): Collection |
|
1466 | |||
1467 | /** |
||
1468 | * Get the instance as an array. |
||
1469 | * |
||
1470 | * @return array |
||
1471 | */ |
||
1472 | 24 | public function toArray(): array |
|
1482 | |||
1483 | /** |
||
1484 | * {@inheritdoc} |
||
1485 | */ |
||
1486 | 2 | public function jsonSerialize() |
|
1500 | |||
1501 | /** |
||
1502 | * Convert the object to its JSON representation. |
||
1503 | * |
||
1504 | * @param int $options |
||
1505 | * |
||
1506 | * @return string |
||
1507 | */ |
||
1508 | 2 | public function toJson(int $options = 0): string |
|
1512 | |||
1513 | /** |
||
1514 | * Get an iterator for the items. |
||
1515 | * |
||
1516 | * @return \ArrayIterator |
||
1517 | */ |
||
1518 | 2 | public function getIterator() |
|
1522 | |||
1523 | /** |
||
1524 | * Get a CachingIterator instance. |
||
1525 | * |
||
1526 | * @param int $flags |
||
1527 | * |
||
1528 | * @return \CachingIterator |
||
1529 | */ |
||
1530 | 1 | public function getCachingIterator(int $flags = CachingIterator::CALL_TOSTRING): CachingIterator |
|
1534 | |||
1535 | /** |
||
1536 | * Count the number of items in the collection. |
||
1537 | * |
||
1538 | * @return int |
||
1539 | */ |
||
1540 | 19 | public function count() |
|
1544 | |||
1545 | /** |
||
1546 | * {@inheritdoc} |
||
1547 | */ |
||
1548 | public function serialize() |
||
1552 | |||
1553 | /** |
||
1554 | * Unserialize a string to a collection object. |
||
1555 | * |
||
1556 | * @param string $serialized |
||
1557 | * |
||
1558 | * @return static |
||
1559 | */ |
||
1560 | public function unserialize($serialized) |
||
1564 | |||
1565 | /** |
||
1566 | * Determine if an item exists at an offset. |
||
1567 | * |
||
1568 | * @param mixed $key |
||
1569 | * |
||
1570 | * @return bool |
||
1571 | */ |
||
1572 | 11 | public function offsetExists($key) |
|
1576 | |||
1577 | /** |
||
1578 | * Get an item at a given offset. |
||
1579 | * |
||
1580 | * @param mixed $key |
||
1581 | * |
||
1582 | * @return mixed |
||
1583 | */ |
||
1584 | 10 | public function offsetGet($key) |
|
1588 | |||
1589 | /** |
||
1590 | * Set the item at a given offset. |
||
1591 | * |
||
1592 | * @param mixed $key |
||
1593 | * @param mixed $value |
||
1594 | */ |
||
1595 | 11 | public function offsetSet($key, $value) |
|
1603 | |||
1604 | /** |
||
1605 | * Unset the item at a given offset. |
||
1606 | * |
||
1607 | * @param string $key |
||
1608 | */ |
||
1609 | 4 | public function offsetUnset($key) |
|
1613 | |||
1614 | /** |
||
1615 | * Register a custom extensions. |
||
1616 | * |
||
1617 | * @param string $name |
||
1618 | * @param callable $callback |
||
1619 | */ |
||
1620 | 3 | public static function extend(string $name, callable $callback) |
|
1624 | |||
1625 | /** |
||
1626 | * Checks if extensions is registered. |
||
1627 | * |
||
1628 | * @param string $name |
||
1629 | * |
||
1630 | * @return bool |
||
1631 | */ |
||
1632 | 3 | public static function hasExtensions(string $name): bool |
|
1636 | |||
1637 | /** |
||
1638 | * Explode the "value" and "key" arguments passed to "pluck". |
||
1639 | * |
||
1640 | * @param string|array $value |
||
1641 | * @param string|array|null $key |
||
1642 | * |
||
1643 | * @return array |
||
1644 | */ |
||
1645 | 8 | protected static function explodePluckParameters($value, $key): array |
|
1653 | |||
1654 | /** |
||
1655 | * Get an operator checker callback. |
||
1656 | * |
||
1657 | * @param string $key |
||
1658 | * @param string $operator |
||
1659 | * @param mixed $value |
||
1660 | * |
||
1661 | * @return \Closure |
||
1662 | */ |
||
1663 | 2 | protected function operatorForWhere(string $key, $operator, $value) |
|
1691 | |||
1692 | /** |
||
1693 | * Get a value retrieving callback. |
||
1694 | * |
||
1695 | * @param mixed $value |
||
1696 | * |
||
1697 | * @return callable |
||
1698 | */ |
||
1699 | 18 | protected function valueRetriever($value) |
|
1709 | |||
1710 | /** |
||
1711 | * Results array of items from Collection or Arrayable. |
||
1712 | * |
||
1713 | * @param callable|Closure|array|Traversable\Iterator|self|IteratorAggregate|JsonSerializable $items |
||
1714 | * |
||
1715 | * @return array |
||
1716 | */ |
||
1717 | 154 | protected function getArrayableItems($items): array |
|
1741 | |||
1742 | /** |
||
1743 | * Determine if the given value is callable, but not a string. |
||
1744 | * |
||
1745 | * @param mixed $value |
||
1746 | * |
||
1747 | * @return bool |
||
1748 | */ |
||
1749 | 23 | protected function useAsCallable($value): bool |
|
1753 | |||
1754 | /** |
||
1755 | * Get an item from an array or object using "dot" notation. |
||
1756 | * |
||
1757 | * @param mixed $target |
||
1758 | * @param string|array $key |
||
1759 | * @param mixed $default |
||
1760 | * |
||
1761 | * @return mixed |
||
1762 | */ |
||
1763 | 24 | protected static function dataGet($target, $key, $default = null) |
|
1764 | { |
||
1765 | 24 | if (is_null($key)) { |
|
1766 | 2 | return $target; |
|
1767 | } |
||
1768 | |||
1769 | 24 | $key = is_array($key) ? $key : explode('.', $key); |
|
1770 | |||
1771 | 24 | while (($segment = array_shift($key)) !== null) { |
|
1772 | 24 | if ($segment === '*') { |
|
1773 | if ($target instanceof self) { |
||
1774 | $target = $target->all(); |
||
1775 | } elseif (! is_array($target)) { |
||
1776 | return Arr::value($default); |
||
1777 | } |
||
1778 | |||
1779 | $result = $this->pluck($target, $key); |
||
1780 | |||
1781 | return in_array('*', $key) ? Arr::collapse($result) : $result; |
||
1782 | } |
||
1783 | |||
1784 | 24 | if (Arr::accessible($target) && Arr::exists($target, $segment)) { |
|
1785 | 19 | $target = $target[$segment]; |
|
1786 | 10 | } elseif (is_object($target) && isset($target->{$segment})) { |
|
1787 | 10 | $target = $target->{$segment}; |
|
1788 | } else { |
||
1789 | return Arr::value($default); |
||
1790 | } |
||
1791 | } |
||
1792 | |||
1793 | 24 | return $target; |
|
1794 | } |
||
1795 | |||
1796 | /** |
||
1797 | * Flatten a multi-dimensional array into a single level. |
||
1798 | * |
||
1799 | * @param array|\Collection $array |
||
1800 | * @param int|float|string $depth |
||
1801 | * |
||
1802 | * @return array |
||
1803 | */ |
||
1804 | 3 | private function flattenCallback($array, $depth = INF): array |
|
1820 | } |
||
1821 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.