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:
1 | <?php |
||
12 | class GeneratorCollection extends \IteratorIterator implements CollectionInterface |
||
13 | { |
||
14 | |||
15 | use CallableUnifierTrait; |
||
16 | |||
17 | /** |
||
18 | * Builder for GeneratorCollection objects |
||
19 | * |
||
20 | * @param iterable $iterable |
||
21 | * |
||
22 | * @return self |
||
23 | */ |
||
24 | 21 | View Code Duplication | public static function from($iterable) |
42 | |||
43 | /** |
||
44 | * @inheritDoc |
||
45 | */ |
||
46 | 20 | public function toArray() |
|
50 | |||
51 | /** |
||
52 | * @inheritDoc |
||
53 | */ |
||
54 | 6 | public function values() |
|
58 | |||
59 | /** |
||
60 | * @return Collection |
||
61 | */ |
||
62 | 1 | public function persist($class = Collection::class) |
|
66 | |||
67 | /** |
||
68 | * @inheritDoc |
||
69 | */ |
||
70 | 6 | View Code Duplication | public function map(callable $callable) |
80 | |||
81 | /** |
||
82 | * @inheritDoc |
||
83 | */ |
||
84 | 5 | View Code Duplication | public function filter(callable $callable) |
96 | |||
97 | /** |
||
98 | * Alias for filter() |
||
99 | * |
||
100 | * @param callable $callable |
||
101 | * |
||
102 | * @return GeneratorCollection |
||
103 | */ |
||
104 | 3 | public function select(callable $callable) |
|
108 | |||
109 | /** |
||
110 | * @inheritDoc |
||
111 | */ |
||
112 | 4 | View Code Duplication | public function reject(callable $callable) |
124 | |||
125 | /** |
||
126 | * @inheritDoc |
||
127 | */ |
||
128 | public function merge(...$collections) |
||
139 | |||
140 | /** |
||
141 | * @inheritDoc |
||
142 | */ |
||
143 | 4 | View Code Duplication | public function reduce(callable $callable, $initial) |
154 | } |
||
155 |
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.