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 |
||
| 5 | class Collection implements \Iterator, \ArrayAccess, \Serializable, \Countable |
||
| 6 | { |
||
| 7 | protected $array = null; |
||
| 8 | protected $stack = []; |
||
| 9 | protected $iterator = null; |
||
| 10 | protected $key = null; |
||
| 11 | protected $val = null; |
||
| 12 | |||
| 13 | 4 | protected static function rangeGenerator($low, $high, $step = 1) |
|
| 20 | /** |
||
| 21 | * Create a collection based on a range generator |
||
| 22 | * @param int|float $low start value |
||
| 23 | * @param int|float $high end value |
||
| 24 | * @param int|float $step increment |
||
| 25 | * @return Collection |
||
| 26 | */ |
||
| 27 | 4 | public static function range($low, $high, $step = 1) : Collection |
|
| 31 | /** |
||
| 32 | * A static alias of the __constructor |
||
| 33 | * @param mixed $input Anything iterable |
||
| 34 | * @return Collection |
||
| 35 | */ |
||
| 36 | 51 | public static function from($input) : Collection |
|
| 40 | /** |
||
| 41 | * Create an instance |
||
| 42 | * @param mixed $input Anything iterable |
||
| 43 | */ |
||
| 44 | 55 | public function __construct($input = []) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Applies all pending operations |
||
| 80 | * @return $this |
||
| 81 | */ |
||
| 82 | 45 | public function squash() : Collection |
|
| 89 | /** |
||
| 90 | * Get an actual array from the collection |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | 44 | public function toArray() : array |
|
| 98 | /** |
||
| 99 | * Gets the first value in the collection or null if empty |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | 3 | public function value() |
|
| 109 | |||
| 110 | // iterator |
||
| 111 | 51 | public function key() |
|
| 155 | |||
| 156 | // array access |
||
| 157 | 1 | public function offsetGet($offset) |
|
| 179 | /** |
||
| 180 | * Get the collection length |
||
| 181 | * @return int |
||
| 182 | */ |
||
| 183 | 1 | public function count() |
|
| 190 | |||
| 191 | // mutators |
||
| 192 | /** |
||
| 193 | * Filter values from the collection based on a predicate. The callback will receive the value, key and collection |
||
| 194 | * @param callable $iterator the predicate |
||
| 195 | * @return $this |
||
| 196 | */ |
||
| 197 | 8 | public function filter(callable $iterator) : Collection |
|
| 202 | /** |
||
| 203 | * Pass all values of the collection through a mutator callable, which will receive the value, key and collection |
||
| 204 | * @param callable $iterator the mutator |
||
| 205 | * @return $this |
||
| 206 | */ |
||
| 207 | 5 | public function map(callable $iterator) : Collection |
|
| 212 | /** |
||
| 213 | * Pass all values of the collection through a key mutator callable, which will receive the value, key and collection |
||
| 214 | * @param callable $iterator the mutator |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | 1 | public function mapKey(callable $iterator) : Collection |
|
| 222 | /** |
||
| 223 | * Clone the current collection and return it. |
||
| 224 | * @return Collection |
||
| 225 | */ |
||
| 226 | public function clone() : Collection |
||
| 230 | /** |
||
| 231 | * Remove all falsy values from the collection (uses filter internally). |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | 1 | public function compact() : Collection |
|
| 240 | /** |
||
| 241 | * Exclude all listed values from the collection (uses filter internally). |
||
| 242 | * @param iterable $values the values to exclude |
||
| 243 | * @return $this |
||
| 244 | */ |
||
| 245 | 3 | public function difference($values) : Collection |
|
| 258 | /** |
||
| 259 | * Append more values to the collection |
||
| 260 | * @param iterable $source the values to add |
||
| 261 | * @return Collection |
||
| 262 | */ |
||
| 263 | 2 | public function extend($source) : Collection |
|
| 270 | /** |
||
| 271 | * Append more values to the collection |
||
| 272 | * @param iterable $source the values to add |
||
| 273 | * @return Collection |
||
| 274 | */ |
||
| 275 | 1 | public function merge($source) : Collection |
|
| 279 | /** |
||
| 280 | * Perform a shallow flatten of the collection |
||
| 281 | * @return Collection |
||
| 282 | */ |
||
| 283 | 1 | public function flatten() : Collection |
|
| 292 | /** |
||
| 293 | * Group by a key (if a callable is used - return the value to group by) |
||
| 294 | * @param string|callable $iterator the key to group by |
||
| 295 | * @return Collection |
||
| 296 | */ |
||
| 297 | 1 | public function groupBy($iterator) : Collection |
|
| 306 | /** |
||
| 307 | * Get the first X items from the collection |
||
| 308 | * @param int $count the number of items to include (defaults to 1) |
||
| 309 | * @return Collection |
||
| 310 | */ |
||
| 311 | 1 | public function first(int $count = 1) : Collection |
|
| 323 | /** |
||
| 324 | * Get the first X items from the collection |
||
| 325 | * @param int $count the number of items to include (defaults to 1) |
||
| 326 | * @return Collection |
||
| 327 | */ |
||
| 328 | 1 | public function head(int $count = 1) : Collection |
|
| 332 | /** |
||
| 333 | * Get the last X items from the collection |
||
| 334 | * @param int $count the number of items to include (defaults to 1) |
||
| 335 | * @return Collection |
||
| 336 | */ |
||
| 337 | 2 | public function last(int $count = 1) : Collection |
|
| 342 | /** |
||
| 343 | * Get the first X items from the collection |
||
| 344 | * @param int $count the number of items to include (defaults to 1) |
||
| 345 | * @return Collection |
||
| 346 | */ |
||
| 347 | 1 | public function tail(int $count = 1) : Collection |
|
| 351 | /** |
||
| 352 | * Get all but the last X items from the collection |
||
| 353 | * @param int $count the number of items to exclude (defaults to 1) |
||
| 354 | * @return Collection |
||
| 355 | */ |
||
| 356 | 1 | public function initial(int $count = 1) : Collection |
|
| 361 | /** |
||
| 362 | * Get all but the first X items from the collection |
||
| 363 | * @param int $count the number of items to exclude (defaults to 1) |
||
| 364 | * @return Collection |
||
| 365 | */ |
||
| 366 | 1 | public function rest(int $count = 1) : Collection |
|
| 371 | /** |
||
| 372 | * Execute a callable for each item in the collection (does not modify the collection) |
||
| 373 | * @param callable $iterator the callable to execute |
||
| 374 | * @return $this |
||
| 375 | */ |
||
| 376 | 1 | public function each(callable $iterator) : Collection |
|
| 383 | /** |
||
| 384 | * Execute a callable for each item in the collection (does not modify the collection) |
||
| 385 | * @param callable $iterator the callable to execute |
||
| 386 | * @return $this |
||
| 387 | */ |
||
| 388 | 1 | public function invoke(callable $iterator) : Collection |
|
| 392 | /** |
||
| 393 | * Get all the collection keys |
||
| 394 | * @return $this |
||
| 395 | */ |
||
| 396 | public function keys() : Collection |
||
| 400 | /** |
||
| 401 | * Pluck a value from each object (uses map internally) |
||
| 402 | * @param string|int $key the key to extract |
||
| 403 | * @return $this |
||
| 404 | */ |
||
| 405 | 2 | public function pluck($key) : Collection |
|
| 413 | /** |
||
| 414 | * Intersect the collection with another iterable (uses filter internally) |
||
| 415 | * @param interable $values the data to intersect with |
||
| 416 | * @return $this |
||
| 417 | */ |
||
| 418 | 1 | public function intersection($values) : Collection |
|
| 431 | /** |
||
| 432 | * Reject values on a given predicate (opposite of filter) |
||
| 433 | * @param callable $iterator the predicate |
||
| 434 | * @return $this |
||
| 435 | */ |
||
| 436 | 1 | public function reject(callable $iterator) : Collection |
|
| 442 | /** |
||
| 443 | * Shuffle the values in the collection |
||
| 444 | * @return Collection |
||
| 445 | */ |
||
| 446 | 1 | public function shuffle() : Collection |
|
| 457 | /** |
||
| 458 | * Sort the collection using a standard sorting function |
||
| 459 | * @param callable $iterator the sort function (must return -1, 0 or 1) |
||
| 460 | * @return Collection |
||
| 461 | */ |
||
| 462 | 1 | public function sortBy(callable $iterator) : Collection |
|
| 468 | /** |
||
| 469 | * Inspect the whole collection (as an array) mid-chain |
||
| 470 | * @param callable $iterator the callable to execute |
||
| 471 | * @return $this |
||
| 472 | */ |
||
| 473 | 1 | public function tap(callable $iterator) : Collection |
|
| 478 | /** |
||
| 479 | * Modify the whole collection (as an array) mid-chain |
||
| 480 | * @param callable $iterator the callable to execute |
||
| 481 | * @return Collection |
||
| 482 | */ |
||
| 483 | 1 | public function thru(callable $iterator) : Collection |
|
| 489 | /** |
||
| 490 | * Leave only unique items in the collection |
||
| 491 | * @return Collection |
||
| 492 | */ |
||
| 493 | 3 | public function unique() : Collection |
|
| 504 | /** |
||
| 505 | * Get only the values of the collection |
||
| 506 | * @return Collection |
||
| 507 | */ |
||
| 508 | 3 | public function values() : Collection |
|
| 512 | /** |
||
| 513 | * Filter items from the collection using key => value pairs |
||
| 514 | * @param array $properties the key => value to check for in each item |
||
| 515 | * @param boolean $strict should the comparison be strict |
||
| 516 | * @return $this |
||
| 517 | */ |
||
| 518 | public function where(array $properties, $strict = true) : Collection |
||
| 530 | /** |
||
| 531 | * Exclude all listed values from the collection (uses filter internally). |
||
| 532 | * @param iterable $values the values to exclude |
||
| 533 | * @return $this |
||
| 534 | */ |
||
| 535 | 2 | public function without($values) : Collection |
|
| 539 | /** |
||
| 540 | * Combine all the values from the collection with a key |
||
| 541 | * @param iterable $keys the keys to use |
||
| 542 | * @return Collection |
||
| 543 | */ |
||
| 544 | 2 | public function zip($keys) : Collection |
|
| 551 | /** |
||
| 552 | * Reverse the collection order |
||
| 553 | * @return Collection |
||
| 554 | */ |
||
| 555 | 1 | public function reverse() : Collection |
|
| 559 | |||
| 560 | // accessors |
||
| 561 | /** |
||
| 562 | * Do all of the items in the collection match a given criteria |
||
| 563 | * @param callable $iterator the criteria - should return true / false |
||
| 564 | * @return bool |
||
| 565 | */ |
||
| 566 | 1 | public function all(callable $iterator) : bool |
|
| 575 | /** |
||
| 576 | * Do any of the items in the collection match a given criteria |
||
| 577 | * @param callable $iterator the criteria - should return true / false |
||
| 578 | * @return bool |
||
| 579 | */ |
||
| 580 | 1 | public function any(callable $iterator) : bool |
|
| 589 | /** |
||
| 590 | * Does the collection contain a given value |
||
| 591 | * @param mixed $needle the value to check for |
||
| 592 | * @return bool |
||
| 593 | */ |
||
| 594 | 1 | public function contains($needle) : bool |
|
| 603 | /** |
||
| 604 | * Get the first element matching a given criteria (or null) |
||
| 605 | * @param callable $iterator the filter criteria |
||
| 606 | * @return mixed |
||
| 607 | */ |
||
| 608 | 1 | public function find(callable $iterator) |
|
| 617 | /** |
||
| 618 | * Get all the elements matching a given criteria (with the option to limit the number of results) |
||
| 619 | * @param callable $iterator the search criteria |
||
| 620 | * @param int|null $limit optional limit to the number of results (default to null - no limit) |
||
| 621 | * @return Collection |
||
| 622 | */ |
||
| 623 | 1 | public function findAll(callable $iterator, int $limit = null) : Collection |
|
| 636 | /** |
||
| 637 | * Get the key corresponding to a value (or false) |
||
| 638 | * @param mixed $needle the value to search for |
||
| 639 | * @return mixed |
||
| 640 | */ |
||
| 641 | 1 | public function indexOf($needle) |
|
| 645 | /** |
||
| 646 | * Get the last key corresponding to a value (or false) |
||
| 647 | * @param mixed $needle the value to search for |
||
| 648 | * @return mixed |
||
| 649 | */ |
||
| 650 | 1 | public function lastIndexOf($needle) |
|
| 660 | /** |
||
| 661 | * Get the number of elements in the collection |
||
| 662 | * @return int |
||
| 663 | */ |
||
| 664 | 1 | public function size() : int |
|
| 668 | /** |
||
| 669 | * Get the minimal item in the collection |
||
| 670 | * @return mixed |
||
| 671 | */ |
||
| 672 | 1 | public function min() |
|
| 684 | /** |
||
| 685 | * Get the maximum item in the collection |
||
| 686 | * @return mixed |
||
| 687 | */ |
||
| 688 | 1 | public function max() |
|
| 700 | /** |
||
| 701 | * Does the collection contain a given key |
||
| 702 | * @param string|int $key the key to check |
||
| 703 | * @return bool |
||
| 704 | */ |
||
| 705 | 1 | public function has($key) : bool |
|
| 709 | /** |
||
| 710 | * Reduce the collection to a single value |
||
| 711 | * @param callable $iterator the reducer |
||
| 712 | * @param mixed $initial the initial value |
||
| 713 | * @return mixed the final value |
||
| 714 | */ |
||
| 715 | 1 | public function reduce(callable $iterator, $initial = null) |
|
| 719 | /** |
||
| 720 | * Reduce the collection to a single value, starting from the last element |
||
| 721 | * @param callable $iterator the reducer |
||
| 722 | * @param mixed $initial the initial value |
||
| 723 | * @return mixed the final value |
||
| 724 | */ |
||
| 725 | 1 | public function reduceRight(callable $iterator, $initial = null) |
|
| 729 | } |
||
| 730 |