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 Map 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 Map, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | final class Map extends Collection |
||
20 | { |
||
21 | 276 | public function __construct(array $values = []) |
|
29 | |||
30 | 3 | View Code Duplication | public function __clone() |
44 | |||
45 | 132 | public function get(string $key) |
|
53 | |||
54 | 144 | public function containsKey(string $key): bool |
|
58 | |||
59 | 63 | View Code Duplication | public function search($value): string |
69 | |||
70 | 42 | public function equals($other): bool |
|
92 | |||
93 | 3 | public function values(): Vector |
|
99 | |||
100 | 3 | public function keys(): Vector |
|
106 | |||
107 | 12 | public function add(string $key, $value): self |
|
119 | |||
120 | 12 | View Code Duplication | public function replace($searchValue, $replacementValue): self |
129 | |||
130 | 12 | public function replaceAll($searchValue, $replacementValue): self |
|
145 | |||
146 | 9 | View Code Duplication | public function replaceKey(string $key, $replacementValue): self |
158 | |||
159 | 12 | View Code Duplication | public function remove($value): self |
168 | |||
169 | 12 | View Code Duplication | public function removeAll($value): self |
184 | |||
185 | 9 | View Code Duplication | public function removeKey(string $key): self |
197 | |||
198 | 9 | public function merge(self $other): self |
|
204 | |||
205 | 6 | View Code Duplication | public function intersect(self $other): self |
217 | |||
218 | 3 | View Code Duplication | public function intersectKeys(self $other): self |
230 | |||
231 | 6 | View Code Duplication | public function diff(self $other): self |
243 | |||
244 | 3 | View Code Duplication | public function diffKeys(self $other): self |
256 | |||
257 | /** |
||
258 | * The filter callable is given an item and it's key, and should |
||
259 | * return a boolean indicating whether the item remains or not. |
||
260 | * |
||
261 | * function ($item, $key): bool { |
||
262 | * return true; |
||
263 | * } |
||
264 | */ |
||
265 | 9 | public function filter(callable $filter): self |
|
271 | |||
272 | /** |
||
273 | * The mapper callable is given an item, and should return |
||
274 | * a new value to use in it's place. |
||
275 | * |
||
276 | * function ($item) { |
||
277 | * return $item; |
||
278 | * } |
||
279 | */ |
||
280 | 6 | public function map(callable $mapper): self |
|
286 | |||
287 | 255 | private function guardAgainstInvalidKey($key) |
|
293 | } |
||
294 |
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.