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 |
||
| 23 | class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable |
||
| 24 | { |
||
| 25 | /** @var array */ |
||
| 26 | protected $items; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Collection constructor. |
||
| 30 | * |
||
| 31 | * @param array $items |
||
| 32 | */ |
||
| 33 | 83 | public function __construct(array $items = []) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Generate a collection from an array of items. |
||
| 41 | * I created this method so that it's possible to extend a collection more easily. |
||
| 42 | * |
||
| 43 | * @param array $items |
||
| 44 | * |
||
| 45 | * @todo call to_array on $items and don't require input to be an array. |
||
| 46 | * |
||
| 47 | * @return Collection |
||
| 48 | */ |
||
| 49 | 26 | public static function factory(array $items = []) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Get collection as an array |
||
| 56 | * |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | 41 | public function toArray() |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Determine if collection has a given key |
||
| 66 | * |
||
| 67 | * @param mixed $key The key to look for |
||
| 68 | * |
||
| 69 | * @return bool |
||
| 70 | */ |
||
| 71 | 41 | public function has($key) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Does collection have item at position? |
||
| 78 | * |
||
| 79 | * Determine if collection has an item at a particular position (indexed from one). |
||
| 80 | * Position can be positive and start from the beginning or it can be negative and |
||
| 81 | * start from the end. |
||
| 82 | * |
||
| 83 | * @param int $position |
||
| 84 | * |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | 2 | public function hasValueAt($position) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Get key at given position |
||
| 99 | * |
||
| 100 | * Returns the key at the given position, starting from one. Position can be positive (start from beginning) or |
||
| 101 | * negative (start from the end). |
||
| 102 | * |
||
| 103 | * If the position does not exist, a RuntimeException is thrown. |
||
| 104 | * |
||
| 105 | * @param int $position |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | * |
||
| 109 | * @throws RuntimeException |
||
| 110 | */ |
||
| 111 | 6 | public function getKeyAt($position) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Get value at given position |
||
| 128 | * |
||
| 129 | * Returns the value at the given position, starting from one. Position can be positive (start from beginning) or |
||
| 130 | * negative (start from the end). |
||
| 131 | * |
||
| 132 | * If the position does not exist, a RuntimeException is thrown. |
||
| 133 | * |
||
| 134 | * @param int $position |
||
| 135 | * |
||
| 136 | * @return mixed |
||
| 137 | * |
||
| 138 | * @throws RuntimeException |
||
| 139 | */ |
||
| 140 | 2 | public function getValueAt($position) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Get the key of the first item found matching $item |
||
| 147 | * |
||
| 148 | * @todo Perhaps this should allow a callback in place of $item? |
||
| 149 | * |
||
| 150 | * @param mixed $item |
||
| 151 | * |
||
| 152 | * @return mixed|null |
||
| 153 | */ |
||
| 154 | 1 | public function keyOf($item) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Get the offset (index) of the first item found that matches $item |
||
| 167 | * |
||
| 168 | * @todo Perhaps this should allow a callback in place of $item? |
||
| 169 | * |
||
| 170 | * @param mixed $item |
||
| 171 | * |
||
| 172 | * @return int|null |
||
| 173 | */ |
||
| 174 | 1 | public function indexOf($item) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Get item by key, with an optional default return value |
||
| 189 | * |
||
| 190 | * @param mixed $key |
||
| 191 | * @param mixed $default |
||
| 192 | * |
||
| 193 | * @return mixed |
||
| 194 | */ |
||
| 195 | 8 | public function get($key, $default = null) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Add an item with no regard to key |
||
| 206 | * |
||
| 207 | * @param mixed $value |
||
| 208 | * |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | 5 | public function add($value) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Set an item at a given key |
||
| 220 | * |
||
| 221 | * @param mixed $key |
||
| 222 | * @param mixed $value |
||
| 223 | * @param bool $overwrite If false, do not overwrite existing key |
||
| 224 | * |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | 12 | public function set($key, $value, $overwrite = true) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Delete an item by key |
||
| 238 | * |
||
| 239 | * @param mixed $key |
||
| 240 | * |
||
| 241 | * @return $this |
||
| 242 | */ |
||
| 243 | 3 | public function delete($key) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Clear the collection of all its items. |
||
| 252 | * |
||
| 253 | * @return $this |
||
| 254 | */ |
||
| 255 | 2 | public function clear() |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Determine if collection contains given value |
||
| 264 | * |
||
| 265 | * @param mixed|callable $val |
||
| 266 | * @param mixed $key |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | 4 | public function contains($val, $key = null) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Fetch item from collection by key and remove it from collection |
||
| 291 | * |
||
| 292 | * @param mixed $key |
||
| 293 | * |
||
| 294 | * @return mixed |
||
| 295 | */ |
||
| 296 | 1 | public function pull($key) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Join collection items using a delimiter |
||
| 307 | * |
||
| 308 | * @param string $delim |
||
| 309 | * |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | 1 | public function join($delim = '') |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Determine if collection has any items |
||
| 319 | * |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | 4 | public function isEmpty() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Get a collection of only this collection's values (without its keys) |
||
| 329 | * |
||
| 330 | * @return Collection |
||
| 331 | */ |
||
| 332 | 1 | public function values() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Get a collection of only this collection's keys |
||
| 339 | * |
||
| 340 | * @return Collection |
||
| 341 | */ |
||
| 342 | 1 | public function keys() |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Get a collection with order reversed |
||
| 349 | * |
||
| 350 | * @return Collection |
||
| 351 | */ |
||
| 352 | 6 | public function reverse() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Get a collection with keys and values flipped |
||
| 359 | * |
||
| 360 | * @return Collection |
||
| 361 | */ |
||
| 362 | 1 | public function flip() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Shuffle the order of this collection's values |
||
| 373 | * |
||
| 374 | * @return Collection |
||
| 375 | */ |
||
| 376 | 1 | public function shuffle() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Get a random value from the collection |
||
| 384 | * |
||
| 385 | * @return mixed |
||
| 386 | */ |
||
| 387 | 1 | public function random() |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Sort the collection (using values) |
||
| 394 | * |
||
| 395 | * @param callable $alg |
||
| 396 | * |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | 2 | public function sort(callable $alg = null) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Sort the collection (using keys) |
||
| 412 | * |
||
| 413 | * @param callable $alg |
||
| 414 | * |
||
| 415 | * @return $this |
||
| 416 | */ |
||
| 417 | 2 | public function ksort(callable $alg = null) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Append items to collection without regard to keys |
||
| 430 | * |
||
| 431 | * @param array|Traversable $items |
||
| 432 | * |
||
| 433 | * @return $this |
||
| 434 | */ |
||
| 435 | 2 | public function append($items) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Return first item or first item where callback returns true |
||
| 450 | * |
||
| 451 | * @param callable|null $callback |
||
| 452 | * |
||
| 453 | * @return mixed|null |
||
| 454 | */ |
||
| 455 | 7 | public function first(callable $callback = null) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Return last item or last item where callback returns true |
||
| 469 | * |
||
| 470 | * @param callable|null $callback |
||
| 471 | * |
||
| 472 | * @return mixed|null |
||
| 473 | */ |
||
| 474 | 3 | public function last(callable $callback = null) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Map collection |
||
| 481 | * |
||
| 482 | * Create a new collection using the results of a callback function on each item in this collection. |
||
| 483 | * |
||
| 484 | * @param callable $callback |
||
| 485 | * |
||
| 486 | * @return Collection |
||
| 487 | */ |
||
| 488 | 3 | public function map(callable $callback) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Combine collection with another traversable/collection |
||
| 502 | * |
||
| 503 | * Using this collection's keys, and the incoming collection's values, a new collection is created and returned. |
||
| 504 | * |
||
| 505 | * @param array|Traversable $items |
||
| 506 | * |
||
| 507 | * @return Collection |
||
| 508 | */ |
||
| 509 | 5 | public function combine($items) |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Get a new collection with only distinct values |
||
| 525 | * |
||
| 526 | * @return Collection |
||
| 527 | */ |
||
| 528 | 1 | public function distinct() |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Remove all duplicate values from collection in-place |
||
| 542 | * |
||
| 543 | * @return Collection |
||
| 544 | */ |
||
| 545 | 1 | public function deduplicate() |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Return a new collection with only filtered keys/values |
||
| 554 | * |
||
| 555 | * The callback accepts value, key, index and should return true if the item should be added to the returned |
||
| 556 | * collection |
||
| 557 | * |
||
| 558 | * @param callable $callback |
||
| 559 | * |
||
| 560 | * @return Collection |
||
| 561 | */ |
||
| 562 | 1 | public function filter(callable $callback) |
|
| 574 | |||
| 575 | /** |
||
| 576 | * Fold collection into a single value |
||
| 577 | * |
||
| 578 | * Loop through collection calling a callback function and passing the result to the next iteration, eventually |
||
| 579 | * returning a single value. |
||
| 580 | * |
||
| 581 | * @param callable $callback |
||
| 582 | * @param mixed $initial |
||
| 583 | * |
||
| 584 | * @return null |
||
| 585 | */ |
||
| 586 | 1 | public function fold(callable $callback, $initial = null) |
|
| 596 | |||
| 597 | /** |
||
| 598 | * Return a merge of this collection and $items |
||
| 599 | * |
||
| 600 | * @param array|Traversable $items |
||
| 601 | * |
||
| 602 | * @return Collection |
||
| 603 | */ |
||
| 604 | 3 | public function merge($items) |
|
| 617 | |||
| 618 | /** |
||
| 619 | * Create a new collection with a union of this collection and $items |
||
| 620 | * |
||
| 621 | * This method is similar to merge, except that existing items will not be overwritten. |
||
| 622 | * |
||
| 623 | * @param $items |
||
| 624 | */ |
||
| 625 | 2 | public function union($items) |
|
| 638 | |||
| 639 | /** |
||
| 640 | * Call callback for each item in collection, passively |
||
| 641 | * |
||
| 642 | * @param callable $callback |
||
| 643 | * |
||
| 644 | * @return $this |
||
| 645 | */ |
||
| 646 | 1 | public function each(callable $callback) |
|
| 655 | |||
| 656 | /** |
||
| 657 | * Assert callback returns $expected value for each item in collection. |
||
| 658 | * |
||
| 659 | * @todo This can be used to easily make methods like all($callback) and none($callback). |
||
| 660 | * |
||
| 661 | * @param callable $callback |
||
| 662 | * @param bool $expected |
||
| 663 | * |
||
| 664 | * @return bool |
||
| 665 | */ |
||
| 666 | 1 | public function assert(callable $callback, $expected = true) |
|
| 677 | |||
| 678 | /** |
||
| 679 | * Pipe collection through a callback |
||
| 680 | * |
||
| 681 | * @param callable $callback |
||
| 682 | * |
||
| 683 | * @return mixed |
||
| 684 | */ |
||
| 685 | 1 | public function pipe(callable $callback) |
|
| 689 | |||
| 690 | /** |
||
| 691 | * Get new collection in chunks of $size |
||
| 692 | * |
||
| 693 | * Creates a new collection of arrays of $size length. The remainder items will be placed at the end. |
||
| 694 | * |
||
| 695 | * @param int $size |
||
| 696 | * |
||
| 697 | * @return Collection |
||
| 698 | */ |
||
| 699 | 2 | public function chunk($size) |
|
| 703 | |||
| 704 | /** |
||
| 705 | * Get a new collection of $count chunks |
||
| 706 | * |
||
| 707 | * @todo It might be useful to have a method that spreads remainder items more evenly so you don't end up with the |
||
| 708 | * last item containing only one or two items. |
||
| 709 | * |
||
| 710 | * @param int $count |
||
| 711 | * |
||
| 712 | * @return Collection |
||
| 713 | */ |
||
| 714 | 1 | public function split($count = 1) |
|
| 718 | |||
| 719 | /** |
||
| 720 | * Get a slice of this collection. |
||
| 721 | * |
||
| 722 | * @param int $offset |
||
| 723 | * @param int|null $length |
||
| 724 | * |
||
| 725 | * @return Collection |
||
| 726 | */ |
||
| 727 | 1 | public function slice($offset, $length = null) |
|
| 731 | |||
| 732 | /** |
||
| 733 | * Get collection with only differing items |
||
| 734 | * |
||
| 735 | * @param array|Traversable $items |
||
| 736 | * |
||
| 737 | * @return Collection |
||
| 738 | */ |
||
| 739 | 1 | public function diff($items) |
|
| 743 | |||
| 744 | /** |
||
| 745 | * Get collection with only differing items (by key) |
||
| 746 | * |
||
| 747 | * @param array|Traversable $items |
||
| 748 | * |
||
| 749 | * @return Collection |
||
| 750 | */ |
||
| 751 | 1 | public function kdiff($items) |
|
| 755 | |||
| 756 | /** |
||
| 757 | * Get collection with only intersecting items |
||
| 758 | * |
||
| 759 | * @param array|Traversable $items |
||
| 760 | * |
||
| 761 | * @return Collection |
||
| 762 | */ |
||
| 763 | 1 | public function intersect($items) |
|
| 767 | |||
| 768 | /** |
||
| 769 | * Get collection with only intersecting items (by key) |
||
| 770 | * |
||
| 771 | * @param array|Traversable $items |
||
| 772 | * |
||
| 773 | * @return Collection |
||
| 774 | */ |
||
| 775 | 1 | public function kintersect($items) |
|
| 779 | |||
| 780 | /** |
||
| 781 | * Remove last item in collection and return it |
||
| 782 | * |
||
| 783 | * @return mixed |
||
| 784 | */ |
||
| 785 | 1 | public function pop() |
|
| 789 | |||
| 790 | /** |
||
| 791 | * Remove first item in collection and return it (and re-index if numerically indexed) |
||
| 792 | * |
||
| 793 | * @return mixed |
||
| 794 | */ |
||
| 795 | 2 | public function shift() |
|
| 799 | |||
| 800 | /** |
||
| 801 | * Add item to the end of the collection |
||
| 802 | * |
||
| 803 | * @note This method is no different than add() but I included it for consistency's sake since I have the others |
||
| 804 | * |
||
| 805 | * @param mixed $item |
||
| 806 | * |
||
| 807 | * @return $this |
||
| 808 | */ |
||
| 809 | 1 | public function push($item) |
|
| 813 | |||
| 814 | /** |
||
| 815 | * Add item to the beginning of the collection (and re-index if a numerically indexed collection) |
||
| 816 | * |
||
| 817 | * @param mixed $item |
||
| 818 | * |
||
| 819 | * @return $this |
||
| 820 | */ |
||
| 821 | 2 | public function unshift($item) |
|
| 827 | |||
| 828 | /** ++++ ++++ **/ |
||
| 829 | /** ++ Interface Compliance ++ **/ |
||
| 830 | /** ++++ ++++ **/ |
||
| 831 | |||
| 832 | /** |
||
| 833 | * @return array |
||
| 834 | */ |
||
| 835 | 1 | public function jsonSerialize() |
|
| 839 | |||
| 840 | /** ++++ ++++ **/ |
||
| 841 | /** ++ Array Access Methods ++ **/ |
||
| 842 | /** ++++ ++++ **/ |
||
| 843 | |||
| 844 | /** |
||
| 845 | * {@inheritDoc} |
||
| 846 | */ |
||
| 847 | 1 | public function offsetExists($offset) |
|
| 851 | |||
| 852 | /** |
||
| 853 | * {@inheritDoc} |
||
| 854 | */ |
||
| 855 | 2 | public function offsetGet($offset) |
|
| 863 | |||
| 864 | /** |
||
| 865 | * {@inheritDoc} |
||
| 866 | */ |
||
| 867 | 1 | public function offsetUnset($offset) |
|
| 871 | |||
| 872 | /** |
||
| 873 | * {@inheritDoc} |
||
| 874 | */ |
||
| 875 | 1 | public function offsetSet($offset, $value) |
|
| 883 | |||
| 884 | /** ++++ ++++ **/ |
||
| 885 | /** ++ Iterator Methods ++ **/ |
||
| 886 | /** ++++ ++++ **/ |
||
| 887 | |||
| 888 | /** |
||
| 889 | * {@inheritDoc} |
||
| 890 | */ |
||
| 891 | 30 | public function current() |
|
| 895 | |||
| 896 | /** |
||
| 897 | * {@inheritDoc} |
||
| 898 | */ |
||
| 899 | 30 | public function key() |
|
| 903 | |||
| 904 | /** |
||
| 905 | * {@inheritDoc} |
||
| 906 | */ |
||
| 907 | 29 | public function next() |
|
| 911 | |||
| 912 | /** |
||
| 913 | * {@inheritDoc} |
||
| 914 | * |
||
| 915 | * @todo Should this return $this? |
||
| 916 | */ |
||
| 917 | 83 | public function rewind() |
|
| 921 | |||
| 922 | /** |
||
| 923 | * {@inheritDoc} |
||
| 924 | */ |
||
| 925 | 30 | public function valid() |
|
| 929 | |||
| 930 | /** ++++ ++++ **/ |
||
| 931 | /** ++ Countable Method ++ **/ |
||
| 932 | /** ++++ ++++ **/ |
||
| 933 | |||
| 934 | /** |
||
| 935 | * {@inheritDoc} |
||
| 936 | */ |
||
| 937 | 7 | public function count() |
|
| 941 | } |