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 EquatableVector 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 EquatableVector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | final class EquatableVector implements Equatable, Countable, IteratorAggregate |
||
21 | { |
||
22 | /** |
||
23 | * @var Equatable[] |
||
24 | */ |
||
25 | private $items = []; |
||
26 | |||
27 | /** |
||
28 | * @param Equatable[] $items |
||
29 | */ |
||
30 | 126 | View Code Duplication | public function __construct(array $items = []) |
40 | |||
41 | 3 | public function __clone() |
|
51 | |||
52 | 6 | public function add(Equatable $value): EquatableVector |
|
60 | |||
61 | 12 | View Code Duplication | public function remove(Equatable $value): EquatableVector |
70 | |||
71 | 9 | public function replace(int $index, Equatable $value): EquatableVector |
|
83 | |||
84 | 42 | public function get(int $index): Equatable |
|
92 | |||
93 | 39 | View Code Duplication | public function search(Equatable $value): int |
103 | |||
104 | 6 | View Code Duplication | public function searchAll(Equatable $value): array |
120 | |||
121 | 18 | public function contains(Equatable $value): bool |
|
130 | |||
131 | 48 | public function containsIndex(int $index): bool |
|
135 | |||
136 | 21 | View Code Duplication | public function equals($other): bool |
154 | |||
155 | 57 | public function count(): int |
|
159 | |||
160 | 15 | View Code Duplication | public function countItem(Equatable $value): int |
172 | |||
173 | 3 | View Code Duplication | public function intersect(self $other): self |
185 | |||
186 | 3 | View Code Duplication | public function diff(self $other): self |
198 | |||
199 | /** |
||
200 | * The filter callable is given an equatable item, and should return |
||
201 | * a boolean indicating whether the item remains or not. |
||
202 | * |
||
203 | * function (Equatable $item): bool { |
||
204 | * return true; |
||
205 | * } |
||
206 | */ |
||
207 | 3 | public function filter(callable $filter): self |
|
213 | |||
214 | /** |
||
215 | * The mapper callable is given an equatable item, and should return |
||
216 | * a new value to use in it's place. |
||
217 | * |
||
218 | * function (Equatable $item) { |
||
219 | * return $item; |
||
220 | * } |
||
221 | */ |
||
222 | 3 | public function map(callable $mapper): self |
|
228 | |||
229 | /** |
||
230 | * The reducer callable is given the carry value and an equatable item, |
||
231 | * and should return the value it should be reduced to. |
||
232 | * |
||
233 | * function ($carry, Equatable $item) { |
||
234 | * return $carry + 1; |
||
235 | * } |
||
236 | */ |
||
237 | 3 | public function reduce(callable $reducer, $initial = null) |
|
241 | |||
242 | 12 | public function getIterator(): Traversable |
|
246 | } |
||
247 |
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.