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 |
||
| 47 | class Collection implements |
||
| 48 | CollectionInterface, |
||
| 49 | Arrayable, |
||
| 50 | Invokable, |
||
| 51 | Countable, |
||
| 52 | Iterator |
||
| 53 | { |
||
| 54 | use IsArrayable, IsContainer; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array The collection of data this object represents |
||
| 58 | */ |
||
| 59 | private $data = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var bool True unless we have advanced past the end of the data array |
||
| 63 | */ |
||
| 64 | protected $isValid = true; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * AbstractCollection constructor. |
||
| 68 | * |
||
| 69 | * @param mixed $data The data to wrap |
||
| 70 | */ |
||
| 71 | public function __construct($data = null) |
||
| 85 | |||
| 86 | public function __invoke() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Set underlying data array. |
||
| 109 | * |
||
| 110 | * Sets the collection data. This method should NEVER be called anywhere other than in __construct(). |
||
| 111 | * |
||
| 112 | * @param array|Traversable $data The data to wrap |
||
| 113 | */ |
||
| 114 | private function setData($data) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get copy of underlying data array. |
||
| 126 | * |
||
| 127 | * Returns a copy of this collection's underlying data array. It returns a copy because collections are supposed to |
||
| 128 | * be immutable. Nothing outside of the constructor should ever have direct access to the actual underlying array. |
||
| 129 | * |
||
| 130 | * @return array |
||
| 131 | */ |
||
| 132 | protected function getData() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @inheritDoc |
||
| 139 | */ |
||
| 140 | public function count(callable $callback = null) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Return the current element. |
||
| 150 | * |
||
| 151 | * Returns the current element in the collection. The internal array pointer |
||
| 152 | * of the data array wrapped by the collection should not be advanced by this |
||
| 153 | * method. No side effects. Return current element only. |
||
| 154 | * |
||
| 155 | * @return mixed |
||
| 156 | */ |
||
| 157 | public function current() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Return the current key. |
||
| 164 | * |
||
| 165 | * Returns the current key in the collection. No side effects. |
||
| 166 | * |
||
| 167 | * @return mixed |
||
| 168 | */ |
||
| 169 | public function key() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Advance the internal pointer forward. |
||
| 176 | * |
||
| 177 | * Although this method will return the current value after advancing the |
||
| 178 | * pointer, you should not expect it to. The interface does not require it |
||
| 179 | * to return any value at all. |
||
| 180 | * |
||
| 181 | * @return mixed |
||
| 182 | */ |
||
| 183 | public function next() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Rewind the internal pointer. |
||
| 195 | * |
||
| 196 | * Return the internal pointer to the first element in the collection. Again, |
||
| 197 | * this method is not required to return anything by its interface, so you |
||
| 198 | * should not count on a return value. |
||
| 199 | * |
||
| 200 | * @return mixed |
||
| 201 | */ |
||
| 202 | public function rewind() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Is internal pointer in a valid position? |
||
| 211 | * |
||
| 212 | * If the internal pointer is advanced beyond the end of the collection, this method will return false. |
||
| 213 | * |
||
| 214 | * @return bool True if internal pointer isn't past the end |
||
| 215 | */ |
||
| 216 | public function valid() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @inheritDoc |
||
| 223 | */ |
||
| 224 | public function sort($alg = null) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @inheritDoc |
||
| 237 | */ |
||
| 238 | public function sortkeys($alg = null) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Does this collection have a value at given index? |
||
| 251 | * |
||
| 252 | * @param mixed $index The index to check |
||
| 253 | * |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | public function has($index) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Set value at given index. |
||
| 263 | * |
||
| 264 | * This method simulates setting a value in this collection, but because collections are immutable, it actually |
||
| 265 | * returns a copy of this collection with the value in the new collection set to specified value. |
||
| 266 | * |
||
| 267 | * @param mixed $index The index to set a value at |
||
| 268 | * @param mixed $val The value to set $index to |
||
| 269 | * |
||
| 270 | * @return CollectionInterface |
||
| 271 | */ |
||
| 272 | public function set($index, $val) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Unset (delete) value at the given index. |
||
| 281 | * |
||
| 282 | * Get copy of collection with given index removed. |
||
| 283 | * |
||
| 284 | * @param mixed $index The index to unset |
||
| 285 | * |
||
| 286 | * @return CollectionInterface |
||
| 287 | */ |
||
| 288 | public function delete($index) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get index of a value. |
||
| 295 | * |
||
| 296 | * Given a value, this method will return the index of the first occurrence of that value. |
||
| 297 | * |
||
| 298 | * @param mixed $value Value to get the index of |
||
| 299 | * |
||
| 300 | * @return int|null|string |
||
| 301 | */ |
||
| 302 | public function indexOf($value) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get this collection's keys as a collection. |
||
| 314 | * |
||
| 315 | * @return CollectionInterface Containing this collection's keys |
||
| 316 | */ |
||
| 317 | public function keys() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get this collection's values as a collection. |
||
| 324 | * |
||
| 325 | * This method returns this collection's values but completely re-indexed (numerically). |
||
| 326 | * |
||
| 327 | * @return CollectionInterface Containing this collection's values |
||
| 328 | */ |
||
| 329 | public function values() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Pad collection to a certain size. |
||
| 336 | * |
||
| 337 | * Returns a new collection, padded to the given size, with the given value. |
||
| 338 | * |
||
| 339 | * @param int $size The number of items that should be in the collection |
||
| 340 | * @param mixed $with The value to pad the collection with |
||
| 341 | * |
||
| 342 | * @return CollectionInterface A new collection padded to specified length |
||
| 343 | */ |
||
| 344 | public function pad($size, $with = null) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Apply a callback to each item in collection. |
||
| 351 | * |
||
| 352 | * Applies a callback to each item in collection and returns a new collection |
||
| 353 | * containing each iteration's return value. |
||
| 354 | * |
||
| 355 | * @param callable $callback The callback to apply |
||
| 356 | * |
||
| 357 | * @return CollectionInterface A new collection with callback return values |
||
| 358 | */ |
||
| 359 | public function map(callable $callback) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Iterate over each item in the collection, calling $callback on it. Return false to stop iterating. |
||
| 371 | * |
||
| 372 | * @param callable $callback A callback to use |
||
| 373 | * |
||
| 374 | * @return $this |
||
| 375 | */ |
||
| 376 | public function each(callable $callback) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Filter the collection. |
||
| 389 | * |
||
| 390 | * Using a callback function, this method will filter out unwanted values, returning |
||
| 391 | * a new collection containing only the values that weren't filtered. |
||
| 392 | * |
||
| 393 | * @param callable $callback The callback function used to filter |
||
| 394 | * |
||
| 395 | * @return CollectionInterface A new collection with only values that weren't filtered |
||
| 396 | */ |
||
| 397 | public function filter(callable $callback) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Filter the collection. |
||
| 411 | * |
||
| 412 | * Using a callback function, this method will filter out unwanted values, returning |
||
| 413 | * a new collection containing only the values that weren't filtered. |
||
| 414 | * |
||
| 415 | * @param callable $callback The callback function used to filter |
||
| 416 | * |
||
| 417 | * @return CollectionInterface A new collection with only values that weren't filtered |
||
| 418 | */ |
||
| 419 | public function exclude(callable $callback) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Return the first item that meets given criteria. |
||
| 433 | * |
||
| 434 | * Using a callback function, this method will return the first item in the collection |
||
| 435 | * that causes the callback function to return true. |
||
| 436 | * |
||
| 437 | * @param callable|null $callback The callback function |
||
| 438 | * @param mixed|null $default The default return value |
||
| 439 | * |
||
| 440 | * @return mixed |
||
| 441 | */ |
||
| 442 | public function first(callable $callback = null, $default = null) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Return the last item that meets given criteria. |
||
| 459 | * |
||
| 460 | * Using a callback function, this method will return the last item in the collection |
||
| 461 | * that causes the callback function to return true. |
||
| 462 | * |
||
| 463 | * @param callable|null $callback The callback function |
||
| 464 | * @param mixed|null $default The default return value |
||
| 465 | * |
||
| 466 | * @return mixed |
||
| 467 | */ |
||
| 468 | public function last(callable $callback = null, $default = null) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Returns collection in reverse order. |
||
| 479 | * |
||
| 480 | * @return CollectionInterface This collection in reverse order. |
||
| 481 | */ |
||
| 482 | public function reverse() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Get unique items. |
||
| 489 | * |
||
| 490 | * Returns a collection of all the unique items in this collection. |
||
| 491 | * |
||
| 492 | * @return CollectionInterface This collection with duplicate items removed |
||
| 493 | */ |
||
| 494 | public function unique() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Collection factory method. |
||
| 501 | * |
||
| 502 | * This method will analyze input data and determine the most appropriate Collection |
||
| 503 | * class to use. It will then instantiate said Collection class with the given |
||
| 504 | * data and return it. |
||
| 505 | * |
||
| 506 | * @param mixed $data The data to wrap |
||
| 507 | * |
||
| 508 | * @return CollectionInterface A collection containing $data |
||
| 509 | */ |
||
| 510 | public static function factory($data = null) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Determine if structure contains all numeric values. |
||
| 517 | * |
||
| 518 | * @return bool |
||
| 519 | */ |
||
| 520 | public function isNumeric() |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @inheritdoc |
||
| 536 | */ |
||
| 537 | public function hasOffset($offset) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @inheritdoc |
||
| 549 | */ |
||
| 550 | public function getOffsetKey($offset) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * @inheritdoc |
||
| 562 | */ |
||
| 563 | public function getOffset($offset) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @param int $offset The numerical offset |
||
| 570 | * |
||
| 571 | * @throws OutOfBoundsException if no pair at position |
||
| 572 | * |
||
| 573 | * @return array |
||
| 574 | */ |
||
| 575 | public function getOffsetPair($offset) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Get each key/value as an array pair. |
||
| 584 | * |
||
| 585 | * Returns a collection of arrays where each item in the collection is [key,value] |
||
| 586 | * |
||
| 587 | * @return CollectionInterface |
||
| 588 | */ |
||
| 589 | public function pairs() |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Get duplicate values. |
||
| 602 | * |
||
| 603 | * Returns a collection of arrays where the key is the duplicate value |
||
| 604 | * and the value is an array of keys from the original collection. |
||
| 605 | * |
||
| 606 | * @return CollectionInterface A new collection with duplicate values. |
||
| 607 | */ |
||
| 608 | public function duplicates() |
||
| 619 | |||
| 620 | // END Iterator methods |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Counts how many times each value occurs in a collection. |
||
| 624 | * |
||
| 625 | * Returns a new collection with values as keys and how many times that |
||
| 626 | * value appears in the collection. Works best with scalar values but will |
||
| 627 | * attempt to work on collections of objects as well. |
||
| 628 | * |
||
| 629 | * @return CollectionInterface |
||
| 630 | * |
||
| 631 | * @todo Right now, collections of arrays or objects are supported via the |
||
| 632 | * __toString() or spl_object_hash() |
||
| 633 | * @todo NumericCollection::counts() does the same thing... |
||
| 634 | */ |
||
| 635 | public function frequency() |
||
| 658 | |||
| 659 | /** |
||
| 660 | * @inheritDoc |
||
| 661 | */ |
||
| 662 | public function add($index, $value) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * @inheritdoc |
||
| 672 | * @todo Maybe read would be a better name for this? |
||
| 673 | */ |
||
| 674 | public function get($index, $default = null) |
||
| 682 | |||
| 683 | /** |
||
| 684 | * @inheritdoc |
||
| 685 | * @todo Maybe read would be a better name for this? |
||
| 686 | */ |
||
| 687 | public function retrieve($index) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @inheritDoc |
||
| 697 | */ |
||
| 698 | public function prepend($item) |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @inheritDoc |
||
| 708 | */ |
||
| 709 | public function append($item) |
||
| 716 | |||
| 717 | /** |
||
| 718 | * @inheritDoc |
||
| 719 | */ |
||
| 720 | public function chunk($size) |
||
| 737 | |||
| 738 | public function combine($values) |
||
| 754 | |||
| 755 | /** |
||
| 756 | * @inheritDoc |
||
| 757 | */ |
||
| 758 | public function diff($data) |
||
| 767 | |||
| 768 | /** |
||
| 769 | * @inheritDoc |
||
| 770 | */ |
||
| 771 | public function diffKeys($data) |
||
| 780 | |||
| 781 | /** |
||
| 782 | * @inheritDoc |
||
| 783 | */ |
||
| 784 | public function nth($nth, $offset = null) |
||
| 790 | |||
| 791 | /** |
||
| 792 | * @inheritDoc |
||
| 793 | */ |
||
| 794 | public function except($indexes) |
||
| 798 | |||
| 799 | /** |
||
| 800 | * @inheritDoc |
||
| 801 | */ |
||
| 802 | public function flip() |
||
| 806 | |||
| 807 | /** |
||
| 808 | * @inheritDoc |
||
| 809 | */ |
||
| 810 | public function intersect($data) |
||
| 819 | |||
| 820 | /** |
||
| 821 | * @inheritDoc |
||
| 822 | */ |
||
| 823 | public function intersectKeys($data) |
||
| 832 | |||
| 833 | /** |
||
| 834 | * @inheritDoc |
||
| 835 | */ |
||
| 836 | public function isEmpty(callable $callback = null) |
||
| 843 | |||
| 844 | /** |
||
| 845 | * @inheritDoc |
||
| 846 | */ |
||
| 847 | public function only($indices) |
||
| 851 | |||
| 852 | /** |
||
| 853 | * @inheritDoc |
||
| 854 | */ |
||
| 855 | public function pipe(callable $callback) |
||
| 859 | |||
| 860 | /** |
||
| 861 | * @inheritDoc |
||
| 862 | */ |
||
| 863 | public function random($num) |
||
| 867 | |||
| 868 | /** |
||
| 869 | * @inheritDoc |
||
| 870 | */ |
||
| 871 | public function indicesOf($value) |
||
| 879 | |||
| 880 | /** |
||
| 881 | * @inheritDoc |
||
| 882 | */ |
||
| 883 | public function shuffle() |
||
| 887 | |||
| 888 | /** |
||
| 889 | * @inheritDoc |
||
| 890 | */ |
||
| 891 | public function slice($offset, $length = null) |
||
| 895 | |||
| 896 | /** |
||
| 897 | * @inheritDoc |
||
| 898 | */ |
||
| 899 | public function split($num) |
||
| 933 | |||
| 934 | /** |
||
| 935 | * @inheritDoc |
||
| 936 | */ |
||
| 937 | public function union($data) |
||
| 946 | |||
| 947 | /** |
||
| 948 | * @inheritDoc |
||
| 949 | */ |
||
| 950 | public function zip(...$data) |
||
| 977 | |||
| 978 | /** |
||
| 979 | * @inheritDoc |
||
| 980 | */ |
||
| 981 | public function fold(callable $callback, $initial = null) |
||
| 990 | |||
| 991 | /** |
||
| 992 | * @inheritDoc |
||
| 993 | */ |
||
| 994 | public function foldl(callable $callback, $initial = null) |
||
| 998 | |||
| 999 | /** |
||
| 1000 | * @inheritDoc |
||
| 1001 | */ |
||
| 1002 | public function all(callable $callback = null) |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * @inheritDoc |
||
| 1014 | */ |
||
| 1015 | public function none(callable $callback = null) |
||
| 1024 | |||
| 1025 | // BEGIN Numeric Collection Methods |
||
| 1026 | // These methods only really work on numeric data. |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * Increment an item. |
||
| 1030 | * |
||
| 1031 | * Increment the item specified by $key by one value. Intended for integers |
||
| 1032 | * but also works (using this term loosely) for letters. Any other data type |
||
| 1033 | * it may modify is unintended behavior at best. |
||
| 1034 | * |
||
| 1035 | * This method modifies its internal data array rather than returning a new |
||
| 1036 | * collection. |
||
| 1037 | * |
||
| 1038 | * @param mixed $index The key of the item you want to increment. |
||
| 1039 | * @param int $interval The interval that $key should be incremented by |
||
| 1040 | * |
||
| 1041 | * @return CollectionInterface |
||
| 1042 | */ |
||
| 1043 | public function increment($index, $interval = 1) |
||
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Decrement an item. |
||
| 1052 | * |
||
| 1053 | * Frcrement the item specified by $key by one value. Intended for integers. |
||
| 1054 | * Does not work for letters and if it does anything to anything else, it's |
||
| 1055 | * unintended at best. |
||
| 1056 | * |
||
| 1057 | * This method modifies its internal data array rather than returning a new |
||
| 1058 | * collection. |
||
| 1059 | * |
||
| 1060 | * @param mixed $index The key of the item you want to decrement. |
||
| 1061 | * @param int $interval The interval that $key should be decremented by |
||
| 1062 | * |
||
| 1063 | * @return CollectionInterface |
||
| 1064 | */ |
||
| 1065 | public function decrement($index, $interval = 1) |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Get the sum. |
||
| 1074 | * |
||
| 1075 | * @return int|float The sum of all values in collection |
||
| 1076 | */ |
||
| 1077 | public function sum() |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Get the average. |
||
| 1084 | * |
||
| 1085 | * @return float|int The average value from the collection |
||
| 1086 | */ |
||
| 1087 | public function average() |
||
| 1091 | |||
| 1092 | /** |
||
| 1093 | * Get the mode. |
||
| 1094 | * |
||
| 1095 | * @return float|int The mode |
||
| 1096 | */ |
||
| 1097 | public function mode() |
||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Get the median value. |
||
| 1108 | * |
||
| 1109 | * @return float|int The median value |
||
| 1110 | */ |
||
| 1111 | public function median() |
||
| 1128 | |||
| 1129 | /** |
||
| 1130 | * Get the maximum value. |
||
| 1131 | * |
||
| 1132 | * @return mixed The maximum |
||
| 1133 | */ |
||
| 1134 | public function max() |
||
| 1138 | |||
| 1139 | /** |
||
| 1140 | * Get the minimum value. |
||
| 1141 | * |
||
| 1142 | * @return mixed The minimum |
||
| 1143 | */ |
||
| 1144 | public function min() |
||
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Get the number of times each item occurs in the collection. |
||
| 1151 | |||
| 1152 | * This method will return a NumericCollection where keys are the |
||
| 1153 | * values and values are the number of times that value occurs in |
||
| 1154 | * the original collection. |
||
| 1155 | |||
| 1156 | * @return CollectionInterface |
||
| 1157 | */ |
||
| 1158 | public function counts() |
||
| 1162 | } |
||
| 1163 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.