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 | ArrayableInterface, |
||
| 49 | Countable, |
||
| 50 | Iterator |
||
| 51 | { |
||
| 52 | use IsArrayable; |
||
| 53 | use IsImmutable; |
||
| 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 | /** |
||
| 86 | * Set underlying data array. |
||
| 87 | * |
||
| 88 | * Sets the collection data. This method should NEVER be called anywhere other than in __construct(). |
||
| 89 | * |
||
| 90 | * @param array|Traversable $data The data to wrap |
||
| 91 | */ |
||
| 92 | private function setData($data) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get copy of underlying data array. |
||
| 102 | * |
||
| 103 | * Returns a copy of this collection's underlying data array. It returns a copy because collections are supposed to |
||
| 104 | * be immutable. Nothing outside of the constructor should ever have direct access to the actual underlying array. |
||
| 105 | * |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | protected function getData() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @inheritDoc |
||
| 115 | */ |
||
| 116 | public function count(callable $callback = null) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Return the current element. |
||
| 126 | * |
||
| 127 | * Returns the current element in the collection. The internal array pointer |
||
| 128 | * of the data array wrapped by the collection should not be advanced by this |
||
| 129 | * method. No side effects. Return current element only. |
||
| 130 | * |
||
| 131 | * @return mixed |
||
| 132 | */ |
||
| 133 | public function current() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Return the current key. |
||
| 140 | * |
||
| 141 | * Returns the current key in the collection. No side effects. |
||
| 142 | * |
||
| 143 | * @return mixed |
||
| 144 | */ |
||
| 145 | public function key() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Advance the internal pointer forward. |
||
| 152 | * |
||
| 153 | * Although this method will return the current value after advancing the |
||
| 154 | * pointer, you should not expect it to. The interface does not require it |
||
| 155 | * to return any value at all. |
||
| 156 | * |
||
| 157 | * @return mixed |
||
| 158 | */ |
||
| 159 | public function next() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Rewind the internal pointer. |
||
| 171 | * |
||
| 172 | * Return the internal pointer to the first element in the collection. Again, |
||
| 173 | * this method is not required to return anything by its interface, so you |
||
| 174 | * should not count on a return value. |
||
| 175 | * |
||
| 176 | * @return mixed |
||
| 177 | */ |
||
| 178 | public function rewind() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Is internal pointer in a valid position? |
||
| 187 | * |
||
| 188 | * If the internal pointer is advanced beyond the end of the collection, this method will return false. |
||
| 189 | * |
||
| 190 | * @return bool True if internal pointer isn't past the end |
||
| 191 | */ |
||
| 192 | public function valid() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @inheritDoc |
||
| 199 | */ |
||
| 200 | public function sort($alg = null) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @inheritDoc |
||
| 213 | */ |
||
| 214 | public function sortkeys($alg = null) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Does this collection have a value at given index? |
||
| 227 | * |
||
| 228 | * @param mixed $index The index to check |
||
| 229 | * |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | public function has($index) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Set value at given index. |
||
| 239 | * |
||
| 240 | * This method simulates setting a value in this collection, but because collections are immutable, it actually |
||
| 241 | * returns a copy of this collection with the value in the new collection set to specified value. |
||
| 242 | * |
||
| 243 | * @param mixed $index The index to set a value at |
||
| 244 | * @param mixed $val The value to set $index to |
||
| 245 | * |
||
| 246 | * @return CollectionInterface |
||
| 247 | */ |
||
| 248 | public function set($index, $val) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Unset (delete) value at the given index. |
||
| 257 | * |
||
| 258 | * Get copy of collection with given index removed. |
||
| 259 | * |
||
| 260 | * @param mixed $index The index to unset |
||
| 261 | * |
||
| 262 | * @return CollectionInterface |
||
| 263 | */ |
||
| 264 | public function delete($index) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get index of a value. |
||
| 271 | * |
||
| 272 | * Given a value, this method will return the index of the first occurrence of that value. |
||
| 273 | * |
||
| 274 | * @param mixed $value Value to get the index of |
||
| 275 | * |
||
| 276 | * @return int|null|string |
||
| 277 | */ |
||
| 278 | public function indexOf($value) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get this collection's keys as a collection. |
||
| 290 | * |
||
| 291 | * @return CollectionInterface Containing this collection's keys |
||
| 292 | */ |
||
| 293 | public function keys() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Get this collection's values as a collection. |
||
| 300 | * |
||
| 301 | * This method returns this collection's values but completely re-indexed (numerically). |
||
| 302 | * |
||
| 303 | * @return CollectionInterface Containing this collection's values |
||
| 304 | */ |
||
| 305 | public function values() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Determine if this collection contains a value. |
||
| 312 | * |
||
| 313 | * Allows you to pass in a value or a callback function and optionally an index, |
||
| 314 | * and tells you whether or not this collection contains that value. |
||
| 315 | * If the $index param is specified, only that index will be looked under. |
||
| 316 | * |
||
| 317 | * @param mixed|callable $value The value to check for |
||
| 318 | * @param mixed $index The (optional) index to look under |
||
| 319 | * |
||
| 320 | * @return bool True if this collection contains $value |
||
| 321 | * |
||
| 322 | * @todo Maybe add $identical param for identical comparison (===) |
||
| 323 | * @todo Allow negative offset for second param |
||
| 324 | */ |
||
| 325 | public function contains($value, $index = null) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Pad collection to a certain size. |
||
| 350 | * |
||
| 351 | * Returns a new collection, padded to the given size, with the given value. |
||
| 352 | * |
||
| 353 | * @param int $size The number of items that should be in the collection |
||
| 354 | * @param mixed $with The value to pad the collection with |
||
| 355 | * |
||
| 356 | * @return CollectionInterface A new collection padded to specified length |
||
| 357 | */ |
||
| 358 | public function pad($size, $with = null) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Apply a callback to each item in collection. |
||
| 365 | * |
||
| 366 | * Applies a callback to each item in collection and returns a new collection |
||
| 367 | * containing each iteration's return value. |
||
| 368 | * |
||
| 369 | * @param callable $callback The callback to apply |
||
| 370 | * |
||
| 371 | * @return CollectionInterface A new collection with callback return values |
||
| 372 | */ |
||
| 373 | public function map(callable $callback) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Iterate over each item in the collection, calling $callback on it. Return false to stop iterating. |
||
| 385 | * |
||
| 386 | * @param callable $callback A callback to use |
||
| 387 | * |
||
| 388 | * @return $this |
||
| 389 | */ |
||
| 390 | public function each(callable $callback) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Filter the collection. |
||
| 403 | * |
||
| 404 | * Using a callback function, this method will filter out unwanted values, returning |
||
| 405 | * a new collection containing only the values that weren't filtered. |
||
| 406 | * |
||
| 407 | * @param callable $callback The callback function used to filter |
||
| 408 | * |
||
| 409 | * @return CollectionInterface A new collection with only values that weren't filtered |
||
| 410 | */ |
||
| 411 | public function filter(callable $callback) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Filter the collection. |
||
| 425 | * |
||
| 426 | * Using a callback function, this method will filter out unwanted values, returning |
||
| 427 | * a new collection containing only the values that weren't filtered. |
||
| 428 | * |
||
| 429 | * @param callable $callback The callback function used to filter |
||
| 430 | * |
||
| 431 | * @return CollectionInterface A new collection with only values that weren't filtered |
||
| 432 | */ |
||
| 433 | public function exclude(callable $callback) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Return the first item that meets given criteria. |
||
| 447 | * |
||
| 448 | * Using a callback function, this method will return the first item in the collection |
||
| 449 | * that causes the callback function to return true. |
||
| 450 | * |
||
| 451 | * @param callable|null $callback The callback function |
||
| 452 | * @param mixed|null $default The default return value |
||
| 453 | * |
||
| 454 | * @return mixed |
||
| 455 | */ |
||
| 456 | public function first(callable $callback = null, $default = null) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Return the last item that meets given criteria. |
||
| 473 | * |
||
| 474 | * Using a callback function, this method will return the last item in the collection |
||
| 475 | * that causes the callback function to return true. |
||
| 476 | * |
||
| 477 | * @param callable|null $callback The callback function |
||
| 478 | * @param mixed|null $default The default return value |
||
| 479 | * |
||
| 480 | * @return mixed |
||
| 481 | */ |
||
| 482 | public function last(callable $callback = null, $default = null) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Returns collection in reverse order. |
||
| 493 | * |
||
| 494 | * @return CollectionInterface This collection in reverse order. |
||
| 495 | */ |
||
| 496 | public function reverse() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Get unique items. |
||
| 503 | * |
||
| 504 | * Returns a collection of all the unique items in this collection. |
||
| 505 | * |
||
| 506 | * @return CollectionInterface This collection with duplicate items removed |
||
| 507 | */ |
||
| 508 | public function unique() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Collection factory method. |
||
| 515 | * |
||
| 516 | * This method will analyze input data and determine the most appropriate Collection |
||
| 517 | * class to use. It will then instantiate said Collection class with the given |
||
| 518 | * data and return it. |
||
| 519 | * |
||
| 520 | * @param mixed $data The data to wrap |
||
| 521 | * |
||
| 522 | * @return CollectionInterface A collection containing $data |
||
| 523 | */ |
||
| 524 | public static function factory($data = null) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Determine if structure contains all numeric values. |
||
| 531 | * |
||
| 532 | * @return bool |
||
| 533 | */ |
||
| 534 | public function isNumeric() |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @inheritdoc |
||
| 550 | */ |
||
| 551 | public function hasOffset($offset) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @inheritdoc |
||
| 563 | */ |
||
| 564 | public function getOffsetKey($offset) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @inheritdoc |
||
| 576 | */ |
||
| 577 | public function getOffset($offset) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * @param int $offset The numerical offset |
||
| 584 | * |
||
| 585 | * @throws OutOfBoundsException if no pair at position |
||
| 586 | * |
||
| 587 | * @return array |
||
| 588 | */ |
||
| 589 | public function getOffsetPair($offset) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Get each key/value as an array pair. |
||
| 598 | * |
||
| 599 | * Returns a collection of arrays where each item in the collection is [key,value] |
||
| 600 | * |
||
| 601 | * @return CollectionInterface |
||
| 602 | */ |
||
| 603 | public function pairs() |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Get duplicate values. |
||
| 616 | * |
||
| 617 | * Returns a collection of arrays where the key is the duplicate value |
||
| 618 | * and the value is an array of keys from the original collection. |
||
| 619 | * |
||
| 620 | * @return CollectionInterface A new collection with duplicate values. |
||
| 621 | */ |
||
| 622 | public function duplicates() |
||
| 633 | |||
| 634 | // END Iterator methods |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Counts how many times each value occurs in a collection. |
||
| 638 | * |
||
| 639 | * Returns a new collection with values as keys and how many times that |
||
| 640 | * value appears in the collection. Works best with scalar values but will |
||
| 641 | * attempt to work on collections of objects as well. |
||
| 642 | * |
||
| 643 | * @return CollectionInterface |
||
| 644 | * |
||
| 645 | * @todo Right now, collections of arrays or objects are supported via the |
||
| 646 | * __toString() or spl_object_hash() |
||
| 647 | * @todo NumericCollection::counts() does the same thing... |
||
| 648 | */ |
||
| 649 | public function frequency() |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @inheritDoc |
||
| 675 | */ |
||
| 676 | public function add($index, $value) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @inheritdoc |
||
| 686 | * @todo Maybe read would be a better name for this? |
||
| 687 | */ |
||
| 688 | public function get($index, $default = null) |
||
| 696 | |||
| 697 | /** |
||
| 698 | * @inheritdoc |
||
| 699 | * @todo Maybe read would be a better name for this? |
||
| 700 | */ |
||
| 701 | public function retrieve($index) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * @inheritDoc |
||
| 711 | */ |
||
| 712 | public function prepend($item) |
||
| 719 | |||
| 720 | /** |
||
| 721 | * @inheritDoc |
||
| 722 | */ |
||
| 723 | public function append($item) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * @inheritDoc |
||
| 733 | */ |
||
| 734 | public function chunk($size) |
||
| 752 | |||
| 753 | public function combine($values) |
||
| 769 | |||
| 770 | /** |
||
| 771 | * @inheritDoc |
||
| 772 | */ |
||
| 773 | public function diff($data) |
||
| 782 | |||
| 783 | /** |
||
| 784 | * @inheritDoc |
||
| 785 | */ |
||
| 786 | public function diffKeys($data) |
||
| 795 | |||
| 796 | /** |
||
| 797 | * @inheritDoc |
||
| 798 | */ |
||
| 799 | public function every($nth, $offset = null) |
||
| 805 | |||
| 806 | /** |
||
| 807 | * @inheritDoc |
||
| 808 | */ |
||
| 809 | public function except($indexes) |
||
| 813 | |||
| 814 | /** |
||
| 815 | * @inheritDoc |
||
| 816 | */ |
||
| 817 | public function flip() |
||
| 821 | |||
| 822 | /** |
||
| 823 | * @inheritDoc |
||
| 824 | */ |
||
| 825 | public function intersect($data) |
||
| 834 | |||
| 835 | /** |
||
| 836 | * @inheritDoc |
||
| 837 | */ |
||
| 838 | public function intersectKeys($data) |
||
| 847 | |||
| 848 | /** |
||
| 849 | * @inheritDoc |
||
| 850 | */ |
||
| 851 | public function isEmpty(callable $callback = null) |
||
| 858 | |||
| 859 | /** |
||
| 860 | * @inheritDoc |
||
| 861 | */ |
||
| 862 | public function only($indices) |
||
| 866 | |||
| 867 | /** |
||
| 868 | * @inheritDoc |
||
| 869 | */ |
||
| 870 | public function pipe(callable $callback) |
||
| 874 | |||
| 875 | /** |
||
| 876 | * @inheritDoc |
||
| 877 | */ |
||
| 878 | public function random($num) |
||
| 882 | |||
| 883 | /** |
||
| 884 | * @inheritDoc |
||
| 885 | */ |
||
| 886 | public function indicesOf($value) |
||
| 894 | |||
| 895 | /** |
||
| 896 | * @inheritDoc |
||
| 897 | */ |
||
| 898 | public function shuffle() |
||
| 902 | |||
| 903 | /** |
||
| 904 | * @inheritDoc |
||
| 905 | */ |
||
| 906 | public function slice($offset, $length = null) |
||
| 910 | |||
| 911 | /** |
||
| 912 | * @inheritDoc |
||
| 913 | */ |
||
| 914 | public function split($num) |
||
| 928 | |||
| 929 | /** |
||
| 930 | * @inheritDoc |
||
| 931 | */ |
||
| 932 | public function union($data) |
||
| 941 | |||
| 942 | /** |
||
| 943 | * @inheritDoc |
||
| 944 | */ |
||
| 945 | public function zip(...$data) |
||
| 954 | |||
| 955 | /** |
||
| 956 | * @inheritDoc |
||
| 957 | */ |
||
| 958 | public function foldRight(callable $callback, $initial = null) |
||
| 967 | |||
| 968 | /** |
||
| 969 | * @inheritDoc |
||
| 970 | */ |
||
| 971 | public function foldLeft(callable $callback, $initial = null) |
||
| 975 | |||
| 976 | /** |
||
| 977 | * @inheritDoc |
||
| 978 | */ |
||
| 979 | public function all(callable $callback = null) |
||
| 988 | |||
| 989 | /** |
||
| 990 | * @inheritDoc |
||
| 991 | */ |
||
| 992 | public function none(callable $callback = null) |
||
| 1001 | |||
| 1002 | // BEGIN Numeric Collection Methods |
||
| 1003 | // These methods only really work on numeric data. |
||
| 1004 | |||
| 1005 | /** |
||
| 1006 | * Increment an item. |
||
| 1007 | * |
||
| 1008 | * Increment the item specified by $key by one value. Intended for integers |
||
| 1009 | * but also works (using this term loosely) for letters. Any other data type |
||
| 1010 | * it may modify is unintended behavior at best. |
||
| 1011 | * |
||
| 1012 | * This method modifies its internal data array rather than returning a new |
||
| 1013 | * collection. |
||
| 1014 | * |
||
| 1015 | * @param mixed $index The key of the item you want to increment. |
||
| 1016 | * @param int $interval The interval that $key should be incremented by |
||
| 1017 | * |
||
| 1018 | * @return CollectionInterface |
||
| 1019 | */ |
||
| 1020 | public function increment($index, $interval = 1) |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Decrement an item. |
||
| 1029 | * |
||
| 1030 | * Frcrement the item specified by $key by one value. Intended for integers. |
||
| 1031 | * Does not work for letters and if it does anything to anything else, it's |
||
| 1032 | * unintended at best. |
||
| 1033 | * |
||
| 1034 | * This method modifies its internal data array rather than returning a new |
||
| 1035 | * collection. |
||
| 1036 | * |
||
| 1037 | * @param mixed $index The key of the item you want to decrement. |
||
| 1038 | * @param int $interval The interval that $key should be decremented by |
||
| 1039 | * |
||
| 1040 | * @return CollectionInterface |
||
| 1041 | */ |
||
| 1042 | public function decrement($index, $interval = 1) |
||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * Get the sum. |
||
| 1051 | * |
||
| 1052 | * @return int|float The sum of all values in collection |
||
| 1053 | */ |
||
| 1054 | public function sum() |
||
| 1058 | |||
| 1059 | /** |
||
| 1060 | * Get the average. |
||
| 1061 | * |
||
| 1062 | * @return float|int The average value from the collection |
||
| 1063 | */ |
||
| 1064 | public function average() |
||
| 1068 | |||
| 1069 | /** |
||
| 1070 | * Get the mode. |
||
| 1071 | * |
||
| 1072 | * @return float|int The mode |
||
| 1073 | */ |
||
| 1074 | public function mode() |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Get the median value. |
||
| 1085 | * |
||
| 1086 | * @return float|int The median value |
||
| 1087 | */ |
||
| 1088 | public function median() |
||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Get the maximum value. |
||
| 1108 | * |
||
| 1109 | * @return mixed The maximum |
||
| 1110 | */ |
||
| 1111 | public function max() |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * Get the minimum value. |
||
| 1118 | * |
||
| 1119 | * @return mixed The minimum |
||
| 1120 | */ |
||
| 1121 | public function min() |
||
| 1125 | |||
| 1126 | /** |
||
| 1127 | * Get the number of times each item occurs in the collection. |
||
| 1128 | |||
| 1129 | * This method will return a NumericCollection where keys are the |
||
| 1130 | * values and values are the number of times that value occurs in |
||
| 1131 | * the original collection. |
||
| 1132 | |||
| 1133 | * @return CollectionInterface |
||
| 1134 | */ |
||
| 1135 | public function counts() |
||
| 1139 | } |
||
| 1140 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.