Complex classes like AbstractCollection 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 AbstractCollection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | abstract class AbstractCollection implements |
||
45 | ArrayAccess, |
||
46 | Countable, |
||
47 | Iterator |
||
48 | /*Collectable*/ |
||
49 | { |
||
50 | /** |
||
51 | * @var array The collection of data this object represents |
||
52 | */ |
||
53 | protected $data = []; |
||
54 | |||
55 | /** |
||
56 | * @var boolean True unless we have advanced past the end of the data array |
||
57 | */ |
||
58 | protected $isValid = true; |
||
59 | |||
60 | /** |
||
61 | * AbstractCollection constructor. |
||
62 | * |
||
63 | * @param mixed $data The data to wrap |
||
64 | */ |
||
65 | public function __construct($data = []) |
||
69 | |||
70 | /** |
||
71 | * Invoke object. |
||
72 | * |
||
73 | * Magic "invoke" method. Called when object is invoked as if it were a function. |
||
74 | * |
||
75 | * @param mixed $val The value (depends on other param value) |
||
76 | * @param mixed $index The index (depends on other param value) |
||
77 | * @return mixed (Depends on parameter values) |
||
78 | */ |
||
79 | public function __invoke($val = null, $index = null) |
||
96 | |||
97 | /** BEGIN ArrayAccess methods */ |
||
98 | |||
99 | /** |
||
100 | * Whether a offset exists. |
||
101 | * |
||
102 | * @param mixed $offset An offset to check for. |
||
103 | * @return boolean true on success or false on failure. |
||
104 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
105 | */ |
||
106 | public function offsetExists($offset) |
||
110 | |||
111 | /** |
||
112 | * Offset to retrieve. |
||
113 | * |
||
114 | * @param mixed $offset The offset to retrieve. |
||
115 | * @return mixed Can return all value types. |
||
116 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
117 | */ |
||
118 | public function offsetGet($offset) |
||
122 | |||
123 | /** |
||
124 | * Offset to set. |
||
125 | * |
||
126 | * @param mixed $offset The offset to assign the value to. |
||
127 | * @param mixed $value The value to set. |
||
128 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
129 | */ |
||
130 | public function offsetSet($offset, $value) |
||
134 | |||
135 | /** |
||
136 | * Offset to unset. |
||
137 | * |
||
138 | * @param mixed $offset The offset to unset. |
||
139 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
140 | */ |
||
141 | public function offsetUnset($offset) |
||
145 | |||
146 | /** END ArrayAccess methods */ |
||
147 | |||
148 | /** BEGIN Countable methods */ |
||
149 | |||
150 | public function count() |
||
154 | |||
155 | /** END Countable methods */ |
||
156 | |||
157 | /** BEGIN Iterator methods */ |
||
158 | |||
159 | /** |
||
160 | * Return the current element. |
||
161 | * |
||
162 | * Returns the current element in the collection. The internal array pointer |
||
163 | * of the data array wrapped by the collection should not be advanced by this |
||
164 | * method. No side effects. Return current element only. |
||
165 | * |
||
166 | * @return mixed |
||
167 | */ |
||
168 | public function current () |
||
172 | |||
173 | /** |
||
174 | * Return the current key. |
||
175 | * |
||
176 | * Returns the current key in the collection. No side effects. |
||
177 | * |
||
178 | * @return mixed |
||
179 | */ |
||
180 | public function key () |
||
184 | |||
185 | /** |
||
186 | * Advance the internal pointer forward. |
||
187 | * |
||
188 | * Although this method will return the current value after advancing the |
||
189 | * pointer, you should not expect it to. The interface does not require it |
||
190 | * to return any value at all. |
||
191 | * |
||
192 | * @return mixed |
||
193 | */ |
||
194 | public function next () |
||
203 | |||
204 | /** |
||
205 | * Rewind the internal pointer. |
||
206 | * |
||
207 | * Return the internal pointer to the first element in the collection. Again, |
||
208 | * this method is not required to return anything by its interface, so you |
||
209 | * should not count on a return value. |
||
210 | * |
||
211 | * @return mixed |
||
212 | */ |
||
213 | public function rewind () |
||
218 | |||
219 | /** |
||
220 | * Is internal pointer in a valid position? |
||
221 | * |
||
222 | * If the internal pointer is advanced beyond the end of the collection, this method will return false. |
||
223 | * |
||
224 | * @return bool True if internal pointer isn't past the end |
||
225 | */ |
||
226 | public function valid () |
||
230 | |||
231 | /** END Iterator methods */ |
||
232 | |||
233 | /** |
||
234 | * Set collection data. |
||
235 | * |
||
236 | * Sets the collection data. |
||
237 | * |
||
238 | * @param array $data The data to wrap |
||
239 | * @return $this |
||
240 | */ |
||
241 | protected function setData($data) |
||
254 | |||
255 | public function sort($alg = null) |
||
263 | |||
264 | /** |
||
265 | * Does this collection have a value at given index? |
||
266 | * |
||
267 | * @param mixed $index The index to check |
||
268 | * @return bool |
||
269 | */ |
||
270 | public function has($index) |
||
274 | |||
275 | /** |
||
276 | * Get value at a given index. |
||
277 | * |
||
278 | * Accessor for this collection of data. You can optionally provide a default |
||
279 | * value for when the collection doesn't find a value at the given index. It can |
||
280 | * also optionally throw an OutOfBoundsException if no value is found. |
||
281 | * |
||
282 | * @param mixed $index The index of the data you want to get |
||
283 | * @param mixed $default The default value to return if none available |
||
284 | * @param bool $throw True if you want an exception to be thrown if no data found at $index |
||
285 | * @throws OutOfBoundsException If $throw is true and $index isn't found |
||
286 | * @return mixed The data found at $index or failing that, the $default |
||
287 | * @todo Use OffsetGet, OffsetSet, etc. internally here and on set, has, delete, etc. |
||
288 | */ |
||
289 | public function get($index, $default = null, $throw = false) |
||
299 | |||
300 | /** |
||
301 | * Set a value at a given index. |
||
302 | * |
||
303 | * Setter for this collection. Allows setting a value at a given index. |
||
304 | * |
||
305 | * @param mixed $index The index to set a value at |
||
306 | * @param mixed $val The value to set $index to |
||
307 | * |
||
308 | * @return $this |
||
309 | */ |
||
310 | public function set($index, $val) |
||
316 | |||
317 | /** |
||
318 | * Unset a value at a given index. |
||
319 | * |
||
320 | * Unset (delete) value at the given index. |
||
321 | * |
||
322 | * @param mixed $index The index to unset |
||
323 | * @param bool $throw True if you want an exception to be thrown if no data found at $index |
||
324 | * |
||
325 | * @throws OutOfBoundsException If $throw is true and $index isn't found |
||
326 | * |
||
327 | * @return $this |
||
328 | */ |
||
329 | public function delete($index, $throw = false) |
||
341 | |||
342 | /** |
||
343 | * Does this collection have a value at specified numerical position? |
||
344 | * |
||
345 | * Returns true if collection contains a value (any value including null) |
||
346 | * at specified numerical position. |
||
347 | * |
||
348 | * @param int $pos The position |
||
349 | * |
||
350 | * @return bool |
||
351 | * |
||
352 | * @todo I feel like it would make more sense to have this start at position 1 rather than 0 |
||
353 | */ |
||
354 | public function hasPosition($pos) |
||
363 | |||
364 | /** |
||
365 | * Return value at specified numerical position. |
||
366 | * |
||
367 | * @param int $pos The numerical position |
||
368 | * |
||
369 | * @throws OutOfBoundsException if no pair at position |
||
370 | * |
||
371 | * @return mixed |
||
372 | */ |
||
373 | public function getValueAtPosition($pos) |
||
377 | |||
378 | /** |
||
379 | * Return key at specified numerical position. |
||
380 | * |
||
381 | * @param int $pos The numerical position |
||
382 | * |
||
383 | * @throws OutOfBoundsException if no pair at position |
||
384 | * |
||
385 | * @return mixed |
||
386 | */ |
||
387 | public function getKeyAtPosition($pos) |
||
398 | |||
399 | /** |
||
400 | * @param int $pos The numerical position |
||
401 | * |
||
402 | * @throws OutOfBoundsException if no pair at position |
||
403 | * |
||
404 | * @return array |
||
405 | */ |
||
406 | public function getPairAtPosition($pos) |
||
411 | |||
412 | /** |
||
413 | * Get collection as array. |
||
414 | * |
||
415 | * @return array This collection as an array |
||
416 | */ |
||
417 | public function toArray() |
||
428 | |||
429 | /** |
||
430 | * Get this collection's keys as a collection. |
||
431 | * |
||
432 | * @return AbstractCollection Containing this collection's keys |
||
433 | */ |
||
434 | public function keys() |
||
438 | |||
439 | /** |
||
440 | * Get this collection's values as a collection. |
||
441 | * |
||
442 | * This method returns this collection's values but completely re-indexed (numerically). |
||
443 | * |
||
444 | * @return AbstractCollection Containing this collection's values |
||
445 | */ |
||
446 | public function values() |
||
450 | |||
451 | /** |
||
452 | * Merge data into collection. |
||
453 | * |
||
454 | * Merges input data into this collection. Input can be an array or another collection. Returns a NEW collection object. |
||
455 | * |
||
456 | * @param Traversable|array $data The data to merge with this collection |
||
457 | * @return AbstractCollection A new collection with $data merged in |
||
458 | */ |
||
459 | public function merge($data) |
||
468 | |||
469 | /** |
||
470 | * Determine if this collection contains a value. |
||
471 | * |
||
472 | * Allows you to pass in a value or a callback function and optionally an index, |
||
473 | * and tells you whether or not this collection contains that value. If the $index param is specified, only that index will be looked under. |
||
474 | * |
||
475 | * @param mixed|callable $value The value to check for |
||
476 | * @param mixed $index The (optional) index to look under |
||
477 | * @return boolean True if this collection contains $value |
||
478 | * @todo Maybe add $identical param for identical comparison (===) |
||
479 | * @todo Allow negative offset for second param |
||
480 | */ |
||
481 | public function contains($value, $index = null) |
||
501 | |||
502 | |||
503 | /** |
||
504 | * Get duplicate values. |
||
505 | * |
||
506 | * Returns a collection of arrays where the key is the duplicate value |
||
507 | * and the value is an array of keys from the original collection. |
||
508 | * |
||
509 | * @return AbstractCollection A new collection with duplicate values. |
||
510 | */ |
||
511 | public function duplicates() |
||
521 | |||
522 | /** |
||
523 | * Pop an element off the end of this collection. |
||
524 | * |
||
525 | * @return mixed The last item in this collectio n |
||
526 | */ |
||
527 | public function pop() |
||
531 | |||
532 | /** |
||
533 | * Shift an element off the beginning of this collection. |
||
534 | * |
||
535 | * @return mixed The first item in this collection |
||
536 | */ |
||
537 | public function shift() |
||
541 | |||
542 | /** |
||
543 | * Push a item(s) onto the end of this collection. |
||
544 | * |
||
545 | * Returns a new collection with $items added. |
||
546 | * |
||
547 | * @param array $items Any number of arguments will be pushed onto the |
||
548 | * @return mixed The first item in this collection |
||
549 | */ |
||
550 | public function push(...$items) |
||
555 | |||
556 | /** |
||
557 | * Unshift item(s) onto the beginning of this collection. |
||
558 | * |
||
559 | * Returns a new collection with $items added. |
||
560 | * |
||
561 | * @return mixed The first item in this collection |
||
562 | */ |
||
563 | public function unshift(...$items) |
||
568 | |||
569 | /** |
||
570 | * Pad this collection to a certain size. |
||
571 | * |
||
572 | * Returns a new collection, padded to the given size, with the given value. |
||
573 | * |
||
574 | * @param int $size The number of items that should be in the collection |
||
575 | * @param null $with The value to pad the collection with |
||
576 | * @return AbstractCollection A new collection padded to specified length |
||
577 | */ |
||
578 | public function pad($size, $with = null) |
||
582 | |||
583 | /** |
||
584 | * Apply a callback to each item in collection. |
||
585 | * |
||
586 | * Applies a callback to each item in collection and returns a new collection |
||
587 | * containing each iteration's return value. |
||
588 | * |
||
589 | * @param callable $callback The callback to apply |
||
590 | * @return AbstractCollection A new collection with callback return values |
||
591 | */ |
||
592 | public function map(callable $callback) |
||
596 | |||
597 | /** |
||
598 | * Apply a callback to each item in collection. |
||
599 | * |
||
600 | * Applies a callback to each item in collection. The callback should return |
||
601 | * false to filter any item from the collection. |
||
602 | * |
||
603 | * @param callable $callback The callback function |
||
604 | * @param null $extraContext Extra context to pass as third param in callback |
||
605 | * @return $this |
||
606 | * @see php.net array_walk |
||
607 | */ |
||
608 | public function walk(callable $callback, $extraContext = null) |
||
613 | |||
614 | /** |
||
615 | * Get each key/value as an array pair |
||
616 | * |
||
617 | * Returns a collection of arrays where each item in the collection is [key,value] |
||
618 | * |
||
619 | * @return AbstractCollection |
||
620 | */ |
||
621 | public function pairs() |
||
631 | |||
632 | /** |
||
633 | * Reduce the collection to a single value. |
||
634 | * |
||
635 | * Using a callback function, this method will reduce this collection to a |
||
636 | * single value. |
||
637 | * |
||
638 | * @param callable $callback The callback function used to reduce |
||
639 | * @param null $initial The initial carry value |
||
640 | * @return mixed The single value produced by reduction algorithm |
||
641 | */ |
||
642 | public function reduce(callable $callback, $initial = null) |
||
646 | |||
647 | /** |
||
648 | * Filter the collection. |
||
649 | * |
||
650 | * Using a callback function, this method will filter out unwanted values, returning |
||
651 | * a new collection containing only the values that weren't filtered. |
||
652 | * |
||
653 | * @param callable $callback The callback function used to filter |
||
654 | * @param int $flag array_filter flag(s) (ARRAY_FILTER_USE_KEY or ARRAY_FILTER_USE_BOTH) |
||
655 | * @return AbstractCollection A new collection with only values that weren't filtered |
||
656 | * @see php.net array_filter |
||
657 | */ |
||
658 | public function filter(callable $callback, $flag = ARRAY_FILTER_USE_BOTH) |
||
662 | |||
663 | /** |
||
664 | * Return the first item that meets given criteria. |
||
665 | * |
||
666 | * Using a callback function, this method will return the first item in the collection |
||
667 | * that causes the callback function to return true. |
||
668 | * |
||
669 | * @param callable $callback The callback function |
||
670 | * @return null|mixed The first item in the collection that causes callback to return true |
||
671 | */ |
||
672 | public function first(callable $callback) |
||
681 | |||
682 | /** |
||
683 | * Return the last item that meets given criteria. |
||
684 | * |
||
685 | * Using a callback function, this method will return the last item in the collection |
||
686 | * that causes the callback function to return true. |
||
687 | * |
||
688 | * @param callable $callback The callback function |
||
689 | * @return null|mixed The last item in the collection that causes callback to return true |
||
690 | */ |
||
691 | public function last(callable $callback) |
||
696 | |||
697 | /** |
||
698 | * Returns collection in reverse order. |
||
699 | * |
||
700 | * @param null $preserveKeys True if you want to preserve collection's keys |
||
701 | * @return AbstractCollection This collection in reverse order. |
||
702 | */ |
||
703 | public function reverse($preserveKeys = null) |
||
707 | |||
708 | /** |
||
709 | * Get unique items. |
||
710 | * |
||
711 | * Returns a collection of all the unique items in this collection. |
||
712 | * |
||
713 | * @return AbstractCollection This collection with duplicate items removed |
||
714 | */ |
||
715 | public function unique() |
||
719 | |||
720 | /** |
||
721 | * Convert collection to string |
||
722 | * |
||
723 | * @return string A string representation of this collection |
||
724 | * @todo Eventually I would like to add a $delim property so that |
||
725 | * I can easily join collection items together with a particular |
||
726 | * character (or set of characters). I would then add a few methods |
||
727 | * to change the delim property. It would default to a comma. |
||
728 | */ |
||
729 | public function __toString() |
||
733 | |||
734 | /** |
||
735 | * Join collection together using a delimiter. |
||
736 | * |
||
737 | * @param string $delimiter The delimiter string/char |
||
738 | * |
||
739 | * @return string |
||
740 | */ |
||
741 | public function join($delimiter = '') |
||
745 | |||
746 | /** |
||
747 | * Counts how many times each value occurs in a collection. |
||
748 | * |
||
749 | * Returns a new collection with values as keys and how many times that |
||
750 | * value appears in the collection. Works best with scalar values but will |
||
751 | * attempt to work on collections of objects as well. |
||
752 | * |
||
753 | * @return AbstractCollection |
||
754 | * |
||
755 | * @todo Right now, collections of arrays or objects are supported via the |
||
756 | * __toString() or spl_object_hash() |
||
757 | * @todo NumericCollection::counts() does the same thing... |
||
758 | */ |
||
759 | public function frequency() |
||
782 | |||
783 | /** |
||
784 | * Collection factory method. |
||
785 | * |
||
786 | * This method will analyze input data and determine the most appropriate Collection |
||
787 | * class to use. It will then instantiate said Collection class with the given |
||
788 | * data and return it. |
||
789 | * |
||
790 | * @param mixed $data The data to wrap |
||
791 | * @return AbstractCollection A collection containing $data |
||
792 | */ |
||
793 | public static function factory($data = null) |
||
808 | |||
809 | /** |
||
810 | * Assert input data is of the correct structure |
||
811 | * |
||
812 | * @param mixed $data Data to check |
||
813 | * @throws InvalidArgumentException If invalid data structure |
||
814 | */ |
||
815 | protected function assertCorrectInputDataType($data) |
||
821 | |||
822 | /** |
||
823 | * Convert input data to an array. |
||
824 | * |
||
825 | * Convert the input data to an array that can be worked with by a collection. |
||
826 | * |
||
827 | * @param mixed $data The input data |
||
828 | * @return array |
||
829 | */ |
||
830 | protected function prepareData($data) |
||
834 | |||
835 | /** |
||
836 | * Is input data tabular? |
||
837 | * |
||
838 | * Returns true if input data is tabular in nature. This means that it is a |
||
839 | * two-dimensional array with the same keys (columns) for each element (row). |
||
840 | * |
||
841 | * @param mixed $data The data structure to check |
||
842 | * @return boolean True if data structure is tabular |
||
843 | */ |
||
844 | public static function isTabular($data) |
||
870 | |||
871 | /** |
||
872 | * Check data for multiple dimensions. |
||
873 | * |
||
874 | * This method is to determine whether a data structure is multi-dimensional. |
||
875 | * That is to say, it is a traversable structure that contains at least one |
||
876 | * traversable structure. |
||
877 | * |
||
878 | * @param mixed $data The input data |
||
879 | * @return bool |
||
880 | */ |
||
881 | public static function isMultiDimensional($data) |
||
893 | |||
894 | /** |
||
895 | * Determine if structure contains all numeric values. |
||
896 | * |
||
897 | * @param mixed $data The input data |
||
898 | * @return bool |
||
899 | */ |
||
900 | public static function isAllNumeric($data) |
||
912 | |||
913 | /** |
||
914 | * Is data a string of characters? |
||
915 | * |
||
916 | * Just checks to see if input is a string of characters or a string |
||
917 | * of digits. |
||
918 | * |
||
919 | * @param mixed $data Data to check |
||
920 | * @return bool |
||
921 | */ |
||
922 | public static function isCharacterSet($data) |
||
929 | |||
930 | /** |
||
931 | * Determine whether data is consistent with a given collection type. |
||
932 | * |
||
933 | * This method is used to determine whether input data is consistent with a |
||
934 | * given collection type. For instance, CharCollection requires a string. |
||
935 | * NumericCollection requires an array or traversable set of numeric data. |
||
936 | * TabularCollection requires a two-dimensional data structure where all the |
||
937 | * keys are the same in every row. |
||
938 | * |
||
939 | * @param mixed $data Data structure to check for consistency |
||
940 | * @return boolean |
||
941 | */ |
||
942 | abstract protected function isConsistentDataStructure($data); |
||
943 | |||
944 | } |