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, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable |
||
| 20 | { |
||
| 21 | use Macroable; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The items contained in the collection. |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $items = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The methods that can be proxied. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected static $proxies = [ |
||
| 36 | 'average', 'avg', 'contains', 'each', 'every', 'filter', 'first', 'flatMap', |
||
| 37 | 'keyBy', 'map', 'partition', 'reject', 'sortBy', 'sortByDesc', 'sum', 'unique', |
||
| 38 | ]; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Create a new collection. |
||
| 42 | * |
||
| 43 | * @param mixed $items |
||
| 44 | * @return void |
||
|
|
|||
| 45 | */ |
||
| 46 | 208 | public function __construct($items = []) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Create a new collection instance if the value isn't one already. |
||
| 53 | * |
||
| 54 | * @param mixed $items |
||
| 55 | * @return static |
||
| 56 | */ |
||
| 57 | 10 | public static function make($items = []) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Wrap the given value in a collection if applicable. |
||
| 64 | * |
||
| 65 | * @param mixed $value |
||
| 66 | * @return static |
||
| 67 | */ |
||
| 68 | 7 | public static function wrap($value) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Get the underlying items from the given collection if applicable. |
||
| 77 | * |
||
| 78 | * @param array|static $value |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | 3 | public static function unwrap($value) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Create a new collection by invoking the callback a given amount of times. |
||
| 88 | * |
||
| 89 | * @param int $number |
||
| 90 | * @param callable $callback |
||
| 91 | * @return static |
||
| 92 | */ |
||
| 93 | 1 | public static function times($number, callable $callback = null) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Get all of the items in the collection. |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | 104 | public function all() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Get the average value of a given key. |
||
| 118 | * |
||
| 119 | * @param callable|string|null $callback |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | 4 | public function avg($callback = null) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Alias for the "avg" method. |
||
| 131 | * |
||
| 132 | * @param callable|string|null $callback |
||
| 133 | * @return mixed |
||
| 134 | */ |
||
| 135 | 3 | public function average($callback = null) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Get the median of a given key. |
||
| 142 | * |
||
| 143 | * @param null $key |
||
| 144 | * @return mixed |
||
| 145 | */ |
||
| 146 | 5 | public function median($key = null) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Get the mode of a given key. |
||
| 170 | * |
||
| 171 | * @param mixed $key |
||
| 172 | * @return array|null |
||
| 173 | */ |
||
| 174 | 4 | public function mode($key = null) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Collapse the collection of items into a single array. |
||
| 201 | * |
||
| 202 | * @return static |
||
| 203 | */ |
||
| 204 | 3 | public function collapse() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Determine if an item exists in the collection. |
||
| 211 | * |
||
| 212 | * @param mixed $key |
||
| 213 | * @param mixed $operator |
||
| 214 | * @param mixed $value |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | 3 | public function contains($key, $operator = null, $value = null) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Determine if an item exists in the collection using strict comparison. |
||
| 234 | * |
||
| 235 | * @param mixed $key |
||
| 236 | * @param mixed $value |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | 1 | public function containsStrict($key, $value = null) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Cross join with the given lists, returning all possible permutations. |
||
| 256 | * |
||
| 257 | * @param mixed ...$lists |
||
| 258 | * @return static |
||
| 259 | */ |
||
| 260 | 1 | public function crossJoin(...$lists) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Dump the collection and end the script. |
||
| 269 | * |
||
| 270 | * @return void |
||
| 271 | */ |
||
| 272 | public function dd(...$args) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Dump the collection. |
||
| 283 | * |
||
| 284 | * @return $this |
||
| 285 | */ |
||
| 286 | public function dump() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Get the items in the collection that are not present in the given items. |
||
| 299 | * |
||
| 300 | * @param mixed $items |
||
| 301 | * @return static |
||
| 302 | */ |
||
| 303 | 2 | public function diff($items) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Get the items in the collection whose keys and values are not present in the given items. |
||
| 310 | * |
||
| 311 | * @param mixed $items |
||
| 312 | * @return static |
||
| 313 | */ |
||
| 314 | 1 | public function diffAssoc($items) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Get the items in the collection whose keys are not present in the given items. |
||
| 321 | * |
||
| 322 | * @param mixed $items |
||
| 323 | * @return static |
||
| 324 | */ |
||
| 325 | 1 | public function diffKeys($items) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Execute a callback over each item. |
||
| 332 | * |
||
| 333 | * @param callable $callback |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | 7 | public function each(callable $callback) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Execute a callback over each nested chunk of items. |
||
| 349 | * |
||
| 350 | * @param callable $callback |
||
| 351 | * @return static |
||
| 352 | */ |
||
| 353 | public function eachSpread(callable $callback) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Determine if all items in the collection pass the given test. |
||
| 364 | * |
||
| 365 | * @param string|callable $key |
||
| 366 | * @param mixed $operator |
||
| 367 | * @param mixed $value |
||
| 368 | * @return bool |
||
| 369 | */ |
||
| 370 | 1 | public function every($key, $operator = null, $value = null) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Get all items except for those with the specified keys. |
||
| 389 | * |
||
| 390 | * @param \IlluminateAgnostic\Collection\Support\Collection|mixed $keys |
||
| 391 | * @return static |
||
| 392 | */ |
||
| 393 | 2 | public function except($keys) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Run a filter over each of the items. |
||
| 406 | * |
||
| 407 | * @param callable|null $callback |
||
| 408 | * @return static |
||
| 409 | */ |
||
| 410 | 21 | public function filter(callable $callback = null) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Apply the callback if the value is truthy. |
||
| 421 | * |
||
| 422 | * @param bool $value |
||
| 423 | * @param callable $callback |
||
| 424 | * @param callable $default |
||
| 425 | * @return mixed |
||
| 426 | */ |
||
| 427 | 4 | public function when($value, callable $callback, callable $default = null) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Apply the callback if the value is falsy. |
||
| 440 | * |
||
| 441 | * @param bool $value |
||
| 442 | * @param callable $callback |
||
| 443 | * @param callable $default |
||
| 444 | * @return mixed |
||
| 445 | */ |
||
| 446 | 2 | public function unless($value, callable $callback, callable $default = null) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Filter items by the given key value pair. |
||
| 453 | * |
||
| 454 | * @param string $key |
||
| 455 | * @param mixed $operator |
||
| 456 | * @param mixed $value |
||
| 457 | * @return static |
||
| 458 | */ |
||
| 459 | 2 | public function where($key, $operator, $value = null) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Get an operator checker callback. |
||
| 466 | * |
||
| 467 | * @param string $key |
||
| 468 | * @param string $operator |
||
| 469 | * @param mixed $value |
||
| 470 | * @return \Closure |
||
| 471 | */ |
||
| 472 | 7 | protected function operatorForWhere($key, $operator, $value = null) |
|
| 473 | { |
||
| 474 | 7 | if (func_num_args() == 2) { |
|
| 475 | 5 | $value = $operator; |
|
| 476 | |||
| 477 | 5 | $operator = '='; |
|
| 478 | } |
||
| 479 | |||
| 480 | 7 | return function ($item) use ($key, $operator, $value) { |
|
| 481 | 7 | $retrieved = data_get($item, $key); |
|
| 482 | |||
| 483 | 7 | $strings = array_filter([$retrieved, $value], function ($value) { |
|
| 484 | 7 | return is_string($value) || (is_object($value) && method_exists($value, '__toString')); |
|
| 485 | 7 | }); |
|
| 486 | |||
| 487 | 7 | if (count($strings) < 2 && count(array_filter([$retrieved, $value], 'is_object')) == 1) { |
|
| 488 | 1 | return in_array($operator, ['!=', '<>', '!==']); |
|
| 489 | } |
||
| 490 | |||
| 491 | 7 | switch ($operator) { |
|
| 492 | default: |
||
| 493 | 7 | case '=': |
|
| 494 | 7 | case '==': return $retrieved == $value; |
|
| 495 | 5 | case '!=': |
|
| 496 | 5 | case '<>': return $retrieved != $value; |
|
| 497 | 5 | case '<': return $retrieved < $value; |
|
| 498 | 5 | case '>': return $retrieved > $value; |
|
| 499 | 5 | case '<=': return $retrieved <= $value; |
|
| 500 | 5 | case '>=': return $retrieved >= $value; |
|
| 501 | 3 | case '===': return $retrieved === $value; |
|
| 502 | 1 | case '!==': return $retrieved !== $value; |
|
| 503 | } |
||
| 504 | 7 | }; |
|
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Filter items by the given key value pair using strict comparison. |
||
| 509 | * |
||
| 510 | * @param string $key |
||
| 511 | * @param mixed $value |
||
| 512 | * @return static |
||
| 513 | */ |
||
| 514 | 1 | public function whereStrict($key, $value) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Filter items by the given key value pair. |
||
| 521 | * |
||
| 522 | * @param string $key |
||
| 523 | * @param mixed $values |
||
| 524 | * @param bool $strict |
||
| 525 | * @return static |
||
| 526 | */ |
||
| 527 | 2 | View Code Duplication | public function whereIn($key, $values, $strict = false) |
| 535 | |||
| 536 | /** |
||
| 537 | * Filter items by the given key value pair using strict comparison. |
||
| 538 | * |
||
| 539 | * @param string $key |
||
| 540 | * @param mixed $values |
||
| 541 | * @return static |
||
| 542 | */ |
||
| 543 | 1 | public function whereInStrict($key, $values) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Filter items by the given key value pair. |
||
| 550 | * |
||
| 551 | * @param string $key |
||
| 552 | * @param mixed $values |
||
| 553 | * @param bool $strict |
||
| 554 | * @return static |
||
| 555 | */ |
||
| 556 | 2 | View Code Duplication | public function whereNotIn($key, $values, $strict = false) |
| 564 | |||
| 565 | /** |
||
| 566 | * Filter items by the given key value pair using strict comparison. |
||
| 567 | * |
||
| 568 | * @param string $key |
||
| 569 | * @param mixed $values |
||
| 570 | * @return static |
||
| 571 | */ |
||
| 572 | 1 | public function whereNotInStrict($key, $values) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Get the first item from the collection. |
||
| 579 | * |
||
| 580 | * @param callable|null $callback |
||
| 581 | * @param mixed $default |
||
| 582 | * @return mixed |
||
| 583 | */ |
||
| 584 | 11 | public function first(callable $callback = null, $default = null) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Get the first item by the given key value pair. |
||
| 591 | * |
||
| 592 | * @param string $key |
||
| 593 | * @param mixed $operator |
||
| 594 | * @param mixed $value |
||
| 595 | * @return static |
||
| 596 | */ |
||
| 597 | 1 | public function firstWhere($key, $operator, $value = null) |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Get a flattened array of the items in the collection. |
||
| 604 | * |
||
| 605 | * @param int $depth |
||
| 606 | * @return static |
||
| 607 | */ |
||
| 608 | 3 | public function flatten($depth = INF) |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Flip the items in the collection. |
||
| 615 | * |
||
| 616 | * @return static |
||
| 617 | */ |
||
| 618 | 1 | public function flip() |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Remove an item from the collection by key. |
||
| 625 | * |
||
| 626 | * @param string|array $keys |
||
| 627 | * @return $this |
||
| 628 | */ |
||
| 629 | 2 | public function forget($keys) |
|
| 637 | |||
| 638 | /** |
||
| 639 | * Get an item from the collection by key. |
||
| 640 | * |
||
| 641 | * @param mixed $key |
||
| 642 | * @param mixed $default |
||
| 643 | * @return mixed |
||
| 644 | */ |
||
| 645 | 5 | public function get($key, $default = null) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Group an associative array by a field or using a callback. |
||
| 656 | * |
||
| 657 | * @param callable|string $groupBy |
||
| 658 | * @param bool $preserveKeys |
||
| 659 | * @return static |
||
| 660 | */ |
||
| 661 | 7 | public function groupBy($groupBy, $preserveKeys = false) |
|
| 699 | |||
| 700 | /** |
||
| 701 | * Key an associative array by a field or using a callback. |
||
| 702 | * |
||
| 703 | * @param callable|string $keyBy |
||
| 704 | * @return static |
||
| 705 | */ |
||
| 706 | 3 | public function keyBy($keyBy) |
|
| 724 | |||
| 725 | /** |
||
| 726 | * Determine if an item exists in the collection by key. |
||
| 727 | * |
||
| 728 | * @param mixed $key |
||
| 729 | * @return bool |
||
| 730 | */ |
||
| 731 | 2 | public function has($key) |
|
| 743 | |||
| 744 | /** |
||
| 745 | * Concatenate values of a given key as a string. |
||
| 746 | * |
||
| 747 | * @param string $value |
||
| 748 | * @param string $glue |
||
| 749 | * @return string |
||
| 750 | */ |
||
| 751 | 1 | public function implode($value, $glue = null) |
|
| 761 | |||
| 762 | /** |
||
| 763 | * Intersect the collection with the given items. |
||
| 764 | * |
||
| 765 | * @param mixed $items |
||
| 766 | * @return static |
||
| 767 | */ |
||
| 768 | 2 | public function intersect($items) |
|
| 772 | |||
| 773 | /** |
||
| 774 | * Intersect the collection with the given items by key. |
||
| 775 | * |
||
| 776 | * @param mixed $items |
||
| 777 | * @return static |
||
| 778 | */ |
||
| 779 | 2 | public function intersectByKeys($items) |
|
| 785 | |||
| 786 | /** |
||
| 787 | * Determine if the collection is empty or not. |
||
| 788 | * |
||
| 789 | * @return bool |
||
| 790 | */ |
||
| 791 | 7 | public function isEmpty() |
|
| 795 | |||
| 796 | /** |
||
| 797 | * Determine if the collection is not empty. |
||
| 798 | * |
||
| 799 | * @return bool |
||
| 800 | */ |
||
| 801 | 1 | public function isNotEmpty() |
|
| 805 | |||
| 806 | /** |
||
| 807 | * Determine if the given value is callable, but not a string. |
||
| 808 | * |
||
| 809 | * @param mixed $value |
||
| 810 | * @return bool |
||
| 811 | */ |
||
| 812 | 40 | protected function useAsCallable($value) |
|
| 816 | |||
| 817 | /** |
||
| 818 | * Get the keys of the collection items. |
||
| 819 | * |
||
| 820 | * @return static |
||
| 821 | */ |
||
| 822 | 6 | public function keys() |
|
| 826 | |||
| 827 | /** |
||
| 828 | * Get the last item from the collection. |
||
| 829 | * |
||
| 830 | * @param callable|null $callback |
||
| 831 | * @param mixed $default |
||
| 832 | * @return mixed |
||
| 833 | */ |
||
| 834 | 7 | public function last(callable $callback = null, $default = null) |
|
| 838 | |||
| 839 | /** |
||
| 840 | * Get the values of a given key. |
||
| 841 | * |
||
| 842 | * @param string|array $value |
||
| 843 | * @param string|null $key |
||
| 844 | * @return static |
||
| 845 | */ |
||
| 846 | 9 | public function pluck($value, $key = null) |
|
| 850 | |||
| 851 | /** |
||
| 852 | * Run a map over each of the items. |
||
| 853 | * |
||
| 854 | * @param callable $callback |
||
| 855 | * @return static |
||
| 856 | */ |
||
| 857 | 16 | public function map(callable $callback) |
|
| 865 | |||
| 866 | /** |
||
| 867 | * Run a map over each nested chunk of items. |
||
| 868 | * |
||
| 869 | * @param callable $callback |
||
| 870 | * @return static |
||
| 871 | */ |
||
| 872 | public function mapSpread(callable $callback) |
||
| 880 | |||
| 881 | /** |
||
| 882 | * Run a dictionary map over the items. |
||
| 883 | * |
||
| 884 | * The callback should return an associative array with a single key/value pair. |
||
| 885 | * |
||
| 886 | * @param callable $callback |
||
| 887 | * @return static |
||
| 888 | */ |
||
| 889 | 4 | public function mapToDictionary(callable $callback) |
|
| 909 | |||
| 910 | /** |
||
| 911 | * Run a grouping map over the items. |
||
| 912 | * |
||
| 913 | * The callback should return an associative array with a single key/value pair. |
||
| 914 | * |
||
| 915 | * @param callable $callback |
||
| 916 | * @return static |
||
| 917 | */ |
||
| 918 | 2 | public function mapToGroups(callable $callback) |
|
| 924 | |||
| 925 | /** |
||
| 926 | * Run an associative map over each of the items. |
||
| 927 | * |
||
| 928 | * The callback should return an associative array with a single key/value pair. |
||
| 929 | * |
||
| 930 | * @param callable $callback |
||
| 931 | * @return static |
||
| 932 | */ |
||
| 933 | 4 | public function mapWithKeys(callable $callback) |
|
| 947 | |||
| 948 | /** |
||
| 949 | * Map a collection and flatten the result by a single level. |
||
| 950 | * |
||
| 951 | * @param callable $callback |
||
| 952 | * @return static |
||
| 953 | */ |
||
| 954 | 1 | public function flatMap(callable $callback) |
|
| 958 | |||
| 959 | /** |
||
| 960 | * Map the values into a new class. |
||
| 961 | * |
||
| 962 | * @param string $class |
||
| 963 | * @return static |
||
| 964 | */ |
||
| 965 | public function mapInto($class) |
||
| 971 | |||
| 972 | /** |
||
| 973 | * Get the max value of a given key. |
||
| 974 | * |
||
| 975 | * @param callable|string|null $callback |
||
| 976 | * @return mixed |
||
| 977 | */ |
||
| 978 | 1 | View Code Duplication | public function max($callback = null) |
| 990 | |||
| 991 | /** |
||
| 992 | * Merge the collection with the given items. |
||
| 993 | * |
||
| 994 | * @param mixed $items |
||
| 995 | * @return static |
||
| 996 | */ |
||
| 997 | 3 | public function merge($items) |
|
| 1001 | |||
| 1002 | /** |
||
| 1003 | * Create a collection by using this collection for keys and another for its values. |
||
| 1004 | * |
||
| 1005 | * @param mixed $values |
||
| 1006 | * @return static |
||
| 1007 | */ |
||
| 1008 | 2 | public function combine($values) |
|
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Union the collection with the given items. |
||
| 1015 | * |
||
| 1016 | * @param mixed $items |
||
| 1017 | * @return static |
||
| 1018 | */ |
||
| 1019 | 3 | public function union($items) |
|
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Get the min value of a given key. |
||
| 1026 | * |
||
| 1027 | * @param callable|string|null $callback |
||
| 1028 | * @return mixed |
||
| 1029 | */ |
||
| 1030 | 1 | View Code Duplication | public function min($callback = null) |
| 1042 | |||
| 1043 | /** |
||
| 1044 | * Create a new collection consisting of every n-th element. |
||
| 1045 | * |
||
| 1046 | * @param int $step |
||
| 1047 | * @param int $offset |
||
| 1048 | * @return static |
||
| 1049 | */ |
||
| 1050 | 1 | public function nth($step, $offset = 0) |
|
| 1066 | |||
| 1067 | /** |
||
| 1068 | * Get the items with the specified keys. |
||
| 1069 | * |
||
| 1070 | * @param mixed $keys |
||
| 1071 | * @return static |
||
| 1072 | */ |
||
| 1073 | 1 | public function only($keys) |
|
| 1087 | |||
| 1088 | /** |
||
| 1089 | * "Paginate" the collection by slicing it into a smaller collection. |
||
| 1090 | * |
||
| 1091 | * @param int $page |
||
| 1092 | * @param int $perPage |
||
| 1093 | * @return static |
||
| 1094 | */ |
||
| 1095 | 1 | public function forPage($page, $perPage) |
|
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Partition the collection into two arrays using the given callback or key. |
||
| 1104 | * |
||
| 1105 | * @param callable|string $key |
||
| 1106 | * @param mixed $operator |
||
| 1107 | * @param mixed $value |
||
| 1108 | * @return static |
||
| 1109 | */ |
||
| 1110 | 7 | public function partition($key, $operator = null, $value = null) |
|
| 1124 | |||
| 1125 | /** |
||
| 1126 | * Pass the collection to the given callback and return the result. |
||
| 1127 | * |
||
| 1128 | * @param callable $callback |
||
| 1129 | * @return mixed |
||
| 1130 | */ |
||
| 1131 | 1 | public function pipe(callable $callback) |
|
| 1135 | |||
| 1136 | /** |
||
| 1137 | * Get and remove the last item from the collection. |
||
| 1138 | * |
||
| 1139 | * @return mixed |
||
| 1140 | */ |
||
| 1141 | 1 | public function pop() |
|
| 1145 | |||
| 1146 | /** |
||
| 1147 | * Push an item onto the beginning of the collection. |
||
| 1148 | * |
||
| 1149 | * @param mixed $value |
||
| 1150 | * @param mixed $key |
||
| 1151 | * @return $this |
||
| 1152 | */ |
||
| 1153 | 1 | public function prepend($value, $key = null) |
|
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Push an item onto the end of the collection. |
||
| 1162 | * |
||
| 1163 | * @param mixed $value |
||
| 1164 | * @return $this |
||
| 1165 | */ |
||
| 1166 | 7 | public function push($value) |
|
| 1172 | |||
| 1173 | /** |
||
| 1174 | * Push all of the given items onto the collection. |
||
| 1175 | * |
||
| 1176 | * @param \Traversable $source |
||
| 1177 | * @return $this |
||
| 1178 | */ |
||
| 1179 | 2 | public function concat($source) |
|
| 1189 | |||
| 1190 | /** |
||
| 1191 | * Get and remove an item from the collection. |
||
| 1192 | * |
||
| 1193 | * @param mixed $key |
||
| 1194 | * @param mixed $default |
||
| 1195 | * @return mixed |
||
| 1196 | */ |
||
| 1197 | 3 | public function pull($key, $default = null) |
|
| 1201 | |||
| 1202 | /** |
||
| 1203 | * Put an item in the collection by key. |
||
| 1204 | * |
||
| 1205 | * @param mixed $key |
||
| 1206 | * @param mixed $value |
||
| 1207 | * @return $this |
||
| 1208 | */ |
||
| 1209 | 3 | public function put($key, $value) |
|
| 1215 | |||
| 1216 | /** |
||
| 1217 | * Get one or a specified number of items randomly from the collection. |
||
| 1218 | * |
||
| 1219 | * @param int|null $number |
||
| 1220 | * @return mixed |
||
| 1221 | * |
||
| 1222 | * @throws \InvalidArgumentException |
||
| 1223 | */ |
||
| 1224 | 3 | public function random($number = null) |
|
| 1232 | |||
| 1233 | /** |
||
| 1234 | * Reduce the collection to a single value. |
||
| 1235 | * |
||
| 1236 | * @param callable $callback |
||
| 1237 | * @param mixed $initial |
||
| 1238 | * @return mixed |
||
| 1239 | */ |
||
| 1240 | 6 | public function reduce(callable $callback, $initial = null) |
|
| 1244 | |||
| 1245 | /** |
||
| 1246 | * Create a collection of all elements that do not pass a given truth test. |
||
| 1247 | * |
||
| 1248 | * @param callable|mixed $callback |
||
| 1249 | * @return static |
||
| 1250 | */ |
||
| 1251 | 8 | public function reject($callback) |
|
| 1263 | |||
| 1264 | /** |
||
| 1265 | * Reverse items order. |
||
| 1266 | * |
||
| 1267 | * @return static |
||
| 1268 | */ |
||
| 1269 | 1 | public function reverse() |
|
| 1273 | |||
| 1274 | /** |
||
| 1275 | * Search the collection for a given value and return the corresponding key if successful. |
||
| 1276 | * |
||
| 1277 | * @param mixed $value |
||
| 1278 | * @param bool $strict |
||
| 1279 | * @return mixed |
||
| 1280 | */ |
||
| 1281 | 2 | public function search($value, $strict = false) |
|
| 1295 | |||
| 1296 | /** |
||
| 1297 | * Get and remove the first item from the collection. |
||
| 1298 | * |
||
| 1299 | * @return mixed |
||
| 1300 | */ |
||
| 1301 | 1 | public function shift() |
|
| 1305 | |||
| 1306 | /** |
||
| 1307 | * Shuffle the items in the collection. |
||
| 1308 | * |
||
| 1309 | * @param int $seed |
||
| 1310 | * @return static |
||
| 1311 | */ |
||
| 1312 | 1 | public function shuffle($seed = null) |
|
| 1328 | |||
| 1329 | /** |
||
| 1330 | * Slice the underlying collection array. |
||
| 1331 | * |
||
| 1332 | * @param int $offset |
||
| 1333 | * @param int $length |
||
| 1334 | * @return static |
||
| 1335 | */ |
||
| 1336 | 10 | public function slice($offset, $length = null) |
|
| 1340 | |||
| 1341 | /** |
||
| 1342 | * Split a collection into a certain number of groups. |
||
| 1343 | * |
||
| 1344 | * @param int $numberOfGroups |
||
| 1345 | * @return static |
||
| 1346 | */ |
||
| 1347 | 4 | public function split($numberOfGroups) |
|
| 1357 | |||
| 1358 | /** |
||
| 1359 | * Chunk the underlying collection array. |
||
| 1360 | * |
||
| 1361 | * @param int $size |
||
| 1362 | * @return static |
||
| 1363 | */ |
||
| 1364 | 6 | public function chunk($size) |
|
| 1378 | |||
| 1379 | /** |
||
| 1380 | * Sort through each item with a callback. |
||
| 1381 | * |
||
| 1382 | * @param callable|null $callback |
||
| 1383 | * @return static |
||
| 1384 | */ |
||
| 1385 | 9 | public function sort(callable $callback = null) |
|
| 1395 | |||
| 1396 | /** |
||
| 1397 | * Sort the collection using the given callback. |
||
| 1398 | * |
||
| 1399 | * @param callable|string $callback |
||
| 1400 | * @param int $options |
||
| 1401 | * @param bool $descending |
||
| 1402 | * @return static |
||
| 1403 | */ |
||
| 1404 | 5 | public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
|
| 1429 | |||
| 1430 | /** |
||
| 1431 | * Sort the collection in descending order using the given callback. |
||
| 1432 | * |
||
| 1433 | * @param callable|string $callback |
||
| 1434 | * @param int $options |
||
| 1435 | * @return static |
||
| 1436 | */ |
||
| 1437 | 1 | public function sortByDesc($callback, $options = SORT_REGULAR) |
|
| 1441 | |||
| 1442 | /** |
||
| 1443 | * Splice a portion of the underlying collection array. |
||
| 1444 | * |
||
| 1445 | * @param int $offset |
||
| 1446 | * @param int|null $length |
||
| 1447 | * @param mixed $replacement |
||
| 1448 | * @return static |
||
| 1449 | */ |
||
| 1450 | 1 | public function splice($offset, $length = null, $replacement = []) |
|
| 1458 | |||
| 1459 | /** |
||
| 1460 | * Get the sum of the given values. |
||
| 1461 | * |
||
| 1462 | * @param callable|string|null $callback |
||
| 1463 | * @return mixed |
||
| 1464 | */ |
||
| 1465 | 8 | public function sum($callback = null) |
|
| 1477 | |||
| 1478 | /** |
||
| 1479 | * Take the first or last {$limit} items. |
||
| 1480 | * |
||
| 1481 | * @param int $limit |
||
| 1482 | * @return static |
||
| 1483 | */ |
||
| 1484 | 2 | public function take($limit) |
|
| 1492 | |||
| 1493 | /** |
||
| 1494 | * Pass the collection to the given callback and then return it. |
||
| 1495 | * |
||
| 1496 | * @param callable $callback |
||
| 1497 | * @return $this |
||
| 1498 | */ |
||
| 1499 | 1 | public function tap(callable $callback) |
|
| 1505 | |||
| 1506 | /** |
||
| 1507 | * Transform each item in the collection using a callback. |
||
| 1508 | * |
||
| 1509 | * @param callable $callback |
||
| 1510 | * @return $this |
||
| 1511 | */ |
||
| 1512 | 1 | public function transform(callable $callback) |
|
| 1518 | |||
| 1519 | /** |
||
| 1520 | * Return only unique items from the collection array. |
||
| 1521 | * |
||
| 1522 | * @param string|callable|null $key |
||
| 1523 | * @param bool $strict |
||
| 1524 | * @return static |
||
| 1525 | */ |
||
| 1526 | 5 | public function unique($key = null, $strict = false) |
|
| 1540 | |||
| 1541 | /** |
||
| 1542 | * Return only unique items from the collection array using strict comparison. |
||
| 1543 | * |
||
| 1544 | * @param string|callable|null $key |
||
| 1545 | * @return static |
||
| 1546 | */ |
||
| 1547 | 1 | public function uniqueStrict($key = null) |
|
| 1551 | |||
| 1552 | /** |
||
| 1553 | * Reset the keys on the underlying array. |
||
| 1554 | * |
||
| 1555 | * @return static |
||
| 1556 | */ |
||
| 1557 | 30 | public function values() |
|
| 1561 | |||
| 1562 | /** |
||
| 1563 | * Get a value retrieving callback. |
||
| 1564 | * |
||
| 1565 | * @param string $value |
||
| 1566 | * @return callable |
||
| 1567 | */ |
||
| 1568 | 32 | protected function valueRetriever($value) |
|
| 1578 | |||
| 1579 | /** |
||
| 1580 | * Zip the collection together with one or more arrays. |
||
| 1581 | * |
||
| 1582 | * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); |
||
| 1583 | * => [[1, 4], [2, 5], [3, 6]] |
||
| 1584 | * |
||
| 1585 | * @param mixed ...$items |
||
| 1586 | * @return static |
||
| 1587 | */ |
||
| 1588 | public function zip($items) |
||
| 1600 | |||
| 1601 | /** |
||
| 1602 | * Pad collection to the specified length with a value. |
||
| 1603 | * |
||
| 1604 | * @param int $size |
||
| 1605 | * @param mixed $value |
||
| 1606 | * @return static |
||
| 1607 | */ |
||
| 1608 | 1 | public function pad($size, $value) |
|
| 1612 | |||
| 1613 | /** |
||
| 1614 | * Get the collection of items as a plain array. |
||
| 1615 | * |
||
| 1616 | * @return array |
||
| 1617 | */ |
||
| 1618 | public function toArray() |
||
| 1624 | |||
| 1625 | /** |
||
| 1626 | * Convert the object into something JSON serializable. |
||
| 1627 | * |
||
| 1628 | * @return array |
||
| 1629 | */ |
||
| 1630 | public function jsonSerialize() |
||
| 1644 | |||
| 1645 | /** |
||
| 1646 | * Get the collection of items as JSON. |
||
| 1647 | * |
||
| 1648 | * @param int $options |
||
| 1649 | * @return string |
||
| 1650 | */ |
||
| 1651 | 2 | public function toJson($options = 0) |
|
| 1655 | |||
| 1656 | /** |
||
| 1657 | * Get an iterator for the items. |
||
| 1658 | * |
||
| 1659 | * @return \ArrayIterator |
||
| 1660 | */ |
||
| 1661 | 5 | public function getIterator() |
|
| 1665 | |||
| 1666 | /** |
||
| 1667 | * Get a CachingIterator instance. |
||
| 1668 | * |
||
| 1669 | * @param int $flags |
||
| 1670 | * @return \CachingIterator |
||
| 1671 | */ |
||
| 1672 | 1 | public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING) |
|
| 1676 | |||
| 1677 | /** |
||
| 1678 | * Count the number of items in the collection. |
||
| 1679 | * |
||
| 1680 | * @return int |
||
| 1681 | */ |
||
| 1682 | 21 | public function count() |
|
| 1686 | |||
| 1687 | /** |
||
| 1688 | * Get a base Support collection instance from this collection. |
||
| 1689 | * |
||
| 1690 | * @return \IlluminateAgnostic\Collection\Support\Collection |
||
| 1691 | */ |
||
| 1692 | public function toBase() |
||
| 1696 | |||
| 1697 | /** |
||
| 1698 | * Determine if an item exists at an offset. |
||
| 1699 | * |
||
| 1700 | * @param mixed $key |
||
| 1701 | * @return bool |
||
| 1702 | */ |
||
| 1703 | 16 | public function offsetExists($key) |
|
| 1707 | |||
| 1708 | /** |
||
| 1709 | * Get an item at a given offset. |
||
| 1710 | * |
||
| 1711 | * @param mixed $key |
||
| 1712 | * @return mixed |
||
| 1713 | */ |
||
| 1714 | 17 | public function offsetGet($key) |
|
| 1718 | |||
| 1719 | /** |
||
| 1720 | * Set the item at a given offset. |
||
| 1721 | * |
||
| 1722 | * @param mixed $key |
||
| 1723 | * @param mixed $value |
||
| 1724 | * @return void |
||
| 1725 | */ |
||
| 1726 | 30 | public function offsetSet($key, $value) |
|
| 1734 | |||
| 1735 | /** |
||
| 1736 | * Unset the item at a given offset. |
||
| 1737 | * |
||
| 1738 | * @param string $key |
||
| 1739 | * @return void |
||
| 1740 | */ |
||
| 1741 | 4 | public function offsetUnset($key) |
|
| 1745 | |||
| 1746 | /** |
||
| 1747 | * Convert the collection to its string representation. |
||
| 1748 | * |
||
| 1749 | * @return string |
||
| 1750 | */ |
||
| 1751 | 1 | public function __toString() |
|
| 1755 | |||
| 1756 | /** |
||
| 1757 | * Results array of items from Collection or Arrayable. |
||
| 1758 | * |
||
| 1759 | * @param mixed $items |
||
| 1760 | * @return array |
||
| 1761 | */ |
||
| 1762 | 208 | protected function getArrayableItems($items) |
|
| 1780 | |||
| 1781 | /** |
||
| 1782 | * Add a method to the list of proxied methods. |
||
| 1783 | * |
||
| 1784 | * @param string $method |
||
| 1785 | * @return void |
||
| 1786 | */ |
||
| 1787 | 1 | public static function proxy($method) |
|
| 1791 | |||
| 1792 | /** |
||
| 1793 | * Dynamically access collection proxies. |
||
| 1794 | * |
||
| 1795 | * @param string $key |
||
| 1796 | * @return mixed |
||
| 1797 | * |
||
| 1798 | * @throws \Exception |
||
| 1799 | */ |
||
| 1800 | 12 | public function __get($key) |
|
| 1808 | } |
||
| 1809 |
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.