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 | 129 | public function __construct(array $values) |
|
19 | |||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | 93 | public function toPrimitive() |
|
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | 2 | public function filter(callable $filter = null) |
|
32 | { |
||
33 | 2 | if ($filter === null) { |
|
34 | 2 | $values = array_filter($this->values); |
|
35 | 2 | } else { |
|
36 | 2 | $values = array_filter( |
|
37 | 2 | $this->values, |
|
38 | 2 | $filter, |
|
39 | ARRAY_FILTER_USE_BOTH |
||
40 | 2 | ); |
|
41 | } |
||
42 | |||
43 | 2 | return new self($values); |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | 2 | public function intersect(CollectionInterface $collection) |
|
50 | { |
||
51 | 2 | return new self(array_intersect( |
|
52 | 2 | $this->values, |
|
53 | 2 | $collection->toPrimitive() |
|
54 | 2 | )); |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | 3 | public function chunk($size) |
|
61 | { |
||
62 | 3 | $chunks = array_chunk($this->values, (int) $size); |
|
63 | 3 | $subs = []; |
|
64 | |||
65 | 3 | foreach ($chunks as $chunk) { |
|
66 | 3 | $subs[] = new self($chunk); |
|
67 | 3 | } |
|
68 | |||
69 | 3 | return new self($subs); |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 2 | public function shift() |
|
76 | { |
||
77 | 2 | $values = $this->values; |
|
78 | 2 | array_shift($values); |
|
79 | |||
80 | 2 | return new self($values); |
|
81 | } |
||
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 = true) |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | 2 | public function uintersect(CollectionInterface $collection, callable $intersecter) |
|
103 | { |
||
104 | 2 | return new self(array_uintersect( |
|
105 | 2 | $this->values, |
|
106 | 2 | $collection->toPrimitive(), |
|
107 | $intersecter |
||
108 | 2 | )); |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | 2 | public function keyIntersect(CollectionInterface $collection) |
|
115 | { |
||
116 | 2 | return new self(array_intersect_key( |
|
117 | 2 | $this->values, |
|
118 | 2 | $collection->toPrimitive() |
|
119 | 2 | )); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 2 | public function map(callable $mapper) |
|
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | 2 | public function pad($size, $value) |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 2 | public function pop() |
|
142 | { |
||
143 | 2 | $values = $this->values; |
|
144 | 2 | array_pop($values); |
|
145 | |||
146 | 2 | return new self($values); |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | 1 | public function sum() |
|
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | 2 | public function diff(CollectionInterface $collection) |
|
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | 1 | public function flip() |
|
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | 1 | public function keys($search = null, $strict = true) |
|
177 | { |
||
178 | 1 | $args = func_get_args(); |
|
179 | |||
180 | 1 | if (count($args) > 0) { |
|
181 | 1 | $keys = array_keys($this->values, $search, (bool) $strict); |
|
182 | 1 | } else { |
|
183 | 1 | $keys = array_keys($this->values); |
|
184 | } |
||
185 | |||
186 | 1 | return new self($keys); |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 2 | public function push($value) |
|
193 | { |
||
194 | 2 | $values = $this->values; |
|
195 | 2 | array_push($values, $value); |
|
196 | |||
197 | 2 | return new self($values); |
|
198 | } |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | 4 | public function rand($num = 1) |
|
204 | { |
||
205 | 4 | if ((int) $num > $this->count()) { |
|
206 | 2 | throw new OutOfBoundException( |
|
207 | 'Trying to return a wider collection than the current one' |
||
208 | 2 | ); |
|
209 | } |
||
210 | |||
211 | 2 | $keys = (array) array_rand($this->values, $num); |
|
212 | 2 | $values = []; |
|
213 | |||
214 | 2 | foreach ($keys as $key) { |
|
215 | 2 | $values[$key] = $this->values[$key]; |
|
216 | 2 | } |
|
217 | |||
218 | 2 | return new self($values); |
|
219 | } |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | 2 | public function merge(CollectionInterface $collection) |
|
225 | { |
||
226 | 2 | return new self(array_merge( |
|
227 | 2 | $this->values, |
|
228 | 2 | $collection->toPrimitive() |
|
229 | 2 | )); |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * {@inheritdoc} |
||
234 | */ |
||
235 | 2 | public function slice($offset, $length = null, $preserveKeys = false) |
|
236 | { |
||
237 | 2 | return new self(array_slice( |
|
238 | 2 | $this->values, |
|
239 | 2 | (int) $offset, |
|
240 | 2 | $length === null ? null : (int) $length, |
|
241 | (bool) $preserveKeys |
||
242 | 2 | )); |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | 2 | public function udiff(CollectionInterface $collection, callable $differ) |
|
249 | { |
||
250 | 2 | return new self(array_udiff( |
|
251 | 2 | $this->values, |
|
252 | 2 | $collection->toPrimitive(), |
|
253 | $differ |
||
254 | 2 | )); |
|
255 | } |
||
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | 1 | public function column($key, $indexKey = null) |
|
261 | { |
||
262 | 1 | return new self(array_column( |
|
263 | 1 | $this->values, |
|
264 | 1 | $key, |
|
265 | $indexKey |
||
266 | 1 | )); |
|
267 | } |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | 2 | public function splice($offset, $length = 0, $replacement = []) |
|
273 | { |
||
274 | 2 | $values = $this->values; |
|
275 | 2 | array_splice($values, (int) $offset, (int) $length, $replacement); |
|
276 | |||
277 | 2 | return new self($values); |
|
278 | } |
||
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | 2 | public function unique($flags = self::SORT_REGULAR) |
|
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | 2 | public function values() |
|
295 | |||
296 | /** |
||
297 | * {@inheritdoc} |
||
298 | */ |
||
299 | 1 | public function product() |
|
303 | |||
304 | /** |
||
305 | * {@inheritdoc} |
||
306 | */ |
||
307 | 2 | public function replace(CollectionInterface $collection) |
|
308 | { |
||
309 | 2 | return new self(array_replace( |
|
310 | 2 | $this->values, |
|
311 | 2 | $collection->toPrimitive() |
|
312 | 2 | )); |
|
313 | } |
||
314 | |||
315 | /** |
||
316 | * {@inheritdoc} |
||
317 | */ |
||
318 | 2 | public function reverse($preserveKeys = false) |
|
322 | |||
323 | /** |
||
324 | * {@inheritdoc} |
||
325 | */ |
||
326 | 2 | public function unshift($value) |
|
327 | { |
||
328 | 2 | $values = $this->values; |
|
329 | 2 | array_unshift($values, $value); |
|
330 | |||
331 | 2 | return new self($values); |
|
332 | } |
||
333 | |||
334 | /** |
||
335 | * {@inheritdoc} |
||
336 | */ |
||
337 | 2 | public function keyDiff(CollectionInterface $collection) |
|
338 | { |
||
339 | 2 | return new self(array_diff_key( |
|
340 | 2 | $this->values, |
|
341 | 2 | $collection->toPrimitive() |
|
342 | 2 | )); |
|
343 | } |
||
344 | |||
345 | /** |
||
346 | * {@inheritdoc} |
||
347 | */ |
||
348 | 2 | public function ukeyDiff(CollectionInterface $collection, callable $differ) |
|
349 | { |
||
350 | 2 | return new self(array_diff_ukey( |
|
351 | 2 | $this->values, |
|
352 | 2 | $collection->toPrimitive(), |
|
353 | $differ |
||
354 | 2 | )); |
|
355 | } |
||
356 | |||
357 | /** |
||
358 | * {@inheritdoc} |
||
359 | */ |
||
360 | 2 | public function associativeDiff(CollectionInterface $collection) |
|
361 | { |
||
362 | 2 | return new self(array_diff_assoc( |
|
363 | 2 | $this->values, |
|
364 | 2 | $collection->toPrimitive() |
|
365 | 2 | )); |
|
366 | } |
||
367 | |||
368 | /** |
||
369 | * {@inheritdoc} |
||
370 | */ |
||
371 | 40 | public function hasKey($key, $strict = true) |
|
372 | { |
||
373 | 40 | if ((bool) $strict === true) { |
|
374 | 40 | $bool = array_key_exists($key, $this->values); |
|
375 | 40 | } else { |
|
376 | 1 | $bool = isset($this->values[$key]); |
|
377 | } |
||
378 | |||
379 | 40 | return $bool; |
|
380 | } |
||
381 | |||
382 | /** |
||
383 | * {@inheritdoc} |
||
384 | */ |
||
385 | 1 | public function countValues() |
|
389 | |||
390 | /** |
||
391 | * {@inheritdoc} |
||
392 | */ |
||
393 | 2 | public function ukeyIntersect(CollectionInterface $collection, callable $intersecter) |
|
394 | { |
||
395 | 2 | return new self(array_intersect_ukey( |
|
396 | 2 | $this->values, |
|
397 | 2 | $collection->toPrimitive(), |
|
398 | $intersecter |
||
399 | 2 | )); |
|
400 | } |
||
401 | |||
402 | /** |
||
403 | * {@inheritdoc} |
||
404 | */ |
||
405 | 2 | public function associativeIntersect(CollectionInterface $collection) |
|
406 | { |
||
407 | 2 | return new self(array_intersect_assoc( |
|
408 | 2 | $this->values, |
|
409 | 2 | $collection->toPrimitive() |
|
410 | 2 | )); |
|
411 | } |
||
412 | |||
413 | /** |
||
414 | * {@inheritdoc} |
||
415 | */ |
||
416 | 2 | View Code Duplication | public function sort($flags = self::SORT_REGULAR) |
|
|||
417 | { |
||
418 | 2 | $values = $this->values; |
|
419 | 2 | $bool = sort($values, (int) $flags); |
|
420 | |||
421 | 2 | if ($bool === false) { |
|
422 | throw new SortException('Sort failure'); |
||
423 | } |
||
424 | |||
425 | 2 | return new self($values); |
|
426 | } |
||
427 | |||
428 | /** |
||
429 | * {@inheritdoc} |
||
430 | */ |
||
431 | 2 | View Code Duplication | public function associativeSort($flags = self::SORT_REGULAR) |
432 | { |
||
433 | 2 | $values = $this->values; |
|
434 | 2 | $bool = asort($values, (int) $flags); |
|
435 | |||
436 | 2 | if ($bool === false) { |
|
437 | throw new SortException('Sort failure'); |
||
438 | } |
||
439 | |||
440 | 2 | return new self($values); |
|
441 | } |
||
442 | |||
443 | /** |
||
444 | * {@inheritdoc} |
||
445 | */ |
||
446 | 2 | View Code Duplication | public function keySort($flags = self::SORT_REGULAR) |
447 | { |
||
448 | 2 | $values = $this->values; |
|
449 | 2 | $bool = ksort($values, (int) $flags); |
|
450 | |||
451 | 2 | if ($bool === false) { |
|
452 | throw new SortException('Sort failure'); |
||
453 | } |
||
454 | |||
455 | 2 | return new self($values); |
|
456 | } |
||
457 | |||
458 | /** |
||
459 | * {@inheritdoc} |
||
460 | */ |
||
461 | 2 | View Code Duplication | public function ukeySort(callable $sorter) |
462 | { |
||
463 | 2 | $values = $this->values; |
|
464 | 2 | $bool = uksort($values, $sorter); |
|
465 | |||
466 | 2 | if ($bool === false) { |
|
467 | throw new SortException('Sort failure'); |
||
468 | } |
||
469 | |||
470 | 2 | return new self($values); |
|
471 | } |
||
472 | |||
473 | /** |
||
474 | * {@inheritdoc} |
||
475 | */ |
||
476 | 2 | View Code Duplication | public function reverseSort($flags = self::SORT_REGULAR) |
477 | { |
||
478 | 2 | $values = $this->values; |
|
479 | 2 | $bool = rsort($values, (int) $flags); |
|
480 | |||
481 | 2 | if ($bool === false) { |
|
482 | throw new SortException('Sort failure'); |
||
483 | } |
||
484 | |||
485 | 2 | return new self($values); |
|
486 | } |
||
487 | |||
488 | /** |
||
489 | * {@inheritdoc} |
||
490 | */ |
||
491 | 2 | View Code Duplication | public function usort(callable $sorter) |
492 | { |
||
493 | 2 | $values = $this->values; |
|
494 | 2 | $bool = usort($values, $sorter); |
|
495 | |||
496 | 2 | if ($bool === false) { |
|
497 | throw new SortException('Sort failure'); |
||
498 | } |
||
499 | |||
500 | 2 | return new self($values); |
|
501 | } |
||
502 | |||
503 | /** |
||
504 | * {@inheritdoc} |
||
505 | */ |
||
506 | 2 | View Code Duplication | public function associativeReverseSort($flags = self::SORT_REGULAR) |
507 | { |
||
508 | 2 | $values = $this->values; |
|
509 | 2 | $bool = arsort($values, (int) $flags); |
|
510 | |||
511 | 2 | if ($bool === false) { |
|
512 | throw new SortException('Sort failure'); |
||
513 | } |
||
514 | |||
515 | 2 | return new self($values); |
|
516 | } |
||
517 | |||
518 | /** |
||
519 | * {@inheritdoc} |
||
520 | */ |
||
521 | 2 | View Code Duplication | public function keyReverseSort($flags = self::SORT_REGULAR) |
522 | { |
||
523 | 2 | $values = $this->values; |
|
524 | 2 | $bool = krsort($values, (int) $flags); |
|
525 | |||
526 | 2 | if ($bool === false) { |
|
527 | throw new SortException('Sort failure'); |
||
528 | } |
||
529 | |||
530 | 2 | return new self($values); |
|
531 | } |
||
532 | |||
533 | /** |
||
534 | * {@inheritdoc} |
||
535 | */ |
||
536 | 2 | View Code Duplication | public function uassociativeSort(callable $sorter) |
537 | { |
||
538 | 2 | $values = $this->values; |
|
539 | 2 | $bool = uasort($values, $sorter); |
|
540 | |||
541 | 2 | if ($bool === false) { |
|
542 | throw new SortException('Sort failure'); |
||
543 | } |
||
544 | |||
545 | 2 | return new self($values); |
|
546 | } |
||
547 | |||
548 | /** |
||
549 | * {@inheritdoc} |
||
550 | */ |
||
551 | 2 | View Code Duplication | public function naturalSort() |
552 | { |
||
553 | 2 | $values = $this->values; |
|
554 | 2 | $bool = natsort($values); |
|
555 | |||
556 | 2 | if ($bool === false) { |
|
557 | throw new SortException('Sort failure'); |
||
558 | } |
||
559 | |||
560 | 2 | return new self($values); |
|
561 | } |
||
562 | |||
563 | /** |
||
564 | * {@inheritdoc} |
||
565 | */ |
||
566 | 2 | View Code Duplication | public function first() |
567 | { |
||
568 | 2 | if ($this->count() === 0) { |
|
569 | 1 | throw new OutOfBoundException('There is no first item'); |
|
570 | } |
||
571 | |||
572 | 1 | return array_values($this->values)[0]; |
|
573 | } |
||
574 | |||
575 | /** |
||
576 | * {@inheritdoc} |
||
577 | */ |
||
578 | 2 | View Code Duplication | public function last() |
579 | { |
||
580 | 2 | if ($this->count() === 0) { |
|
581 | 1 | throw new OutOfBoundException('There is no last item'); |
|
582 | } |
||
583 | |||
584 | 1 | $values = array_values($this->values); |
|
585 | |||
586 | 1 | return end($values); |
|
587 | } |
||
588 | |||
589 | /** |
||
590 | * {@inheritdoc} |
||
591 | */ |
||
592 | 1 | public function each(callable $callback) |
|
593 | { |
||
594 | 1 | foreach ($this->values as $key => $value) { |
|
595 | 1 | $callback($key, $value); |
|
596 | 1 | } |
|
597 | |||
598 | 1 | return $this; |
|
599 | } |
||
600 | |||
601 | /** |
||
602 | * {@inheritdoc} |
||
603 | */ |
||
604 | 1 | public function join($separator) |
|
608 | |||
609 | /** |
||
610 | * {@inheritdoc} |
||
611 | */ |
||
612 | 2 | public function shuffle() |
|
613 | { |
||
614 | 2 | $values = $this->values; |
|
615 | 2 | $result = shuffle($values); |
|
616 | |||
617 | 2 | if ($result === false) { |
|
618 | throw new RuntimeException('Shuffle operation failed'); |
||
619 | } |
||
620 | |||
621 | 2 | return new self($values); |
|
622 | } |
||
623 | |||
624 | /** |
||
625 | * {@inheritdoc} |
||
626 | */ |
||
627 | 2 | public function take($size, $preserveKeys = false) |
|
628 | { |
||
629 | 2 | $took = []; |
|
630 | 2 | $keys = array_keys($this->values); |
|
631 | 2 | $size = (int) $size; |
|
632 | |||
633 | 2 | while (count($took) < $size) { |
|
634 | do { |
||
635 | 2 | $random = mt_rand(0, count($keys) - 1); |
|
636 | 2 | } while (!isset($keys[$random])); |
|
637 | 2 | $key = $keys[$random]; |
|
638 | 2 | $took[$key] = $this->values[$key]; |
|
639 | 2 | unset($keys[$random]); |
|
640 | 2 | } |
|
641 | |||
642 | 2 | if ($preserveKeys === false) { |
|
643 | 2 | $took = array_values($took); |
|
644 | 2 | } |
|
645 | |||
648 | |||
649 | /** |
||
650 | * {@inheritdoc} |
||
651 | */ |
||
652 | 2 | public function grep($pattern, $revert = false) |
|
660 | |||
661 | /** |
||
662 | * {@inheritdoc} |
||
663 | */ |
||
664 | 2 | public function set($key, $value) |
|
671 | |||
672 | /** |
||
673 | * {@inheritdoc} |
||
674 | */ |
||
675 | 2 | public function contains($value) |
|
679 | |||
680 | /** |
||
681 | * {@inheritdoc} |
||
682 | */ |
||
683 | 15 | public function count() |
|
687 | |||
688 | /** |
||
689 | * {@inheritdoc} |
||
690 | */ |
||
691 | 5 | public function current() |
|
695 | |||
696 | /** |
||
697 | * {@inheritdoc} |
||
698 | */ |
||
699 | 5 | public function key() |
|
703 | |||
704 | /** |
||
705 | * {@inheritdoc} |
||
706 | */ |
||
707 | 5 | public function next() |
|
711 | |||
712 | /** |
||
713 | * {@inheritdoc} |
||
714 | */ |
||
715 | 5 | public function rewind() |
|
719 | |||
720 | /** |
||
721 | * {@inheritdoc} |
||
722 | */ |
||
723 | 5 | public function valid() |
|
727 | |||
728 | /** |
||
729 | * {@inheritdoc} |
||
730 | */ |
||
731 | 1 | public function offsetExists($offset) |
|
735 | |||
736 | /** |
||
737 | * {@inheritdoc} |
||
738 | */ |
||
739 | 39 | public function offsetGet($offset) |
|
750 | |||
751 | /** |
||
752 | * {@inheritdoc} |
||
753 | */ |
||
754 | 1 | public function offsetSet($offset, $value) |
|
758 | |||
759 | /** |
||
760 | * {@inheritdoc} |
||
761 | */ |
||
762 | 1 | public function offsetUnset($offset) |
|
766 | } |
||
767 |
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.