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 |
||
16 | class Collection implements CollectionContract { |
||
17 | |||
18 | /** |
||
19 | * Collection elements. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $elements = array(); |
||
24 | |||
25 | /** |
||
26 | * Collection type to enforce. |
||
27 | * |
||
28 | * @var Type |
||
29 | */ |
||
30 | private $type; |
||
31 | |||
32 | /** |
||
33 | * Where Collection is in loop. |
||
34 | * |
||
35 | * @var int |
||
36 | */ |
||
37 | protected $position = 0; |
||
38 | |||
39 | /** |
||
40 | * Collection constructor. |
||
41 | * |
||
42 | * @param string $type |
||
43 | * @param array $elements |
||
44 | */ |
||
45 | 57 | public function __construct( $type, array $elements = array() ) { |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | 12 | public function get_type() { |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | * |
||
75 | * @param mixed $element |
||
76 | * |
||
77 | * @return Collection |
||
78 | * |
||
79 | * @throws InvalidArgumentException |
||
80 | */ |
||
81 | 12 | public function add( $element ) { |
|
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | * |
||
100 | * @return Collection |
||
101 | */ |
||
102 | public function clear() { |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | * |
||
109 | * @param callable $condition Condition to satisfy. |
||
110 | * |
||
111 | * @return bool |
||
112 | */ |
||
113 | public function contains( callable $condition ) { |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | * |
||
120 | * @param callable $condition Condition to satisfy. |
||
121 | * |
||
122 | * @return mixed |
||
123 | */ |
||
124 | public function find( callable $condition ) { |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | * |
||
133 | * @param callable $condition Condition to satisfy. |
||
134 | * |
||
135 | * @return int |
||
136 | */ |
||
137 | public function find_index( callable $condition ) { |
||
149 | |||
150 | /** |
||
151 | * Fetches the element at the provided index. |
||
152 | * |
||
153 | * @param int $index Index to get element from. |
||
154 | * |
||
155 | * @return mixed |
||
156 | * |
||
157 | * @throws OutOfRangeException |
||
158 | */ |
||
159 | 12 | public function at( $index ) { |
|
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | * |
||
168 | * @param int $index Index to check for existence. |
||
169 | * |
||
170 | * @return bool |
||
171 | * |
||
172 | * @throws InvalidArgumentException |
||
173 | */ |
||
174 | 12 | public function index_exists( $index ) { |
|
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | * |
||
189 | * @param callable $condition Condition to satisfy. |
||
190 | * |
||
191 | * @return mixed |
||
192 | */ |
||
193 | public function filter( callable $condition ) { |
||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | * |
||
207 | * @param callable $condition Condition to satisfy. |
||
208 | * |
||
209 | * @return mixed |
||
210 | */ |
||
211 | public function find_last( callable $condition ) { |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | * |
||
220 | * @param callable $condition |
||
221 | * @return int |
||
222 | */ |
||
223 | public function find_last_index( callable $condition ) { |
||
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | * |
||
239 | * @param int $start Begining index to slice from. |
||
240 | * @param int $end End index to slice to. |
||
241 | * |
||
242 | * @return Collection |
||
243 | * |
||
244 | * @throws InvalidArgumentException |
||
245 | */ |
||
246 | public function slice( $start, $end ) { |
||
271 | |||
272 | /** |
||
273 | * {@inheritdoc} |
||
274 | * |
||
275 | * @param int $index Index to start at. |
||
276 | * @param mixed $element Element to insert. |
||
277 | * |
||
278 | * @return Collection |
||
279 | * |
||
280 | * @throws InvalidArgumentException |
||
281 | * @throws OutOfRangeException |
||
282 | */ |
||
283 | public function insert( $index, $element ) { |
||
294 | |||
295 | /** |
||
296 | * {@inheritdoc} |
||
297 | * |
||
298 | * @param int $index Index to start insertion at. |
||
299 | * @param array $elements Elements in insert. |
||
300 | * |
||
301 | * @return Collection |
||
302 | * |
||
303 | * @throws OutOfRangeException |
||
304 | */ |
||
305 | public function insert_range( $index, array $elements ) { |
||
323 | |||
324 | /** |
||
325 | * {@inheritdoc} |
||
326 | * |
||
327 | * @param callable $condition Condition to satisfy. |
||
328 | * |
||
329 | * @return Collection |
||
330 | */ |
||
331 | public function without( callable $condition ) { |
||
338 | |||
339 | /** |
||
340 | * {@inheritdoc} |
||
341 | * |
||
342 | * @param int $index Index to remove. |
||
343 | * |
||
344 | * @return Collection |
||
345 | * |
||
346 | * @throws OutOfRangeException |
||
347 | */ |
||
348 | public function remove_at( $index ) { |
||
360 | /** |
||
361 | * {@inheritdoc} |
||
362 | * |
||
363 | * @return Collection |
||
364 | */ |
||
365 | public function reverse() { |
||
370 | |||
371 | /** |
||
372 | * {@inheritdoc} |
||
373 | * |
||
374 | * @param callable $callback Sort callback. |
||
375 | * |
||
376 | * @return Collection |
||
377 | */ |
||
378 | public function sort( callable $callback ) { |
||
383 | |||
384 | /** |
||
385 | * {@inheritdoc} |
||
386 | * |
||
387 | * @return array |
||
388 | */ |
||
389 | 9 | public function to_array() { |
|
392 | |||
393 | /** |
||
394 | * {@inheritdoc} |
||
395 | * |
||
396 | * @param callable $callable Reducer function. |
||
397 | * |
||
398 | * @param null $initial Initial reducer value. |
||
399 | * |
||
400 | * @return mixed |
||
401 | */ |
||
402 | public function reduce( callable $callable, $initial = null ) { |
||
405 | |||
406 | /** |
||
407 | * {@inheritdoc} |
||
408 | * |
||
409 | * @param callable $condition Condition callback. |
||
410 | * |
||
411 | * @return bool |
||
412 | */ |
||
413 | public function every( callable $condition ) { |
||
427 | |||
428 | /** |
||
429 | * {@inheritdoc} |
||
430 | * |
||
431 | * @param int $num Number of elements to drop. |
||
432 | * |
||
433 | * @return Collection |
||
434 | * |
||
435 | * @throws InvalidArgumentException |
||
436 | */ |
||
437 | public function drop( $num ) { |
||
440 | |||
441 | /** |
||
442 | * {@inheritdoc} |
||
443 | * |
||
444 | * @param int $num Number of elements to drop. |
||
445 | * |
||
446 | * @return Collection |
||
447 | * |
||
448 | * @throws InvalidArgumentException |
||
449 | */ |
||
450 | public function drop_right( $num ) { |
||
455 | |||
456 | /** |
||
457 | * {@inheritdoc} |
||
458 | * |
||
459 | * @param callable $condition Condition callback. |
||
460 | * |
||
461 | * @return Collection |
||
462 | */ |
||
463 | public function drop_while( callable $condition ) { |
||
467 | /** |
||
468 | * {@inheritdoc} |
||
469 | * |
||
470 | * @return Collection |
||
471 | * |
||
472 | * @throws InvalidArgumentException |
||
473 | */ |
||
474 | public function tail() { |
||
477 | |||
478 | /** |
||
479 | * {@inheritdoc} |
||
480 | * |
||
481 | * @param int $num Number of elements to take. |
||
482 | * |
||
483 | * @return Collection |
||
484 | * |
||
485 | * @throws InvalidArgumentException |
||
486 | */ |
||
487 | public function take( $num ) { |
||
490 | |||
491 | /** |
||
492 | * {@inheritdoc} |
||
493 | * |
||
494 | * @param int $num Number of elements to take. |
||
495 | * |
||
496 | * @return Collection |
||
497 | * |
||
498 | * @throws InvalidArgumentException |
||
499 | */ |
||
500 | public function take_right( $num ) { |
||
503 | |||
504 | /** |
||
505 | * {@inheritdoc} |
||
506 | * |
||
507 | * @param callable $condition Callback function. |
||
508 | * |
||
509 | * @return Collection |
||
510 | */ |
||
511 | public function take_while( callable $condition ) { |
||
516 | |||
517 | /** |
||
518 | * {@inheritdoc} |
||
519 | * |
||
520 | * @param callable $callable Callback function. |
||
521 | */ |
||
522 | public function each( callable $callable ) { |
||
527 | |||
528 | /** |
||
529 | * {@inheritdoc} |
||
530 | * |
||
531 | * @param callable $callable Callback function. |
||
532 | * |
||
533 | * @return Collection |
||
534 | */ |
||
535 | 9 | public function map( callable $callable ) { |
|
554 | |||
555 | /** |
||
556 | * {@inheritdoc} |
||
557 | * |
||
558 | * @param callable $callable Reducer function. |
||
559 | * @param null $initial Initial value. |
||
560 | * |
||
561 | * @return mixed |
||
562 | */ |
||
563 | public function reduce_right( callable $callable, $initial = null ) { |
||
570 | |||
571 | /** |
||
572 | * {@inheritdoc} |
||
573 | * |
||
574 | * @return Collection |
||
575 | */ |
||
576 | public function shuffle() { |
||
582 | |||
583 | /** |
||
584 | * {@inheritdoc} |
||
585 | * |
||
586 | * @param array|Collection $elements Array of elements to merge. |
||
587 | * |
||
588 | * @return Collection |
||
589 | * |
||
590 | * @throws InvalidArgumentException |
||
591 | */ |
||
592 | public function merge( $elements ) { |
||
607 | |||
608 | /** |
||
609 | * {@inheritdoc} |
||
610 | * |
||
611 | * @return mixed |
||
612 | * |
||
613 | * @throws OutOfBoundsException |
||
614 | */ |
||
615 | public function first() { |
||
622 | |||
623 | /** |
||
624 | * {@inheritdoc} |
||
625 | * |
||
626 | * @return mixed |
||
627 | * |
||
628 | * @throws OutOfBoundsException |
||
629 | */ |
||
630 | public function last() { |
||
637 | |||
638 | /** |
||
639 | * {@inheritdoc} |
||
640 | * |
||
641 | * @return int |
||
642 | */ |
||
643 | 18 | public function count() { |
|
646 | |||
647 | /** |
||
648 | * {@inheritDoc} |
||
649 | * |
||
650 | * @return array |
||
651 | */ |
||
652 | public function serialize() { |
||
661 | |||
662 | /** |
||
663 | * Return the current element. |
||
664 | * |
||
665 | * @return mixed |
||
666 | */ |
||
667 | 3 | public function current() { |
|
670 | |||
671 | /** |
||
672 | * Move forward to next element. |
||
673 | */ |
||
674 | 3 | public function next() { |
|
677 | |||
678 | /** |
||
679 | * Return the key of the current element. |
||
680 | * |
||
681 | * @return mixed |
||
682 | */ |
||
683 | 3 | public function key() { |
|
686 | |||
687 | /** |
||
688 | * Checks if current position is valid. |
||
689 | * |
||
690 | * @return bool |
||
691 | */ |
||
692 | 3 | public function valid() { |
|
695 | |||
696 | /** |
||
697 | * Rewind the Iterator to the first element. |
||
698 | */ |
||
699 | 3 | public function rewind() { |
|
702 | |||
703 | /** |
||
704 | * Creates a new instance of the Collection |
||
705 | * from a trusted set of elements. |
||
706 | * |
||
707 | * @param array $elements Array of elements to pass into new collection. |
||
708 | * @param null|mixed $type |
||
709 | * |
||
710 | * @return static |
||
711 | */ |
||
712 | 9 | protected function new_from_trusted( array $elements, $type = null ) { |
|
718 | |||
719 | /** |
||
720 | * Sets the elements without validating them. |
||
721 | * |
||
722 | * @param array $elements Pre-validated elements to set. |
||
723 | */ |
||
724 | 18 | protected function set_from_trusted( array $elements ) { |
|
727 | |||
728 | /** |
||
729 | * Number of elements true for the condition. |
||
730 | * |
||
731 | * @param callable $condition Condition to check. |
||
732 | * @return int |
||
733 | */ |
||
734 | protected function count_while_true( callable $condition ) { |
||
746 | |||
747 | /** |
||
748 | * Validates a number to be used as an index. |
||
749 | * |
||
750 | * @param integer $index The number to be validated as an index. |
||
751 | * |
||
752 | * @throws OutOfRangeException |
||
753 | */ |
||
754 | 12 | protected function validate_index( $index ) { |
|
761 | } |
||
762 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: