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 |
||
14 | final class Map implements \IteratorAggregate, \ArrayAccess, Collection |
||
15 | { |
||
16 | use Traits\Collection; |
||
17 | use Traits\SquaredCapacity; |
||
18 | |||
19 | const MIN_CAPACITY = 8; |
||
20 | |||
21 | /** |
||
22 | * @var Pair[] |
||
23 | */ |
||
24 | private $pairs; |
||
25 | |||
26 | /** |
||
27 | * Creates an instance using the values of an array or Traversable object. |
||
28 | * |
||
29 | * @param array|\Traversable|null $values |
||
30 | */ |
||
31 | public function __construct($values = null) |
||
40 | |||
41 | /** |
||
42 | * @inheritDoc |
||
43 | */ |
||
44 | public function clear() |
||
49 | |||
50 | /** |
||
51 | * Return the first Pair from the Map |
||
52 | * |
||
53 | * @return Pair |
||
54 | * |
||
55 | * @throws UnderflowException |
||
56 | */ |
||
57 | public function first(): Pair |
||
65 | |||
66 | /** |
||
67 | * Return the last Pair from the Map |
||
68 | * |
||
69 | * @return Pair |
||
70 | * |
||
71 | * @throws UnderflowException |
||
72 | */ |
||
73 | public function last(): Pair |
||
81 | |||
82 | /** |
||
83 | * Return the pair at a specified position in the Map |
||
84 | * |
||
85 | * @param int $position |
||
86 | * |
||
87 | * @return Pair |
||
88 | * |
||
89 | * @throws OutOfRangeException |
||
90 | */ |
||
91 | public function skip(int $position): Pair |
||
99 | |||
100 | /** |
||
101 | * Merge an array of values with the current Map |
||
102 | * |
||
103 | * @param array|\Traversable $values |
||
104 | * |
||
105 | * @return Map |
||
106 | */ |
||
107 | public function merge($values): Map |
||
114 | |||
115 | /** |
||
116 | * Intersect |
||
117 | * |
||
118 | * @param Map $map |
||
119 | * |
||
120 | * @return Map |
||
121 | */ |
||
122 | public function intersect(Map $map): Map |
||
128 | |||
129 | /** |
||
130 | * Diff |
||
131 | * |
||
132 | * @param Map $map |
||
133 | * |
||
134 | * @return Map |
||
135 | */ |
||
136 | public function diff(Map $map): Map |
||
142 | |||
143 | /** |
||
144 | * XOR |
||
145 | * |
||
146 | * @param Map $map |
||
147 | * |
||
148 | * @return Map |
||
149 | */ |
||
150 | public function xor(Map $map): Map |
||
156 | |||
157 | /** |
||
158 | * Identical |
||
159 | * |
||
160 | * @param mixed $a |
||
161 | * @param mixed $b |
||
162 | * |
||
163 | * @return bool |
||
164 | */ |
||
165 | private function keysAreEqual($a, $b): bool |
||
173 | |||
174 | /** |
||
175 | * @param $key |
||
176 | * |
||
177 | * @return Pair|null |
||
178 | */ |
||
179 | private function lookupKey($key) |
||
187 | |||
188 | /** |
||
189 | * @param $value |
||
190 | * |
||
191 | * @return Pair|null |
||
192 | */ |
||
193 | private function lookupValue($value) |
||
201 | |||
202 | /** |
||
203 | * |
||
204 | */ |
||
205 | View Code Duplication | private function contains(string $lookup, array $values): bool |
|
219 | |||
220 | /** |
||
221 | * Returns whether an association for all of zero or more keys exist. |
||
222 | * |
||
223 | * @param mixed ...$keys |
||
224 | * |
||
225 | * @return bool true if at least one value was provided and the map |
||
226 | * contains all given keys, false otherwise. |
||
227 | */ |
||
228 | public function hasKey(...$keys): bool |
||
232 | |||
233 | /** |
||
234 | * Returns whether an association for all of zero or more values exist. |
||
235 | * |
||
236 | * @param mixed ...$values |
||
237 | * |
||
238 | * @return bool true if at least one value was provided and the map |
||
239 | * contains all given values, false otherwise. |
||
240 | */ |
||
241 | public function hasValue(...$values): bool |
||
245 | |||
246 | /** |
||
247 | * @inheritDoc |
||
248 | */ |
||
249 | public function count(): int |
||
253 | |||
254 | /** |
||
255 | * Returns a new map containing only the values for which a predicate |
||
256 | * returns true. A boolean test will be used if a predicate is not provided. |
||
257 | * |
||
258 | * @param callable|null $predicate Accepts a key and a value, and returns: |
||
259 | * true : include the value, |
||
260 | * false: skip the value. |
||
261 | * |
||
262 | * @return Map |
||
263 | */ |
||
264 | View Code Duplication | public function filter(callable $predicate = null): Map |
|
276 | |||
277 | /** |
||
278 | * Returns the value associated with a key, or an optional default if the |
||
279 | * key is not associated with a value. |
||
280 | * |
||
281 | * @param mixed $key |
||
282 | * @param mixed $default |
||
283 | * |
||
284 | * @return mixed The associated value or fallback default if provided. |
||
285 | * |
||
286 | * @throws OutOfBoundsException if no default was provided and the key is |
||
287 | * not associated with a value. |
||
288 | */ |
||
289 | public function get($key, $default = null) |
||
301 | |||
302 | /** |
||
303 | * Returns a set of all the keys in the map. |
||
304 | * |
||
305 | * @return Set |
||
306 | */ |
||
307 | public function keys(): Set |
||
317 | |||
318 | /** |
||
319 | * Returns a new map using the results of applying a callback to each value. |
||
320 | * The keys will be keysAreEqual in both maps. |
||
321 | * |
||
322 | * @param callable $callback Accepts two arguments: key and value, should |
||
323 | * return what the updated value will be. |
||
324 | * |
||
325 | * @return Map |
||
326 | */ |
||
327 | public function map(callable $callback): Map |
||
337 | |||
338 | /** |
||
339 | * Returns a sequence of pairs representing all associations. |
||
340 | * |
||
341 | * @return Sequence |
||
342 | */ |
||
343 | public function pairs(): Sequence |
||
353 | |||
354 | /** |
||
355 | * Associates a key with a value, replacing a previous association if there |
||
356 | * was one. |
||
357 | * |
||
358 | * @param mixed $key |
||
359 | * @param mixed $value |
||
360 | */ |
||
361 | public function put($key, $value) |
||
373 | |||
374 | /** |
||
375 | * Creates associations for all keys and corresponding values of either an |
||
376 | * array or iterable object. |
||
377 | * |
||
378 | * @param array|\Traversable $values |
||
379 | */ |
||
380 | public function putAll($values) |
||
386 | |||
387 | /** |
||
388 | * Iteratively reduces the map to a single value using a callback. |
||
389 | * |
||
390 | * @param callable $callback Accepts the carry, key, and value, and |
||
391 | * returns an updated carry value. |
||
392 | * |
||
393 | * @param mixed|null $initial Optional initial carry value. |
||
394 | * |
||
395 | * @return mixed The carry value of the final iteration, or the initial |
||
396 | * value if the map was empty. |
||
397 | */ |
||
398 | public function reduce(callable $callback, $initial = null) |
||
408 | |||
409 | /** |
||
410 | * |
||
411 | */ |
||
412 | private function delete(int $position) |
||
422 | |||
423 | /** |
||
424 | * Removes a key's association from the map and returns the associated value |
||
425 | * or a provided default if provided. |
||
426 | * |
||
427 | * @param mixed $key |
||
428 | * @param mixed $default |
||
429 | * |
||
430 | * @return mixed The associated value or fallback default if provided. |
||
431 | * |
||
432 | * @throws \OutOfBoundsException if no default was provided and the key is |
||
433 | * not associated with a value. |
||
434 | */ |
||
435 | public function remove($key, $default = null) |
||
450 | |||
451 | /** |
||
452 | * Returns a reversed copy of the map. |
||
453 | */ |
||
454 | public function reverse(): Map |
||
461 | |||
462 | /** |
||
463 | * Returns a sub-sequence of a given length starting at a specified offset. |
||
464 | * |
||
465 | * @param int $offset If the offset is non-negative, the map will |
||
466 | * start at that offset in the map. If offset is |
||
467 | * negative, the map will start that far from the |
||
468 | * end. |
||
469 | * |
||
470 | * @param int|null $length If a length is given and is positive, the |
||
471 | * resulting set will have up to that many pairs in |
||
472 | * it. If the requested length results in an |
||
473 | * overflow, only pairs up to the end of the map |
||
474 | * will be included. |
||
475 | * |
||
476 | * If a length is given and is negative, the map |
||
477 | * will stop that many pairs from the end. |
||
478 | * |
||
479 | * If a length is not provided, the resulting map |
||
480 | * will contains all pairs between the offset and |
||
481 | * the end of the map. |
||
482 | * |
||
483 | * @return Map |
||
484 | */ |
||
485 | public function slice(int $offset, int $length = null): Map |
||
501 | |||
502 | /** |
||
503 | * Returns a sorted copy of the map, based on an optional callable |
||
504 | * comparator. The map will be sorted by value. |
||
505 | * |
||
506 | * @param callable|null $comparator Accepts two values to be compared. |
||
507 | * |
||
508 | * @return Map |
||
509 | */ |
||
510 | View Code Duplication | public function sort(callable $comparator = null): Map |
|
527 | |||
528 | /** |
||
529 | * Returns a sorted copy of the map, based on an optional callable |
||
530 | * comparator. The map will be sorted by key. |
||
531 | * |
||
532 | * @param callable|null $comparator Accepts two keys to be compared. |
||
533 | * |
||
534 | * @return Map |
||
535 | */ |
||
536 | View Code Duplication | public function ksort(callable $comparator = null): Map |
|
553 | |||
554 | /** |
||
555 | * @inheritDoc |
||
556 | */ |
||
557 | public function toArray(): array |
||
567 | |||
568 | /** |
||
569 | * Returns a sequence of all the associated values in the Map. |
||
570 | * |
||
571 | * @return Sequence |
||
572 | */ |
||
573 | public function values(): Sequence |
||
583 | |||
584 | /** |
||
585 | * Get iterator |
||
586 | */ |
||
587 | public function getIterator() |
||
593 | |||
594 | /** |
||
595 | * Debug Info |
||
596 | */ |
||
597 | public function __debugInfo() |
||
601 | |||
602 | /** |
||
603 | * @inheritdoc |
||
604 | */ |
||
605 | public function offsetSet($offset, $value) |
||
609 | |||
610 | /** |
||
611 | * @inheritdoc |
||
612 | * |
||
613 | * @throws OutOfBoundsException |
||
614 | */ |
||
615 | public function &offsetGet($offset) |
||
625 | |||
626 | /** |
||
627 | * @inheritdoc |
||
628 | */ |
||
629 | public function offsetUnset($offset) |
||
633 | |||
634 | /** |
||
635 | * @inheritdoc |
||
636 | */ |
||
637 | public function offsetExists($offset) |
||
641 | } |
||
642 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.