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 |
||
12 | class Collection implements Iterator |
||
13 | { |
||
14 | /** |
||
15 | * @var Iterator |
||
16 | */ |
||
17 | protected $input; |
||
18 | |||
19 | /** |
||
20 | * @var callable |
||
21 | */ |
||
22 | private $generatorFactory; |
||
23 | |||
24 | /** |
||
25 | * @param callable|Closure|array|Traversable $input If callable is passed, it must be a generator factory function |
||
26 | */ |
||
27 | 78 | public function __construct($input) |
|
44 | |||
45 | /** |
||
46 | * Static alias of normal constructor. |
||
47 | * |
||
48 | * @param array|Traversable $input |
||
49 | * @return Collection |
||
50 | */ |
||
51 | 4 | public static function from($input) |
|
55 | |||
56 | /** |
||
57 | * Returns lazy collection of values, where first value is $input and all subsequent values are computed by applying |
||
58 | * $function to the last value in the collection. By default this produces an infinite collection. However you can |
||
59 | * end the collection by throwing a NoMoreItems exception. |
||
60 | * |
||
61 | * @param mixed $input |
||
62 | * @param callable $function |
||
63 | * @return Collection |
||
64 | */ |
||
65 | 2 | public static function iterate($input, callable $function) |
|
69 | |||
70 | /** |
||
71 | * Returns a lazy collection of $value repeated $times times. If $times is not provided the collection is infinite. |
||
72 | * |
||
73 | * @param mixed $value |
||
74 | * @param int $times |
||
75 | * @return Collection |
||
76 | */ |
||
77 | 3 | public static function repeat($value, $times = -1) |
|
81 | |||
82 | /** |
||
83 | * Returns a lazy collection of numbers starting at $start, incremented by $step until $end is reached. |
||
84 | * |
||
85 | * @param int $start |
||
86 | * @param int|null $end |
||
87 | * @param int $step |
||
88 | * @return Collection |
||
89 | */ |
||
90 | 2 | public static function range($start = 0, $end = null, $step = 1) |
|
94 | |||
95 | /** |
||
96 | * @inheritdoc |
||
97 | */ |
||
98 | 73 | public function current() |
|
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | 72 | public function next() |
|
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | 71 | public function key() |
|
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | 76 | public function valid() |
|
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 76 | public function rewind() |
|
138 | |||
139 | /** |
||
140 | * @return array |
||
141 | */ |
||
142 | 57 | public function toArray() |
|
146 | |||
147 | /** |
||
148 | * Returns a lazy collection of items for which $function returned true. |
||
149 | * |
||
150 | * @param callable $function ($value, $key) |
||
151 | * @return Collection |
||
152 | */ |
||
153 | 1 | public function filter(callable $function) |
|
157 | |||
158 | /** |
||
159 | * Returns a lazy collection of distinct items. The comparison is the same as in in_array. |
||
160 | * |
||
161 | * @return Collection |
||
162 | */ |
||
163 | 1 | public function distinct() |
|
167 | |||
168 | /** |
||
169 | * Returns a lazy collection with items from all $collections passed as argument appended together |
||
170 | * |
||
171 | * @param Traversable|array ...$collections |
||
172 | * @return Collection |
||
173 | */ |
||
174 | 1 | public function concat(...$collections) |
|
178 | |||
179 | /** |
||
180 | * Returns collection where each item is changed to the output of executing $function on each key/item. |
||
181 | * |
||
182 | * @param callable $function |
||
183 | * @return Collection |
||
184 | */ |
||
185 | 1 | public function map(callable $function) |
|
189 | |||
190 | /** |
||
191 | * Reduces the collection to single value by iterating over the collection and calling $function while |
||
192 | * passing $startValue and current key/item as parameters. The output of $function is used as $startValue in |
||
193 | * next iteration. The output of $function on last element is the return value of this function. |
||
194 | * |
||
195 | * @param mixed $startValue |
||
196 | * @param callable $function ($tmpValue, $value, $key) |
||
197 | * @return mixed |
||
198 | */ |
||
199 | 2 | public function reduce(callable $function, $startValue) |
|
205 | |||
206 | /** |
||
207 | * Returns a lazy collection with one or multiple levels of nesting flattened. Removes all nesting when no value |
||
208 | * is passed. |
||
209 | * |
||
210 | * @param int $depth How many levels should be flatten, default (-1) is infinite. |
||
211 | * @return Collection |
||
212 | */ |
||
213 | 1 | public function flatten($depth = -1) |
|
217 | |||
218 | /** |
||
219 | * Returns a non-lazy collection sorted using $function($item1, $item2, $key1, $key2 ). $function should |
||
220 | * return true if first item is larger than the second and false otherwise. |
||
221 | * |
||
222 | * @param callable $function ($value1, $value2, $key1, $key2) |
||
223 | * @return Collection |
||
224 | */ |
||
225 | 1 | public function sort(callable $function) |
|
229 | |||
230 | /** |
||
231 | * Returns lazy collection items of which are part of the original collection from item number $from to item |
||
232 | * number $to. The items before $from are also iterated over, just not returned. |
||
233 | * |
||
234 | * @param int $from |
||
235 | * @param int $to If omitted, will slice until end |
||
236 | * @return Collection |
||
237 | */ |
||
238 | 1 | public function slice($from, $to = -1) |
|
242 | |||
243 | /** |
||
244 | * Returns collection which items are separated into groups indexed by the return value of $function. |
||
245 | * |
||
246 | * @param callable $function ($value, $key) |
||
247 | * @return Collection |
||
248 | */ |
||
249 | 1 | public function groupBy(callable $function) |
|
253 | |||
254 | /** |
||
255 | * Returns a lazy collection in which $function is executed for each item. |
||
256 | * |
||
257 | * @param callable $function ($value, $key) |
||
258 | * @return Collection |
||
259 | */ |
||
260 | 1 | public function each(callable $function) |
|
264 | |||
265 | /** |
||
266 | * Returns the number of items in this collection. |
||
267 | * |
||
268 | * @return int |
||
269 | */ |
||
270 | 6 | public function size() |
|
274 | |||
275 | /** |
||
276 | * Returns value at the key $key. If multiple values have this key, return first. If no value has this key, throw |
||
277 | * ItemNotFound. If $convertToCollection is true and the return value is a collection (array|Traversable) an |
||
278 | * instance of Collection will be returned. |
||
279 | * |
||
280 | * @param mixed $key |
||
281 | * @param bool $convertToCollection |
||
282 | * @return Collection|mixed |
||
283 | * @throws ItemNotFound |
||
284 | */ |
||
285 | 2 | public function get($key, $convertToCollection = false) |
|
291 | |||
292 | /** |
||
293 | * Returns item at the key $key. If multiple items have this key, return first. If no item has this key, return |
||
294 | * $ifNotFound. If no value has this key, throw ItemNotFound. If $convertToCollection is true and the return value |
||
295 | * is a collection (array|Traversable) an instance of Collection will be returned. |
||
296 | * |
||
297 | * @param mixed $key |
||
298 | * @param mixed $default |
||
299 | * @param bool $convertToCollection |
||
300 | * @return mixed |
||
301 | * @throws ItemNotFound |
||
302 | */ |
||
303 | 1 | public function getOrDefault($key, $default = null, $convertToCollection = false) |
|
309 | |||
310 | /** |
||
311 | * Returns nth item in the collection starting from 0. If the size of this collection is smaller than $position, |
||
312 | * throw ItemNotFound. If $convertToCollection is true and the return value is a collection (array|Traversable) an |
||
313 | * instance of Collection will be returned. |
||
314 | * |
||
315 | * @param int $position |
||
316 | * @param bool $convertToCollection |
||
317 | * @return Collection|mixed |
||
318 | * @throws ItemNotFound |
||
319 | */ |
||
320 | 6 | public function getNth($position, $convertToCollection = false) |
|
326 | |||
327 | /** |
||
328 | * Returns first value matched by $function. If no value matches, return $default. If $convertToCollection is true |
||
329 | * and the return value is a collection (array|Traversable) an instance of Collection will be returned. |
||
330 | * |
||
331 | * @param callable $function |
||
332 | * @param mixed|null $default |
||
333 | * @param bool $convertToCollection |
||
334 | * @return Collection|mixed |
||
335 | */ |
||
336 | 1 | public function find(callable $function, $default = null, $convertToCollection = false) |
|
342 | |||
343 | /** |
||
344 | * Returns a non-lazy collection of items whose keys are the return values of $function and values are the number of |
||
345 | * items in this collection for which the $function returned this value. |
||
346 | * |
||
347 | * @param callable $function |
||
348 | * @return Collection |
||
349 | */ |
||
350 | 1 | public function countBy(callable $function) |
|
354 | |||
355 | /** |
||
356 | * Returns a lazy collection by changing keys of this collection for each item to the result of $function for |
||
357 | * that item. |
||
358 | * |
||
359 | * @param callable $function |
||
360 | * @return Collection |
||
361 | */ |
||
362 | 1 | public function indexBy(callable $function) |
|
366 | |||
367 | /** |
||
368 | * Returns true if $function returns true for every item in this collection, false otherwise. |
||
369 | * |
||
370 | * @param callable $function |
||
371 | * @return bool |
||
372 | */ |
||
373 | 1 | public function every(callable $function) |
|
377 | |||
378 | /** |
||
379 | * Returns true if $function returns true for at least one item in this collection, false otherwise. |
||
380 | * |
||
381 | * @param callable $function |
||
382 | * @return bool |
||
383 | */ |
||
384 | 1 | public function some(callable $function) |
|
388 | |||
389 | /** |
||
390 | * Returns true if $value is present in the collection. |
||
391 | * |
||
392 | * @param mixed $value |
||
393 | * @return bool |
||
394 | */ |
||
395 | 1 | public function contains($value) |
|
399 | |||
400 | /** |
||
401 | * Returns collection of items in this collection in reverse order. |
||
402 | * |
||
403 | * @return Collection |
||
404 | */ |
||
405 | 1 | public function reverse() |
|
409 | |||
410 | /** |
||
411 | * Reduce the collection to single value. Walks from right to left. |
||
412 | * |
||
413 | * @param callable $function Must take 2 arguments, intermediate value and item from the iterator. |
||
414 | * @param mixed $startValue |
||
415 | * @return mixed |
||
416 | */ |
||
417 | 1 | public function reduceRight(callable $function, $startValue) |
|
421 | |||
422 | /** |
||
423 | * A form of slice that returns first $numberOfItems items. |
||
424 | * |
||
425 | * @param int $numberOfItems |
||
426 | * @return Collection |
||
427 | */ |
||
428 | 8 | public function take($numberOfItems) |
|
432 | |||
433 | /** |
||
434 | * A form of slice that returns all but first $numberOfItems items. |
||
435 | * |
||
436 | * @param int $numberOfItems |
||
437 | * @return Collection |
||
438 | */ |
||
439 | 2 | public function drop($numberOfItems) |
|
443 | |||
444 | /** |
||
445 | * Returns collection of values from this collection but with keys being numerical from 0 upwards. |
||
446 | * |
||
447 | * @return Collection |
||
448 | */ |
||
449 | 10 | public function values() |
|
453 | |||
454 | /** |
||
455 | * Returns a lazy collection without elements matched by $function. |
||
456 | * |
||
457 | * @param callable $function |
||
458 | * @return Collection |
||
459 | */ |
||
460 | 1 | public function reject(callable $function) |
|
464 | |||
465 | /** |
||
466 | * Returns a lazy collection of the keys of this collection. |
||
467 | * |
||
468 | * @return Collection |
||
469 | */ |
||
470 | 1 | public function keys() |
|
474 | |||
475 | /** |
||
476 | * Returns a lazy collection of items of this collection separated by $separator |
||
477 | * |
||
478 | * @param mixed $separator |
||
479 | * @return Collection |
||
480 | */ |
||
481 | 1 | public function interpose($separator) |
|
485 | |||
486 | /** |
||
487 | * Returns a lazy collection with last $numberOfItems items skipped. These are still iterated over, just skipped. |
||
488 | * |
||
489 | * @param int $numberOfItems |
||
490 | * @return Collection |
||
491 | */ |
||
492 | 1 | public function dropLast($numberOfItems = 1) |
|
496 | |||
497 | /** |
||
498 | * Returns a lazy collection of first item from first collection, first item from second, second from first and |
||
499 | * so on. Accepts any number of collections. |
||
500 | * |
||
501 | * @param array|Traversable ...$collections |
||
502 | * @return Collection |
||
503 | */ |
||
504 | 1 | public function interleave(...$collections) |
|
508 | |||
509 | /** |
||
510 | * Returns an infinite lazy collection of items in this collection repeated infinitely. |
||
511 | * |
||
512 | * @return Collection |
||
513 | */ |
||
514 | 1 | public function cycle() |
|
518 | |||
519 | /** |
||
520 | * Returns a lazy collection of items of this collection with $value added as first element. If $key is not provided |
||
521 | * it will be next integer index. |
||
522 | * |
||
523 | * @param mixed $value |
||
524 | * @param mixed|null $key |
||
525 | * @return Collection |
||
526 | */ |
||
527 | 2 | public function prepend($value, $key = null) |
|
531 | |||
532 | /** |
||
533 | * Returns a lazy collection of items of this collection with $value added as last element. If $key is not provided |
||
534 | * it will be next integer index. |
||
535 | * |
||
536 | * @param mixed $value |
||
537 | * @param mixed $key |
||
538 | * @return Collection |
||
539 | */ |
||
540 | 7 | public function append($value, $key = null) |
|
544 | |||
545 | /** |
||
546 | * Returns a lazy collection by removing items from this collection until first item for which $function returns |
||
547 | * false. |
||
548 | * |
||
549 | * @param callable $function |
||
550 | * @return Collection |
||
551 | */ |
||
552 | 1 | public function dropWhile(callable $function) |
|
556 | |||
557 | /** |
||
558 | * Returns a lazy collection which is a result of calling map($function) and then flatten(1) |
||
559 | * |
||
560 | * @param callable $function |
||
561 | * @return Collection |
||
562 | */ |
||
563 | 1 | public function mapcat(callable $function) |
|
567 | |||
568 | /** |
||
569 | * Returns a lazy collection of items from the start of the ollection until the first item for which $function |
||
570 | * returns false. |
||
571 | * |
||
572 | * @param callable $function |
||
573 | * @return Collection |
||
574 | */ |
||
575 | 1 | public function takeWhile(callable $function) |
|
579 | |||
580 | /** |
||
581 | * Returns a collection of [take($position), drop($position)] |
||
582 | * |
||
583 | * @param int $position |
||
584 | * @return Collection |
||
585 | */ |
||
586 | 1 | public function splitAt($position) |
|
590 | |||
591 | /** |
||
592 | * Returns a collection of [takeWhile($predicament), dropWhile($predicament] |
||
593 | * |
||
594 | * @param callable $function |
||
595 | * @return Collection |
||
596 | */ |
||
597 | 1 | public function splitWith(callable $function) |
|
601 | |||
602 | /** |
||
603 | * Returns a lazy collection with items from this collection but values that are found in keys of $replacementMap |
||
604 | * are replaced by their values. |
||
605 | * |
||
606 | * @param Traversable|array $replacementMap |
||
607 | * @return Collection |
||
608 | */ |
||
609 | 1 | public function replace($replacementMap) |
|
613 | |||
614 | /** |
||
615 | * Returns a lazy collection of reduction steps. |
||
616 | * |
||
617 | * @param callable $function |
||
618 | * @param mixed $startValue |
||
619 | * @return Collection |
||
620 | */ |
||
621 | 1 | public function reductions(callable $function, $startValue) |
|
625 | |||
626 | /** |
||
627 | * Returns a lazy collection of every nth item in this collection |
||
628 | * |
||
629 | * @param int $step |
||
630 | * @return Collection |
||
631 | */ |
||
632 | 1 | public function takeNth($step) |
|
636 | |||
637 | /** |
||
638 | * Returns a non-collection of shuffled items from this collection |
||
639 | * |
||
640 | * @return Collection |
||
641 | */ |
||
642 | 1 | public function shuffle() |
|
646 | |||
647 | /** |
||
648 | * Returns a lazy collection of collections of $numberOfItems items each, at $step step |
||
649 | * apart. If $step is not supplied, defaults to $numberOfItems, i.e. the partitions |
||
650 | * do not overlap. If a $padding collection is supplied, use its elements as |
||
651 | * necessary to complete last partition up to $numberOfItems items. In case there are |
||
652 | * not enough padding elements, return a partition with less than $numberOfItems items. |
||
653 | * |
||
654 | * @param int $numberOfItems |
||
655 | * @param int $step |
||
656 | * @param array|Traversable $padding |
||
657 | * @return Collection |
||
658 | */ |
||
659 | 1 | public function partition($numberOfItems, $step = 0, $padding = []) |
|
663 | |||
664 | /** |
||
665 | * Creates a lazy collection of collections created by partitioning this collection every time $function will |
||
666 | * return different result. |
||
667 | * |
||
668 | * @param callable $function |
||
669 | * @return Collection |
||
670 | */ |
||
671 | 2 | public function partitionBy(callable $function) |
|
675 | |||
676 | /** |
||
677 | * Returns true if this collection is empty. False otherwise. |
||
678 | * |
||
679 | * @return bool |
||
680 | */ |
||
681 | 2 | public function isEmpty() |
|
685 | |||
686 | /** |
||
687 | * Opposite of isEmpty. |
||
688 | * |
||
689 | * @return bool |
||
690 | */ |
||
691 | 2 | public function isNotEmpty() |
|
695 | |||
696 | /** |
||
697 | * Returns a collection where keys are distinct items from this collection and their values are number of |
||
698 | * occurrences of each value. |
||
699 | * |
||
700 | * @return Collection |
||
701 | */ |
||
702 | 1 | public function frequencies() |
|
706 | |||
707 | /** |
||
708 | * Returns first item of this collection. If the collection is empty, throws ItemNotFound. If $convertToCollection |
||
709 | * is true and the return value is a collection (array|Traversable) an instance of Collection is returned. |
||
710 | * |
||
711 | * @param bool $convertToCollection |
||
712 | * @return mixed|Collection |
||
713 | * @throws ItemNotFound |
||
714 | */ |
||
715 | 10 | public function first($convertToCollection = false) |
|
720 | |||
721 | /** |
||
722 | * Returns last item of this collection. If the collection is empty, throws ItemNotFound. If $convertToCollection |
||
723 | * is true and the return value is a collection (array|Traversable) an |
||
724 | * |
||
725 | * @param bool $convertToCollection |
||
726 | * @return mixed|Collection |
||
727 | * @throws ItemNotFound |
||
728 | */ |
||
729 | 2 | public function last($convertToCollection = false) |
|
734 | |||
735 | /** |
||
736 | * Returns a lazy collection by picking a $key key from each sub-collection of $this. |
||
737 | * |
||
738 | * @param mixed $key |
||
739 | * @return Collection |
||
740 | */ |
||
741 | 2 | public function pluck($key) |
|
745 | |||
746 | /** |
||
747 | * Realizes collection - turns lazy collection into non-lazy one by iterating over it and storing the key/values. |
||
748 | * |
||
749 | * @return Collection |
||
750 | */ |
||
751 | 1 | public function realize() |
|
755 | |||
756 | /** |
||
757 | * Returns the second item in this collection or throws ItemNotFound if the collection is empty or has 1 item. |
||
758 | * |
||
759 | * @return mixed |
||
760 | */ |
||
761 | 2 | public function second() |
|
765 | |||
766 | /** |
||
767 | * Combines the values of this collection as keys, with values of $collection as values. The resulting collection |
||
768 | * has length equal to the size of smaller collection. If $strict is true, the size of both collections must be |
||
769 | * equal, otherwise ItemNotFound is thrown. When strict, the collection is realized immediately. |
||
770 | * |
||
771 | * @param array|Traversable $collection |
||
772 | * @param bool $strict |
||
773 | * @return Collection |
||
774 | */ |
||
775 | 1 | public function combine($collection, $strict = false) |
|
779 | |||
780 | /** |
||
781 | * Returns a lazy collection without the items associated to any of the keys from $keys. |
||
782 | * |
||
783 | * @param array|Traversable $keys |
||
784 | * @return Collection |
||
785 | */ |
||
786 | 1 | public function except($keys) |
|
790 | |||
791 | /** |
||
792 | * Returns a lazy collection of items associated to any of the keys from $keys. |
||
793 | * |
||
794 | * @param array|Traversable $keys |
||
795 | * @return Collection |
||
796 | */ |
||
797 | 1 | public function only($keys) |
|
801 | |||
802 | /** |
||
803 | * Returns a lazy collection of items that are in $this but are not in any of the other arguments. Note that the |
||
804 | * ...$collections are iterated non-lazily. |
||
805 | * |
||
806 | * @param array|Traversable ...$collections |
||
807 | * @return Collection |
||
808 | */ |
||
809 | 1 | public function difference(...$collections) |
|
813 | |||
814 | |||
815 | /** |
||
816 | * Returns a lazy collection where keys and values are flipped. |
||
817 | * |
||
818 | * @return Collection |
||
819 | */ |
||
820 | 1 | public function flip() |
|
824 | |||
825 | /** |
||
826 | * Checks for the existence of $key in this collection. |
||
827 | * |
||
828 | * @param mixed $key |
||
829 | * @return bool |
||
830 | */ |
||
831 | 1 | public function has($key) |
|
835 | } |
||
836 |