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 |
||
11 | class Collection implements ArrayAccess, Enumerable |
||
12 | { |
||
13 | use EnumeratesValues, Macroable; |
||
14 | |||
15 | /** |
||
16 | * The items contained in the collection. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $items = []; |
||
21 | |||
22 | /** |
||
23 | * Create a new collection. |
||
24 | * |
||
25 | * @param mixed $items |
||
26 | * @return void |
||
|
|||
27 | */ |
||
28 | 281 | public function __construct($items = []) |
|
32 | |||
33 | /** |
||
34 | * Create a new collection by invoking the callback a given amount of times. |
||
35 | * |
||
36 | * @param int $number |
||
37 | * @param callable $callback |
||
38 | * @return static |
||
39 | */ |
||
40 | 1 | public static function times($number, callable $callback = null) |
|
52 | |||
53 | /** |
||
54 | * Get all of the items in the collection. |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | 176 | public function all() |
|
62 | |||
63 | /** |
||
64 | * Get a lazy collection for the items in this collection. |
||
65 | * |
||
66 | * @return \IlluminateAgnostic\Collection\Support\LazyCollection |
||
67 | */ |
||
68 | 1 | public function lazy() |
|
72 | |||
73 | /** |
||
74 | * Get the average value of a given key. |
||
75 | * |
||
76 | * @param callable|string|null $callback |
||
77 | * @return mixed |
||
78 | */ |
||
79 | 2 | public function avg($callback = null) |
|
80 | { |
||
81 | 2 | $callback = $this->valueRetriever($callback); |
|
82 | |||
83 | $items = $this->map(function ($value) use ($callback) { |
||
84 | 2 | return $callback($value); |
|
85 | })->filter(function ($value) { |
||
86 | 2 | return ! is_null($value); |
|
87 | 2 | }); |
|
88 | |||
89 | 2 | if ($count = $items->count()) { |
|
90 | 2 | return $items->sum() / $count; |
|
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get the median of a given key. |
||
96 | * |
||
97 | * @param string|array|null $key |
||
98 | * @return mixed |
||
99 | */ |
||
100 | 2 | public function median($key = null) |
|
101 | { |
||
102 | 2 | $values = (isset($key) ? $this->pluck($key) : $this) |
|
103 | ->filter(function ($item) { |
||
104 | return ! is_null($item); |
||
105 | 2 | })->sort()->values(); |
|
106 | |||
107 | 2 | $count = $values->count(); |
|
108 | |||
109 | 2 | if ($count === 0) { |
|
110 | 2 | return; |
|
111 | } |
||
112 | |||
113 | $middle = (int) ($count / 2); |
||
114 | |||
115 | if ($count % 2) { |
||
116 | return $values->get($middle); |
||
117 | } |
||
118 | |||
119 | return (new static([ |
||
120 | $values->get($middle - 1), $values->get($middle), |
||
121 | ]))->average(); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Get the mode of a given key. |
||
126 | * |
||
127 | * @param string|array|null $key |
||
128 | * @return array|null |
||
129 | */ |
||
130 | 6 | public function mode($key = null) |
|
152 | |||
153 | /** |
||
154 | * Collapse the collection of items into a single array. |
||
155 | * |
||
156 | * @return static |
||
157 | */ |
||
158 | 3 | public function collapse() |
|
162 | |||
163 | /** |
||
164 | * Determine if an item exists in the collection. |
||
165 | * |
||
166 | * @param mixed $key |
||
167 | * @param mixed $operator |
||
168 | * @param mixed $value |
||
169 | * @return bool |
||
170 | */ |
||
171 | 3 | public function contains($key, $operator = null, $value = null) |
|
172 | { |
||
173 | 3 | if (func_num_args() === 1) { |
|
174 | 3 | if ($this->useAsCallable($key)) { |
|
175 | 3 | $placeholder = new stdClass; |
|
176 | |||
177 | 3 | return $this->first($key, $placeholder) !== $placeholder; |
|
178 | } |
||
179 | |||
180 | 2 | return in_array($key, $this->items); |
|
181 | } |
||
182 | |||
183 | return $this->contains($this->operatorForWhere(...func_get_args())); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Cross join with the given lists, returning all possible permutations. |
||
188 | * |
||
189 | * @param mixed ...$lists |
||
190 | * @return static |
||
191 | */ |
||
192 | 2 | public function crossJoin(...$lists) |
|
198 | |||
199 | /** |
||
200 | * Get the items in the collection that are not present in the given items. |
||
201 | * |
||
202 | * @param mixed $items |
||
203 | * @return static |
||
204 | */ |
||
205 | 6 | public function diff($items) |
|
209 | |||
210 | /** |
||
211 | * Get the items in the collection that are not present in the given items. |
||
212 | * |
||
213 | * @param mixed $items |
||
214 | * @param callable $callback |
||
215 | * @return static |
||
216 | */ |
||
217 | 4 | public function diffUsing($items, callable $callback) |
|
221 | |||
222 | /** |
||
223 | * Get the items in the collection whose keys and values are not present in the given items. |
||
224 | * |
||
225 | * @param mixed $items |
||
226 | * @return static |
||
227 | */ |
||
228 | 4 | public function diffAssoc($items) |
|
232 | |||
233 | /** |
||
234 | * Get the items in the collection whose keys and values are not present in the given items. |
||
235 | * |
||
236 | * @param mixed $items |
||
237 | * @param callable $callback |
||
238 | * @return static |
||
239 | */ |
||
240 | 2 | public function diffAssocUsing($items, callable $callback) |
|
244 | |||
245 | /** |
||
246 | * Get the items in the collection whose keys are not present in the given items. |
||
247 | * |
||
248 | * @param mixed $items |
||
249 | * @return static |
||
250 | */ |
||
251 | 4 | public function diffKeys($items) |
|
255 | |||
256 | /** |
||
257 | * Get the items in the collection whose keys are not present in the given items. |
||
258 | * |
||
259 | * @param mixed $items |
||
260 | * @param callable $callback |
||
261 | * @return static |
||
262 | */ |
||
263 | 2 | public function diffKeysUsing($items, callable $callback) |
|
267 | |||
268 | /** |
||
269 | * Retrieve duplicate items from the collection. |
||
270 | * |
||
271 | * @param callable|null $callback |
||
272 | * @param bool $strict |
||
273 | * @return static |
||
274 | */ |
||
275 | public function duplicates($callback = null, $strict = false) |
||
276 | { |
||
277 | $items = $this->map($this->valueRetriever($callback)); |
||
278 | |||
279 | $uniqueItems = $items->unique(null, $strict); |
||
280 | |||
281 | $compare = $this->duplicateComparator($strict); |
||
282 | |||
283 | $duplicates = new static; |
||
284 | |||
285 | foreach ($items as $key => $value) { |
||
286 | if ($uniqueItems->isNotEmpty() && $compare($value, $uniqueItems->first())) { |
||
287 | $uniqueItems->shift(); |
||
288 | } else { |
||
289 | $duplicates[$key] = $value; |
||
290 | } |
||
291 | } |
||
292 | |||
293 | return $duplicates; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Retrieve duplicate items from the collection using strict comparison. |
||
298 | * |
||
299 | * @param callable|null $callback |
||
300 | * @return static |
||
301 | */ |
||
302 | public function duplicatesStrict($callback = null) |
||
303 | { |
||
304 | return $this->duplicates($callback, true); |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Get the comparison function to detect duplicates. |
||
309 | * |
||
310 | * @param bool $strict |
||
311 | * @return \Closure |
||
312 | */ |
||
313 | protected function duplicateComparator($strict) |
||
314 | { |
||
315 | if ($strict) { |
||
316 | return function ($a, $b) { |
||
317 | return $a === $b; |
||
318 | }; |
||
319 | } |
||
320 | |||
321 | return function ($a, $b) { |
||
322 | return $a == $b; |
||
323 | }; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Get all items except for those with the specified keys. |
||
328 | * |
||
329 | * @param \IlluminateAgnostic\Collection\Support\Collection|mixed $keys |
||
330 | * @return static |
||
331 | */ |
||
332 | 4 | public function except($keys) |
|
342 | |||
343 | /** |
||
344 | * Run a filter over each of the items. |
||
345 | * |
||
346 | * @param callable|null $callback |
||
347 | * @return static |
||
348 | */ |
||
349 | 18 | public function filter(callable $callback = null) |
|
357 | |||
358 | /** |
||
359 | * Get the first item from the collection passing the given truth test. |
||
360 | * |
||
361 | * @param callable|null $callback |
||
362 | * @param mixed $default |
||
363 | * @return mixed |
||
364 | */ |
||
365 | 11 | public function first(callable $callback = null, $default = null) |
|
369 | |||
370 | /** |
||
371 | * Get a flattened array of the items in the collection. |
||
372 | * |
||
373 | * @param int $depth |
||
374 | * @return static |
||
375 | */ |
||
376 | 3 | public function flatten($depth = INF) |
|
380 | |||
381 | /** |
||
382 | * Flip the items in the collection. |
||
383 | * |
||
384 | * @return static |
||
385 | */ |
||
386 | 1 | public function flip() |
|
390 | |||
391 | /** |
||
392 | * Remove an item from the collection by key. |
||
393 | * |
||
394 | * @param string|array $keys |
||
395 | * @return $this |
||
396 | */ |
||
397 | 2 | public function forget($keys) |
|
405 | |||
406 | /** |
||
407 | * Get an item from the collection by key. |
||
408 | * |
||
409 | * @param mixed $key |
||
410 | * @param mixed $default |
||
411 | * @return mixed |
||
412 | */ |
||
413 | 6 | public function get($key, $default = null) |
|
414 | { |
||
415 | 6 | if ($this->offsetExists($key)) { |
|
416 | 6 | return $this->items[$key]; |
|
417 | } |
||
418 | |||
419 | return value($default); |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Group an associative array by a field or using a callback. |
||
424 | * |
||
425 | * @param array|callable|string $groupBy |
||
426 | * @param bool $preserveKeys |
||
427 | * @return static |
||
428 | */ |
||
429 | 14 | public function groupBy($groupBy, $preserveKeys = false) |
|
430 | { |
||
431 | 14 | if (is_array($groupBy)) { |
|
432 | $nextGroups = $groupBy; |
||
433 | |||
434 | $groupBy = array_shift($nextGroups); |
||
435 | } |
||
436 | |||
437 | 14 | $groupBy = $this->valueRetriever($groupBy); |
|
438 | |||
439 | 14 | $results = []; |
|
440 | |||
441 | 14 | foreach ($this->items as $key => $value) { |
|
442 | 14 | $groupKeys = $groupBy($value, $key); |
|
443 | |||
444 | 14 | if (! is_array($groupKeys)) { |
|
445 | 10 | $groupKeys = [$groupKeys]; |
|
446 | } |
||
447 | |||
448 | 14 | foreach ($groupKeys as $groupKey) { |
|
449 | 14 | $groupKey = is_bool($groupKey) ? (int) $groupKey : $groupKey; |
|
450 | |||
451 | 14 | if (! array_key_exists($groupKey, $results)) { |
|
452 | 14 | $results[$groupKey] = new static; |
|
453 | } |
||
454 | |||
455 | 14 | $results[$groupKey]->offsetSet($preserveKeys ? $key : null, $value); |
|
456 | } |
||
457 | } |
||
458 | |||
459 | 14 | $result = new static($results); |
|
460 | |||
461 | 14 | if (! empty($nextGroups)) { |
|
462 | return $result->map->groupBy($nextGroups, $preserveKeys); |
||
463 | } |
||
464 | |||
465 | 14 | return $result; |
|
466 | } |
||
467 | |||
468 | /** |
||
469 | * Key an associative array by a field or using a callback. |
||
470 | * |
||
471 | * @param callable|string $keyBy |
||
472 | * @return static |
||
473 | */ |
||
474 | 3 | public function keyBy($keyBy) |
|
492 | |||
493 | /** |
||
494 | * Determine if an item exists in the collection by key. |
||
495 | * |
||
496 | * @param mixed $key |
||
497 | * @return bool |
||
498 | */ |
||
499 | 2 | public function has($key) |
|
511 | |||
512 | /** |
||
513 | * Concatenate values of a given key as a string. |
||
514 | * |
||
515 | * @param string $value |
||
516 | * @param string $glue |
||
517 | * @return string |
||
518 | */ |
||
519 | 2 | public function implode($value, $glue = null) |
|
520 | { |
||
521 | 2 | $first = $this->first(); |
|
522 | |||
523 | 2 | if (is_array($first) || is_object($first)) { |
|
524 | return implode($glue, $this->pluck($value)->all()); |
||
525 | } |
||
526 | |||
527 | 2 | return implode($value, $this->items); |
|
528 | } |
||
529 | |||
530 | /** |
||
531 | * Intersect the collection with the given items. |
||
532 | * |
||
533 | * @param mixed $items |
||
534 | * @return static |
||
535 | */ |
||
536 | 4 | public function intersect($items) |
|
540 | |||
541 | /** |
||
542 | * Intersect the collection with the given items by key. |
||
543 | * |
||
544 | * @param mixed $items |
||
545 | * @return static |
||
546 | */ |
||
547 | 4 | public function intersectByKeys($items) |
|
553 | |||
554 | /** |
||
555 | * Determine if the collection is empty or not. |
||
556 | * |
||
557 | * @return bool |
||
558 | */ |
||
559 | 26 | public function isEmpty() |
|
563 | |||
564 | /** |
||
565 | * Join all items from the collection using a string. The final items can use a separate glue string. |
||
566 | * |
||
567 | * @param string $glue |
||
568 | * @param string $finalGlue |
||
569 | * @return string |
||
570 | */ |
||
571 | 2 | public function join($glue, $finalGlue = '') |
|
593 | |||
594 | /** |
||
595 | * Get the keys of the collection items. |
||
596 | * |
||
597 | * @return static |
||
598 | */ |
||
599 | 7 | public function keys() |
|
603 | |||
604 | /** |
||
605 | * Get the last item from the collection. |
||
606 | * |
||
607 | * @param callable|null $callback |
||
608 | * @param mixed $default |
||
609 | * @return mixed |
||
610 | */ |
||
611 | 8 | public function last(callable $callback = null, $default = null) |
|
615 | |||
616 | /** |
||
617 | * Get the values of a given key. |
||
618 | * |
||
619 | * @param string|array $value |
||
620 | * @param string|null $key |
||
621 | * @return static |
||
622 | */ |
||
623 | public function pluck($value, $key = null) |
||
624 | { |
||
625 | return new static(Arr::pluck($this->items, $value, $key)); |
||
626 | } |
||
627 | |||
628 | /** |
||
629 | * Run a map over each of the items. |
||
630 | * |
||
631 | * @param callable $callback |
||
632 | * @return static |
||
633 | */ |
||
634 | 79 | public function map(callable $callback) |
|
642 | |||
643 | /** |
||
644 | * Run a dictionary map over the items. |
||
645 | * |
||
646 | * The callback should return an associative array with a single key/value pair. |
||
647 | * |
||
648 | * @param callable $callback |
||
649 | * @return static |
||
650 | */ |
||
651 | 8 | public function mapToDictionary(callable $callback) |
|
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 | * @return static |
||
679 | */ |
||
680 | 5 | public function mapWithKeys(callable $callback) |
|
694 | |||
695 | /** |
||
696 | * Merge the collection with the given items. |
||
697 | * |
||
698 | * @param mixed $items |
||
699 | * @return static |
||
700 | */ |
||
701 | 6 | public function merge($items) |
|
705 | |||
706 | /** |
||
707 | * Recursively merge the collection with the given items. |
||
708 | * |
||
709 | * @param mixed $items |
||
710 | * @return static |
||
711 | */ |
||
712 | 6 | public function mergeRecursive($items) |
|
716 | |||
717 | /** |
||
718 | * Create a collection by using this collection for keys and another for its values. |
||
719 | * |
||
720 | * @param mixed $values |
||
721 | * @return static |
||
722 | */ |
||
723 | 2 | public function combine($values) |
|
727 | |||
728 | /** |
||
729 | * Union the collection with the given items. |
||
730 | * |
||
731 | * @param mixed $items |
||
732 | * @return static |
||
733 | */ |
||
734 | 6 | public function union($items) |
|
738 | |||
739 | /** |
||
740 | * Create a new collection consisting of every n-th element. |
||
741 | * |
||
742 | * @param int $step |
||
743 | * @param int $offset |
||
744 | * @return static |
||
745 | */ |
||
746 | 1 | public function nth($step, $offset = 0) |
|
762 | |||
763 | /** |
||
764 | * Get the items with the specified keys. |
||
765 | * |
||
766 | * @param mixed $keys |
||
767 | * @return static |
||
768 | */ |
||
769 | 1 | public function only($keys) |
|
770 | { |
||
771 | 1 | if (is_null($keys)) { |
|
772 | 1 | return new static($this->items); |
|
773 | } |
||
774 | |||
775 | 1 | if ($keys instanceof Enumerable) { |
|
776 | $keys = $keys->all(); |
||
777 | } |
||
778 | |||
779 | 1 | $keys = is_array($keys) ? $keys : func_get_args(); |
|
780 | |||
781 | 1 | return new static(Arr::only($this->items, $keys)); |
|
782 | } |
||
783 | |||
784 | /** |
||
785 | * Get and remove the last item from the collection. |
||
786 | * |
||
787 | * @return mixed |
||
788 | */ |
||
789 | 3 | public function pop() |
|
793 | |||
794 | /** |
||
795 | * Push an item onto the beginning of the collection. |
||
796 | * |
||
797 | * @param mixed $value |
||
798 | * @param mixed $key |
||
799 | * @return $this |
||
800 | */ |
||
801 | 2 | public function prepend($value, $key = null) |
|
807 | |||
808 | /** |
||
809 | * Push an item onto the end of the collection. |
||
810 | * |
||
811 | * @param mixed $value |
||
812 | * @return $this |
||
813 | */ |
||
814 | 26 | public function push($value) |
|
820 | |||
821 | /** |
||
822 | * Push all of the given items onto the collection. |
||
823 | * |
||
824 | * @param iterable $source |
||
825 | * @return static |
||
826 | */ |
||
827 | 14 | public function concat($source) |
|
837 | |||
838 | /** |
||
839 | * Get and remove an item from the collection. |
||
840 | * |
||
841 | * @param mixed $key |
||
842 | * @param mixed $default |
||
843 | * @return mixed |
||
844 | */ |
||
845 | 2 | public function pull($key, $default = null) |
|
849 | |||
850 | /** |
||
851 | * Put an item in the collection by key. |
||
852 | * |
||
853 | * @param mixed $key |
||
854 | * @param mixed $value |
||
855 | * @return $this |
||
856 | */ |
||
857 | 3 | public function put($key, $value) |
|
863 | |||
864 | /** |
||
865 | * Get one or a specified number of items randomly from the collection. |
||
866 | * |
||
867 | * @param int|null $number |
||
868 | * @return static|mixed |
||
869 | * |
||
870 | * @throws \InvalidArgumentException |
||
871 | */ |
||
872 | 6 | public function random($number = null) |
|
880 | |||
881 | /** |
||
882 | * Reduce the collection to a single value. |
||
883 | * |
||
884 | * @param callable $callback |
||
885 | * @param mixed $initial |
||
886 | * @return mixed |
||
887 | */ |
||
888 | 8 | public function reduce(callable $callback, $initial = null) |
|
892 | |||
893 | /** |
||
894 | * Replace the collection items with the given items. |
||
895 | * |
||
896 | * @param mixed $items |
||
897 | * @return static |
||
898 | */ |
||
899 | 3 | public function replace($items) |
|
903 | |||
904 | /** |
||
905 | * Recursively replace the collection items with the given items. |
||
906 | * |
||
907 | * @param mixed $items |
||
908 | * @return static |
||
909 | */ |
||
910 | 6 | public function replaceRecursive($items) |
|
914 | |||
915 | /** |
||
916 | * Reverse items order. |
||
917 | * |
||
918 | * @return static |
||
919 | */ |
||
920 | 2 | public function reverse() |
|
924 | |||
925 | /** |
||
926 | * Search the collection for a given value and return the corresponding key if successful. |
||
927 | * |
||
928 | * @param mixed $value |
||
929 | * @param bool $strict |
||
930 | * @return mixed |
||
931 | */ |
||
932 | 3 | public function search($value, $strict = false) |
|
946 | |||
947 | /** |
||
948 | * Get and remove the first item from the collection. |
||
949 | * |
||
950 | * @return mixed |
||
951 | */ |
||
952 | 1 | public function shift() |
|
956 | |||
957 | /** |
||
958 | * Shuffle the items in the collection. |
||
959 | * |
||
960 | * @param int $seed |
||
961 | * @return static |
||
962 | */ |
||
963 | 1 | public function shuffle($seed = null) |
|
967 | |||
968 | /** |
||
969 | * Skip the first {$count} items. |
||
970 | * |
||
971 | * @param int $count |
||
972 | * @return static |
||
973 | */ |
||
974 | 1 | public function skip($count) |
|
978 | |||
979 | /** |
||
980 | * Slice the underlying collection array. |
||
981 | * |
||
982 | * @param int $offset |
||
983 | * @param int $length |
||
984 | * @return static |
||
985 | */ |
||
986 | 16 | public function slice($offset, $length = null) |
|
990 | |||
991 | /** |
||
992 | * Split a collection into a certain number of groups. |
||
993 | * |
||
994 | * @param int $numberOfGroups |
||
995 | * @return static |
||
996 | */ |
||
997 | 14 | public function split($numberOfGroups) |
|
1027 | |||
1028 | /** |
||
1029 | * Chunk the collection into chunks of the given size. |
||
1030 | * |
||
1031 | * @param int $size |
||
1032 | * @return static |
||
1033 | */ |
||
1034 | 3 | public function chunk($size) |
|
1048 | |||
1049 | /** |
||
1050 | * Sort through each item with a callback. |
||
1051 | * |
||
1052 | * @param callable|null $callback |
||
1053 | * @return static |
||
1054 | */ |
||
1055 | 10 | public function sort(callable $callback = null) |
|
1065 | |||
1066 | /** |
||
1067 | * Sort the collection using the given callback. |
||
1068 | * |
||
1069 | * @param callable|string $callback |
||
1070 | * @param int $options |
||
1071 | * @param bool $descending |
||
1072 | * @return static |
||
1073 | */ |
||
1074 | 4 | public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
|
1099 | |||
1100 | /** |
||
1101 | * Sort the collection in descending order using the given callback. |
||
1102 | * |
||
1103 | * @param callable|string $callback |
||
1104 | * @param int $options |
||
1105 | * @return static |
||
1106 | */ |
||
1107 | 2 | public function sortByDesc($callback, $options = SORT_REGULAR) |
|
1111 | |||
1112 | /** |
||
1113 | * Sort the collection keys. |
||
1114 | * |
||
1115 | * @param int $options |
||
1116 | * @param bool $descending |
||
1117 | * @return static |
||
1118 | */ |
||
1119 | 4 | public function sortKeys($options = SORT_REGULAR, $descending = false) |
|
1127 | |||
1128 | /** |
||
1129 | * Sort the collection keys in descending order. |
||
1130 | * |
||
1131 | * @param int $options |
||
1132 | * @return static |
||
1133 | */ |
||
1134 | 2 | public function sortKeysDesc($options = SORT_REGULAR) |
|
1138 | |||
1139 | /** |
||
1140 | * Splice a portion of the underlying collection array. |
||
1141 | * |
||
1142 | * @param int $offset |
||
1143 | * @param int|null $length |
||
1144 | * @param mixed $replacement |
||
1145 | * @return static |
||
1146 | */ |
||
1147 | 1 | public function splice($offset, $length = null, $replacement = []) |
|
1155 | |||
1156 | /** |
||
1157 | * Take the first or last {$limit} items. |
||
1158 | * |
||
1159 | * @param int $limit |
||
1160 | * @return static |
||
1161 | */ |
||
1162 | 3 | public function take($limit) |
|
1170 | |||
1171 | /** |
||
1172 | * Transform each item in the collection using a callback. |
||
1173 | * |
||
1174 | * @param callable $callback |
||
1175 | * @return $this |
||
1176 | */ |
||
1177 | 1 | public function transform(callable $callback) |
|
1183 | |||
1184 | /** |
||
1185 | * Reset the keys on the underlying array. |
||
1186 | * |
||
1187 | * @return static |
||
1188 | */ |
||
1189 | 30 | public function values() |
|
1193 | |||
1194 | /** |
||
1195 | * Zip the collection together with one or more arrays. |
||
1196 | * |
||
1197 | * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); |
||
1198 | * => [[1, 4], [2, 5], [3, 6]] |
||
1199 | * |
||
1200 | * @param mixed ...$items |
||
1201 | * @return static |
||
1202 | */ |
||
1203 | 1 | public function zip($items) |
|
1215 | |||
1216 | /** |
||
1217 | * Pad collection to the specified length with a value. |
||
1218 | * |
||
1219 | * @param int $size |
||
1220 | * @param mixed $value |
||
1221 | * @return static |
||
1222 | */ |
||
1223 | 2 | public function pad($size, $value) |
|
1227 | |||
1228 | /** |
||
1229 | * Get an iterator for the items. |
||
1230 | * |
||
1231 | * @return \ArrayIterator |
||
1232 | */ |
||
1233 | 79 | public function getIterator() |
|
1237 | |||
1238 | /** |
||
1239 | * Count the number of items in the collection. |
||
1240 | * |
||
1241 | * @return int |
||
1242 | */ |
||
1243 | 37 | public function count() |
|
1247 | |||
1248 | /** |
||
1249 | * Add an item to the collection. |
||
1250 | * |
||
1251 | * @param mixed $item |
||
1252 | * @return $this |
||
1253 | */ |
||
1254 | 1 | public function add($item) |
|
1260 | |||
1261 | /** |
||
1262 | * Get a base Support collection instance from this collection. |
||
1263 | * |
||
1264 | * @return \IlluminateAgnostic\Collection\Support\Collection |
||
1265 | */ |
||
1266 | public function toBase() |
||
1270 | |||
1271 | /** |
||
1272 | * Determine if an item exists at an offset. |
||
1273 | * |
||
1274 | * @param mixed $key |
||
1275 | * @return bool |
||
1276 | */ |
||
1277 | 16 | public function offsetExists($key) |
|
1281 | |||
1282 | /** |
||
1283 | * Get an item at a given offset. |
||
1284 | * |
||
1285 | * @param mixed $key |
||
1286 | * @return mixed |
||
1287 | */ |
||
1288 | 7 | public function offsetGet($key) |
|
1292 | |||
1293 | /** |
||
1294 | * Set the item at a given offset. |
||
1295 | * |
||
1296 | * @param mixed $key |
||
1297 | * @param mixed $value |
||
1298 | * @return void |
||
1299 | */ |
||
1300 | 27 | public function offsetSet($key, $value) |
|
1308 | |||
1309 | /** |
||
1310 | * Unset the item at a given offset. |
||
1311 | * |
||
1312 | * @param string $key |
||
1313 | * @return void |
||
1314 | */ |
||
1315 | 4 | public function offsetUnset($key) |
|
1319 | } |
||
1320 |
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.