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 | * Should an integer be provided the Map will allocate the memory capacity |
||
| 30 | * to the size of $values. |
||
| 31 | * |
||
| 32 | * @param array|\Traversable|int|null $values |
||
| 33 | */ |
||
| 34 | public function __construct($values = null) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @inheritDoc |
||
| 46 | */ |
||
| 47 | public function clear() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Removes all Pairs from the Map |
||
| 55 | * |
||
| 56 | * @param mixed[] $keys |
||
| 57 | */ |
||
| 58 | public function removeAll($keys) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Return the first Pair from the Map |
||
| 67 | * |
||
| 68 | * @return Pair |
||
| 69 | * |
||
| 70 | * @throws UnderflowException |
||
| 71 | */ |
||
| 72 | public function first(): Pair |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Return the last Pair from the Map |
||
| 83 | * |
||
| 84 | * @return Pair |
||
| 85 | * |
||
| 86 | * @throws UnderflowException |
||
| 87 | */ |
||
| 88 | public function last(): Pair |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Return the pair at a specified position in the Map |
||
| 99 | * |
||
| 100 | * @param int $position |
||
| 101 | * |
||
| 102 | * @return Pair |
||
| 103 | * |
||
| 104 | * @throws OutOfRangeException |
||
| 105 | */ |
||
| 106 | public function skip(int $position): Pair |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Merge an array of values with the current Map |
||
| 117 | * |
||
| 118 | * @param array|\Traversable $values |
||
| 119 | * |
||
| 120 | * @return Map |
||
| 121 | */ |
||
| 122 | public function merge($values): Map |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Intersect |
||
| 132 | * |
||
| 133 | * @param Map $map |
||
| 134 | * |
||
| 135 | * @return Map |
||
| 136 | */ |
||
| 137 | public function intersect(Map $map): Map |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Diff |
||
| 146 | * |
||
| 147 | * @param Map $map |
||
| 148 | * |
||
| 149 | * @return Map |
||
| 150 | */ |
||
| 151 | public function diff(Map $map): Map |
||
| 157 | |||
| 158 | /** |
||
| 159 | * XOR |
||
| 160 | * |
||
| 161 | * @param Map $map |
||
| 162 | * |
||
| 163 | * @return Map |
||
| 164 | */ |
||
| 165 | public function xor(Map $map): Map |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Identical |
||
| 174 | * |
||
| 175 | * @param mixed $a |
||
| 176 | * @param mixed $b |
||
| 177 | * |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | private function keysAreEqual($a, $b): bool |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param $key |
||
| 191 | * |
||
| 192 | * @return Pair|null |
||
| 193 | */ |
||
| 194 | private function lookupKey($key) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param $value |
||
| 205 | * |
||
| 206 | * @return Pair|null |
||
| 207 | */ |
||
| 208 | private function lookupValue($value) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * |
||
| 219 | */ |
||
| 220 | View Code Duplication | private function contains(string $lookup, array $values): bool |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Returns whether an association for all of zero or more keys exist. |
||
| 237 | * |
||
| 238 | * @param mixed ...$keys |
||
| 239 | * |
||
| 240 | * @return bool true if at least one value was provided and the map |
||
| 241 | * contains all given keys, false otherwise. |
||
| 242 | */ |
||
| 243 | public function hasKey(...$keys): bool |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Returns whether an association for all of zero or more values exist. |
||
| 250 | * |
||
| 251 | * @param mixed ...$values |
||
| 252 | * |
||
| 253 | * @return bool true if at least one value was provided and the map |
||
| 254 | * contains all given values, false otherwise. |
||
| 255 | */ |
||
| 256 | public function hasValue(...$values): bool |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @inheritDoc |
||
| 263 | */ |
||
| 264 | public function count(): int |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Returns a new map containing only the values for which a predicate |
||
| 271 | * returns true. A boolean test will be used if a predicate is not provided. |
||
| 272 | * |
||
| 273 | * @param callable|null $predicate Accepts a key and a value, and returns: |
||
| 274 | * true : include the value, |
||
| 275 | * false: skip the value. |
||
| 276 | * |
||
| 277 | * @return Map |
||
| 278 | */ |
||
| 279 | View Code Duplication | public function filter(callable $predicate = null): Map |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Returns the value associated with a key, or an optional default if the |
||
| 294 | * key is not associated with a value. |
||
| 295 | * |
||
| 296 | * @param mixed $key |
||
| 297 | * @param mixed $default |
||
| 298 | * |
||
| 299 | * @return mixed The associated value or fallback default if provided. |
||
| 300 | * |
||
| 301 | * @throws OutOfBoundsException if no default was provided and the key is |
||
| 302 | * not associated with a value. |
||
| 303 | */ |
||
| 304 | public function get($key, $default = null) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns a set of all the keys in the map. |
||
| 319 | * |
||
| 320 | * @return Set |
||
| 321 | */ |
||
| 322 | public function keys(): Set |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Returns a new map using the results of applying a callback to each value. |
||
| 335 | * The keys will be keysAreEqual in both maps. |
||
| 336 | * |
||
| 337 | * @param callable $callback Accepts two arguments: key and value, should |
||
| 338 | * return what the updated value will be. |
||
| 339 | * |
||
| 340 | * @return Map |
||
| 341 | */ |
||
| 342 | public function map(callable $callback): Map |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Returns a sequence of pairs representing all associations. |
||
| 355 | * |
||
| 356 | * @return Sequence |
||
| 357 | */ |
||
| 358 | public function pairs(): Sequence |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Associates a key with a value, replacing a previous association if there |
||
| 371 | * was one. |
||
| 372 | * |
||
| 373 | * @param mixed $key |
||
| 374 | * @param mixed $value |
||
| 375 | */ |
||
| 376 | public function put($key, $value) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Creates associations for all keys and corresponding values of either an |
||
| 391 | * array or iterable object. |
||
| 392 | * |
||
| 393 | * @param array|\Traversable $values |
||
| 394 | */ |
||
| 395 | public function putAll($values) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Iteratively reduces the map to a single value using a callback. |
||
| 404 | * |
||
| 405 | * @param callable $callback Accepts the carry, key, and value, and |
||
| 406 | * returns an updated carry value. |
||
| 407 | * |
||
| 408 | * @param mixed|null $initial Optional initial carry value. |
||
| 409 | * |
||
| 410 | * @return mixed The carry value of the final iteration, or the initial |
||
| 411 | * value if the map was empty. |
||
| 412 | */ |
||
| 413 | public function reduce(callable $callback, $initial = null) |
||
| 423 | |||
| 424 | private function delete(int $position) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Removes a key's association from the map and returns the associated value |
||
| 437 | * or a provided default if provided. |
||
| 438 | * |
||
| 439 | * @param mixed $key |
||
| 440 | * @param mixed $default |
||
| 441 | * |
||
| 442 | * @return mixed The associated value or fallback default if provided. |
||
| 443 | * |
||
| 444 | * @throws \OutOfBoundsException if no default was provided and the key is |
||
| 445 | * not associated with a value. |
||
| 446 | */ |
||
| 447 | public function remove($key, $default = null) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Returns a reversed copy of the map. |
||
| 465 | */ |
||
| 466 | public function reverse(): Map |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Returns a sub-sequence of a given length starting at a specified offset. |
||
| 476 | * |
||
| 477 | * @param int $offset If the offset is non-negative, the map will |
||
| 478 | * start at that offset in the map. If offset is |
||
| 479 | * negative, the map will start that far from the |
||
| 480 | * end. |
||
| 481 | * |
||
| 482 | * @param int|null $length If a length is given and is positive, the |
||
| 483 | * resulting set will have up to that many pairs in |
||
| 484 | * it. If the requested length results in an |
||
| 485 | * overflow, only pairs up to the end of the map |
||
| 486 | * will be included. |
||
| 487 | * |
||
| 488 | * If a length is given and is negative, the map |
||
| 489 | * will stop that many pairs from the end. |
||
| 490 | * |
||
| 491 | * If a length is not provided, the resulting map |
||
| 492 | * will contains all pairs between the offset and |
||
| 493 | * the end of the map. |
||
| 494 | * |
||
| 495 | * @return Map |
||
| 496 | */ |
||
| 497 | public function slice(int $offset, int $length = null): Map |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Returns a sorted copy of the map, based on an optional callable |
||
| 516 | * comparator. The map will be sorted by value. |
||
| 517 | * |
||
| 518 | * @param callable|null $comparator Accepts two values to be compared. |
||
| 519 | * |
||
| 520 | * @return Map |
||
| 521 | */ |
||
| 522 | View Code Duplication | public function sort(callable $comparator = null): Map |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Returns a sorted copy of the map, based on an optional callable |
||
| 542 | * comparator. The map will be sorted by key. |
||
| 543 | * |
||
| 544 | * @param callable|null $comparator Accepts two keys to be compared. |
||
| 545 | * |
||
| 546 | * @return Map |
||
| 547 | */ |
||
| 548 | View Code Duplication | public function ksort(callable $comparator = null): Map |
|
| 565 | |||
| 566 | /** |
||
| 567 | * @inheritDoc |
||
| 568 | */ |
||
| 569 | public function toArray(): array |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Returns a sequence of all the associated values in the Map. |
||
| 582 | * |
||
| 583 | * @return Sequence |
||
| 584 | */ |
||
| 585 | public function values(): Sequence |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Get iterator |
||
| 598 | */ |
||
| 599 | public function getIterator() |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Debug Info |
||
| 608 | */ |
||
| 609 | public function __debugInfo() |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @inheritdoc |
||
| 616 | */ |
||
| 617 | public function offsetSet($offset, $value) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * @inheritdoc |
||
| 624 | * |
||
| 625 | * @throws OutOfBoundsException |
||
| 626 | */ |
||
| 627 | public function &offsetGet($offset) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @inheritdoc |
||
| 640 | */ |
||
| 641 | public function offsetUnset($offset) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @inheritdoc |
||
| 648 | */ |
||
| 649 | public function offsetExists($offset) |
||
| 653 | } |
||
| 654 |
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.