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 |
||
13 | class Map implements MapInterface |
||
14 | { |
||
15 | use Type; |
||
16 | |||
17 | private $keyType; |
||
18 | private $valueType; |
||
19 | private $keySpec; |
||
20 | private $valueSpec; |
||
21 | private $keys; |
||
22 | private $values; |
||
23 | private $pairs; |
||
24 | |||
25 | 36 | public function __construct(string $keyType, string $valueType) |
|
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | 14 | public function keyType(): StringPrimitive |
|
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | 13 | public function valueType(): StringPrimitive |
|
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | 11 | public function size(): int |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function count(): int |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 1 | public function current() |
|
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 1 | public function key() |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 1 | public function next() |
|
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | 1 | public function rewind() |
|
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | 1 | public function valid() |
|
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | 1 | public function offsetExists($offset): bool |
|
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | 2 | public function offsetGet($offset) |
|
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | 1 | public function offsetSet($offset, $value) |
|
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | 1 | public function offsetUnset($offset) |
|
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | 30 | public function put($key, $value): MapInterface |
|
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | 12 | public function get($key) |
|
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | 10 | public function contains($key): bool |
|
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | 1 | View Code Duplication | public function drop(int $size): MapInterface |
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | 1 | View Code Duplication | public function dropEnd(int $size): MapInterface |
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | 4 | public function clear(): MapInterface |
|
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | 2 | public function equals(MapInterface $map): bool |
|
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | */ |
||
249 | 1 | public function filter(\Closure $predicate): MapInterface |
|
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | public function foreach(\Closure $function): MapInterface |
||
270 | { |
||
271 | 2 | foreach ($this->keys as $index => $key) { |
|
272 | 2 | $function($key, $this->values->get($index)); |
|
273 | } |
||
274 | |||
275 | 2 | return $this; |
|
276 | } |
||
277 | |||
278 | /** |
||
279 | * {@inheritdoc} |
||
280 | */ |
||
281 | 2 | public function groupBy(\Closure $discriminator): MapInterface |
|
282 | { |
||
283 | 2 | if ($this->size() === 0) { |
|
284 | 1 | throw new GroupEmptyMapException; |
|
285 | } |
||
286 | |||
287 | 1 | $map = null; |
|
288 | |||
289 | 1 | foreach ($this->keys as $index => $key) { |
|
290 | 1 | $newKey = $discriminator($key, $this->values->get($index)); |
|
291 | |||
292 | 1 | View Code Duplication | if ($map === null) { |
293 | 1 | $type = gettype($newKey); |
|
294 | 1 | $map = new self( |
|
295 | 1 | $type === 'object' ? get_class($newKey) : $type, |
|
296 | 1 | SequenceInterface::class |
|
297 | ); |
||
298 | } |
||
299 | |||
300 | 1 | $pair = $this->pairs->get($index); |
|
301 | |||
302 | 1 | if ($map->contains($newKey)) { |
|
303 | 1 | $map = $map->put( |
|
304 | $newKey, |
||
305 | 1 | $map->get($newKey)->add($pair) |
|
306 | ); |
||
307 | } else { |
||
308 | 1 | $map = $map->put($newKey, new Sequence($pair)); |
|
309 | } |
||
310 | } |
||
311 | |||
312 | 1 | return $map; |
|
313 | } |
||
314 | |||
315 | /** |
||
316 | * {@inheritdoc} |
||
317 | */ |
||
318 | 1 | public function first(): Pair |
|
322 | |||
323 | /** |
||
324 | * {@inheritdoc} |
||
325 | */ |
||
326 | 1 | public function last(): Pair |
|
330 | |||
331 | /** |
||
332 | * {@inheritdoc} |
||
333 | */ |
||
334 | 8 | public function keys(): SequenceInterface |
|
338 | |||
339 | /** |
||
340 | * {@inheritdoc} |
||
341 | */ |
||
342 | 6 | public function values(): SequenceInterface |
|
346 | |||
347 | /** |
||
348 | * {@inheritdoc} |
||
349 | */ |
||
350 | 1 | public function map(\Closure $function): MapInterface |
|
351 | { |
||
352 | 1 | $map = $this->clear(); |
|
353 | |||
354 | 1 | View Code Duplication | foreach ($this->keys as $index => $key) { |
355 | 1 | $return = $function( |
|
356 | $key, |
||
357 | 1 | $this->values->get($index) |
|
358 | ); |
||
359 | |||
360 | 1 | if ($return instanceof Pair) { |
|
361 | 1 | $key = $return->key(); |
|
362 | 1 | $value = $return->value(); |
|
363 | } else { |
||
364 | 1 | $value = $return; |
|
365 | } |
||
366 | |||
367 | 1 | $map = $map->put($key, $value); |
|
368 | } |
||
369 | |||
370 | 1 | return $map; |
|
371 | } |
||
372 | |||
373 | /** |
||
374 | * {@inheritdoc} |
||
375 | */ |
||
376 | 1 | View Code Duplication | public function take(int $size): MapInterface |
385 | |||
386 | /** |
||
387 | * {@inheritdoc} |
||
388 | */ |
||
389 | 1 | View Code Duplication | public function takeEnd(int $size): MapInterface |
398 | |||
399 | /** |
||
400 | * {@inheritdoc} |
||
401 | */ |
||
402 | 1 | public function join(string $separator): StringPrimitive |
|
406 | |||
407 | /** |
||
408 | * {@inheritdoc} |
||
409 | */ |
||
410 | 1 | public function remove($key): MapInterface |
|
433 | |||
434 | /** |
||
435 | * {@inheritdoc} |
||
436 | */ |
||
437 | 2 | public function merge(MapInterface $map): MapInterface |
|
456 | |||
457 | /** |
||
458 | * {@inheritdoc} |
||
459 | */ |
||
460 | 1 | public function partition(\Closure $predicate): MapInterface |
|
482 | } |
||
483 |
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.