Complex classes like CollectionTrait 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 CollectionTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | trait CollectionTrait |
||
8 | { |
||
9 | /** |
||
10 | * Converts $collection to array. If there are multiple items with the same key, only the last will be preserved. |
||
11 | * |
||
12 | * @return array |
||
13 | */ |
||
14 | 61 | public function toArray() |
|
18 | |||
19 | /** |
||
20 | * Returns a lazy collection of items for which $function returned true. |
||
21 | * |
||
22 | * @param callable $function ($value, $key) |
||
23 | * @return Collection |
||
24 | */ |
||
25 | 1 | public function filter(callable $function) |
|
29 | |||
30 | /** |
||
31 | * Returns a lazy collection of distinct items. The comparison is the same as in in_array. |
||
32 | * |
||
33 | * @return Collection |
||
34 | */ |
||
35 | 1 | public function distinct() |
|
39 | |||
40 | /** |
||
41 | * Returns a lazy collection with items from all $collections passed as argument appended together |
||
42 | * |
||
43 | * @param \Traversable[]|array ...$collections |
||
44 | * @return Collection |
||
45 | */ |
||
46 | 1 | public function concat(...$collections) |
|
50 | |||
51 | /** |
||
52 | * Returns collection where each item is changed to the output of executing $function on each key/item. |
||
53 | * |
||
54 | * @param callable $function |
||
55 | * @return Collection |
||
56 | */ |
||
57 | 3 | public function map(callable $function) |
|
61 | |||
62 | /** |
||
63 | * Reduces the collection to single value by iterating over the collection and calling $function while |
||
64 | * passing $startValue and current key/item as parameters. The output of $function is used as $startValue in |
||
65 | * next iteration. The output of $function on last element is the return value of this function. If |
||
66 | * $convertToCollection is true and the return value is a collection (array|Traversable) an instance of Collection |
||
67 | * is returned. |
||
68 | * |
||
69 | * @param callable $function ($tmpValue, $value, $key) |
||
70 | * @param mixed $startValue |
||
71 | * @param bool $convertToCollection |
||
72 | * @return mixed|Collection |
||
73 | */ |
||
74 | 2 | public function reduce(callable $function, $startValue, $convertToCollection = false) |
|
80 | |||
81 | /** |
||
82 | * Returns a lazy collection with one or multiple levels of nesting flattened. Removes all nesting when no value |
||
83 | * is passed. |
||
84 | * |
||
85 | * @param int $depth How many levels should be flatten, default (-1) is infinite. |
||
86 | * @return Collection |
||
87 | */ |
||
88 | 1 | public function flatten($depth = -1) |
|
92 | |||
93 | /** |
||
94 | * Returns a non-lazy collection sorted using $function($item1, $item2, $key1, $key2 ). $function should |
||
95 | * return true if first item is larger than the second and false otherwise. |
||
96 | * |
||
97 | * @param callable $function ($value1, $value2, $key1, $key2) |
||
98 | * @return Collection |
||
99 | */ |
||
100 | 1 | public function sort(callable $function) |
|
104 | |||
105 | /** |
||
106 | * Returns lazy collection items of which are part of the original collection from item number $from to item |
||
107 | * number $to. The items before $from are also iterated over, just not returned. |
||
108 | * |
||
109 | * @param int $from |
||
110 | * @param int $to If omitted, will slice until end |
||
111 | * @return Collection |
||
112 | */ |
||
113 | 1 | public function slice($from, $to = -1) |
|
117 | |||
118 | /** |
||
119 | * Returns collection which items are separated into groups indexed by the return value of $function. |
||
120 | * |
||
121 | * @param callable $function ($value, $key) |
||
122 | * @return Collection |
||
123 | */ |
||
124 | 1 | public function groupBy(callable $function) |
|
128 | |||
129 | /** |
||
130 | * Returns collection where items are separated into groups indexed by the value at given key. |
||
131 | * |
||
132 | * @param $key |
||
133 | * @return Collection |
||
134 | */ |
||
135 | 1 | public function groupByKey($key) |
|
139 | |||
140 | /** |
||
141 | * Returns a lazy collection in which $function is executed for each item. |
||
142 | * |
||
143 | * @param callable $function ($value, $key) |
||
144 | * @return Collection |
||
145 | */ |
||
146 | 1 | public function each(callable $function) |
|
150 | |||
151 | /** |
||
152 | * Returns the number of items in this collection. |
||
153 | * |
||
154 | * @return int |
||
155 | */ |
||
156 | 6 | public function size() |
|
160 | |||
161 | /** |
||
162 | * Returns value at the key $key. If multiple values have this key, return first. If no value has this key, throw |
||
163 | * ItemNotFound. If $convertToCollection is true and the return value is a collection (array|Traversable an |
||
164 | * instance of Collection will be returned. |
||
165 | * |
||
166 | * @param mixed $key |
||
167 | * @param bool $convertToCollection |
||
168 | * @return Collection|mixed |
||
169 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
170 | */ |
||
171 | 7 | public function get($key, $convertToCollection = false) |
|
177 | |||
178 | /** |
||
179 | * Returns item at the key $key. If multiple items have this key, return first. If no item has this key, return |
||
180 | * $ifNotFound. If no value has this key, throw ItemNotFound. If $convertToCollection is true and the return value |
||
181 | * is a collection (array|Traversable) an instance of Collection will be returned. |
||
182 | * |
||
183 | * @param mixed $key |
||
184 | * @param mixed $default |
||
185 | * @param bool $convertToCollection |
||
186 | * @return mixed|Collection |
||
187 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
188 | */ |
||
189 | 1 | public function getOrDefault($key, $default = null, $convertToCollection = false) |
|
195 | |||
196 | /** |
||
197 | * Returns first value matched by $function. If no value matches, return $default. If $convertToCollection is true |
||
198 | * and the return value is a collection (array|Traversable an instance of Collection will be returned. |
||
199 | * |
||
200 | * @param callable $function |
||
201 | * @param mixed|null $default |
||
202 | * @param bool $convertToCollection |
||
203 | * @return Collection|mixed |
||
204 | */ |
||
205 | 1 | public function find(callable $function, $default = null, $convertToCollection = false) |
|
211 | |||
212 | /** |
||
213 | * Returns a non-lazy collection of items whose keys are the return values of $function and values are the number of |
||
214 | * items in this collection for which the $function returned this value. |
||
215 | * |
||
216 | * @param callable $function |
||
217 | * @return Collection |
||
218 | */ |
||
219 | 1 | public function countBy(callable $function) |
|
223 | |||
224 | /** |
||
225 | * Returns a lazy collection by changing keys of this collection for each item to the result of $function for |
||
226 | * that item. |
||
227 | * |
||
228 | * @param callable $function |
||
229 | * @return Collection |
||
230 | */ |
||
231 | 1 | public function indexBy(callable $function) |
|
235 | |||
236 | /** |
||
237 | * Returns true if $function returns true for every item in this collection, false otherwise. |
||
238 | * |
||
239 | * @param callable $function |
||
240 | * @return bool |
||
241 | */ |
||
242 | 1 | public function every(callable $function) |
|
246 | |||
247 | /** |
||
248 | * Returns true if $function returns true for at least one item in this collection, false otherwise. |
||
249 | * |
||
250 | * @param callable $function |
||
251 | * @return bool |
||
252 | */ |
||
253 | 1 | public function some(callable $function) |
|
257 | |||
258 | /** |
||
259 | * Returns true if $value is present in the collection. |
||
260 | * |
||
261 | * @param mixed $value |
||
262 | * @return bool |
||
263 | */ |
||
264 | 1 | public function contains($value) |
|
268 | |||
269 | /** |
||
270 | * Returns collection of items in this collection in reverse order. |
||
271 | * |
||
272 | * @return Collection |
||
273 | */ |
||
274 | 1 | public function reverse() |
|
278 | |||
279 | /** |
||
280 | * Reduce the collection to single value. Walks from right to left. |
||
281 | * |
||
282 | * @param callable $function Must take 2 arguments, intermediate value and item from the iterator. If |
||
283 | * $convertToCollection is true and the return value is a collection (array|Traversable) an instance of Collection |
||
284 | * is returned. |
||
285 | * @param mixed $startValue |
||
286 | * @param bool $convertToCollection |
||
287 | * @return mixed|Collection |
||
288 | */ |
||
289 | 1 | public function reduceRight(callable $function, $startValue, $convertToCollection = false) |
|
295 | |||
296 | /** |
||
297 | * A form of slice that returns first $numberOfItems items. |
||
298 | * |
||
299 | * @param int $numberOfItems |
||
300 | * @return Collection |
||
301 | */ |
||
302 | 9 | public function take($numberOfItems) |
|
306 | |||
307 | /** |
||
308 | * A form of slice that returns all but first $numberOfItems items. |
||
309 | * |
||
310 | * @param int $numberOfItems |
||
311 | * @return Collection |
||
312 | */ |
||
313 | 2 | public function drop($numberOfItems) |
|
317 | |||
318 | /** |
||
319 | * Returns collection of values from this collection but with keys being numerical from 0 upwards. |
||
320 | * |
||
321 | * @return Collection |
||
322 | */ |
||
323 | 12 | public function values() |
|
327 | |||
328 | /** |
||
329 | * Returns a lazy collection without elements matched by $function. |
||
330 | * |
||
331 | * @param callable $function |
||
332 | * @return Collection |
||
333 | */ |
||
334 | 1 | public function reject(callable $function) |
|
338 | |||
339 | /** |
||
340 | * Returns a lazy collection of the keys of this collection. |
||
341 | * |
||
342 | * @return Collection |
||
343 | */ |
||
344 | 1 | public function keys() |
|
348 | |||
349 | /** |
||
350 | * Returns a lazy collection of items of this collection separated by $separator |
||
351 | * |
||
352 | * @param mixed $separator |
||
353 | * @return Collection |
||
354 | */ |
||
355 | 1 | public function interpose($separator) |
|
359 | |||
360 | /** |
||
361 | * Returns a lazy collection with last $numberOfItems items skipped. These are still iterated over, just skipped. |
||
362 | * |
||
363 | * @param int $numberOfItems |
||
364 | * @return Collection |
||
365 | */ |
||
366 | 1 | public function dropLast($numberOfItems = 1) |
|
370 | |||
371 | /** |
||
372 | * Returns a lazy collection of first item from first collection, first item from second, second from first and |
||
373 | * so on. Accepts any number of collections. |
||
374 | * |
||
375 | * @param array|\Traversable ...$collections |
||
376 | * @return Collection |
||
377 | */ |
||
378 | 1 | public function interleave(...$collections) |
|
382 | |||
383 | /** |
||
384 | * Returns an infinite lazy collection of items in this collection repeated infinitely. |
||
385 | * |
||
386 | * @return Collection |
||
387 | */ |
||
388 | 1 | public function cycle() |
|
392 | |||
393 | /** |
||
394 | * Returns a lazy collection of items of this collection with $value added as first element. If $key is not provided |
||
395 | * it will be next integer index. |
||
396 | * |
||
397 | * @param mixed $value |
||
398 | * @param mixed|null $key |
||
399 | * @return Collection |
||
400 | */ |
||
401 | 2 | public function prepend($value, $key = null) |
|
405 | |||
406 | /** |
||
407 | * Returns a lazy collection of items of this collection with $value added as last element. If $key is not provided |
||
408 | * it will be next integer index. |
||
409 | * |
||
410 | * @param mixed $value |
||
411 | * @param mixed $key |
||
412 | * @return Collection |
||
413 | */ |
||
414 | 8 | public function append($value, $key = null) |
|
418 | |||
419 | /** |
||
420 | * Returns a lazy collection by removing items from this collection until first item for which $function returns |
||
421 | * false. |
||
422 | * |
||
423 | * @param callable $function |
||
424 | * @return Collection |
||
425 | */ |
||
426 | 1 | public function dropWhile(callable $function) |
|
430 | |||
431 | /** |
||
432 | * Returns a lazy collection which is a result of calling map($function) and then flatten(1) |
||
433 | * |
||
434 | * @param callable $function |
||
435 | * @return Collection |
||
436 | */ |
||
437 | 1 | public function mapcat(callable $function) |
|
441 | |||
442 | /** |
||
443 | * Returns a lazy collection of items from the start of the ollection until the first item for which $function |
||
444 | * returns false. |
||
445 | * |
||
446 | * @param callable $function |
||
447 | * @return Collection |
||
448 | */ |
||
449 | 1 | public function takeWhile(callable $function) |
|
453 | |||
454 | /** |
||
455 | * Returns a collection of [take($position), drop($position)] |
||
456 | * |
||
457 | * @param int $position |
||
458 | * @return Collection |
||
459 | */ |
||
460 | 1 | public function splitAt($position) |
|
464 | |||
465 | /** |
||
466 | * Returns a collection of [takeWhile($predicament), dropWhile($predicament] |
||
467 | * |
||
468 | * @param callable $function |
||
469 | * @return Collection |
||
470 | */ |
||
471 | 1 | public function splitWith(callable $function) |
|
475 | |||
476 | /** |
||
477 | * Returns a lazy collection with items from this collection but values that are found in keys of $replacementMap |
||
478 | * are replaced by their values. |
||
479 | * |
||
480 | * @param \Traversable|array $replacementMap |
||
481 | * @return Collection |
||
482 | */ |
||
483 | 1 | public function replace($replacementMap) |
|
487 | |||
488 | /** |
||
489 | * Returns a lazy collection of reduction steps. |
||
490 | * |
||
491 | * @param callable $function |
||
492 | * @param mixed $startValue |
||
493 | * @return Collection |
||
494 | */ |
||
495 | 1 | public function reductions(callable $function, $startValue) |
|
499 | |||
500 | /** |
||
501 | * Returns a lazy collection of every nth item in this collection |
||
502 | * |
||
503 | * @param int $step |
||
504 | * @return Collection |
||
505 | */ |
||
506 | 1 | public function takeNth($step) |
|
510 | |||
511 | /** |
||
512 | * Returns a non-collection of shuffled items from this collection |
||
513 | * |
||
514 | * @return Collection |
||
515 | */ |
||
516 | 1 | public function shuffle() |
|
520 | |||
521 | /** |
||
522 | * Returns a lazy collection of collections of $numberOfItems items each, at $step step |
||
523 | * apart. If $step is not supplied, defaults to $numberOfItems, i.e. the partitions |
||
524 | * do not overlap. If a $padding collection is supplied, use its elements as |
||
525 | * necessary to complete last partition up to $numberOfItems items. In case there are |
||
526 | * not enough padding elements, return a partition with less than $numberOfItems items. |
||
527 | * |
||
528 | * @param int $numberOfItems |
||
529 | * @param int $step |
||
530 | * @param array|\Traversable $padding |
||
531 | * @return Collection |
||
532 | */ |
||
533 | 1 | public function partition($numberOfItems, $step = 0, $padding = []) |
|
537 | |||
538 | /** |
||
539 | * Creates a lazy collection of collections created by partitioning this collection every time $function will |
||
540 | * return different result. |
||
541 | * |
||
542 | * @param callable $function |
||
543 | * @return Collection |
||
544 | */ |
||
545 | 1 | public function partitionBy(callable $function) |
|
549 | |||
550 | /** |
||
551 | * Returns true if this collection is empty. False otherwise. |
||
552 | * |
||
553 | * @return bool |
||
554 | */ |
||
555 | 2 | public function isEmpty() |
|
559 | |||
560 | /** |
||
561 | * Opposite of isEmpty. |
||
562 | * |
||
563 | * @return bool |
||
564 | */ |
||
565 | 2 | public function isNotEmpty() |
|
569 | |||
570 | /** |
||
571 | * Returns a collection where keys are distinct items from this collection and their values are number of |
||
572 | * occurrences of each value. |
||
573 | * |
||
574 | * @return Collection |
||
575 | */ |
||
576 | 1 | public function frequencies() |
|
580 | |||
581 | /** |
||
582 | * Returns first item of this collection. If the collection is empty, throws ItemNotFound. If $convertToCollection |
||
583 | * is true and the return value is a collection (array|Traversable an instance of Collection is returned. |
||
584 | * |
||
585 | * @param bool $convertToCollection |
||
586 | * @return mixed|Collection |
||
587 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
588 | */ |
||
589 | 11 | public function first($convertToCollection = false) |
|
594 | |||
595 | /** |
||
596 | * Returns last item of this collection. If the collection is empty, throws ItemNotFound. If $convertToCollection |
||
597 | * is true and the return value is a collection (array|Traversable) it is converted to Collection. |
||
598 | * |
||
599 | * @param bool $convertToCollection |
||
600 | * @return mixed|Collection |
||
601 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
602 | */ |
||
603 | 2 | public function last($convertToCollection = false) |
|
608 | |||
609 | /** |
||
610 | * Realizes collection - turns lazy collection into non-lazy one by iterating over it and storing the key/values. |
||
611 | * |
||
612 | * @return Collection |
||
613 | */ |
||
614 | 1 | public function realize() |
|
618 | |||
619 | /** |
||
620 | * Returns the second item in this collection or throws ItemNotFound if the collection is empty or has 1 item. If |
||
621 | * $convertToCollection is true and the return value is a collection (array|Traversable) it is converted to |
||
622 | * Collection. |
||
623 | * |
||
624 | * @param bool $convertToCollection |
||
625 | * @return Collection|mixed |
||
626 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
627 | */ |
||
628 | 6 | public function second($convertToCollection = false) |
|
633 | |||
634 | /** |
||
635 | * Combines the values of this collection as keys, with values of $collection as values. The resulting collection |
||
636 | * has length equal to the size of smaller collection. |
||
637 | * |
||
638 | * @param array|\Traversable $collection |
||
639 | * @return Collection |
||
640 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
641 | */ |
||
642 | 1 | public function combine($collection) |
|
646 | |||
647 | /** |
||
648 | * Returns a lazy collection without the items associated to any of the keys from $keys. |
||
649 | * |
||
650 | * @param array|\Traversable $keys |
||
651 | * @return Collection |
||
652 | */ |
||
653 | 1 | public function except($keys) |
|
657 | |||
658 | /** |
||
659 | * Returns a lazy collection of items associated to any of the keys from $keys. |
||
660 | * |
||
661 | * @param array|\Traversable $keys |
||
662 | * @return Collection |
||
663 | */ |
||
664 | 1 | public function only($keys) |
|
668 | |||
669 | /** |
||
670 | * Returns a lazy collection of items that are in $this but are not in any of the other arguments. Note that the |
||
671 | * ...$collections are iterated non-lazily. |
||
672 | * |
||
673 | * @param array|\Traversable ...$collections |
||
674 | * @return Collection |
||
675 | */ |
||
676 | 1 | public function diff(...$collections) |
|
680 | |||
681 | |||
682 | /** |
||
683 | * Returns a lazy collection where keys and values are flipped. |
||
684 | * |
||
685 | * @return Collection |
||
686 | */ |
||
687 | 1 | public function flip() |
|
691 | |||
692 | /** |
||
693 | * Checks for the existence of an item with$key in this collection. |
||
694 | * |
||
695 | * @param mixed $key |
||
696 | * @return bool |
||
697 | */ |
||
698 | 1 | public function has($key) |
|
702 | |||
703 | |||
704 | /** |
||
705 | * Returns a lazy collection of non-lazy collections of items from nth position from this collection and each |
||
706 | * passed collection. Stops when any of the collections don't have an item at the nth position. |
||
707 | * |
||
708 | * @param array|\Traversable[] ...$collections |
||
709 | * @return Collection |
||
710 | */ |
||
711 | 1 | public function zip(...$collections) |
|
716 | |||
717 | /** |
||
718 | * Uses a $transformer callable that takes a Collection and returns Collection on itself. |
||
719 | * |
||
720 | * @param callable $transformer Collection => Collection |
||
721 | * @return Collection |
||
722 | * @throws InvalidReturnValue |
||
723 | */ |
||
724 | 1 | public function transform(callable $transformer) |
|
736 | |||
737 | /** |
||
738 | * Extracts data from collection items by dot separated key path. Supports the * wildcard. If a key contains \ or |
||
739 | * it must be escaped using \ character. |
||
740 | * |
||
741 | * @param mixed $keyPath |
||
742 | * @return Collection |
||
743 | */ |
||
744 | 1 | public function extract($keyPath) |
|
748 | |||
749 | /** |
||
750 | * Returns a lazy collection of items that are in $collection and all the other arguments, indexed by the keys from |
||
751 | * the first collection. Note that the ...$collections are iterated non-lazily. |
||
752 | * |
||
753 | * @param array|\Traversable[] ...$collections |
||
754 | * @return Collection |
||
755 | */ |
||
756 | 1 | public function intersect(...$collections) |
|
760 | |||
761 | /** |
||
762 | * @return array|\Traversable |
||
763 | */ |
||
764 | 79 | protected function getItems() |
|
768 | } |
||
769 |