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 |
||
| 26 | class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable |
||
| 27 | { |
||
| 28 | /** @var array */ |
||
| 29 | protected $items; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Collection constructor. |
||
| 33 | * |
||
| 34 | * @param array $items |
||
| 35 | */ |
||
| 36 | 90 | public function __construct(array $items = []) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Generate a collection from an array of items. |
||
| 44 | * I created this method so that it's possible to extend a collection more easily. |
||
| 45 | * |
||
| 46 | * @param array $items |
||
| 47 | * |
||
| 48 | * @todo call to_array on $items and don't require input to be an array. |
||
| 49 | * |
||
| 50 | * @return Collection |
||
| 51 | */ |
||
| 52 | 29 | public static function factory(array $items = []) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Get collection as an array |
||
| 59 | * |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | 46 | public function toArray() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Determine if collection has a given key |
||
| 69 | * |
||
| 70 | * @param mixed $key The key to look for |
||
| 71 | * |
||
| 72 | * @return bool |
||
| 73 | */ |
||
| 74 | 45 | public function has($key) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Does collection have item at position? |
||
| 81 | * |
||
| 82 | * Determine if collection has an item at a particular position (indexed from one). |
||
| 83 | * Position can be positive and start from the beginning or it can be negative and |
||
| 84 | * start from the end. |
||
| 85 | * |
||
| 86 | * @param int $position |
||
| 87 | * |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | 2 | public function hasValueAt($position) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Get key at given position |
||
| 102 | * |
||
| 103 | * Returns the key at the given position, starting from one. Position can be positive (start from beginning) or |
||
| 104 | * negative (start from the end). |
||
| 105 | * |
||
| 106 | * If the position does not exist, a RuntimeException is thrown. |
||
| 107 | * |
||
| 108 | * @param int $position |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | * |
||
| 112 | * @throws RuntimeException |
||
| 113 | */ |
||
| 114 | 6 | public function getKeyAt($position) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Get value at given position |
||
| 131 | * |
||
| 132 | * Returns the value at the given position, starting from one. Position can be positive (start from beginning) or |
||
| 133 | * negative (start from the end). |
||
| 134 | * |
||
| 135 | * If the position does not exist, a RuntimeException is thrown. |
||
| 136 | * |
||
| 137 | * @param int $position |
||
| 138 | * |
||
| 139 | * @return mixed |
||
| 140 | * |
||
| 141 | * @throws RuntimeException |
||
| 142 | */ |
||
| 143 | 2 | public function getValueAt($position) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Get the key of the first item found matching $item |
||
| 150 | * |
||
| 151 | * @todo Perhaps this should allow a callback in place of $item? |
||
| 152 | * |
||
| 153 | * @param mixed $item |
||
| 154 | * |
||
| 155 | * @return mixed|null |
||
| 156 | */ |
||
| 157 | 1 | public function keyOf($item) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Get the offset (index) of the first item found that matches $item |
||
| 170 | * |
||
| 171 | * @todo Perhaps this should allow a callback in place of $item? |
||
| 172 | * |
||
| 173 | * @param mixed $item |
||
| 174 | * |
||
| 175 | * @return int|null |
||
| 176 | */ |
||
| 177 | 1 | public function indexOf($item) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Get item by key, with an optional default return value |
||
| 192 | * |
||
| 193 | * @param mixed $key |
||
| 194 | * @param mixed $default |
||
| 195 | * |
||
| 196 | * @return mixed |
||
| 197 | */ |
||
| 198 | 8 | public function get($key, $default = null) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Add an item with no regard to key |
||
| 209 | * |
||
| 210 | * @param mixed $value |
||
| 211 | * |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | 8 | public function add($value) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Set an item at a given key |
||
| 223 | * |
||
| 224 | * @param mixed $key |
||
| 225 | * @param mixed $value |
||
| 226 | * @param bool $overwrite If false, do not overwrite existing key |
||
| 227 | * |
||
| 228 | * @return $this |
||
| 229 | */ |
||
| 230 | 14 | public function set($key, $value, $overwrite = true) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Delete an item by key |
||
| 241 | * |
||
| 242 | * @param mixed $key |
||
| 243 | * |
||
| 244 | * @return $this |
||
| 245 | */ |
||
| 246 | 3 | public function delete($key) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Clear the collection of all its items. |
||
| 255 | * |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | 2 | public function clear() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Determine if collection contains given value |
||
| 267 | * |
||
| 268 | * @param mixed|callable $val |
||
| 269 | * @param mixed $key |
||
| 270 | * |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | 4 | public function contains($val, $key = null) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Fetch item from collection by key and remove it from collection |
||
| 294 | * |
||
| 295 | * @param mixed $key |
||
| 296 | * |
||
| 297 | * @return mixed |
||
| 298 | */ |
||
| 299 | 1 | public function pull($key) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Join collection items using a delimiter |
||
| 310 | * |
||
| 311 | * @param string $delim |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | 1 | public function join($delim = '') |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Determine if collection has any items |
||
| 322 | * |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | 4 | public function isEmpty() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Get a collection of only this collection's values (without its keys) |
||
| 332 | * |
||
| 333 | * @return Collection |
||
| 334 | */ |
||
| 335 | 1 | public function values() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Get a collection of only this collection's keys |
||
| 342 | * |
||
| 343 | * @return Collection |
||
| 344 | */ |
||
| 345 | 1 | public function keys() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Get a collection with order reversed |
||
| 352 | * |
||
| 353 | * @return Collection |
||
| 354 | */ |
||
| 355 | 6 | public function reverse() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Get a collection with keys and values flipped |
||
| 362 | * |
||
| 363 | * @return Collection |
||
| 364 | */ |
||
| 365 | 1 | public function flip() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Shuffle the order of this collection's values |
||
| 376 | * |
||
| 377 | * @return Collection |
||
| 378 | */ |
||
| 379 | 1 | public function shuffle() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Get a random value from the collection |
||
| 387 | * |
||
| 388 | * @todo might be useful to add a $count param to specify you want $count random items... |
||
| 389 | * |
||
| 390 | * @return mixed |
||
| 391 | */ |
||
| 392 | 1 | public function random() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Sort the collection (using values) |
||
| 399 | * |
||
| 400 | * @param callable $alg |
||
| 401 | * |
||
| 402 | * @return $this |
||
| 403 | */ |
||
| 404 | 2 | public function sort(callable $alg = null) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Sort the collection (using keys) |
||
| 417 | * |
||
| 418 | * @param callable $alg |
||
| 419 | * |
||
| 420 | * @return $this |
||
| 421 | */ |
||
| 422 | 2 | public function ksort(callable $alg = null) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Append items to collection without regard to keys |
||
| 435 | * |
||
| 436 | * @param array|Traversable $items |
||
| 437 | * |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | 2 | public function append($items) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Return first item or first item where callback returns true |
||
| 455 | * |
||
| 456 | * @param callable|null $callback |
||
| 457 | * |
||
| 458 | * @return mixed|null |
||
| 459 | */ |
||
| 460 | 7 | public function first(callable $callback = null) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Return last item or last item where callback returns true |
||
| 474 | * |
||
| 475 | * @param callable|null $callback |
||
| 476 | * |
||
| 477 | * @return mixed|null |
||
| 478 | */ |
||
| 479 | 3 | public function last(callable $callback = null) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Map collection |
||
| 486 | * |
||
| 487 | * Create a new collection using the results of a callback function on each item in this collection. |
||
| 488 | * |
||
| 489 | * @param callable $callback |
||
| 490 | * |
||
| 491 | * @return Collection |
||
| 492 | */ |
||
| 493 | 3 | public function map(callable $callback) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Combine collection with another traversable/collection |
||
| 507 | * |
||
| 508 | * Using this collection's keys, and the incoming collection's values, a new collection is created and returned. |
||
| 509 | * |
||
| 510 | * @param array|Traversable $items |
||
| 511 | * |
||
| 512 | * @return Collection |
||
| 513 | */ |
||
| 514 | 5 | public function combine($items) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Get a new collection with only distinct values |
||
| 530 | * |
||
| 531 | * @return Collection |
||
| 532 | */ |
||
| 533 | 1 | public function distinct() |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Remove all duplicate values from collection in-place |
||
| 547 | * |
||
| 548 | * @return Collection |
||
| 549 | */ |
||
| 550 | 1 | public function deduplicate() |
|
| 556 | |||
| 557 | /** |
||
| 558 | * Return a new collection with only filtered keys/values |
||
| 559 | * |
||
| 560 | * The callback accepts value, key, index and should return true if the item should be added to the returned |
||
| 561 | * collection |
||
| 562 | * |
||
| 563 | * @param callable $callback |
||
| 564 | * |
||
| 565 | * @return Collection |
||
| 566 | */ |
||
| 567 | 2 | public function filter(callable $callback = null) |
|
| 585 | |||
| 586 | /** |
||
| 587 | * Fold collection into a single value |
||
| 588 | * |
||
| 589 | * Loop through collection calling a callback function and passing the result to the next iteration, eventually |
||
| 590 | * returning a single value. |
||
| 591 | * |
||
| 592 | * @param callable $callback |
||
| 593 | * @param mixed $initial |
||
| 594 | * |
||
| 595 | * @return null |
||
| 596 | */ |
||
| 597 | 1 | public function fold(callable $callback, $initial = null) |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Return a merge of this collection and $items |
||
| 610 | * |
||
| 611 | * @param array|Traversable $items |
||
| 612 | * |
||
| 613 | * @return Collection |
||
| 614 | */ |
||
| 615 | 3 | public function merge($items) |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Create a new collection with a union of this collection and $items |
||
| 631 | * |
||
| 632 | * This method is similar to merge, except that existing items will not be overwritten. |
||
| 633 | * |
||
| 634 | * @param $items |
||
| 635 | */ |
||
| 636 | 2 | public function union($items) |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Call callback for each item in collection, passively |
||
| 652 | * If at any point the callback returns false, iteration stops. |
||
| 653 | * |
||
| 654 | * @param callable $callback |
||
| 655 | * |
||
| 656 | * @return $this |
||
| 657 | */ |
||
| 658 | 2 | public function each(callable $callback) |
|
| 669 | |||
| 670 | /** |
||
| 671 | * Assert callback returns $expected value for each item in collection. |
||
| 672 | * |
||
| 673 | * @todo This can be used to easily make methods like all($callback) and none($callback). |
||
| 674 | * |
||
| 675 | * @param callable $callback |
||
| 676 | * @param bool $expected |
||
| 677 | * |
||
| 678 | * @return bool |
||
| 679 | */ |
||
| 680 | 1 | public function assert(callable $callback, $expected = true) |
|
| 691 | |||
| 692 | /** |
||
| 693 | * Pipe collection through a callback |
||
| 694 | * |
||
| 695 | * @param callable $callback |
||
| 696 | * |
||
| 697 | * @return mixed |
||
| 698 | */ |
||
| 699 | 1 | public function pipe(callable $callback) |
|
| 703 | |||
| 704 | /** |
||
| 705 | * Get new collection in chunks of $size |
||
| 706 | * |
||
| 707 | * Creates a new collection of arrays of $size length. The remainder items will be placed at the end. |
||
| 708 | * |
||
| 709 | * @param int $size |
||
| 710 | * |
||
| 711 | * @return Collection |
||
| 712 | */ |
||
| 713 | 2 | public function chunk($size) |
|
| 717 | |||
| 718 | /** |
||
| 719 | * Get a new collection of $count chunks |
||
| 720 | * |
||
| 721 | * @todo It might be useful to have a method that spreads remainder items more evenly so you don't end up with the |
||
| 722 | * last item containing only one or two items. |
||
| 723 | * |
||
| 724 | * @param int $count |
||
| 725 | * |
||
| 726 | * @return Collection |
||
| 727 | */ |
||
| 728 | 1 | public function split($count = 1) |
|
| 732 | |||
| 733 | /** |
||
| 734 | * Get a slice of this collection. |
||
| 735 | * |
||
| 736 | * @param int $offset |
||
| 737 | * @param int|null $length |
||
| 738 | * |
||
| 739 | * @return Collection |
||
| 740 | */ |
||
| 741 | 1 | public function slice($offset, $length = null) |
|
| 745 | |||
| 746 | /** |
||
| 747 | * Get collection with only differing items |
||
| 748 | * |
||
| 749 | * @param array|Traversable $items |
||
| 750 | * |
||
| 751 | * @return Collection |
||
| 752 | */ |
||
| 753 | 1 | public function diff($items) |
|
| 757 | |||
| 758 | /** |
||
| 759 | * Get collection with only differing items (by key) |
||
| 760 | * |
||
| 761 | * @param array|Traversable $items |
||
| 762 | * |
||
| 763 | * @return Collection |
||
| 764 | */ |
||
| 765 | 1 | public function kdiff($items) |
|
| 769 | |||
| 770 | /** |
||
| 771 | * Get collection with only intersecting items |
||
| 772 | * |
||
| 773 | * @param array|Traversable $items |
||
| 774 | * |
||
| 775 | * @return Collection |
||
| 776 | */ |
||
| 777 | 1 | public function intersect($items) |
|
| 781 | |||
| 782 | /** |
||
| 783 | * Get collection with only intersecting items (by key) |
||
| 784 | * |
||
| 785 | * @param array|Traversable $items |
||
| 786 | * |
||
| 787 | * @return Collection |
||
| 788 | */ |
||
| 789 | 1 | public function kintersect($items) |
|
| 793 | |||
| 794 | /** |
||
| 795 | * Remove last item in collection and return it |
||
| 796 | * |
||
| 797 | * @return mixed |
||
| 798 | */ |
||
| 799 | 1 | public function pop() |
|
| 803 | |||
| 804 | /** |
||
| 805 | * Remove first item in collection and return it (and re-index if numerically indexed) |
||
| 806 | * |
||
| 807 | * @return mixed |
||
| 808 | */ |
||
| 809 | 2 | public function shift() |
|
| 813 | |||
| 814 | /** |
||
| 815 | * Add item to the end of the collection |
||
| 816 | * |
||
| 817 | * @note This method is no different than add() but I included it for consistency's sake since I have the others |
||
| 818 | * |
||
| 819 | * @param mixed $item |
||
| 820 | * |
||
| 821 | * @return $this |
||
| 822 | */ |
||
| 823 | 1 | public function push($item) |
|
| 827 | |||
| 828 | /** |
||
| 829 | * Add item to the beginning of the collection (and re-index if a numerically indexed collection) |
||
| 830 | * |
||
| 831 | * @param mixed $item |
||
| 832 | * |
||
| 833 | * @return $this |
||
| 834 | */ |
||
| 835 | 5 | public function unshift($item) |
|
| 841 | |||
| 842 | /** |
||
| 843 | * Get new collection padded to specified $size with $value |
||
| 844 | * |
||
| 845 | * Using $value, pad the collection to specified $size. If $size is smaller or equal to the size of the collection, |
||
| 846 | * then no padding takes place. If $size is positive, padding is added to the end, while if negative, padding will |
||
| 847 | * be added to the beginning. |
||
| 848 | * |
||
| 849 | * @param int $size |
||
| 850 | * @param mixed $value |
||
| 851 | * |
||
| 852 | * @return Collection |
||
| 853 | */ |
||
| 854 | 3 | public function pad($size, $value = null) |
|
| 867 | |||
| 868 | /** |
||
| 869 | * Partition collection into two collections using a callback |
||
| 870 | * |
||
| 871 | * Iterates over each element in the collection with a callback. Items where callback returns true are placed in one |
||
| 872 | * collection and the rest in another. Finally, the two collections are placed in an array and returned for easy use |
||
| 873 | * with the list() function. ( list($a, $b) = $col->partition(function($val, $key, $index) {}) ) |
||
| 874 | * |
||
| 875 | * @param callable $callback |
||
| 876 | * |
||
| 877 | * @return array<Collection> |
||
| 878 | */ |
||
| 879 | 2 | public function partition(callable $callback) |
|
| 895 | |||
| 896 | /** ++++ ++++ **/ |
||
| 897 | /** ++ Interface Compliance ++ **/ |
||
| 898 | /** ++++ ++++ **/ |
||
| 899 | |||
| 900 | /** |
||
| 901 | * @return array |
||
| 902 | */ |
||
| 903 | 1 | public function jsonSerialize() |
|
| 907 | |||
| 908 | /** ++++ ++++ **/ |
||
| 909 | /** ++ Array Access Methods ++ **/ |
||
| 910 | /** ++++ ++++ **/ |
||
| 911 | |||
| 912 | /** |
||
| 913 | * {@inheritDoc} |
||
| 914 | */ |
||
| 915 | 1 | public function offsetExists($offset) |
|
| 919 | |||
| 920 | /** |
||
| 921 | * {@inheritDoc} |
||
| 922 | */ |
||
| 923 | 2 | public function offsetGet($offset) |
|
| 931 | |||
| 932 | /** |
||
| 933 | * {@inheritDoc} |
||
| 934 | */ |
||
| 935 | 1 | public function offsetUnset($offset) |
|
| 939 | |||
| 940 | /** |
||
| 941 | * {@inheritDoc} |
||
| 942 | */ |
||
| 943 | 1 | public function offsetSet($offset, $value) |
|
| 951 | |||
| 952 | /** ++++ ++++ **/ |
||
| 953 | /** ++ Iterator Methods ++ **/ |
||
| 954 | /** ++++ ++++ **/ |
||
| 955 | |||
| 956 | /** |
||
| 957 | * {@inheritDoc} |
||
| 958 | */ |
||
| 959 | 33 | public function current() |
|
| 963 | |||
| 964 | /** |
||
| 965 | * {@inheritDoc} |
||
| 966 | */ |
||
| 967 | 33 | public function key() |
|
| 971 | |||
| 972 | /** |
||
| 973 | * {@inheritDoc} |
||
| 974 | */ |
||
| 975 | 32 | public function next() |
|
| 979 | |||
| 980 | /** |
||
| 981 | * {@inheritDoc} |
||
| 982 | * |
||
| 983 | * @todo Should this return $this? |
||
| 984 | */ |
||
| 985 | 90 | public function rewind() |
|
| 989 | |||
| 990 | /** |
||
| 991 | * {@inheritDoc} |
||
| 992 | */ |
||
| 993 | 34 | public function valid() |
|
| 997 | |||
| 998 | /** ++++ ++++ **/ |
||
| 999 | /** ++ Countable Method ++ **/ |
||
| 1000 | /** ++++ ++++ **/ |
||
| 1001 | |||
| 1002 | /** |
||
| 1003 | * {@inheritDoc} |
||
| 1004 | */ |
||
| 1005 | 10 | public function count() |
|
| 1009 | } |