Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
11 | class Collection implements CollectionInterface |
||
12 | { |
||
13 | private $values; |
||
14 | |||
15 | 124 | public function __construct(array $values) |
|
19 | |||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | 91 | public function toPrimitive() |
|
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | 2 | public function filter(callable $filter = null) |
|
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | 2 | public function intersect(CollectionInterface $collection) |
|
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | 3 | public function chunk($size) |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 2 | public function shift() |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 1 | public function reduce(callable $reducer, $initial = null) |
|
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | 1 | public function search($needle, $strict = self::STRICT) |
|
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | 2 | public function uintersect(CollectionInterface $collection, callable $intersecter) |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 2 | public function keyIntersect(CollectionInterface $collection) |
|
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | 2 | public function map(callable $mapper) |
|
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | 2 | public function pad($size, $value) |
|
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | 2 | public function pop() |
|
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | 1 | public function sum() |
|
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | 2 | public function diff(CollectionInterface $collection) |
|
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | 1 | public function flip() |
|
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | 1 | public function keys($search = null, $strict = self::STRICT) |
|
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | 2 | public function push($value) |
|
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | 4 | public function rand($num = 1) |
|
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | 2 | public function merge(CollectionInterface $collection) |
|
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | 2 | public function slice($offset, $length = null, $preserveKeys = false) |
|
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | 2 | public function udiff(CollectionInterface $collection, callable $differ) |
|
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | 1 | public function column($key, $indexKey = null) |
|
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | 2 | public function splice($offset, $length = 0, $replacement = []) |
|
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | 2 | public function unique($flags = SORT_REGULAR) |
|
291 | |||
292 | /** |
||
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | 2 | public function values() |
|
299 | |||
300 | /** |
||
301 | * {@inheritdoc} |
||
302 | */ |
||
303 | 1 | public function product() |
|
307 | |||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | 2 | public function replace(CollectionInterface $collection) |
|
318 | |||
319 | /** |
||
320 | * {@inheritdoc} |
||
321 | */ |
||
322 | 2 | public function reverse($preserveKeys = false) |
|
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | 2 | public function unshift($value) |
|
337 | |||
338 | /** |
||
339 | * {@inheritdoc} |
||
340 | */ |
||
341 | 2 | public function keyDiff(CollectionInterface $collection) |
|
348 | |||
349 | /** |
||
350 | * {@inheritdoc} |
||
351 | */ |
||
352 | 2 | public function ukeyDiff(CollectionInterface $collection, callable $differ) |
|
360 | |||
361 | /** |
||
362 | * {@inheritdoc} |
||
363 | */ |
||
364 | 2 | public function associativeDiff(CollectionInterface $collection) |
|
371 | |||
372 | /** |
||
373 | * {@inheritdoc} |
||
374 | */ |
||
375 | 38 | public function hasKey($key, $strict = self::STRICT) |
|
385 | |||
386 | /** |
||
387 | * {@inheritdoc} |
||
388 | */ |
||
389 | 1 | public function countValues() |
|
393 | |||
394 | /** |
||
395 | * {@inheritdoc} |
||
396 | */ |
||
397 | 2 | public function ukeyIntersect(CollectionInterface $collection, callable $intersecter) |
|
405 | |||
406 | /** |
||
407 | * {@inheritdoc} |
||
408 | */ |
||
409 | 2 | public function associativeIntersect(CollectionInterface $collection) |
|
416 | |||
417 | /** |
||
418 | * {@inheritdoc} |
||
419 | */ |
||
420 | 2 | View Code Duplication | public function sort($flags = SORT_REGULAR) |
431 | |||
432 | /** |
||
433 | * {@inheritdoc} |
||
434 | */ |
||
435 | 2 | View Code Duplication | public function associativeSort($flags = SORT_REGULAR) |
446 | |||
447 | /** |
||
448 | * {@inheritdoc} |
||
449 | */ |
||
450 | 2 | View Code Duplication | public function keySort($flags = SORT_REGULAR) |
461 | |||
462 | /** |
||
463 | * {@inheritdoc} |
||
464 | */ |
||
465 | 2 | View Code Duplication | public function ukeySort(callable $sorter) |
476 | |||
477 | /** |
||
478 | * {@inheritdoc} |
||
479 | */ |
||
480 | 2 | View Code Duplication | public function reverseSort($flags = SORT_REGULAR) |
491 | |||
492 | /** |
||
493 | * {@inheritdoc} |
||
494 | */ |
||
495 | 2 | View Code Duplication | public function usort(callable $sorter) |
506 | |||
507 | /** |
||
508 | * {@inheritdoc} |
||
509 | */ |
||
510 | 2 | View Code Duplication | public function associativeReverseSort($flags = SORT_REGULAR) |
521 | |||
522 | /** |
||
523 | * {@inheritdoc} |
||
524 | */ |
||
525 | 2 | View Code Duplication | public function keyReverseSort($flags = SORT_REGULAR) |
536 | |||
537 | /** |
||
538 | * {@inheritdoc} |
||
539 | */ |
||
540 | 2 | View Code Duplication | public function uassociativeSort(callable $sorter) |
551 | |||
552 | /** |
||
553 | * {@inheritdoc} |
||
554 | */ |
||
555 | 2 | public function naturalSort() |
|
566 | |||
567 | /** |
||
568 | * {@inheritdoc} |
||
569 | */ |
||
570 | 2 | View Code Duplication | public function first() |
578 | |||
579 | /** |
||
580 | * {@inheritdoc} |
||
581 | */ |
||
582 | 2 | View Code Duplication | public function last() |
592 | |||
593 | /** |
||
594 | * {@inheritdoc} |
||
595 | */ |
||
596 | 1 | public function each(callable $callback) |
|
604 | |||
605 | /** |
||
606 | * {@inheritdoc} |
||
607 | */ |
||
608 | 1 | public function join($separator) |
|
612 | |||
613 | /** |
||
614 | * {@inheritdoc} |
||
615 | */ |
||
616 | 2 | public function shuffle() |
|
627 | |||
628 | /** |
||
629 | * {@inheritdoc} |
||
630 | */ |
||
631 | 2 | public function take($size, $preserveKeys = false) |
|
652 | |||
653 | /** |
||
654 | * {@inheritdoc} |
||
655 | */ |
||
656 | 2 | public function grep($pattern, $revert = false) |
|
664 | |||
665 | /** |
||
666 | * {@inheritdoc} |
||
667 | */ |
||
668 | 15 | public function count() |
|
672 | |||
673 | /** |
||
674 | * {@inheritdoc} |
||
675 | */ |
||
676 | 5 | public function current() |
|
680 | |||
681 | /** |
||
682 | * {@inheritdoc} |
||
683 | */ |
||
684 | 5 | public function key() |
|
688 | |||
689 | /** |
||
690 | * {@inheritdoc} |
||
691 | */ |
||
692 | 5 | public function next() |
|
696 | |||
697 | /** |
||
698 | * {@inheritdoc} |
||
699 | */ |
||
700 | 5 | public function rewind() |
|
704 | |||
705 | /** |
||
706 | * {@inheritdoc} |
||
707 | */ |
||
708 | 5 | public function valid() |
|
712 | |||
713 | /** |
||
714 | * {@inheritdoc} |
||
715 | */ |
||
716 | 1 | public function offsetExists($offset) |
|
720 | |||
721 | /** |
||
722 | * {@inheritdoc} |
||
723 | */ |
||
724 | 37 | public function offsetGet($offset) |
|
735 | |||
736 | /** |
||
737 | * {@inheritdoc} |
||
738 | */ |
||
739 | 1 | public function offsetSet($offset, $value) |
|
743 | |||
744 | /** |
||
745 | * {@inheritdoc} |
||
746 | */ |
||
747 | 1 | public function offsetUnset($offset) |
|
751 | } |
||
752 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.