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 |
||
17 | final class Vector extends Collection |
||
18 | { |
||
19 | 273 | public function __construct(array $values = []) |
|
26 | |||
27 | 3 | View Code Duplication | public function __clone() |
41 | |||
42 | 81 | public function get(int $index) |
|
50 | |||
51 | 66 | View Code Duplication | public function search($value): int |
61 | |||
62 | 63 | public function equals($other): bool |
|
80 | |||
81 | 9 | public function add($value): self |
|
89 | |||
90 | 12 | View Code Duplication | public function replace($searchValue, $replacementValue): self |
99 | |||
100 | 12 | View Code Duplication | public function replaceAll($searchValue, $replacementValue): self |
115 | |||
116 | 12 | View Code Duplication | public function remove($value): self |
125 | |||
126 | 12 | View Code Duplication | public function removeAll($value): self |
141 | |||
142 | 6 | public function merge(self $other): self |
|
148 | |||
149 | 6 | View Code Duplication | public function intersect(self $other): self |
161 | |||
162 | 6 | View Code Duplication | public function diff(self $other): self |
174 | |||
175 | /** |
||
176 | * The filter callable is given an item, and should return |
||
177 | * a boolean indicating whether the item remains or not. |
||
178 | * |
||
179 | * function ($item): bool { |
||
180 | * return true; |
||
181 | * } |
||
182 | */ |
||
183 | 6 | public function filter(callable $filter): self |
|
189 | |||
190 | /** |
||
191 | * The mapper callable is given an item, and should return |
||
192 | * a new value to use in it's place. |
||
193 | * |
||
194 | * function ($item) { |
||
195 | * return $item; |
||
196 | * } |
||
197 | */ |
||
198 | 6 | public function map(callable $mapper): self |
|
204 | |||
205 | 81 | private function containsIndex(int $index): bool |
|
209 | } |
||
210 |
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.