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 |
||
| 22 | class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable |
||
| 23 | { |
||
| 24 | /** @var array */ |
||
| 25 | protected $items; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Collection constructor. |
||
| 29 | * |
||
| 30 | * @param array $items |
||
| 31 | */ |
||
| 32 | 70 | public function __construct(array $items = []) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Generate a collection from an array of items. |
||
| 40 | * I created this method so that it's possible to extend a collection more easily. |
||
| 41 | * |
||
| 42 | * @param array $items |
||
| 43 | * |
||
| 44 | * @todo call to_array on $items and don't require input to be an array. |
||
| 45 | * |
||
| 46 | * @return Collection |
||
| 47 | */ |
||
| 48 | 21 | public static function factory(array $items = []) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Get collection as an array |
||
| 55 | * |
||
| 56 | * @return array |
||
| 57 | */ |
||
| 58 | 30 | public function toArray() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Determine if collection has a given key |
||
| 65 | * |
||
| 66 | * @param mixed $key The key to look for |
||
| 67 | * |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | 38 | public function has($key) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Does collection have item at position? |
||
| 77 | * |
||
| 78 | * Determine if collection has an item at a particular position (indexed from one). |
||
| 79 | * Position can be positive and start from the beginning or it can be negative and |
||
| 80 | * start from the end. |
||
| 81 | * |
||
| 82 | * @param int $position |
||
| 83 | * |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | 2 | public function hasValueAt($position) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Get key at given position |
||
| 98 | * |
||
| 99 | * Returns the key at the given position, starting from one. Position can be positive (start from beginning) or |
||
| 100 | * negative (start from the end). |
||
| 101 | * |
||
| 102 | * If the position does not exist, a RuntimeException is thrown. |
||
| 103 | * |
||
| 104 | * @param int $position |
||
| 105 | * |
||
| 106 | * @return string |
||
| 107 | * |
||
| 108 | * @throws RuntimeException |
||
| 109 | */ |
||
| 110 | 6 | public function getKeyAt($position) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Get value at given position |
||
| 127 | * |
||
| 128 | * Returns the value at the given position, starting from one. Position can be positive (start from beginning) or |
||
| 129 | * negative (start from the end). |
||
| 130 | * |
||
| 131 | * If the position does not exist, a RuntimeException is thrown. |
||
| 132 | * |
||
| 133 | * @param int $position |
||
| 134 | * |
||
| 135 | * @return mixed |
||
| 136 | * |
||
| 137 | * @throws RuntimeException |
||
| 138 | */ |
||
| 139 | 2 | public function getValueAt($position) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Get the key of the first item found matching $item |
||
| 146 | * |
||
| 147 | * @todo Perhaps this should allow a callback in place of $item? |
||
| 148 | * |
||
| 149 | * @param mixed $item |
||
| 150 | * |
||
| 151 | * @return mixed|null |
||
| 152 | */ |
||
| 153 | 1 | public function keyOf($item) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Get the offset (index) of the first item found that matches $item |
||
| 166 | * |
||
| 167 | * @todo Perhaps this should allow a callback in place of $item? |
||
| 168 | * |
||
| 169 | * @param mixed $item |
||
| 170 | * |
||
| 171 | * @return int|null |
||
| 172 | */ |
||
| 173 | 1 | public function indexOf($item) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Get item by key, with an optional default return value |
||
| 188 | * |
||
| 189 | * @param mixed $key |
||
| 190 | * @param mixed $default |
||
| 191 | * |
||
| 192 | * @return mixed |
||
| 193 | */ |
||
| 194 | 8 | public function get($key, $default = null) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Add an item with no regard to key |
||
| 205 | * |
||
| 206 | * @param mixed $value |
||
| 207 | * |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | 4 | public function add($value) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Set an item at a given key |
||
| 219 | * |
||
| 220 | * @param mixed $key |
||
| 221 | * @param mixed $value |
||
| 222 | * @param bool $overwrite If false, do not overwrite existing key |
||
| 223 | * |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | 12 | public function set($key, $value, $overwrite = true) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Delete an item by key |
||
| 237 | * |
||
| 238 | * @param mixed $key |
||
| 239 | * |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | 3 | public function delete($key) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Clear the collection of all its items. |
||
| 251 | * |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | 2 | public function clear() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Determine if collection contains given value |
||
| 263 | * |
||
| 264 | * @param mixed $val |
||
| 265 | * |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | 2 | public function contains($val) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Fetch item from collection by key and remove it from collection |
||
| 275 | * |
||
| 276 | * @param mixed $key |
||
| 277 | * |
||
| 278 | * @return mixed |
||
| 279 | */ |
||
| 280 | 1 | public function pull($key) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Join collection items using a delimiter |
||
| 291 | * |
||
| 292 | * @param string $delim |
||
| 293 | * |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | 1 | public function join($delim = '') |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Determine if collection has any items |
||
| 303 | * |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | 4 | public function isEmpty() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Get a collection of only this collection's values (without its keys) |
||
| 313 | * |
||
| 314 | * @return Collection |
||
| 315 | */ |
||
| 316 | 1 | public function values() |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Get a collection of only this collection's keys |
||
| 323 | * |
||
| 324 | * @return Collection |
||
| 325 | */ |
||
| 326 | 1 | public function keys() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Get a collection with order reversed |
||
| 333 | * |
||
| 334 | * @return Collection |
||
| 335 | */ |
||
| 336 | 6 | public function reverse() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Get a collection with keys and values flipped |
||
| 343 | * |
||
| 344 | * @return Collection |
||
| 345 | */ |
||
| 346 | 1 | public function flip() |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Shuffle the order of this collection's values |
||
| 357 | * |
||
| 358 | * @return Collection |
||
| 359 | */ |
||
| 360 | 1 | public function shuffle() |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Get a random value from the collection |
||
| 368 | * |
||
| 369 | * @return mixed |
||
| 370 | */ |
||
| 371 | 1 | public function random() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Sort the collection (using values) |
||
| 378 | * |
||
| 379 | * @param callable $alg |
||
| 380 | * |
||
| 381 | * @return $this |
||
| 382 | */ |
||
| 383 | 2 | public function sort(callable $alg = null) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Sort the collection (using keys) |
||
| 396 | * |
||
| 397 | * @param callable $alg |
||
| 398 | * |
||
| 399 | * @return $this |
||
| 400 | */ |
||
| 401 | 2 | public function ksort(callable $alg = null) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Append items to collection without regard to keys |
||
| 414 | * |
||
| 415 | * @param array|Traversable $items |
||
| 416 | * |
||
| 417 | * @return $this |
||
| 418 | */ |
||
| 419 | 2 | public function append($items) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Return first item or first item where callback returns true |
||
| 434 | * |
||
| 435 | * @param callable|null $callback |
||
| 436 | * |
||
| 437 | * @return mixed|null |
||
| 438 | */ |
||
| 439 | 7 | public function first(callable $callback = null) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Return last item or last item where callback returns true |
||
| 453 | * |
||
| 454 | * @param callable|null $callback |
||
| 455 | * |
||
| 456 | * @return mixed|null |
||
| 457 | */ |
||
| 458 | 3 | public function last(callable $callback = null) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Map collection |
||
| 465 | * |
||
| 466 | * Create a new collection using the results of a callback function on each item in this collection. |
||
| 467 | * |
||
| 468 | * @param callable $callback |
||
| 469 | * |
||
| 470 | * @return Collection |
||
| 471 | */ |
||
| 472 | 3 | public function map(callable $callback) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Combine collection with another traversable/collection |
||
| 486 | * |
||
| 487 | * Using this collection's keys, and the incoming collection's values, a new collection is created and returned. |
||
| 488 | * |
||
| 489 | * @param array|Traversable $items |
||
| 490 | * |
||
| 491 | * @return Collection |
||
| 492 | */ |
||
| 493 | 5 | public function combine($items) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Get a new collection with only distinct values |
||
| 509 | * |
||
| 510 | * @return Collection |
||
| 511 | */ |
||
| 512 | 1 | public function distinct() |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Remove all duplicate values from collection in-place |
||
| 526 | * |
||
| 527 | * @return Collection |
||
| 528 | */ |
||
| 529 | 1 | public function deduplicate() |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Return a new collection with only filtered keys/values |
||
| 538 | * |
||
| 539 | * The callback accepts value, key, index and should return true if the item should be added to the returned |
||
| 540 | * collection |
||
| 541 | * |
||
| 542 | * @param callable $callback |
||
| 543 | * |
||
| 544 | * @return Collection |
||
| 545 | */ |
||
| 546 | 1 | public function filter(callable $callback) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Fold collection into a single value |
||
| 561 | * |
||
| 562 | * Loop through collection calling a callback function and passing the result to the next iteration, eventually |
||
| 563 | * returning a single value. |
||
| 564 | * |
||
| 565 | * @param callable $callback |
||
| 566 | * @param mixed $initial |
||
| 567 | * |
||
| 568 | * @return null |
||
| 569 | */ |
||
| 570 | 1 | public function fold(callable $callback, $initial = null) |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Return a merge of this collection and $items |
||
| 583 | * |
||
| 584 | * @param array|Traversable $items |
||
| 585 | * |
||
| 586 | * @return Collection |
||
| 587 | */ |
||
| 588 | 3 | public function merge($items) |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Create a new collection with a union of this collection and $items |
||
| 604 | * |
||
| 605 | * This method is similar to merge, except that existing items will not be overwritten. |
||
| 606 | * |
||
| 607 | * @param $items |
||
| 608 | */ |
||
| 609 | 2 | public function union($items) |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Call callback for each item in collection, passively |
||
| 625 | * |
||
| 626 | * @param callable $callback |
||
| 627 | * |
||
| 628 | * @return $this |
||
| 629 | */ |
||
| 630 | 1 | public function each(callable $callback) |
|
| 639 | |||
| 640 | /** |
||
| 641 | * Assert callback returns $expected value for each item in collection. |
||
| 642 | * |
||
| 643 | * @todo This can be used to easily make methods like all($callback) and none($callback). |
||
| 644 | * |
||
| 645 | * @param callable $callback |
||
| 646 | * @param bool $expected |
||
| 647 | * |
||
| 648 | * @return bool |
||
| 649 | */ |
||
| 650 | 1 | public function assert(callable $callback, $expected = true) |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Pipe collection through a callback |
||
| 664 | * |
||
| 665 | * @param callable $callback |
||
| 666 | * |
||
| 667 | * @return mixed |
||
| 668 | */ |
||
| 669 | 1 | public function pipe(callable $callback) |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Get new collection in chunks of $size |
||
| 676 | * |
||
| 677 | * Creates a new collection of arrays of $size length. The remainder items will be placed at the end. |
||
| 678 | * |
||
| 679 | * @param int $size |
||
| 680 | * |
||
| 681 | * @return Collection |
||
| 682 | */ |
||
| 683 | 2 | public function chunk($size) |
|
| 687 | |||
| 688 | /** |
||
| 689 | * Get a new collection of $count chunks |
||
| 690 | * |
||
| 691 | * @todo It might be useful to have a method that spreads remainder items more evenly so you don't end up with the |
||
| 692 | * last item containing only one or two items. |
||
| 693 | * |
||
| 694 | * @param int $count |
||
| 695 | * |
||
| 696 | * @return Collection |
||
| 697 | */ |
||
| 698 | 1 | public function slice($count = 1) |
|
| 702 | |||
| 703 | /** ++++ ++++ **/ |
||
| 704 | /** ++ Interface Compliance ++ **/ |
||
| 705 | /** ++++ ++++ **/ |
||
| 706 | |||
| 707 | /** |
||
| 708 | * @return array |
||
| 709 | */ |
||
| 710 | 1 | public function jsonSerialize() |
|
| 714 | |||
| 715 | /** ++++ ++++ **/ |
||
| 716 | /** ++ Array Access Methods ++ **/ |
||
| 717 | /** ++++ ++++ **/ |
||
| 718 | |||
| 719 | /** |
||
| 720 | * {@inheritDoc} |
||
| 721 | */ |
||
| 722 | 1 | public function offsetExists($offset) |
|
| 726 | |||
| 727 | /** |
||
| 728 | * {@inheritDoc} |
||
| 729 | */ |
||
| 730 | 2 | public function offsetGet($offset) |
|
| 738 | |||
| 739 | /** |
||
| 740 | * {@inheritDoc} |
||
| 741 | */ |
||
| 742 | 1 | public function offsetUnset($offset) |
|
| 746 | |||
| 747 | /** |
||
| 748 | * {@inheritDoc} |
||
| 749 | */ |
||
| 750 | 1 | public function offsetSet($offset, $value) |
|
| 758 | |||
| 759 | /** ++++ ++++ **/ |
||
| 760 | /** ++ Iterator Methods ++ **/ |
||
| 761 | /** ++++ ++++ **/ |
||
| 762 | |||
| 763 | /** |
||
| 764 | * {@inheritDoc} |
||
| 765 | */ |
||
| 766 | 27 | public function current() |
|
| 770 | |||
| 771 | /** |
||
| 772 | * {@inheritDoc} |
||
| 773 | */ |
||
| 774 | 27 | public function key() |
|
| 778 | |||
| 779 | /** |
||
| 780 | * {@inheritDoc} |
||
| 781 | */ |
||
| 782 | 26 | public function next() |
|
| 786 | |||
| 787 | /** |
||
| 788 | * {@inheritDoc} |
||
| 789 | * |
||
| 790 | * @todo Should this return $this? |
||
| 791 | */ |
||
| 792 | 70 | public function rewind() |
|
| 796 | |||
| 797 | /** |
||
| 798 | * {@inheritDoc} |
||
| 799 | */ |
||
| 800 | 27 | public function valid() |
|
| 804 | |||
| 805 | /** ++++ ++++ **/ |
||
| 806 | /** ++ Countable Method ++ **/ |
||
| 807 | /** ++++ ++++ **/ |
||
| 808 | |||
| 809 | /** |
||
| 810 | * {@inheritDoc} |
||
| 811 | */ |
||
| 812 | 7 | public function count() |
|
| 816 | } |