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 |
||
| 15 | class Collection implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * The items in the collection. |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $items = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Create a new collection instance. |
||
| 26 | * |
||
| 27 | * @param array $items |
||
| 28 | */ |
||
| 29 | public function __construct($items = []) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Create a new collection instance if the value isn't one already. |
||
| 36 | * |
||
| 37 | * @param mixed $items |
||
| 38 | * |
||
| 39 | * @return static |
||
| 40 | */ |
||
| 41 | public static function make($items = null) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Return all the items inside the collection. |
||
| 48 | * |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | public function all() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Determine if an item exists in the collection. |
||
| 58 | * |
||
| 59 | * @param mixed $key |
||
| 60 | * @param mixed $value |
||
| 61 | * |
||
| 62 | * @return bool |
||
| 63 | */ |
||
| 64 | public function contains($key, $value = null) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Diff the collection with the given items. |
||
| 81 | * |
||
| 82 | * @param mixed $items |
||
| 83 | * |
||
| 84 | * @return static |
||
| 85 | */ |
||
| 86 | public function diff($items) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Execute a callback over each item. |
||
| 93 | * |
||
| 94 | * @param callable $callback |
||
| 95 | * |
||
| 96 | * @return $this |
||
| 97 | */ |
||
| 98 | public function each(callable $callback) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Run a filter over each of the items. |
||
| 107 | * |
||
| 108 | * @param callable|null $callback |
||
| 109 | * |
||
| 110 | * @return static |
||
| 111 | */ |
||
| 112 | public function filter(callable $callback = null) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Filter items by the given key value pair. |
||
| 123 | * |
||
| 124 | * @param string $key |
||
| 125 | * @param mixed $value |
||
| 126 | * @param bool $strict |
||
| 127 | * |
||
| 128 | * @return static |
||
| 129 | */ |
||
| 130 | public function where($key, $value, $strict = true) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Filter items by the given key value pair using loose comparison. |
||
| 139 | * |
||
| 140 | * @param string $key |
||
| 141 | * @param mixed $value |
||
| 142 | * |
||
| 143 | * @return static |
||
| 144 | */ |
||
| 145 | public function whereLoose($key, $value) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Find a model in the collection by key. |
||
| 152 | * |
||
| 153 | * @param mixed $key |
||
| 154 | * @param mixed $default |
||
| 155 | * |
||
| 156 | * @return \Magister\Services\Database\Elegant\Model |
||
| 157 | */ |
||
| 158 | public function find($key, $default = null) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get the first item from the collection. |
||
| 167 | * |
||
| 168 | * @param callable $callback |
||
| 169 | * @param mixed $default |
||
| 170 | * |
||
| 171 | * @return mixed|null |
||
| 172 | */ |
||
| 173 | public function first(callable $callback = null, $default = null) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get a flattened array of the items in the collection. |
||
| 184 | * |
||
| 185 | * @return static |
||
| 186 | */ |
||
| 187 | public function flatten() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Flip the items in the collection. |
||
| 194 | * |
||
| 195 | * @return static |
||
| 196 | */ |
||
| 197 | public function flip() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Remove an item from the collection by key. |
||
| 204 | * |
||
| 205 | * @param mixed $key |
||
| 206 | * |
||
| 207 | * @return void |
||
| 208 | */ |
||
| 209 | public function forget($key) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get an item from the collection by key. |
||
| 216 | * |
||
| 217 | * @param mixed $key |
||
| 218 | * @param mixed $default |
||
| 219 | * |
||
| 220 | * @return mixed |
||
| 221 | */ |
||
| 222 | public function get($key, $default = null) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Group an associative array by a field or using a callback. |
||
| 233 | * |
||
| 234 | * @param callable|string $groupBy |
||
| 235 | * @param bool $preserveKeys |
||
| 236 | * |
||
| 237 | * @return static |
||
| 238 | */ |
||
| 239 | public function groupBy($groupBy, $preserveKeys = false) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Key an associative array by a field or using a callback. |
||
| 260 | * |
||
| 261 | * @param callable|string $keyBy |
||
| 262 | * |
||
| 263 | * @return static |
||
| 264 | */ |
||
| 265 | public function keyBy($keyBy) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Determine if an item exists in the collection by key. |
||
| 280 | * |
||
| 281 | * @param mixed $key |
||
| 282 | * |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | public function has($key) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Concatenate values of a given key as a string. |
||
| 292 | * |
||
| 293 | * @param string $value |
||
| 294 | * @param string $glue |
||
| 295 | * |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | public function implode($value, $glue = null) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Intersect the collection with the given items. |
||
| 311 | * |
||
| 312 | * @param mixed $items |
||
| 313 | * |
||
| 314 | * @return static |
||
| 315 | */ |
||
| 316 | public function intersect($items) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Determine if the collection is empty. |
||
| 323 | * |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | public function isEmpty() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Determine if the given value is callable, but not a string. |
||
| 333 | * |
||
| 334 | * @param mixed $value |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | protected function useAsCallable($value) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get the keys of the collection items. |
||
| 345 | * |
||
| 346 | * @return static |
||
| 347 | */ |
||
| 348 | public function keys() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get the last item from the collection. |
||
| 355 | * |
||
| 356 | * @return mixed|null |
||
| 357 | */ |
||
| 358 | public function last() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get an array with the values of a given key. |
||
| 365 | * |
||
| 366 | * @param string $value |
||
| 367 | * @param string $key |
||
| 368 | * |
||
| 369 | * @return static |
||
| 370 | */ |
||
| 371 | public function pluck($value, $key = null) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Alias for the "pluck" method. |
||
| 378 | * |
||
| 379 | * @param string $value |
||
| 380 | * @param string $key |
||
| 381 | * |
||
| 382 | * @return static |
||
| 383 | */ |
||
| 384 | public function lists($value, $key = null) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Run a map over each of the items. |
||
| 391 | * |
||
| 392 | * @param callable $callback |
||
| 393 | * |
||
| 394 | * @return static |
||
| 395 | */ |
||
| 396 | public function map(callable $callback) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Merge the collection with the given items. |
||
| 407 | * |
||
| 408 | * @param mixed $items |
||
| 409 | * |
||
| 410 | * @return static |
||
| 411 | */ |
||
| 412 | public function merge($items) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Get and remove the last item from the collection. |
||
| 419 | * |
||
| 420 | * @return mixed|null |
||
| 421 | */ |
||
| 422 | public function pop() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Push an item onto the beginning of the collection. |
||
| 429 | * |
||
| 430 | * @param mixed $value |
||
| 431 | * |
||
| 432 | * @return void |
||
| 433 | */ |
||
| 434 | public function prepend($value) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Put an item in the collection by key. |
||
| 441 | * |
||
| 442 | * @param mixed $key |
||
| 443 | * @param mixed $value |
||
| 444 | * |
||
| 445 | * @return void |
||
| 446 | */ |
||
| 447 | public function put($key, $value) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Get one or more items randomly from the collection. |
||
| 454 | * |
||
| 455 | * @param int $amount |
||
| 456 | * |
||
| 457 | * @return mixed |
||
| 458 | */ |
||
| 459 | public function random($amount = 1) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Reduce the collection to a single value. |
||
| 472 | * |
||
| 473 | * @param callable $callback |
||
| 474 | * @param mixed $initial |
||
| 475 | * |
||
| 476 | * @return mixed |
||
| 477 | */ |
||
| 478 | public function reduce(callable $callback, $initial = null) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Reverse items order. |
||
| 485 | * |
||
| 486 | * @return static |
||
| 487 | */ |
||
| 488 | public function reverse() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Get and remove the first item from the collection. |
||
| 495 | * |
||
| 496 | * @return mixed|null |
||
| 497 | */ |
||
| 498 | public function shift() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Shuffle the items in the collection. |
||
| 505 | * |
||
| 506 | * @return $this |
||
| 507 | */ |
||
| 508 | public function shuffle() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Slice the underlying collection array. |
||
| 519 | * |
||
| 520 | * @param int $offset |
||
| 521 | * @param int $length |
||
| 522 | * @param bool $preserveKeys |
||
| 523 | * |
||
| 524 | * @return static |
||
| 525 | */ |
||
| 526 | public function slice($offset, $length = null, $preserveKeys = false) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Chunk the underlying collection array. |
||
| 533 | * |
||
| 534 | * @param int $size |
||
| 535 | * @param bool $preserveKeys |
||
| 536 | * |
||
| 537 | * @return static |
||
| 538 | */ |
||
| 539 | public function chunk($size, $preserveKeys = false) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Sort through each item with a callback. |
||
| 552 | * |
||
| 553 | * @param callable $callback |
||
| 554 | * |
||
| 555 | * @return $this |
||
| 556 | */ |
||
| 557 | public function sort(callable $callback) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Sort the collection using the given callback. |
||
| 566 | * |
||
| 567 | * @param callable|string $callback |
||
| 568 | * @param int $options |
||
| 569 | * @param bool $descending |
||
| 570 | * |
||
| 571 | * @return static |
||
| 572 | */ |
||
| 573 | public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Sort the collection in descending order using the given callback. |
||
| 594 | * |
||
| 595 | * @param callable|string $callback |
||
| 596 | * @param int $options |
||
| 597 | * |
||
| 598 | * @return static |
||
| 599 | */ |
||
| 600 | public function sortByDesc($callback, $options = SORT_REGULAR) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Take the first or last {$limit} items. |
||
| 607 | * |
||
| 608 | * @param int $limit |
||
| 609 | * |
||
| 610 | * @return static |
||
| 611 | */ |
||
| 612 | public function take($limit = null) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Return only unique items from the collection array. |
||
| 623 | * |
||
| 624 | * @return static |
||
| 625 | */ |
||
| 626 | public function unique() |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Reset the keys on the underlying array. |
||
| 633 | * |
||
| 634 | * @return static |
||
| 635 | */ |
||
| 636 | public function values() |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Get a value retrieving callback. |
||
| 643 | * |
||
| 644 | * @param string $value |
||
| 645 | * |
||
| 646 | * @return callable |
||
| 647 | */ |
||
| 648 | protected function valueRetriever($value) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Results array of items from Collection or Arrayable. |
||
| 661 | * |
||
| 662 | * @param mixed $items |
||
| 663 | * |
||
| 664 | * @return array |
||
| 665 | */ |
||
| 666 | protected function getArrayableItems($items) |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Get the collection of items as a plain array. |
||
| 681 | * |
||
| 682 | * @return array |
||
| 683 | */ |
||
| 684 | public function toArray() |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Get the collection of items as JSON. |
||
| 693 | * |
||
| 694 | * @param int $options |
||
| 695 | * |
||
| 696 | * @return string |
||
| 697 | */ |
||
| 698 | public function toJson($options = 0) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Get an iterator for the items. |
||
| 705 | * |
||
| 706 | * @return \ArrayIterator |
||
| 707 | */ |
||
| 708 | public function getIterator() |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Count the number of items in the collection. |
||
| 715 | * |
||
| 716 | * @return int |
||
| 717 | */ |
||
| 718 | public function count() |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Set the item at a given offset. |
||
| 725 | * |
||
| 726 | * @param mixed $key |
||
| 727 | * @param mixed $value |
||
| 728 | * |
||
| 729 | * @return void |
||
| 730 | */ |
||
| 731 | public function offsetSet($key, $value) |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Get an item at a given offset. |
||
| 738 | * |
||
| 739 | * @param mixed $key |
||
| 740 | * |
||
| 741 | * @return mixed |
||
| 742 | */ |
||
| 743 | public function offsetGet($key) |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Determine if an item exists at a given offset. |
||
| 750 | * |
||
| 751 | * @param mixed $key |
||
| 752 | * |
||
| 753 | * @return bool |
||
| 754 | */ |
||
| 755 | public function offsetExists($key) |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Unset the item at a given offset. |
||
| 762 | * |
||
| 763 | * @param string $key |
||
| 764 | * |
||
| 765 | * @return void |
||
| 766 | */ |
||
| 767 | public function offsetUnset($key) |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Convert the collection to its string representation. |
||
| 774 | * |
||
| 775 | * @return string |
||
| 776 | */ |
||
| 777 | public function __toString() |
||
| 781 | } |
||
| 782 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.