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 |
||
| 50 | class Collection implements |
||
| 51 | CollectionInterface, |
||
| 52 | Arrayable, |
||
| 53 | Invokable, |
||
| 54 | Countable, |
||
| 55 | Iterator |
||
| 56 | { |
||
| 57 | use IsArrayable, |
||
| 58 | IsContainer, |
||
| 59 | IsSerializable; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array The collection of data this object represents |
||
| 63 | */ |
||
| 64 | private $data = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var bool True unless we have advanced past the end of the data array |
||
| 68 | */ |
||
| 69 | protected $isValid = true; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * AbstractCollection constructor. |
||
| 73 | * |
||
| 74 | * @param mixed $data The data to wrap |
||
| 75 | */ |
||
| 76 | public function __construct($data = null) |
||
| 90 | |||
| 91 | public function __invoke() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set underlying data array. |
||
| 114 | * |
||
| 115 | * Sets the collection data. This method should NEVER be called anywhere other than in __construct(). |
||
| 116 | * |
||
| 117 | * @param array|Traversable $data The data to wrap |
||
| 118 | */ |
||
| 119 | private function setData($data) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get copy of underlying data array. |
||
| 131 | * |
||
| 132 | * Returns a copy of this collection's underlying data array. It returns a copy because collections are supposed to |
||
| 133 | * be immutable. Nothing outside of the constructor should ever have direct access to the actual underlying array. |
||
| 134 | * |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | protected function getData() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @inheritDoc |
||
| 144 | */ |
||
| 145 | public function count(callable $callback = null) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Return the current element. |
||
| 155 | * |
||
| 156 | * Returns the current element in the collection. The internal array pointer |
||
| 157 | * of the data array wrapped by the collection should not be advanced by this |
||
| 158 | * method. No side effects. Return current element only. |
||
| 159 | * |
||
| 160 | * @return mixed |
||
| 161 | */ |
||
| 162 | public function current() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return the current key. |
||
| 169 | * |
||
| 170 | * Returns the current key in the collection. No side effects. |
||
| 171 | * |
||
| 172 | * @return mixed |
||
| 173 | */ |
||
| 174 | public function key() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Advance the internal pointer forward. |
||
| 181 | * |
||
| 182 | * Although this method will return the current value after advancing the |
||
| 183 | * pointer, you should not expect it to. The interface does not require it |
||
| 184 | * to return any value at all. |
||
| 185 | * |
||
| 186 | * @return mixed |
||
| 187 | */ |
||
| 188 | public function next() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Rewind the internal pointer. |
||
| 200 | * |
||
| 201 | * Return the internal pointer to the first element in the collection. Again, |
||
| 202 | * this method is not required to return anything by its interface, so you |
||
| 203 | * should not count on a return value. |
||
| 204 | * |
||
| 205 | * @return mixed |
||
| 206 | */ |
||
| 207 | public function rewind() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Is internal pointer in a valid position? |
||
| 216 | * |
||
| 217 | * If the internal pointer is advanced beyond the end of the collection, this method will return false. |
||
| 218 | * |
||
| 219 | * @return bool True if internal pointer isn't past the end |
||
| 220 | */ |
||
| 221 | public function valid() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @inheritDoc |
||
| 228 | */ |
||
| 229 | public function sort($alg = null) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @inheritDoc |
||
| 242 | */ |
||
| 243 | public function sortkeys($alg = null) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Does this collection have a value at given index? |
||
| 256 | * |
||
| 257 | * @param mixed $index The index to check |
||
| 258 | * |
||
| 259 | * @return bool |
||
| 260 | */ |
||
| 261 | public function has($index) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Set value at given index. |
||
| 268 | * |
||
| 269 | * This method simulates setting a value in this collection, but because collections are immutable, it actually |
||
| 270 | * returns a copy of this collection with the value in the new collection set to specified value. |
||
| 271 | * |
||
| 272 | * @param mixed $index The index to set a value at |
||
| 273 | * @param mixed $val The value to set $index to |
||
| 274 | * |
||
| 275 | * @return CollectionInterface |
||
| 276 | */ |
||
| 277 | public function set($index, $val) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Unset (delete) value at the given index. |
||
| 286 | * |
||
| 287 | * Get copy of collection with given index removed. |
||
| 288 | * |
||
| 289 | * @param mixed $index The index to unset |
||
| 290 | * |
||
| 291 | * @return CollectionInterface |
||
| 292 | */ |
||
| 293 | public function delete($index) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Get index of a value. |
||
| 300 | * |
||
| 301 | * Given a value, this method will return the index of the first occurrence of that value. |
||
| 302 | * |
||
| 303 | * @param mixed $value Value to get the index of |
||
| 304 | * |
||
| 305 | * @return int|null|string |
||
| 306 | */ |
||
| 307 | public function indexOf($value) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Get this collection's keys as a collection. |
||
| 319 | * |
||
| 320 | * @return CollectionInterface Containing this collection's keys |
||
| 321 | */ |
||
| 322 | public function keys() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get this collection's values as a collection. |
||
| 329 | * |
||
| 330 | * This method returns this collection's values but completely re-indexed (numerically). |
||
| 331 | * |
||
| 332 | * @return CollectionInterface Containing this collection's values |
||
| 333 | */ |
||
| 334 | public function values() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Pad collection to a certain size. |
||
| 341 | * |
||
| 342 | * Returns a new collection, padded to the given size, with the given value. |
||
| 343 | * |
||
| 344 | * @param int $size The number of items that should be in the collection |
||
| 345 | * @param mixed $with The value to pad the collection with |
||
| 346 | * |
||
| 347 | * @return CollectionInterface A new collection padded to specified length |
||
| 348 | */ |
||
| 349 | public function pad($size, $with = null) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Apply a callback to each item in collection. |
||
| 356 | * |
||
| 357 | * Applies a callback to each item in collection and returns a new collection |
||
| 358 | * containing each iteration's return value. |
||
| 359 | * |
||
| 360 | * @param callable $callback The callback to apply |
||
| 361 | * |
||
| 362 | * @return CollectionInterface A new collection with callback return values |
||
| 363 | */ |
||
| 364 | public function map(callable $callback) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Iterate over each item in the collection, calling $callback on it. Return false to stop iterating. |
||
| 376 | * |
||
| 377 | * @param callable $callback A callback to use |
||
| 378 | * |
||
| 379 | * @return $this |
||
| 380 | */ |
||
| 381 | public function each(callable $callback) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Filter the collection. |
||
| 394 | * |
||
| 395 | * Using a callback function, this method will filter out unwanted values, returning |
||
| 396 | * a new collection containing only the values that weren't filtered. |
||
| 397 | * |
||
| 398 | * @param callable $callback The callback function used to filter |
||
| 399 | * |
||
| 400 | * @return CollectionInterface A new collection with only values that weren't filtered |
||
| 401 | */ |
||
| 402 | public function filter(callable $callback) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Filter the collection. |
||
| 416 | * |
||
| 417 | * Using a callback function, this method will filter out unwanted values, returning |
||
| 418 | * a new collection containing only the values that weren't filtered. |
||
| 419 | * |
||
| 420 | * @param callable $callback The callback function used to filter |
||
| 421 | * |
||
| 422 | * @return CollectionInterface A new collection with only values that weren't filtered |
||
| 423 | */ |
||
| 424 | public function exclude(callable $callback) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Return the first item that meets given criteria. |
||
| 438 | * |
||
| 439 | * Using a callback function, this method will return the first item in the collection |
||
| 440 | * that causes the callback function to return true. |
||
| 441 | * |
||
| 442 | * @param callable|null $callback The callback function |
||
| 443 | * @param mixed|null $default The default return value |
||
| 444 | * |
||
| 445 | * @return mixed |
||
| 446 | */ |
||
| 447 | public function first(callable $callback = null, $default = null) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Return the last item that meets given criteria. |
||
| 464 | * |
||
| 465 | * Using a callback function, this method will return the last item in the collection |
||
| 466 | * that causes the callback function to return true. |
||
| 467 | * |
||
| 468 | * @param callable|null $callback The callback function |
||
| 469 | * @param mixed|null $default The default return value |
||
| 470 | * |
||
| 471 | * @return mixed |
||
| 472 | */ |
||
| 473 | public function last(callable $callback = null, $default = null) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Returns collection in reverse order. |
||
| 484 | * |
||
| 485 | * @return CollectionInterface This collection in reverse order. |
||
| 486 | */ |
||
| 487 | public function reverse() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Get unique items. |
||
| 494 | * |
||
| 495 | * Returns a collection of all the unique items in this collection. |
||
| 496 | * |
||
| 497 | * @return CollectionInterface This collection with duplicate items removed |
||
| 498 | */ |
||
| 499 | public function unique() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Collection factory method. |
||
| 506 | * |
||
| 507 | * This method will analyze input data and determine the most appropriate Collection |
||
| 508 | * class to use. It will then instantiate said Collection class with the given |
||
| 509 | * data and return it. |
||
| 510 | * |
||
| 511 | * @param mixed $data The data to wrap |
||
| 512 | * |
||
| 513 | * @return CollectionInterface A collection containing $data |
||
| 514 | */ |
||
| 515 | public static function factory($data = null) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Determine if structure contains all numeric values. |
||
| 522 | * |
||
| 523 | * @return bool |
||
| 524 | */ |
||
| 525 | public function isNumeric() |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @inheritdoc |
||
| 541 | */ |
||
| 542 | public function hasOffset($offset) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @inheritdoc |
||
| 554 | */ |
||
| 555 | public function getOffsetKey($offset) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @inheritdoc |
||
| 567 | */ |
||
| 568 | public function getOffset($offset) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @param int $offset The numerical offset |
||
| 575 | * |
||
| 576 | * @throws OutOfBoundsException if no pair at position |
||
| 577 | * |
||
| 578 | * @return array |
||
| 579 | */ |
||
| 580 | public function getOffsetPair($offset) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Get each key/value as an array pair. |
||
| 589 | * |
||
| 590 | * Returns a collection of arrays where each item in the collection is [key,value] |
||
| 591 | * |
||
| 592 | * @return CollectionInterface |
||
| 593 | */ |
||
| 594 | public function pairs() |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Get duplicate values. |
||
| 607 | * |
||
| 608 | * Returns a collection of arrays where the key is the duplicate value |
||
| 609 | * and the value is an array of keys from the original collection. |
||
| 610 | * |
||
| 611 | * @return CollectionInterface A new collection with duplicate values. |
||
| 612 | */ |
||
| 613 | public function duplicates() |
||
| 624 | |||
| 625 | // END Iterator methods |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Counts how many times each value occurs in a collection. |
||
| 629 | * |
||
| 630 | * Returns a new collection with values as keys and how many times that |
||
| 631 | * value appears in the collection. Works best with scalar values but will |
||
| 632 | * attempt to work on collections of objects as well. |
||
| 633 | * |
||
| 634 | * @return CollectionInterface |
||
| 635 | * |
||
| 636 | * @todo Right now, collections of arrays or objects are supported via the |
||
| 637 | * __toString() or spl_object_hash() |
||
| 638 | * @todo NumericCollection::counts() does the same thing... |
||
| 639 | */ |
||
| 640 | public function frequency() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @inheritDoc |
||
| 666 | */ |
||
| 667 | public function add($index, $value) |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @inheritdoc |
||
| 677 | * @todo Maybe read would be a better name for this? |
||
| 678 | */ |
||
| 679 | public function get($index, $default = null) |
||
| 687 | |||
| 688 | /** |
||
| 689 | * @inheritdoc |
||
| 690 | * @todo Maybe read would be a better name for this? |
||
| 691 | */ |
||
| 692 | public function retrieve($index) |
||
| 699 | |||
| 700 | /** |
||
| 701 | * @inheritDoc |
||
| 702 | */ |
||
| 703 | public function prepend($item) |
||
| 710 | |||
| 711 | /** |
||
| 712 | * @inheritDoc |
||
| 713 | */ |
||
| 714 | public function append($item) |
||
| 721 | |||
| 722 | /** |
||
| 723 | * @inheritDoc |
||
| 724 | */ |
||
| 725 | public function chunk($size) |
||
| 742 | |||
| 743 | public function combine($values) |
||
| 759 | |||
| 760 | /** |
||
| 761 | * @inheritDoc |
||
| 762 | */ |
||
| 763 | public function diff($data) |
||
| 772 | |||
| 773 | /** |
||
| 774 | * @inheritDoc |
||
| 775 | */ |
||
| 776 | public function diffKeys($data) |
||
| 785 | |||
| 786 | /** |
||
| 787 | * @inheritDoc |
||
| 788 | */ |
||
| 789 | public function nth($nth, $offset = null) |
||
| 795 | |||
| 796 | /** |
||
| 797 | * @inheritDoc |
||
| 798 | */ |
||
| 799 | public function except($indexes) |
||
| 803 | |||
| 804 | /** |
||
| 805 | * @inheritDoc |
||
| 806 | */ |
||
| 807 | public function flip() |
||
| 811 | |||
| 812 | /** |
||
| 813 | * @inheritDoc |
||
| 814 | */ |
||
| 815 | public function intersect($data) |
||
| 824 | |||
| 825 | /** |
||
| 826 | * @inheritDoc |
||
| 827 | */ |
||
| 828 | public function intersectKeys($data) |
||
| 837 | |||
| 838 | /** |
||
| 839 | * @inheritDoc |
||
| 840 | */ |
||
| 841 | public function isEmpty(callable $callback = null) |
||
| 848 | |||
| 849 | /** |
||
| 850 | * @inheritDoc |
||
| 851 | */ |
||
| 852 | public function only($indices) |
||
| 856 | |||
| 857 | /** |
||
| 858 | * @inheritDoc |
||
| 859 | */ |
||
| 860 | public function pipe(callable $callback) |
||
| 864 | |||
| 865 | /** |
||
| 866 | * @inheritDoc |
||
| 867 | */ |
||
| 868 | public function random($num) |
||
| 872 | |||
| 873 | /** |
||
| 874 | * @inheritDoc |
||
| 875 | */ |
||
| 876 | public function indicesOf($value) |
||
| 884 | |||
| 885 | /** |
||
| 886 | * @inheritDoc |
||
| 887 | */ |
||
| 888 | public function shuffle() |
||
| 892 | |||
| 893 | /** |
||
| 894 | * @inheritDoc |
||
| 895 | */ |
||
| 896 | public function slice($offset, $length = null) |
||
| 900 | |||
| 901 | /** |
||
| 902 | * @inheritDoc |
||
| 903 | */ |
||
| 904 | public function split($num) |
||
| 938 | |||
| 939 | /** |
||
| 940 | * @inheritDoc |
||
| 941 | */ |
||
| 942 | public function union($data) |
||
| 951 | |||
| 952 | /** |
||
| 953 | * @inheritDoc |
||
| 954 | */ |
||
| 955 | public function zip(...$data) |
||
| 982 | |||
| 983 | /** |
||
| 984 | * @inheritDoc |
||
| 985 | */ |
||
| 986 | public function fold(callable $callback, $initial = null) |
||
| 995 | |||
| 996 | /** |
||
| 997 | * @inheritDoc |
||
| 998 | */ |
||
| 999 | public function foldl(callable $callback, $initial = null) |
||
| 1003 | |||
| 1004 | /** |
||
| 1005 | * @inheritDoc |
||
| 1006 | */ |
||
| 1007 | public function all(callable $callback = null) |
||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * @inheritDoc |
||
| 1019 | */ |
||
| 1020 | public function none(callable $callback = null) |
||
| 1029 | |||
| 1030 | // BEGIN Numeric Collection Methods |
||
| 1031 | // These methods only really work on numeric data. |
||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Increment an item. |
||
| 1035 | * |
||
| 1036 | * Increment the item specified by $key by one value. Intended for integers |
||
| 1037 | * but also works (using this term loosely) for letters. Any other data type |
||
| 1038 | * it may modify is unintended behavior at best. |
||
| 1039 | * |
||
| 1040 | * This method modifies its internal data array rather than returning a new |
||
| 1041 | * collection. |
||
| 1042 | * |
||
| 1043 | * @param mixed $index The key of the item you want to increment. |
||
| 1044 | * @param int $interval The interval that $key should be incremented by |
||
| 1045 | * |
||
| 1046 | * @return CollectionInterface |
||
| 1047 | */ |
||
| 1048 | public function increment($index, $interval = 1) |
||
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Decrement an item. |
||
| 1057 | * |
||
| 1058 | * Frcrement the item specified by $key by one value. Intended for integers. |
||
| 1059 | * Does not work for letters and if it does anything to anything else, it's |
||
| 1060 | * unintended at best. |
||
| 1061 | * |
||
| 1062 | * This method modifies its internal data array rather than returning a new |
||
| 1063 | * collection. |
||
| 1064 | * |
||
| 1065 | * @param mixed $index The key of the item you want to decrement. |
||
| 1066 | * @param int $interval The interval that $key should be decremented by |
||
| 1067 | * |
||
| 1068 | * @return CollectionInterface |
||
| 1069 | */ |
||
| 1070 | public function decrement($index, $interval = 1) |
||
| 1076 | |||
| 1077 | /** |
||
| 1078 | * Get the sum. |
||
| 1079 | * |
||
| 1080 | * @return int|float The sum of all values in collection |
||
| 1081 | */ |
||
| 1082 | public function sum() |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * Get the average. |
||
| 1089 | * |
||
| 1090 | * @return float|int The average value from the collection |
||
| 1091 | */ |
||
| 1092 | public function average() |
||
| 1096 | |||
| 1097 | /** |
||
| 1098 | * Get the mode. |
||
| 1099 | * |
||
| 1100 | * @return float|int The mode |
||
| 1101 | */ |
||
| 1102 | public function mode() |
||
| 1110 | |||
| 1111 | /** |
||
| 1112 | * Get the median value. |
||
| 1113 | * |
||
| 1114 | * @return float|int The median value |
||
| 1115 | */ |
||
| 1116 | public function median() |
||
| 1133 | |||
| 1134 | /** |
||
| 1135 | * Get the maximum value. |
||
| 1136 | * |
||
| 1137 | * @return mixed The maximum |
||
| 1138 | */ |
||
| 1139 | public function max() |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * Get the minimum value. |
||
| 1146 | * |
||
| 1147 | * @return mixed The minimum |
||
| 1148 | */ |
||
| 1149 | public function min() |
||
| 1153 | |||
| 1154 | /** |
||
| 1155 | * Get the number of times each item occurs in the collection. |
||
| 1156 | |||
| 1157 | * This method will return a NumericCollection where keys are the |
||
| 1158 | * values and values are the number of times that value occurs in |
||
| 1159 | * the original collection. |
||
| 1160 | |||
| 1161 | * @return CollectionInterface |
||
| 1162 | */ |
||
| 1163 | public function counts() |
||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * @param $serialized |
||
| 1170 | */ |
||
| 1171 | public function unserialize($serialized) |
||
| 1175 | |||
| 1176 | } |
||
| 1177 |
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.