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 |
||
| 35 | class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable |
||
| 36 | 93 | { |
|
| 37 | /** @var array The items for this collection */ |
||
| 38 | 93 | protected $items; |
|
| 39 | 93 | ||
| 40 | 93 | /** |
|
| 41 | * Collection constructor. |
||
| 42 | * |
||
| 43 | * @param array $items |
||
| 44 | */ |
||
| 45 | public function __construct(array $items = []) |
||
| 50 | 30 | ||
| 51 | /** |
||
| 52 | 30 | * Generate a collection from an array of items. |
|
| 53 | 10 | * I created this method so that it's possible to extend a collection more easily. |
|
| 54 | 10 | * |
|
| 55 | 30 | * @param mixed $items |
|
| 56 | * |
||
| 57 | * @return Collection |
||
| 58 | */ |
||
| 59 | public static function factory($items = null) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get collection as an array |
||
| 69 | * |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public function toArray() |
||
| 76 | |||
| 77 | 47 | /** |
|
| 78 | * Determine if collection has a given key |
||
| 79 | * |
||
| 80 | * @param mixed $key The key to look for |
||
| 81 | * |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | public function has($key) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Does collection have item at position? |
||
| 91 | 2 | * |
|
| 92 | * Determine if collection has an item at a particular position (indexed from one). |
||
| 93 | * Position can be positive and start from the beginning or it can be negative and |
||
| 94 | 2 | * start from the end. |
|
| 95 | 2 | * |
|
| 96 | 2 | * @param int $position |
|
| 97 | 2 | * |
|
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | public function hasValueAt($position) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get key at given position |
||
| 112 | * |
||
| 113 | * Returns the key at the given position, starting from one. Position can be positive (start from beginning) or |
||
| 114 | * negative (start from the end). |
||
| 115 | 6 | * |
|
| 116 | * If the position does not exist, a RuntimeException is thrown. |
||
| 117 | 6 | * |
|
| 118 | 6 | * @param int $position |
|
| 119 | 3 | * |
|
| 120 | 3 | * @return string |
|
| 121 | 6 | * |
|
| 122 | 6 | * @throws RuntimeException |
|
| 123 | 6 | */ |
|
| 124 | 5 | public function getKeyAt($position) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Get value at given position |
||
| 141 | * |
||
| 142 | * Returns the value at the given position, starting from one. Position can be positive (start from beginning) or |
||
| 143 | * negative (start from the end). |
||
| 144 | 2 | * |
|
| 145 | * If the position does not exist, a RuntimeException is thrown. |
||
| 146 | 2 | * |
|
| 147 | * @param int $position |
||
| 148 | * |
||
| 149 | * @return mixed |
||
| 150 | * |
||
| 151 | * @throws RuntimeException |
||
| 152 | */ |
||
| 153 | public function getValueAt($position) |
||
| 157 | |||
| 158 | 2 | /** |
|
| 159 | * Get the key of the first item found matching $item |
||
| 160 | 2 | * |
|
| 161 | 2 | * @param mixed|callable $item |
|
| 162 | 2 | * |
|
| 163 | 1 | * @return mixed|null |
|
| 164 | 1 | */ |
|
| 165 | public function keyOf($item) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get the offset (index) of the first item found that matches $item |
||
| 183 | 2 | * |
|
| 184 | * @param mixed|callable $item |
||
| 185 | 2 | * |
|
| 186 | 2 | * @return int|null |
|
| 187 | 2 | */ |
|
| 188 | 1 | public function indexOf($item) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Get item by key, with an optional default return value |
||
| 209 | * |
||
| 210 | 8 | * @param mixed $key |
|
| 211 | * @param mixed $default |
||
| 212 | 8 | * |
|
| 213 | 8 | * @return mixed |
|
| 214 | */ |
||
| 215 | public function get($key, $default = null) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Add an item with no regard to key |
||
| 226 | 8 | * |
|
| 227 | * @param mixed $value |
||
| 228 | 8 | * |
|
| 229 | * @return $this |
||
| 230 | 8 | */ |
|
| 231 | public function add($value) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Set an item at a given key |
||
| 240 | * |
||
| 241 | * @param mixed $key |
||
| 242 | 14 | * @param mixed $value |
|
| 243 | * @param bool $overwrite If false, do not overwrite existing key |
||
| 244 | 14 | * |
|
| 245 | 14 | * @return $this |
|
| 246 | 14 | */ |
|
| 247 | public function set($key, $value, $overwrite = true) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Delete an item by key |
||
| 258 | 3 | * |
|
| 259 | * @param mixed $key |
||
| 260 | 3 | * |
|
| 261 | * @return $this |
||
| 262 | 3 | */ |
|
| 263 | public function delete($key) |
||
| 269 | |||
| 270 | 2 | /** |
|
| 271 | * Clear the collection of all its items. |
||
| 272 | 2 | * |
|
| 273 | * @return $this |
||
| 274 | 2 | */ |
|
| 275 | public function clear() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Determine if collection contains given value |
||
| 284 | * |
||
| 285 | 4 | * @param mixed|callable $val |
|
| 286 | * @param mixed $key |
||
| 287 | 4 | * |
|
| 288 | 4 | * @return bool |
|
| 289 | 4 | */ |
|
| 290 | 4 | public function contains($val, $key = null) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Fetch item from collection by key and remove it from collection |
||
| 311 | 1 | * |
|
| 312 | * @param mixed $key |
||
| 313 | 1 | * |
|
| 314 | 1 | * @return mixed |
|
| 315 | 1 | */ |
|
| 316 | 1 | public function pull($key) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Join collection items using a delimiter |
||
| 327 | 1 | * |
|
| 328 | * @param string $delim |
||
| 329 | 1 | * |
|
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public function join($delim = '') |
||
| 336 | |||
| 337 | 4 | /** |
|
| 338 | * Determine if collection has any items |
||
| 339 | 4 | * |
|
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | public function isEmpty() |
||
| 346 | |||
| 347 | 1 | /** |
|
| 348 | * Get a collection of only this collection's values (without its keys) |
||
| 349 | 1 | * |
|
| 350 | * @return Collection |
||
| 351 | */ |
||
| 352 | public function values() |
||
| 356 | |||
| 357 | 1 | /** |
|
| 358 | * Get a collection of only this collection's keys |
||
| 359 | 1 | * |
|
| 360 | * @return Collection |
||
| 361 | */ |
||
| 362 | public function keys() |
||
| 366 | |||
| 367 | 6 | /** |
|
| 368 | * Get a collection with order reversed |
||
| 369 | 6 | * |
|
| 370 | * @return Collection |
||
| 371 | */ |
||
| 372 | public function reverse() |
||
| 376 | |||
| 377 | 1 | /** |
|
| 378 | * Get a collection with keys and values flipped |
||
| 379 | 1 | * |
|
| 380 | 1 | * @return Collection |
|
| 381 | 1 | */ |
|
| 382 | 1 | public function flip() |
|
| 390 | |||
| 391 | 1 | /** |
|
| 392 | * Shuffle the order of this collection's values |
||
| 393 | 1 | * |
|
| 394 | 1 | * @return Collection |
|
| 395 | */ |
||
| 396 | public function shuffle() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get a random value from the collection |
||
| 404 | 1 | * |
|
| 405 | * @return mixed |
||
| 406 | 1 | */ |
|
| 407 | public function random() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Sort the collection (using values) |
||
| 414 | * |
||
| 415 | * @param callable $alg |
||
| 416 | 2 | * |
|
| 417 | * @return $this |
||
| 418 | 2 | */ |
|
| 419 | public function sort(callable $alg = null) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Sort the collection (using keys) |
||
| 432 | * |
||
| 433 | * @param callable $alg |
||
| 434 | 2 | * |
|
| 435 | * @return $this |
||
| 436 | 2 | */ |
|
| 437 | public function ksort(callable $alg = null) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Append items to collection without regard to keys |
||
| 450 | * |
||
| 451 | * @param array|Traversable $items |
||
| 452 | 2 | * |
|
| 453 | * @return $this |
||
| 454 | 2 | */ |
|
| 455 | 1 | public function append($items) |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Return first item or first item where callback returns true |
||
| 470 | * |
||
| 471 | * @param callable|null $callback |
||
| 472 | 7 | * |
|
| 473 | * @return mixed|null |
||
| 474 | 7 | */ |
|
| 475 | 7 | public function first(callable $callback = null) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Return last item or last item where callback returns true |
||
| 489 | * |
||
| 490 | * @param callable|null $callback |
||
| 491 | 3 | * |
|
| 492 | * @return mixed|null |
||
| 493 | 3 | */ |
|
| 494 | public function last(callable $callback = null) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Map collection |
||
| 501 | * |
||
| 502 | * Create a new collection using the results of a callback function on each item in this collection. |
||
| 503 | * |
||
| 504 | * @param callable $callback |
||
| 505 | 3 | * |
|
| 506 | * @return Collection |
||
| 507 | 3 | */ |
|
| 508 | public function map(callable $callback) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Combine collection with another traversable/collection |
||
| 522 | * |
||
| 523 | * Using this collection's keys, and the incoming collection's values, a new collection is created and returned. |
||
| 524 | * |
||
| 525 | * @param array|Traversable $items |
||
| 526 | 5 | * |
|
| 527 | * @return Collection |
||
| 528 | 5 | */ |
|
| 529 | 1 | public function combine($items) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Get a new collection with only distinct values |
||
| 545 | 1 | * |
|
| 546 | * @return Collection |
||
| 547 | 1 | */ |
|
| 548 | 1 | public function distinct() |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Remove all duplicate values from collection in-place |
||
| 562 | 1 | * |
|
| 563 | * @return Collection |
||
| 564 | 1 | */ |
|
| 565 | public function deduplicate() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Return a new collection with only filtered keys/values |
||
| 574 | * |
||
| 575 | * The callback accepts value, key, index and should return true if the item should be added to the returned |
||
| 576 | * collection |
||
| 577 | * |
||
| 578 | * @param callable $callback |
||
| 579 | 2 | * |
|
| 580 | * @return Collection |
||
| 581 | 2 | */ |
|
| 582 | 2 | public function filter(callable $callback = null) |
|
| 600 | |||
| 601 | /** |
||
| 602 | * Fold collection into a single value |
||
| 603 | * |
||
| 604 | * Loop through collection calling a callback function and passing the result to the next iteration, eventually |
||
| 605 | * returning a single value. |
||
| 606 | * |
||
| 607 | * @param callable $callback |
||
| 608 | * @param mixed $initial |
||
| 609 | 1 | * |
|
| 610 | * @return null |
||
| 611 | 1 | */ |
|
| 612 | 1 | public function fold(callable $callback, $initial = null) |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Return a merge of this collection and $items |
||
| 625 | * |
||
| 626 | * @param array|Traversable $items |
||
| 627 | 3 | * |
|
| 628 | * @return Collection |
||
| 629 | 3 | */ |
|
| 630 | 1 | public function merge($items) |
|
| 643 | |||
| 644 | /** |
||
| 645 | * Create a new collection with a union of this collection and $items |
||
| 646 | * |
||
| 647 | * This method is similar to merge, except that existing items will not be overwritten. |
||
| 648 | 2 | * |
|
| 649 | * @param $items |
||
| 650 | 2 | */ |
|
| 651 | 1 | public function union($items) |
|
| 664 | |||
| 665 | /** |
||
| 666 | * Call callback for each item in collection, passively |
||
| 667 | * If at any point the callback returns false, iteration stops. |
||
| 668 | * |
||
| 669 | * @param callable $callback |
||
| 670 | 2 | * |
|
| 671 | * @return $this |
||
| 672 | 2 | */ |
|
| 673 | 2 | public function each(callable $callback) |
|
| 684 | |||
| 685 | /** |
||
| 686 | * Assert callback returns $expected value for each item in collection. |
||
| 687 | * |
||
| 688 | * @param callable $callback |
||
| 689 | * @param bool $expected |
||
| 690 | * |
||
| 691 | * @return bool |
||
| 692 | 1 | */ |
|
| 693 | public function assert(callable $callback, $expected = true) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Pipe collection through a callback |
||
| 707 | * |
||
| 708 | * @param callable $callback |
||
| 709 | * |
||
| 710 | * @return mixed |
||
| 711 | 1 | */ |
|
| 712 | public function pipe(callable $callback) |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Get new collection in chunks of $size |
||
| 719 | * |
||
| 720 | * Creates a new collection of arrays of $size length. The remainder items will be placed at the end. |
||
| 721 | * |
||
| 722 | * @param int $size |
||
| 723 | * |
||
| 724 | * @return Collection |
||
| 725 | 2 | */ |
|
| 726 | public function chunk($size) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Get a new collection of $count chunks |
||
| 733 | * |
||
| 734 | * @param int $count |
||
| 735 | * |
||
| 736 | * @return Collection |
||
| 737 | */ |
||
| 738 | public function split($count = 1) |
||
| 742 | 1 | ||
| 743 | /** |
||
| 744 | * Get a slice of this collection. |
||
| 745 | * |
||
| 746 | * @param int $offset |
||
| 747 | * @param int|null $length |
||
| 748 | * |
||
| 749 | * @return Collection |
||
| 750 | */ |
||
| 751 | public function slice($offset, $length = null) |
||
| 755 | 1 | ||
| 756 | /** |
||
| 757 | * Get collection with only differing items |
||
| 758 | * |
||
| 759 | * @param array|Traversable $items |
||
| 760 | * |
||
| 761 | * @return Collection |
||
| 762 | */ |
||
| 763 | public function diff($items) |
||
| 767 | 1 | ||
| 768 | /** |
||
| 769 | * Get collection with only differing items (by key) |
||
| 770 | * |
||
| 771 | * @param array|Traversable $items |
||
| 772 | * |
||
| 773 | * @return Collection |
||
| 774 | */ |
||
| 775 | public function kdiff($items) |
||
| 779 | 1 | ||
| 780 | /** |
||
| 781 | * Get collection with only intersecting items |
||
| 782 | * |
||
| 783 | * @param array|Traversable $items |
||
| 784 | * |
||
| 785 | * @return Collection |
||
| 786 | */ |
||
| 787 | public function intersect($items) |
||
| 791 | 1 | ||
| 792 | /** |
||
| 793 | * Get collection with only intersecting items (by key) |
||
| 794 | * |
||
| 795 | * @param array|Traversable $items |
||
| 796 | * |
||
| 797 | * @return Collection |
||
| 798 | */ |
||
| 799 | public function kintersect($items) |
||
| 803 | 1 | ||
| 804 | /** |
||
| 805 | * Remove last item in collection and return it |
||
| 806 | * |
||
| 807 | * @return mixed |
||
| 808 | */ |
||
| 809 | public function pop() |
||
| 813 | 1 | ||
| 814 | /** |
||
| 815 | * Remove first item in collection and return it (and re-index if numerically indexed) |
||
| 816 | * |
||
| 817 | * @return mixed |
||
| 818 | */ |
||
| 819 | public function shift() |
||
| 823 | 2 | ||
| 824 | /** |
||
| 825 | * Add item to the end of the collection |
||
| 826 | * |
||
| 827 | * @note This method is no different than add() but I included it for consistency's sake since I have the others |
||
| 828 | * |
||
| 829 | * @param mixed $item |
||
| 830 | * |
||
| 831 | * @return $this |
||
| 832 | */ |
||
| 833 | public function push($item) |
||
| 837 | 1 | ||
| 838 | /** |
||
| 839 | * Add item to the beginning of the collection (and re-index if a numerically indexed collection) |
||
| 840 | * |
||
| 841 | * @param mixed $item |
||
| 842 | * |
||
| 843 | * @return $this |
||
| 844 | */ |
||
| 845 | public function unshift($item) |
||
| 851 | 5 | ||
| 852 | /** |
||
| 853 | * Get new collection padded to specified $size with $value |
||
| 854 | * |
||
| 855 | * Using $value, pad the collection to specified $size. If $size is smaller or equal to the size of the collection, |
||
| 856 | * then no padding takes place. If $size is positive, padding is added to the end, while if negative, padding will |
||
| 857 | * be added to the beginning. |
||
| 858 | * |
||
| 859 | * @param int $size |
||
| 860 | * @param mixed $value |
||
| 861 | * |
||
| 862 | * @return Collection |
||
| 863 | */ |
||
| 864 | public function pad($size, $value = null) |
||
| 877 | 3 | ||
| 878 | /** |
||
| 879 | * Partition collection into two collections using a callback |
||
| 880 | * |
||
| 881 | * Iterates over each element in the collection with a callback. Items where callback returns true are placed in one |
||
| 882 | * collection and the rest in another. Finally, the two collections are placed in an array and returned for easy use |
||
| 883 | * with the list() function. ( list($a, $b) = $col->partition(function($val, $key, $index) {}) ) |
||
| 884 | * |
||
| 885 | * @param callable $callback |
||
| 886 | * |
||
| 887 | * @return array<Collection> |
||
| 888 | */ |
||
| 889 | public function partition(callable $callback) |
||
| 905 | 2 | ||
| 906 | /** |
||
| 907 | * Get column values by key |
||
| 908 | * |
||
| 909 | * This method expects the collection's data to be tabular in nature (two-dimensional and for the rows to have |
||
| 910 | * consistently named keys). If the data is not structured this way, it will do the best it can but it is not meant |
||
| 911 | * for unstructured, non-tabular data so don't expect consistent results. |
||
| 912 | * |
||
| 913 | * @param string|int $column The key of the column you want to get |
||
| 914 | * |
||
| 915 | 1 | * @return Collection |
|
| 916 | */ |
||
| 917 | 1 | public function getColumn($column) |
|
| 921 | |||
| 922 | /** |
||
| 923 | * Is collection tabular? |
||
| 924 | * |
||
| 925 | * Returns true if the data in the collection is tabular in nature, meaning it is at least two-dimensional and each |
||
| 926 | * row contains the same number of values with the same keys. |
||
| 927 | 1 | * |
|
| 928 | * @return bool |
||
| 929 | 1 | */ |
|
| 930 | public function isTabular() |
||
| 942 | |||
| 943 | /** ++++ ++++ **/ |
||
| 944 | /** ++ Interface Compliance ++ **/ |
||
| 945 | /** ++++ ++++ **/ |
||
| 946 | |||
| 947 | 1 | /** |
|
| 948 | * JSON serialize |
||
| 949 | 1 | * |
|
| 950 | 1 | * @return array |
|
| 951 | */ |
||
| 952 | public function jsonSerialize() |
||
| 956 | |||
| 957 | 1 | /** ++++ ++++ **/ |
|
| 958 | 1 | /** ++ Array Access Methods ++ **/ |
|
| 959 | 1 | /** ++++ ++++ **/ |
|
| 960 | |||
| 961 | 1 | /** |
|
| 962 | 1 | * Does offset exist? |
|
| 963 | * |
||
| 964 | * @param mixed $offset |
||
| 965 | * |
||
| 966 | * @return bool |
||
| 967 | */ |
||
| 968 | public function offsetExists($offset) |
||
| 972 | |||
| 973 | 35 | /** |
|
| 974 | * Get item at offset |
||
| 975 | * |
||
| 976 | * @param mixed $offset |
||
| 977 | * |
||
| 978 | * @return mixed |
||
| 979 | 35 | */ |
|
| 980 | public function offsetGet($offset) |
||
| 988 | |||
| 989 | 34 | /** |
|
| 990 | * Unset item at offset |
||
| 991 | * |
||
| 992 | * @param mixed $offset |
||
| 993 | * |
||
| 994 | * @return void |
||
| 995 | */ |
||
| 996 | public function offsetUnset($offset) |
||
| 1000 | 93 | ||
| 1001 | /** |
||
| 1002 | * Set item at offset |
||
| 1003 | * |
||
| 1004 | * @param mixed $offset |
||
| 1005 | 36 | * @param mixed $value |
|
| 1006 | * |
||
| 1007 | 36 | * @return $this |
|
| 1008 | */ |
||
| 1009 | public function offsetSet($offset, $value) |
||
| 1017 | 10 | ||
| 1018 | /** ++++ ++++ **/ |
||
| 1019 | 10 | /** ++ Iterator Methods ++ **/ |
|
| 1020 | /** ++++ ++++ **/ |
||
| 1021 | |||
| 1022 | /** |
||
| 1023 | * {@inheritDoc} |
||
| 1024 | */ |
||
| 1025 | public function current() |
||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * {@inheritDoc} |
||
| 1032 | */ |
||
| 1033 | public function key() |
||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * {@inheritDoc} |
||
| 1040 | */ |
||
| 1041 | public function next() |
||
| 1045 | |||
| 1046 | /** |
||
| 1047 | * {@inheritDoc} |
||
| 1048 | */ |
||
| 1049 | public function rewind() |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * {@inheritDoc} |
||
| 1056 | */ |
||
| 1057 | public function valid() |
||
| 1061 | |||
| 1062 | /** ++++ ++++ **/ |
||
| 1063 | /** ++ Countable Method ++ **/ |
||
| 1064 | /** ++++ ++++ **/ |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * {@inheritDoc} |
||
| 1068 | */ |
||
| 1069 | public function count() |
||
| 1073 | } |