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 |
||
| 46 | class Collection implements |
||
| 47 | CollectionInterface, |
||
| 48 | Arrayable, |
||
| 49 | Invokable, |
||
| 50 | Countable, |
||
| 51 | Iterator |
||
| 52 | { |
||
| 53 | use IsArrayable; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array The collection of data this object represents |
||
| 57 | */ |
||
| 58 | private $data = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var bool True unless we have advanced past the end of the data array |
||
| 62 | */ |
||
| 63 | protected $isValid = true; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * AbstractCollection constructor. |
||
| 67 | * |
||
| 68 | * @param mixed $data The data to wrap |
||
| 69 | */ |
||
| 70 | public function __construct($data = null) |
||
| 84 | |||
| 85 | public function __invoke() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Set underlying data array. |
||
| 108 | * |
||
| 109 | * Sets the collection data. This method should NEVER be called anywhere other than in __construct(). |
||
| 110 | * |
||
| 111 | * @param array|Traversable $data The data to wrap |
||
| 112 | */ |
||
| 113 | private function setData($data) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get copy of underlying data array. |
||
| 125 | * |
||
| 126 | * Returns a copy of this collection's underlying data array. It returns a copy because collections are supposed to |
||
| 127 | * be immutable. Nothing outside of the constructor should ever have direct access to the actual underlying array. |
||
| 128 | * |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | protected function getData() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @inheritDoc |
||
| 138 | */ |
||
| 139 | public function count(callable $callback = null) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Return the current element. |
||
| 149 | * |
||
| 150 | * Returns the current element in the collection. The internal array pointer |
||
| 151 | * of the data array wrapped by the collection should not be advanced by this |
||
| 152 | * method. No side effects. Return current element only. |
||
| 153 | * |
||
| 154 | * @return mixed |
||
| 155 | */ |
||
| 156 | public function current() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Return the current key. |
||
| 163 | * |
||
| 164 | * Returns the current key in the collection. No side effects. |
||
| 165 | * |
||
| 166 | * @return mixed |
||
| 167 | */ |
||
| 168 | public function key() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Advance the internal pointer forward. |
||
| 175 | * |
||
| 176 | * Although this method will return the current value after advancing the |
||
| 177 | * pointer, you should not expect it to. The interface does not require it |
||
| 178 | * to return any value at all. |
||
| 179 | * |
||
| 180 | * @return mixed |
||
| 181 | */ |
||
| 182 | public function next() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Rewind the internal pointer. |
||
| 194 | * |
||
| 195 | * Return the internal pointer to the first element in the collection. Again, |
||
| 196 | * this method is not required to return anything by its interface, so you |
||
| 197 | * should not count on a return value. |
||
| 198 | * |
||
| 199 | * @return mixed |
||
| 200 | */ |
||
| 201 | public function rewind() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Is internal pointer in a valid position? |
||
| 210 | * |
||
| 211 | * If the internal pointer is advanced beyond the end of the collection, this method will return false. |
||
| 212 | * |
||
| 213 | * @return bool True if internal pointer isn't past the end |
||
| 214 | */ |
||
| 215 | public function valid() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @inheritDoc |
||
| 222 | */ |
||
| 223 | public function sort($alg = null) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @inheritDoc |
||
| 236 | */ |
||
| 237 | public function sortkeys($alg = null) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Does this collection have a value at given index? |
||
| 250 | * |
||
| 251 | * @param mixed $index The index to check |
||
| 252 | * |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | public function has($index) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set value at given index. |
||
| 262 | * |
||
| 263 | * This method simulates setting a value in this collection, but because collections are immutable, it actually |
||
| 264 | * returns a copy of this collection with the value in the new collection set to specified value. |
||
| 265 | * |
||
| 266 | * @param mixed $index The index to set a value at |
||
| 267 | * @param mixed $val The value to set $index to |
||
| 268 | * |
||
| 269 | * @return CollectionInterface |
||
| 270 | */ |
||
| 271 | public function set($index, $val) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Unset (delete) value at the given index. |
||
| 280 | * |
||
| 281 | * Get copy of collection with given index removed. |
||
| 282 | * |
||
| 283 | * @param mixed $index The index to unset |
||
| 284 | * |
||
| 285 | * @return CollectionInterface |
||
| 286 | */ |
||
| 287 | public function delete($index) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get index of a value. |
||
| 294 | * |
||
| 295 | * Given a value, this method will return the index of the first occurrence of that value. |
||
| 296 | * |
||
| 297 | * @param mixed $value Value to get the index of |
||
| 298 | * |
||
| 299 | * @return int|null|string |
||
| 300 | */ |
||
| 301 | public function indexOf($value) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get this collection's keys as a collection. |
||
| 313 | * |
||
| 314 | * @return CollectionInterface Containing this collection's keys |
||
| 315 | */ |
||
| 316 | public function keys() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get this collection's values as a collection. |
||
| 323 | * |
||
| 324 | * This method returns this collection's values but completely re-indexed (numerically). |
||
| 325 | * |
||
| 326 | * @return CollectionInterface Containing this collection's values |
||
| 327 | */ |
||
| 328 | public function values() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Determine if this collection contains a value. |
||
| 335 | * |
||
| 336 | * Allows you to pass in a value or a callback function and optionally an index, |
||
| 337 | * and tells you whether or not this collection contains that value. |
||
| 338 | * If the $index param is specified, only that index will be looked under. |
||
| 339 | * |
||
| 340 | * @param mixed|callable $value The value to check for |
||
| 341 | * @param mixed $index The (optional) index to look under |
||
| 342 | * |
||
| 343 | * @return bool True if this collection contains $value |
||
| 344 | * |
||
| 345 | * @todo Maybe add $identical param for identical comparison (===) |
||
| 346 | * @todo Allow negative offset for second param |
||
| 347 | */ |
||
| 348 | public function contains($value, $index = null) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Pad collection to a certain size. |
||
| 373 | * |
||
| 374 | * Returns a new collection, padded to the given size, with the given value. |
||
| 375 | * |
||
| 376 | * @param int $size The number of items that should be in the collection |
||
| 377 | * @param mixed $with The value to pad the collection with |
||
| 378 | * |
||
| 379 | * @return CollectionInterface A new collection padded to specified length |
||
| 380 | */ |
||
| 381 | public function pad($size, $with = null) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Apply a callback to each item in collection. |
||
| 388 | * |
||
| 389 | * Applies a callback to each item in collection and returns a new collection |
||
| 390 | * containing each iteration's return value. |
||
| 391 | * |
||
| 392 | * @param callable $callback The callback to apply |
||
| 393 | * |
||
| 394 | * @return CollectionInterface A new collection with callback return values |
||
| 395 | */ |
||
| 396 | public function map(callable $callback) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Iterate over each item in the collection, calling $callback on it. Return false to stop iterating. |
||
| 408 | * |
||
| 409 | * @param callable $callback A callback to use |
||
| 410 | * |
||
| 411 | * @return $this |
||
| 412 | */ |
||
| 413 | public function each(callable $callback) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Filter the collection. |
||
| 426 | * |
||
| 427 | * Using a callback function, this method will filter out unwanted values, returning |
||
| 428 | * a new collection containing only the values that weren't filtered. |
||
| 429 | * |
||
| 430 | * @param callable $callback The callback function used to filter |
||
| 431 | * |
||
| 432 | * @return CollectionInterface A new collection with only values that weren't filtered |
||
| 433 | */ |
||
| 434 | public function filter(callable $callback) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Filter the collection. |
||
| 448 | * |
||
| 449 | * Using a callback function, this method will filter out unwanted values, returning |
||
| 450 | * a new collection containing only the values that weren't filtered. |
||
| 451 | * |
||
| 452 | * @param callable $callback The callback function used to filter |
||
| 453 | * |
||
| 454 | * @return CollectionInterface A new collection with only values that weren't filtered |
||
| 455 | */ |
||
| 456 | public function exclude(callable $callback) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Return the first item that meets given criteria. |
||
| 470 | * |
||
| 471 | * Using a callback function, this method will return the first item in the collection |
||
| 472 | * that causes the callback function to return true. |
||
| 473 | * |
||
| 474 | * @param callable|null $callback The callback function |
||
| 475 | * @param mixed|null $default The default return value |
||
| 476 | * |
||
| 477 | * @return mixed |
||
| 478 | */ |
||
| 479 | public function first(callable $callback = null, $default = null) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Return the last item that meets given criteria. |
||
| 496 | * |
||
| 497 | * Using a callback function, this method will return the last item in the collection |
||
| 498 | * that causes the callback function to return true. |
||
| 499 | * |
||
| 500 | * @param callable|null $callback The callback function |
||
| 501 | * @param mixed|null $default The default return value |
||
| 502 | * |
||
| 503 | * @return mixed |
||
| 504 | */ |
||
| 505 | public function last(callable $callback = null, $default = null) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Returns collection in reverse order. |
||
| 516 | * |
||
| 517 | * @return CollectionInterface This collection in reverse order. |
||
| 518 | */ |
||
| 519 | public function reverse() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Get unique items. |
||
| 526 | * |
||
| 527 | * Returns a collection of all the unique items in this collection. |
||
| 528 | * |
||
| 529 | * @return CollectionInterface This collection with duplicate items removed |
||
| 530 | */ |
||
| 531 | public function unique() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Collection factory method. |
||
| 538 | * |
||
| 539 | * This method will analyze input data and determine the most appropriate Collection |
||
| 540 | * class to use. It will then instantiate said Collection class with the given |
||
| 541 | * data and return it. |
||
| 542 | * |
||
| 543 | * @param mixed $data The data to wrap |
||
| 544 | * |
||
| 545 | * @return CollectionInterface A collection containing $data |
||
| 546 | */ |
||
| 547 | public static function factory($data = null) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Determine if structure contains all numeric values. |
||
| 554 | * |
||
| 555 | * @return bool |
||
| 556 | */ |
||
| 557 | public function isNumeric() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @inheritdoc |
||
| 573 | */ |
||
| 574 | public function hasOffset($offset) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @inheritdoc |
||
| 586 | */ |
||
| 587 | public function getOffsetKey($offset) |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @inheritdoc |
||
| 599 | */ |
||
| 600 | public function getOffset($offset) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @param int $offset The numerical offset |
||
| 607 | * |
||
| 608 | * @throws OutOfBoundsException if no pair at position |
||
| 609 | * |
||
| 610 | * @return array |
||
| 611 | */ |
||
| 612 | public function getOffsetPair($offset) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Get each key/value as an array pair. |
||
| 621 | * |
||
| 622 | * Returns a collection of arrays where each item in the collection is [key,value] |
||
| 623 | * |
||
| 624 | * @return CollectionInterface |
||
| 625 | */ |
||
| 626 | public function pairs() |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Get duplicate values. |
||
| 639 | * |
||
| 640 | * Returns a collection of arrays where the key is the duplicate value |
||
| 641 | * and the value is an array of keys from the original collection. |
||
| 642 | * |
||
| 643 | * @return CollectionInterface A new collection with duplicate values. |
||
| 644 | */ |
||
| 645 | public function duplicates() |
||
| 656 | |||
| 657 | // END Iterator methods |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Counts how many times each value occurs in a collection. |
||
| 661 | * |
||
| 662 | * Returns a new collection with values as keys and how many times that |
||
| 663 | * value appears in the collection. Works best with scalar values but will |
||
| 664 | * attempt to work on collections of objects as well. |
||
| 665 | * |
||
| 666 | * @return CollectionInterface |
||
| 667 | * |
||
| 668 | * @todo Right now, collections of arrays or objects are supported via the |
||
| 669 | * __toString() or spl_object_hash() |
||
| 670 | * @todo NumericCollection::counts() does the same thing... |
||
| 671 | */ |
||
| 672 | public function frequency() |
||
| 695 | |||
| 696 | /** |
||
| 697 | * @inheritDoc |
||
| 698 | */ |
||
| 699 | public function add($index, $value) |
||
| 706 | |||
| 707 | /** |
||
| 708 | * @inheritdoc |
||
| 709 | * @todo Maybe read would be a better name for this? |
||
| 710 | */ |
||
| 711 | public function get($index, $default = null) |
||
| 719 | |||
| 720 | /** |
||
| 721 | * @inheritdoc |
||
| 722 | * @todo Maybe read would be a better name for this? |
||
| 723 | */ |
||
| 724 | public function retrieve($index) |
||
| 731 | |||
| 732 | /** |
||
| 733 | * @inheritDoc |
||
| 734 | */ |
||
| 735 | public function prepend($item) |
||
| 742 | |||
| 743 | /** |
||
| 744 | * @inheritDoc |
||
| 745 | */ |
||
| 746 | public function append($item) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * @inheritDoc |
||
| 756 | */ |
||
| 757 | public function chunk($size) |
||
| 774 | |||
| 775 | public function combine($values) |
||
| 791 | |||
| 792 | /** |
||
| 793 | * @inheritDoc |
||
| 794 | */ |
||
| 795 | public function diff($data) |
||
| 804 | |||
| 805 | /** |
||
| 806 | * @inheritDoc |
||
| 807 | */ |
||
| 808 | public function diffKeys($data) |
||
| 817 | |||
| 818 | /** |
||
| 819 | * @inheritDoc |
||
| 820 | */ |
||
| 821 | public function nth($nth, $offset = null) |
||
| 827 | |||
| 828 | /** |
||
| 829 | * @inheritDoc |
||
| 830 | */ |
||
| 831 | public function except($indexes) |
||
| 835 | |||
| 836 | /** |
||
| 837 | * @inheritDoc |
||
| 838 | */ |
||
| 839 | public function flip() |
||
| 843 | |||
| 844 | /** |
||
| 845 | * @inheritDoc |
||
| 846 | */ |
||
| 847 | public function intersect($data) |
||
| 856 | |||
| 857 | /** |
||
| 858 | * @inheritDoc |
||
| 859 | */ |
||
| 860 | public function intersectKeys($data) |
||
| 869 | |||
| 870 | /** |
||
| 871 | * @inheritDoc |
||
| 872 | */ |
||
| 873 | public function isEmpty(callable $callback = null) |
||
| 880 | |||
| 881 | /** |
||
| 882 | * @inheritDoc |
||
| 883 | */ |
||
| 884 | public function only($indices) |
||
| 888 | |||
| 889 | /** |
||
| 890 | * @inheritDoc |
||
| 891 | */ |
||
| 892 | public function pipe(callable $callback) |
||
| 896 | |||
| 897 | /** |
||
| 898 | * @inheritDoc |
||
| 899 | */ |
||
| 900 | public function random($num) |
||
| 904 | |||
| 905 | /** |
||
| 906 | * @inheritDoc |
||
| 907 | */ |
||
| 908 | public function indicesOf($value) |
||
| 916 | |||
| 917 | /** |
||
| 918 | * @inheritDoc |
||
| 919 | */ |
||
| 920 | public function shuffle() |
||
| 924 | |||
| 925 | /** |
||
| 926 | * @inheritDoc |
||
| 927 | */ |
||
| 928 | public function slice($offset, $length = null) |
||
| 932 | |||
| 933 | /** |
||
| 934 | * @inheritDoc |
||
| 935 | */ |
||
| 936 | public function split($num) |
||
| 970 | |||
| 971 | /** |
||
| 972 | * @inheritDoc |
||
| 973 | */ |
||
| 974 | public function union($data) |
||
| 983 | |||
| 984 | /** |
||
| 985 | * @inheritDoc |
||
| 986 | */ |
||
| 987 | public function zip(...$data) |
||
| 1014 | |||
| 1015 | /** |
||
| 1016 | * @inheritDoc |
||
| 1017 | */ |
||
| 1018 | public function foldRight(callable $callback, $initial = null) |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * @inheritDoc |
||
| 1030 | */ |
||
| 1031 | public function foldLeft(callable $callback, $initial = null) |
||
| 1035 | |||
| 1036 | /** |
||
| 1037 | * @inheritDoc |
||
| 1038 | */ |
||
| 1039 | public function all(callable $callback = null) |
||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * @inheritDoc |
||
| 1051 | */ |
||
| 1052 | public function none(callable $callback = null) |
||
| 1061 | |||
| 1062 | // BEGIN Numeric Collection Methods |
||
| 1063 | // These methods only really work on numeric data. |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Increment an item. |
||
| 1067 | * |
||
| 1068 | * Increment the item specified by $key by one value. Intended for integers |
||
| 1069 | * but also works (using this term loosely) for letters. Any other data type |
||
| 1070 | * it may modify is unintended behavior at best. |
||
| 1071 | * |
||
| 1072 | * This method modifies its internal data array rather than returning a new |
||
| 1073 | * collection. |
||
| 1074 | * |
||
| 1075 | * @param mixed $index The key of the item you want to increment. |
||
| 1076 | * @param int $interval The interval that $key should be incremented by |
||
| 1077 | * |
||
| 1078 | * @return CollectionInterface |
||
| 1079 | */ |
||
| 1080 | public function increment($index, $interval = 1) |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * Decrement an item. |
||
| 1089 | * |
||
| 1090 | * Frcrement the item specified by $key by one value. Intended for integers. |
||
| 1091 | * Does not work for letters and if it does anything to anything else, it's |
||
| 1092 | * unintended at best. |
||
| 1093 | * |
||
| 1094 | * This method modifies its internal data array rather than returning a new |
||
| 1095 | * collection. |
||
| 1096 | * |
||
| 1097 | * @param mixed $index The key of the item you want to decrement. |
||
| 1098 | * @param int $interval The interval that $key should be decremented by |
||
| 1099 | * |
||
| 1100 | * @return CollectionInterface |
||
| 1101 | */ |
||
| 1102 | public function decrement($index, $interval = 1) |
||
| 1108 | |||
| 1109 | /** |
||
| 1110 | * Get the sum. |
||
| 1111 | * |
||
| 1112 | * @return int|float The sum of all values in collection |
||
| 1113 | */ |
||
| 1114 | public function sum() |
||
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Get the average. |
||
| 1121 | * |
||
| 1122 | * @return float|int The average value from the collection |
||
| 1123 | */ |
||
| 1124 | public function average() |
||
| 1128 | |||
| 1129 | /** |
||
| 1130 | * Get the mode. |
||
| 1131 | * |
||
| 1132 | * @return float|int The mode |
||
| 1133 | */ |
||
| 1134 | public function mode() |
||
| 1142 | |||
| 1143 | /** |
||
| 1144 | * Get the median value. |
||
| 1145 | * |
||
| 1146 | * @return float|int The median value |
||
| 1147 | */ |
||
| 1148 | public function median() |
||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Get the maximum value. |
||
| 1168 | * |
||
| 1169 | * @return mixed The maximum |
||
| 1170 | */ |
||
| 1171 | public function max() |
||
| 1175 | |||
| 1176 | /** |
||
| 1177 | * Get the minimum value. |
||
| 1178 | * |
||
| 1179 | * @return mixed The minimum |
||
| 1180 | */ |
||
| 1181 | public function min() |
||
| 1185 | |||
| 1186 | /** |
||
| 1187 | * Get the number of times each item occurs in the collection. |
||
| 1188 | |||
| 1189 | * This method will return a NumericCollection where keys are the |
||
| 1190 | * values and values are the number of times that value occurs in |
||
| 1191 | * the original collection. |
||
| 1192 | |||
| 1193 | * @return CollectionInterface |
||
| 1194 | */ |
||
| 1195 | public function counts() |
||
| 1199 | } |
||
| 1200 |
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.