Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Collection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Collection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable |
||
| 43 | { |
||
| 44 | use Macroable; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The items contained in the collection. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $items = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The methods that can be proxied. |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected static $proxies = [ |
||
| 59 | 'average', 'avg', 'contains', 'each', 'every', 'filter', 'first', |
||
| 60 | 'flatMap', 'groupBy', 'keyBy', 'map', 'max', 'min', 'partition', |
||
| 61 | 'reject', 'sortBy', 'sortByDesc', 'sum', 'unique', |
||
| 62 | ]; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Create a new collection. |
||
| 66 | * |
||
| 67 | * @param mixed $items |
||
| 68 | * @return void |
||
|
|
|||
| 69 | */ |
||
| 70 | 221 | 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 | 110 | 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 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 mixed $key |
||
| 206 | * @return array|null |
||
| 207 | */ |
||
| 208 | 4 | public function mode($key = null) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Collapse the collection of items into a single array. |
||
| 235 | * |
||
| 236 | * @return static |
||
| 237 | */ |
||
| 238 | 3 | public function collapse() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Determine if an item exists in the collection. |
||
| 245 | * |
||
| 246 | * @param mixed $key |
||
| 247 | * @param mixed $operator |
||
| 248 | * @param mixed $value |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | 3 | public function contains($key, $operator = null, $value = null) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Determine if an item exists in the collection using strict comparison. |
||
| 268 | * |
||
| 269 | * @param mixed $key |
||
| 270 | * @param mixed $value |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | 1 | public function containsStrict($key, $value = null) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Cross join with the given lists, returning all possible permutations. |
||
| 290 | * |
||
| 291 | * @param mixed ...$lists |
||
| 292 | * @return static |
||
| 293 | */ |
||
| 294 | 1 | public function crossJoin(...$lists) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Dump the collection and end the script. |
||
| 303 | * |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | public function dd(...$args) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Dump the collection. |
||
| 315 | * |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function dump() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get the items in the collection that are not present in the given items. |
||
| 331 | * |
||
| 332 | * @param mixed $items |
||
| 333 | * @return static |
||
| 334 | */ |
||
| 335 | 3 | public function diff($items) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Get the items in the collection that are not present in the given items. |
||
| 342 | * |
||
| 343 | * @param mixed $items |
||
| 344 | * @param callable $callback |
||
| 345 | * @return static |
||
| 346 | */ |
||
| 347 | 2 | public function diffUsing($items, callable $callback) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Get the items in the collection whose keys and values are not present in the given items. |
||
| 354 | * |
||
| 355 | * @param mixed $items |
||
| 356 | * @return static |
||
| 357 | */ |
||
| 358 | 2 | public function diffAssoc($items) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Get the items in the collection whose keys and values are not present in the given items. |
||
| 365 | * |
||
| 366 | * @param mixed $items |
||
| 367 | * @param callable $callback |
||
| 368 | * @return static |
||
| 369 | */ |
||
| 370 | 1 | public function diffAssocUsing($items, callable $callback) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Get the items in the collection whose keys are not present in the given items. |
||
| 377 | * |
||
| 378 | * @param mixed $items |
||
| 379 | * @return static |
||
| 380 | */ |
||
| 381 | 2 | public function diffKeys($items) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Get the items in the collection whose keys are not present in the given items. |
||
| 388 | * |
||
| 389 | * @param mixed $items |
||
| 390 | * @param callable $callback |
||
| 391 | * @return static |
||
| 392 | */ |
||
| 393 | 1 | public function diffKeysUsing($items, callable $callback) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Execute a callback over each item. |
||
| 400 | * |
||
| 401 | * @param callable $callback |
||
| 402 | * @return $this |
||
| 403 | */ |
||
| 404 | 7 | public function each(callable $callback) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Execute a callback over each nested chunk of items. |
||
| 417 | * |
||
| 418 | * @param callable $callback |
||
| 419 | * @return static |
||
| 420 | */ |
||
| 421 | 1 | public function eachSpread(callable $callback) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Determine if all items in the collection pass the given test. |
||
| 432 | * |
||
| 433 | * @param string|callable $key |
||
| 434 | * @param mixed $operator |
||
| 435 | * @param mixed $value |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | 1 | public function every($key, $operator = null, $value = null) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Get all items except for those with the specified keys. |
||
| 457 | * |
||
| 458 | * @param \IlluminateAgnostic\Collection\Support\Collection|mixed $keys |
||
| 459 | * @return static |
||
| 460 | */ |
||
| 461 | 2 | public function except($keys) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Run a filter over each of the items. |
||
| 474 | * |
||
| 475 | * @param callable|null $callback |
||
| 476 | * @return static |
||
| 477 | */ |
||
| 478 | 29 | public function filter(callable $callback = null) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Apply the callback if the value is truthy. |
||
| 489 | * |
||
| 490 | * @param bool $value |
||
| 491 | * @param callable $callback |
||
| 492 | * @param callable $default |
||
| 493 | * @return static|mixed |
||
| 494 | */ |
||
| 495 | 4 | public function when($value, callable $callback, callable $default = null) |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Apply the callback if the value is falsy. |
||
| 508 | * |
||
| 509 | * @param bool $value |
||
| 510 | * @param callable $callback |
||
| 511 | * @param callable $default |
||
| 512 | * @return static|mixed |
||
| 513 | */ |
||
| 514 | 2 | public function unless($value, callable $callback, callable $default = null) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Filter items by the given key value pair. |
||
| 521 | * |
||
| 522 | * @param string $key |
||
| 523 | * @param mixed $operator |
||
| 524 | * @param mixed $value |
||
| 525 | * @return static |
||
| 526 | */ |
||
| 527 | 2 | public function where($key, $operator = null, $value = null) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Get an operator checker callback. |
||
| 534 | * |
||
| 535 | * @param string $key |
||
| 536 | * @param string $operator |
||
| 537 | * @param mixed $value |
||
| 538 | * @return \Closure |
||
| 539 | */ |
||
| 540 | 7 | protected function operatorForWhere($key, $operator = null, $value = null) |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Filter items by the given key value pair using strict comparison. |
||
| 583 | * |
||
| 584 | * @param string $key |
||
| 585 | * @param mixed $value |
||
| 586 | * @return static |
||
| 587 | */ |
||
| 588 | 1 | public function whereStrict($key, $value) |
|
| 592 | |||
| 593 | /** |
||
| 594 | * Filter items by the given key value pair. |
||
| 595 | * |
||
| 596 | * @param string $key |
||
| 597 | * @param mixed $values |
||
| 598 | * @param bool $strict |
||
| 599 | * @return static |
||
| 600 | */ |
||
| 601 | 2 | View Code Duplication | public function whereIn($key, $values, $strict = false) |
| 609 | |||
| 610 | /** |
||
| 611 | * Filter items by the given key value pair using strict comparison. |
||
| 612 | * |
||
| 613 | * @param string $key |
||
| 614 | * @param mixed $values |
||
| 615 | * @return static |
||
| 616 | */ |
||
| 617 | 1 | public function whereInStrict($key, $values) |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Filter items by the given key value pair. |
||
| 624 | * |
||
| 625 | * @param string $key |
||
| 626 | * @param mixed $values |
||
| 627 | * @param bool $strict |
||
| 628 | * @return static |
||
| 629 | */ |
||
| 630 | 2 | View Code Duplication | public function whereNotIn($key, $values, $strict = false) |
| 638 | |||
| 639 | /** |
||
| 640 | * Filter items by the given key value pair using strict comparison. |
||
| 641 | * |
||
| 642 | * @param string $key |
||
| 643 | * @param mixed $values |
||
| 644 | * @return static |
||
| 645 | */ |
||
| 646 | 1 | public function whereNotInStrict($key, $values) |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Filter the items, removing any items that don't match the given type. |
||
| 653 | * |
||
| 654 | * @param string $type |
||
| 655 | * @return static |
||
| 656 | */ |
||
| 657 | 1 | public function whereInstanceOf($type) |
|
| 663 | |||
| 664 | /** |
||
| 665 | * Get the first item from the collection. |
||
| 666 | * |
||
| 667 | * @param callable|null $callback |
||
| 668 | * @param mixed $default |
||
| 669 | * @return mixed |
||
| 670 | */ |
||
| 671 | 11 | public function first(callable $callback = null, $default = null) |
|
| 675 | |||
| 676 | /** |
||
| 677 | * Get the first item by the given key value pair. |
||
| 678 | * |
||
| 679 | * @param string $key |
||
| 680 | * @param mixed $operator |
||
| 681 | * @param mixed $value |
||
| 682 | * @return static |
||
| 683 | */ |
||
| 684 | 1 | public function firstWhere($key, $operator, $value = null) |
|
| 688 | |||
| 689 | /** |
||
| 690 | * Get a flattened array of the items in the collection. |
||
| 691 | * |
||
| 692 | * @param int $depth |
||
| 693 | * @return static |
||
| 694 | */ |
||
| 695 | 3 | public function flatten($depth = INF) |
|
| 699 | |||
| 700 | /** |
||
| 701 | * Flip the items in the collection. |
||
| 702 | * |
||
| 703 | * @return static |
||
| 704 | */ |
||
| 705 | 1 | public function flip() |
|
| 709 | |||
| 710 | /** |
||
| 711 | * Remove an item from the collection by key. |
||
| 712 | * |
||
| 713 | * @param string|array $keys |
||
| 714 | * @return $this |
||
| 715 | */ |
||
| 716 | 2 | public function forget($keys) |
|
| 724 | |||
| 725 | /** |
||
| 726 | * Get an item from the collection by key. |
||
| 727 | * |
||
| 728 | * @param mixed $key |
||
| 729 | * @param mixed $default |
||
| 730 | * @return mixed |
||
| 731 | */ |
||
| 732 | 6 | public function get($key, $default = null) |
|
| 740 | |||
| 741 | /** |
||
| 742 | * Group an associative array by a field or using a callback. |
||
| 743 | * |
||
| 744 | * @param callable|string $groupBy |
||
| 745 | * @param bool $preserveKeys |
||
| 746 | * @return static |
||
| 747 | */ |
||
| 748 | 8 | public function groupBy($groupBy, $preserveKeys = false) |
|
| 786 | |||
| 787 | /** |
||
| 788 | * Key an associative array by a field or using a callback. |
||
| 789 | * |
||
| 790 | * @param callable|string $keyBy |
||
| 791 | * @return static |
||
| 792 | */ |
||
| 793 | 3 | public function keyBy($keyBy) |
|
| 811 | |||
| 812 | /** |
||
| 813 | * Determine if an item exists in the collection by key. |
||
| 814 | * |
||
| 815 | * @param mixed $key |
||
| 816 | * @return bool |
||
| 817 | */ |
||
| 818 | 2 | public function has($key) |
|
| 830 | |||
| 831 | /** |
||
| 832 | * Concatenate values of a given key as a string. |
||
| 833 | * |
||
| 834 | * @param string $value |
||
| 835 | * @param string $glue |
||
| 836 | * @return string |
||
| 837 | */ |
||
| 838 | 1 | public function implode($value, $glue = null) |
|
| 848 | |||
| 849 | /** |
||
| 850 | * Intersect the collection with the given items. |
||
| 851 | * |
||
| 852 | * @param mixed $items |
||
| 853 | * @return static |
||
| 854 | */ |
||
| 855 | 2 | public function intersect($items) |
|
| 859 | |||
| 860 | /** |
||
| 861 | * Intersect the collection with the given items by key. |
||
| 862 | * |
||
| 863 | * @param mixed $items |
||
| 864 | * @return static |
||
| 865 | */ |
||
| 866 | 2 | public function intersectByKeys($items) |
|
| 872 | |||
| 873 | /** |
||
| 874 | * Determine if the collection is empty or not. |
||
| 875 | * |
||
| 876 | * @return bool |
||
| 877 | */ |
||
| 878 | 10 | public function isEmpty() |
|
| 882 | |||
| 883 | /** |
||
| 884 | * Determine if the collection is not empty. |
||
| 885 | * |
||
| 886 | * @return bool |
||
| 887 | */ |
||
| 888 | 1 | public function isNotEmpty() |
|
| 892 | |||
| 893 | /** |
||
| 894 | * Determine if the given value is callable, but not a string. |
||
| 895 | * |
||
| 896 | * @param mixed $value |
||
| 897 | * @return bool |
||
| 898 | */ |
||
| 899 | 44 | protected function useAsCallable($value) |
|
| 903 | |||
| 904 | /** |
||
| 905 | * Get the keys of the collection items. |
||
| 906 | * |
||
| 907 | * @return static |
||
| 908 | */ |
||
| 909 | 6 | public function keys() |
|
| 913 | |||
| 914 | /** |
||
| 915 | * Get the last item from the collection. |
||
| 916 | * |
||
| 917 | * @param callable|null $callback |
||
| 918 | * @param mixed $default |
||
| 919 | * @return mixed |
||
| 920 | */ |
||
| 921 | 7 | public function last(callable $callback = null, $default = null) |
|
| 925 | |||
| 926 | /** |
||
| 927 | * Get the values of a given key. |
||
| 928 | * |
||
| 929 | * @param string|array $value |
||
| 930 | * @param string|null $key |
||
| 931 | * @return static |
||
| 932 | */ |
||
| 933 | 10 | public function pluck($value, $key = null) |
|
| 937 | |||
| 938 | /** |
||
| 939 | * Run a map over each of the items. |
||
| 940 | * |
||
| 941 | * @param callable $callback |
||
| 942 | * @return static |
||
| 943 | */ |
||
| 944 | 24 | public function map(callable $callback) |
|
| 952 | |||
| 953 | /** |
||
| 954 | * Run a map over each nested chunk of items. |
||
| 955 | * |
||
| 956 | * @param callable $callback |
||
| 957 | * @return static |
||
| 958 | */ |
||
| 959 | 1 | public function mapSpread(callable $callback) |
|
| 967 | |||
| 968 | /** |
||
| 969 | * Run a dictionary map over the items. |
||
| 970 | * |
||
| 971 | * The callback should return an associative array with a single key/value pair. |
||
| 972 | * |
||
| 973 | * @param callable $callback |
||
| 974 | * @return static |
||
| 975 | */ |
||
| 976 | 4 | public function mapToDictionary(callable $callback) |
|
| 996 | |||
| 997 | /** |
||
| 998 | * Run a grouping map over the items. |
||
| 999 | * |
||
| 1000 | * The callback should return an associative array with a single key/value pair. |
||
| 1001 | * |
||
| 1002 | * @param callable $callback |
||
| 1003 | * @return static |
||
| 1004 | */ |
||
| 1005 | 2 | public function mapToGroups(callable $callback) |
|
| 1011 | |||
| 1012 | /** |
||
| 1013 | * Run an associative map over each of the items. |
||
| 1014 | * |
||
| 1015 | * The callback should return an associative array with a single key/value pair. |
||
| 1016 | * |
||
| 1017 | * @param callable $callback |
||
| 1018 | * @return static |
||
| 1019 | */ |
||
| 1020 | 5 | public function mapWithKeys(callable $callback) |
|
| 1034 | |||
| 1035 | /** |
||
| 1036 | * Map a collection and flatten the result by a single level. |
||
| 1037 | * |
||
| 1038 | * @param callable $callback |
||
| 1039 | * @return static |
||
| 1040 | */ |
||
| 1041 | 1 | public function flatMap(callable $callback) |
|
| 1045 | |||
| 1046 | /** |
||
| 1047 | * Map the values into a new class. |
||
| 1048 | * |
||
| 1049 | * @param string $class |
||
| 1050 | * @return static |
||
| 1051 | */ |
||
| 1052 | 1 | public function mapInto($class) |
|
| 1058 | |||
| 1059 | /** |
||
| 1060 | * Get the max value of a given key. |
||
| 1061 | * |
||
| 1062 | * @param callable|string|null $callback |
||
| 1063 | * @return mixed |
||
| 1064 | */ |
||
| 1065 | 1 | public function max($callback = null) |
|
| 1077 | |||
| 1078 | /** |
||
| 1079 | * Merge the collection with the given items. |
||
| 1080 | * |
||
| 1081 | * @param mixed $items |
||
| 1082 | * @return static |
||
| 1083 | */ |
||
| 1084 | 3 | public function merge($items) |
|
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Create a collection by using this collection for keys and another for its values. |
||
| 1091 | * |
||
| 1092 | * @param mixed $values |
||
| 1093 | * @return static |
||
| 1094 | */ |
||
| 1095 | 2 | public function combine($values) |
|
| 1099 | |||
| 1100 | /** |
||
| 1101 | * Union the collection with the given items. |
||
| 1102 | * |
||
| 1103 | * @param mixed $items |
||
| 1104 | * @return static |
||
| 1105 | */ |
||
| 1106 | 3 | public function union($items) |
|
| 1110 | |||
| 1111 | /** |
||
| 1112 | * Get the min value of a given key. |
||
| 1113 | * |
||
| 1114 | * @param callable|string|null $callback |
||
| 1115 | * @return mixed |
||
| 1116 | */ |
||
| 1117 | 1 | public function min($callback = null) |
|
| 1129 | |||
| 1130 | /** |
||
| 1131 | * Create a new collection consisting of every n-th element. |
||
| 1132 | * |
||
| 1133 | * @param int $step |
||
| 1134 | * @param int $offset |
||
| 1135 | * @return static |
||
| 1136 | */ |
||
| 1137 | 1 | public function nth($step, $offset = 0) |
|
| 1153 | |||
| 1154 | /** |
||
| 1155 | * Get the items with the specified keys. |
||
| 1156 | * |
||
| 1157 | * @param mixed $keys |
||
| 1158 | * @return static |
||
| 1159 | */ |
||
| 1160 | 1 | public function only($keys) |
|
| 1174 | |||
| 1175 | /** |
||
| 1176 | * "Paginate" the collection by slicing it into a smaller collection. |
||
| 1177 | * |
||
| 1178 | * @param int $page |
||
| 1179 | * @param int $perPage |
||
| 1180 | * @return static |
||
| 1181 | */ |
||
| 1182 | 1 | public function forPage($page, $perPage) |
|
| 1188 | |||
| 1189 | /** |
||
| 1190 | * Partition the collection into two arrays using the given callback or key. |
||
| 1191 | * |
||
| 1192 | * @param callable|string $key |
||
| 1193 | * @param mixed $operator |
||
| 1194 | * @param mixed $value |
||
| 1195 | * @return static |
||
| 1196 | */ |
||
| 1197 | 7 | public function partition($key, $operator = null, $value = null) |
|
| 1211 | |||
| 1212 | /** |
||
| 1213 | * Pass the collection to the given callback and return the result. |
||
| 1214 | * |
||
| 1215 | * @param callable $callback |
||
| 1216 | * @return mixed |
||
| 1217 | */ |
||
| 1218 | 1 | public function pipe(callable $callback) |
|
| 1222 | |||
| 1223 | /** |
||
| 1224 | * Get and remove the last item from the collection. |
||
| 1225 | * |
||
| 1226 | * @return mixed |
||
| 1227 | */ |
||
| 1228 | 1 | public function pop() |
|
| 1232 | |||
| 1233 | /** |
||
| 1234 | * Push an item onto the beginning of the collection. |
||
| 1235 | * |
||
| 1236 | * @param mixed $value |
||
| 1237 | * @param mixed $key |
||
| 1238 | * @return $this |
||
| 1239 | */ |
||
| 1240 | 1 | public function prepend($value, $key = null) |
|
| 1246 | |||
| 1247 | /** |
||
| 1248 | * Push an item onto the end of the collection. |
||
| 1249 | * |
||
| 1250 | * @param mixed $value |
||
| 1251 | * @return $this |
||
| 1252 | */ |
||
| 1253 | 13 | public function push($value) |
|
| 1259 | |||
| 1260 | /** |
||
| 1261 | * Push all of the given items onto the collection. |
||
| 1262 | * |
||
| 1263 | * @param \Traversable|array $source |
||
| 1264 | * @return static |
||
| 1265 | */ |
||
| 1266 | 2 | public function concat($source) |
|
| 1276 | |||
| 1277 | /** |
||
| 1278 | * Get and remove an item from the collection. |
||
| 1279 | * |
||
| 1280 | * @param mixed $key |
||
| 1281 | * @param mixed $default |
||
| 1282 | * @return mixed |
||
| 1283 | */ |
||
| 1284 | 3 | public function pull($key, $default = null) |
|
| 1288 | |||
| 1289 | /** |
||
| 1290 | * Put an item in the collection by key. |
||
| 1291 | * |
||
| 1292 | * @param mixed $key |
||
| 1293 | * @param mixed $value |
||
| 1294 | * @return $this |
||
| 1295 | */ |
||
| 1296 | 3 | public function put($key, $value) |
|
| 1302 | |||
| 1303 | /** |
||
| 1304 | * Get one or a specified number of items randomly from the collection. |
||
| 1305 | * |
||
| 1306 | * @param int|null $number |
||
| 1307 | * @return static|mixed |
||
| 1308 | * |
||
| 1309 | * @throws \InvalidArgumentException |
||
| 1310 | */ |
||
| 1311 | 3 | public function random($number = null) |
|
| 1319 | |||
| 1320 | /** |
||
| 1321 | * Reduce the collection to a single value. |
||
| 1322 | * |
||
| 1323 | * @param callable $callback |
||
| 1324 | * @param mixed $initial |
||
| 1325 | * @return mixed |
||
| 1326 | */ |
||
| 1327 | 5 | public function reduce(callable $callback, $initial = null) |
|
| 1331 | |||
| 1332 | /** |
||
| 1333 | * Create a collection of all elements that do not pass a given truth test. |
||
| 1334 | * |
||
| 1335 | * @param callable|mixed $callback |
||
| 1336 | * @return static |
||
| 1337 | */ |
||
| 1338 | 8 | public function reject($callback) |
|
| 1350 | |||
| 1351 | /** |
||
| 1352 | * Reverse items order. |
||
| 1353 | * |
||
| 1354 | * @return static |
||
| 1355 | */ |
||
| 1356 | 1 | public function reverse() |
|
| 1360 | |||
| 1361 | /** |
||
| 1362 | * Search the collection for a given value and return the corresponding key if successful. |
||
| 1363 | * |
||
| 1364 | * @param mixed $value |
||
| 1365 | * @param bool $strict |
||
| 1366 | * @return mixed |
||
| 1367 | */ |
||
| 1368 | 2 | public function search($value, $strict = false) |
|
| 1382 | |||
| 1383 | /** |
||
| 1384 | * Get and remove the first item from the collection. |
||
| 1385 | * |
||
| 1386 | * @return mixed |
||
| 1387 | */ |
||
| 1388 | 1 | public function shift() |
|
| 1392 | |||
| 1393 | /** |
||
| 1394 | * Shuffle the items in the collection. |
||
| 1395 | * |
||
| 1396 | * @param int $seed |
||
| 1397 | * @return static |
||
| 1398 | */ |
||
| 1399 | 1 | public function shuffle($seed = null) |
|
| 1403 | |||
| 1404 | /** |
||
| 1405 | * Slice the underlying collection array. |
||
| 1406 | * |
||
| 1407 | * @param int $offset |
||
| 1408 | * @param int $length |
||
| 1409 | * @return static |
||
| 1410 | */ |
||
| 1411 | 10 | public function slice($offset, $length = null) |
|
| 1415 | |||
| 1416 | /** |
||
| 1417 | * Split a collection into a certain number of groups. |
||
| 1418 | * |
||
| 1419 | * @param int $numberOfGroups |
||
| 1420 | * @return static |
||
| 1421 | */ |
||
| 1422 | 7 | public function split($numberOfGroups) |
|
| 1452 | |||
| 1453 | /** |
||
| 1454 | * Chunk the underlying collection array. |
||
| 1455 | * |
||
| 1456 | * @param int $size |
||
| 1457 | * @return static |
||
| 1458 | */ |
||
| 1459 | 3 | public function chunk($size) |
|
| 1473 | |||
| 1474 | /** |
||
| 1475 | * Sort through each item with a callback. |
||
| 1476 | * |
||
| 1477 | * @param callable|null $callback |
||
| 1478 | * @return static |
||
| 1479 | */ |
||
| 1480 | 11 | public function sort(callable $callback = null) |
|
| 1490 | |||
| 1491 | /** |
||
| 1492 | * Sort the collection using the given callback. |
||
| 1493 | * |
||
| 1494 | * @param callable|string $callback |
||
| 1495 | * @param int $options |
||
| 1496 | * @param bool $descending |
||
| 1497 | * @return static |
||
| 1498 | */ |
||
| 1499 | 5 | public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
|
| 1524 | |||
| 1525 | /** |
||
| 1526 | * Sort the collection in descending order using the given callback. |
||
| 1527 | * |
||
| 1528 | * @param callable|string $callback |
||
| 1529 | * @param int $options |
||
| 1530 | * @return static |
||
| 1531 | */ |
||
| 1532 | 1 | public function sortByDesc($callback, $options = SORT_REGULAR) |
|
| 1536 | |||
| 1537 | /** |
||
| 1538 | * Sort the collection keys. |
||
| 1539 | * |
||
| 1540 | * @param int $options |
||
| 1541 | * @param bool $descending |
||
| 1542 | * @return static |
||
| 1543 | */ |
||
| 1544 | 2 | public function sortKeys($options = SORT_REGULAR, $descending = false) |
|
| 1552 | |||
| 1553 | /** |
||
| 1554 | * Sort the collection keys in descending order. |
||
| 1555 | * |
||
| 1556 | * @param int $options |
||
| 1557 | * @return static |
||
| 1558 | */ |
||
| 1559 | public function sortKeysDesc($options = SORT_REGULAR) |
||
| 1563 | |||
| 1564 | /** |
||
| 1565 | * Splice a portion of the underlying collection array. |
||
| 1566 | * |
||
| 1567 | * @param int $offset |
||
| 1568 | * @param int|null $length |
||
| 1569 | * @param mixed $replacement |
||
| 1570 | * @return static |
||
| 1571 | */ |
||
| 1572 | 1 | public function splice($offset, $length = null, $replacement = []) |
|
| 1580 | |||
| 1581 | /** |
||
| 1582 | * Get the sum of the given values. |
||
| 1583 | * |
||
| 1584 | * @param callable|string|null $callback |
||
| 1585 | * @return mixed |
||
| 1586 | */ |
||
| 1587 | 8 | public function sum($callback = null) |
|
| 1599 | |||
| 1600 | /** |
||
| 1601 | * Take the first or last {$limit} items. |
||
| 1602 | * |
||
| 1603 | * @param int $limit |
||
| 1604 | * @return static |
||
| 1605 | */ |
||
| 1606 | 2 | public function take($limit) |
|
| 1614 | |||
| 1615 | /** |
||
| 1616 | * Pass the collection to the given callback and then return it. |
||
| 1617 | * |
||
| 1618 | * @param callable $callback |
||
| 1619 | * @return $this |
||
| 1620 | */ |
||
| 1621 | 1 | public function tap(callable $callback) |
|
| 1627 | |||
| 1628 | /** |
||
| 1629 | * Transform each item in the collection using a callback. |
||
| 1630 | * |
||
| 1631 | * @param callable $callback |
||
| 1632 | * @return $this |
||
| 1633 | */ |
||
| 1634 | 1 | public function transform(callable $callback) |
|
| 1640 | |||
| 1641 | /** |
||
| 1642 | * Return only unique items from the collection array. |
||
| 1643 | * |
||
| 1644 | * @param string|callable|null $key |
||
| 1645 | * @param bool $strict |
||
| 1646 | * @return static |
||
| 1647 | */ |
||
| 1648 | 5 | public function unique($key = null, $strict = false) |
|
| 1662 | |||
| 1663 | /** |
||
| 1664 | * Return only unique items from the collection array using strict comparison. |
||
| 1665 | * |
||
| 1666 | * @param string|callable|null $key |
||
| 1667 | * @return static |
||
| 1668 | */ |
||
| 1669 | 1 | public function uniqueStrict($key = null) |
|
| 1673 | |||
| 1674 | /** |
||
| 1675 | * Reset the keys on the underlying array. |
||
| 1676 | * |
||
| 1677 | * @return static |
||
| 1678 | */ |
||
| 1679 | 37 | public function values() |
|
| 1683 | |||
| 1684 | /** |
||
| 1685 | * Get a value retrieving callback. |
||
| 1686 | * |
||
| 1687 | * @param string $value |
||
| 1688 | * @return callable |
||
| 1689 | */ |
||
| 1690 | 36 | protected function valueRetriever($value) |
|
| 1700 | |||
| 1701 | /** |
||
| 1702 | * Zip the collection together with one or more arrays. |
||
| 1703 | * |
||
| 1704 | * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); |
||
| 1705 | * => [[1, 4], [2, 5], [3, 6]] |
||
| 1706 | * |
||
| 1707 | * @param mixed ...$items |
||
| 1708 | * @return static |
||
| 1709 | */ |
||
| 1710 | 1 | public function zip($items) |
|
| 1722 | |||
| 1723 | /** |
||
| 1724 | * Pad collection to the specified length with a value. |
||
| 1725 | * |
||
| 1726 | * @param int $size |
||
| 1727 | * @param mixed $value |
||
| 1728 | * @return static |
||
| 1729 | */ |
||
| 1730 | 1 | public function pad($size, $value) |
|
| 1734 | |||
| 1735 | /** |
||
| 1736 | * Get the collection of items as a plain array. |
||
| 1737 | * |
||
| 1738 | * @return array |
||
| 1739 | */ |
||
| 1740 | 52 | public function toArray() |
|
| 1746 | |||
| 1747 | /** |
||
| 1748 | * Convert the object into something JSON serializable. |
||
| 1749 | * |
||
| 1750 | * @return array |
||
| 1751 | */ |
||
| 1752 | 2 | public function jsonSerialize() |
|
| 1766 | |||
| 1767 | /** |
||
| 1768 | * Get the collection of items as JSON. |
||
| 1769 | * |
||
| 1770 | * @param int $options |
||
| 1771 | * @return string |
||
| 1772 | */ |
||
| 1773 | 2 | public function toJson($options = 0) |
|
| 1777 | |||
| 1778 | /** |
||
| 1779 | * Get an iterator for the items. |
||
| 1780 | * |
||
| 1781 | * @return \ArrayIterator |
||
| 1782 | */ |
||
| 1783 | 5 | public function getIterator() |
|
| 1787 | |||
| 1788 | /** |
||
| 1789 | * Get a CachingIterator instance. |
||
| 1790 | * |
||
| 1791 | * @param int $flags |
||
| 1792 | * @return \CachingIterator |
||
| 1793 | */ |
||
| 1794 | 1 | public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING) |
|
| 1798 | |||
| 1799 | /** |
||
| 1800 | * Count the number of items in the collection. |
||
| 1801 | * |
||
| 1802 | * @return int |
||
| 1803 | */ |
||
| 1804 | 26 | public function count() |
|
| 1808 | |||
| 1809 | /** |
||
| 1810 | * Get a base Support collection instance from this collection. |
||
| 1811 | * |
||
| 1812 | * @return \IlluminateAgnostic\Collection\Support\Collection |
||
| 1813 | */ |
||
| 1814 | public function toBase() |
||
| 1818 | |||
| 1819 | /** |
||
| 1820 | * Determine if an item exists at an offset. |
||
| 1821 | * |
||
| 1822 | * @param mixed $key |
||
| 1823 | * @return bool |
||
| 1824 | */ |
||
| 1825 | 17 | public function offsetExists($key) |
|
| 1829 | |||
| 1830 | /** |
||
| 1831 | * Get an item at a given offset. |
||
| 1832 | * |
||
| 1833 | * @param mixed $key |
||
| 1834 | * @return mixed |
||
| 1835 | */ |
||
| 1836 | 18 | public function offsetGet($key) |
|
| 1840 | |||
| 1841 | /** |
||
| 1842 | * Set the item at a given offset. |
||
| 1843 | * |
||
| 1844 | * @param mixed $key |
||
| 1845 | * @param mixed $value |
||
| 1846 | * @return void |
||
| 1847 | */ |
||
| 1848 | 37 | public function offsetSet($key, $value) |
|
| 1856 | |||
| 1857 | /** |
||
| 1858 | * Unset the item at a given offset. |
||
| 1859 | * |
||
| 1860 | * @param string $key |
||
| 1861 | * @return void |
||
| 1862 | */ |
||
| 1863 | 4 | public function offsetUnset($key) |
|
| 1867 | |||
| 1868 | /** |
||
| 1869 | * Convert the collection to its string representation. |
||
| 1870 | * |
||
| 1871 | * @return string |
||
| 1872 | */ |
||
| 1873 | 1 | public function __toString() |
|
| 1877 | |||
| 1878 | /** |
||
| 1879 | * Results array of items from Collection or Arrayable. |
||
| 1880 | * |
||
| 1881 | * @param mixed $items |
||
| 1882 | * @return array |
||
| 1883 | */ |
||
| 1884 | 221 | protected function getArrayableItems($items) |
|
| 1902 | |||
| 1903 | /** |
||
| 1904 | * Add a method to the list of proxied methods. |
||
| 1905 | * |
||
| 1906 | * @param string $method |
||
| 1907 | * @return void |
||
| 1908 | */ |
||
| 1909 | 1 | public static function proxy($method) |
|
| 1913 | |||
| 1914 | /** |
||
| 1915 | * Dynamically access collection proxies. |
||
| 1916 | * |
||
| 1917 | * @param string $key |
||
| 1918 | * @return mixed |
||
| 1919 | * |
||
| 1920 | * @throws \Exception |
||
| 1921 | */ |
||
| 1922 | 15 | public function __get($key) |
|
| 1930 | } |
||
| 1931 |
Adding a
@returnannotation 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.