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 |
||
| 15 | final class Map implements \IteratorAggregate, \ArrayAccess, Collection |
||
| 16 | { |
||
| 17 | use Traits\GenericCollection; |
||
| 18 | use Traits\SquaredCapacity; |
||
| 19 | |||
| 20 | const MIN_CAPACITY = 8; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var array internal array to store pairs |
||
| 24 | */ |
||
| 25 | private $pairs = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Creates a new instance. |
||
| 29 | * |
||
| 30 | * @param array|\Traversable|null $values |
||
| 31 | */ |
||
| 32 | public function __construct($values = null) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Updates all values by applying a callback function to each value. |
||
| 41 | * |
||
| 42 | * @param callable $callback Accepts two arguments: key and value, should |
||
| 43 | * return what the updated value will be. |
||
| 44 | */ |
||
| 45 | public function apply(callable $callback) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @inheritDoc |
||
| 54 | */ |
||
| 55 | public function clear() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Return the first Pair from the Map |
||
| 63 | * |
||
| 64 | * @return Pair |
||
| 65 | * |
||
| 66 | * @throws UnderflowException |
||
| 67 | */ |
||
| 68 | public function first(): Pair |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Return the last Pair from the Map |
||
| 79 | * |
||
| 80 | * @return Pair |
||
| 81 | * |
||
| 82 | * @throws UnderflowException |
||
| 83 | */ |
||
| 84 | public function last(): Pair |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Return the pair at a specified position in the Map |
||
| 95 | * |
||
| 96 | * @param int $position |
||
| 97 | * |
||
| 98 | * @return Pair |
||
| 99 | * |
||
| 100 | * @throws OutOfRangeException |
||
| 101 | */ |
||
| 102 | public function skip(int $position): Pair |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the result of associating all keys of a given traversable object |
||
| 113 | * or array with their corresponding values, as well as those of this map. |
||
| 114 | * |
||
| 115 | * @param array|\Traversable $values |
||
| 116 | * |
||
| 117 | * @return Map |
||
| 118 | */ |
||
| 119 | public function merge($values): Map |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Creates a new map containing the pairs of the current instance whose keys |
||
| 128 | * are also present in the given map. In other words, returns a copy of the |
||
| 129 | * current map with all keys removed that are not also in the other map. |
||
| 130 | * |
||
| 131 | * @param Map $map The other map. |
||
| 132 | * |
||
| 133 | * @return Map A new map containing the pairs of the current instance |
||
| 134 | * whose keys are also present in the given map. In other |
||
| 135 | * words, returns a copy of the current map with all keys |
||
| 136 | * removed that are not also in the other map. |
||
| 137 | */ |
||
| 138 | public function intersect(Map $map): Map |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns the result of removing all keys from the current instance that |
||
| 147 | * are present in a given map. |
||
| 148 | * |
||
| 149 | * @param Map $map The map containing the keys to exclude. |
||
| 150 | * |
||
| 151 | * @return Map The result of removing all keys from the current instance |
||
| 152 | * that are present in a given map. |
||
| 153 | */ |
||
| 154 | public function diff(Map $map): Map |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Determines whether two keys are equal. |
||
| 163 | * |
||
| 164 | * @param mixed $a |
||
| 165 | * @param mixed $b |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | private function keysAreEqual($a, $b): bool |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Attempts to look up a key in the table. |
||
| 180 | * |
||
| 181 | * @param $key |
||
| 182 | * |
||
| 183 | * @return Pair|null |
||
| 184 | */ |
||
| 185 | private function lookupKey($key) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Attempts to look up a key in the table. |
||
| 196 | * |
||
| 197 | * @param $value |
||
| 198 | * |
||
| 199 | * @return Pair|null |
||
| 200 | */ |
||
| 201 | private function lookupValue($value) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns whether an association a given key exists. |
||
| 212 | * |
||
| 213 | * @param mixed $key |
||
| 214 | * |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | public function hasKey($key): bool |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns whether an association for a given value exists. |
||
| 224 | * |
||
| 225 | * @param mixed $value |
||
| 226 | * |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | public function hasValue($value): bool |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @inheritDoc |
||
| 236 | */ |
||
| 237 | public function count(): int |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Returns a new map containing only the values for which a predicate |
||
| 244 | * returns true. A boolean test will be used if a predicate is not provided. |
||
| 245 | * |
||
| 246 | * @param callable|null $callback Accepts a key and a value, and returns: |
||
| 247 | * true : include the value, |
||
| 248 | * false: skip the value. |
||
| 249 | * |
||
| 250 | * @return Map |
||
| 251 | */ |
||
| 252 | public function filter(callable $callback = null): Map |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Returns the value associated with a key, or an optional default if the |
||
| 267 | * key is not associated with a value. |
||
| 268 | * |
||
| 269 | * @param mixed $key |
||
| 270 | * @param mixed $default |
||
| 271 | * |
||
| 272 | * @return mixed The associated value or fallback default if provided. |
||
| 273 | * |
||
| 274 | * @throws OutOfBoundsException if no default was provided and the key is |
||
| 275 | * not associated with a value. |
||
| 276 | */ |
||
| 277 | public function get($key, $default = null) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns a set of all the keys in the map. |
||
| 293 | * |
||
| 294 | * @return Set |
||
| 295 | */ |
||
| 296 | public function keys(): Set |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns a new map using the results of applying a callback to each value. |
||
| 307 | * |
||
| 308 | * The keys will be equal in both maps. |
||
| 309 | * |
||
| 310 | * @param callable $callback Accepts two arguments: key and value, should |
||
| 311 | * return what the updated value will be. |
||
| 312 | * |
||
| 313 | * @return Map |
||
| 314 | */ |
||
| 315 | public function map(callable $callback): Map |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Returns a sequence of pairs representing all associations. |
||
| 326 | * |
||
| 327 | * @return Sequence |
||
| 328 | */ |
||
| 329 | public function pairs(): Sequence |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Associates a key with a value, replacing a previous association if there |
||
| 340 | * was one. |
||
| 341 | * |
||
| 342 | * @param mixed $key |
||
| 343 | * @param mixed $value |
||
| 344 | */ |
||
| 345 | public function put($key, $value) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Creates associations for all keys and corresponding values of either an |
||
| 360 | * array or iterable object. |
||
| 361 | * |
||
| 362 | * @param \Traversable|array $values |
||
| 363 | */ |
||
| 364 | public function putAll($values) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Iteratively reduces the map to a single value using a callback. |
||
| 373 | * |
||
| 374 | * @param callable $callback Accepts the carry, key, and value, and |
||
| 375 | * returns an updated carry value. |
||
| 376 | * |
||
| 377 | * @param mixed|null $initial Optional initial carry value. |
||
| 378 | * |
||
| 379 | * @return mixed The carry value of the final iteration, or the initial |
||
| 380 | * value if the map was empty. |
||
| 381 | */ |
||
| 382 | public function reduce(callable $callback, $initial = null) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Completely removes a pair from the internal array by position. It is |
||
| 395 | * important to remove it from the array and not just use 'unset'. |
||
| 396 | */ |
||
| 397 | private function delete(int $position) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Removes a key's association from the map and returns the associated value |
||
| 410 | * or a provided default if provided. |
||
| 411 | * |
||
| 412 | * @param mixed $key |
||
| 413 | * @param mixed $default |
||
| 414 | * |
||
| 415 | * @return mixed The associated value or fallback default if provided. |
||
| 416 | * |
||
| 417 | * @throws \OutOfBoundsException if no default was provided and the key is |
||
| 418 | * not associated with a value. |
||
| 419 | */ |
||
| 420 | public function remove($key, $default = null) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Returns a reversed copy of the map. |
||
| 438 | * |
||
| 439 | * @return Map |
||
|
|
|||
| 440 | */ |
||
| 441 | public function reverse() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Returns a reversed copy of the map. |
||
| 448 | * |
||
| 449 | * @return Map |
||
| 450 | */ |
||
| 451 | public function reversed(): Map |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Returns a sub-sequence of a given length starting at a specified offset. |
||
| 461 | * |
||
| 462 | * @param int $offset If the offset is non-negative, the map will |
||
| 463 | * start at that offset in the map. If offset is |
||
| 464 | * negative, the map will start that far from the |
||
| 465 | * end. |
||
| 466 | * |
||
| 467 | * @param int|null $length If a length is given and is positive, the |
||
| 468 | * resulting set will have up to that many pairs in |
||
| 469 | * it. If the requested length results in an |
||
| 470 | * overflow, only pairs up to the end of the map |
||
| 471 | * will be included. |
||
| 472 | * |
||
| 473 | * If a length is given and is negative, the map |
||
| 474 | * will stop that many pairs from the end. |
||
| 475 | * |
||
| 476 | * If a length is not provided, the resulting map |
||
| 477 | * will contains all pairs between the offset and |
||
| 478 | * the end of the map. |
||
| 479 | * |
||
| 480 | * @return Map |
||
| 481 | */ |
||
| 482 | public function slice(int $offset, int $length = null): Map |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Sorts the map in-place, based on an optional callable comparator. |
||
| 501 | * |
||
| 502 | * The map will be sorted by value. |
||
| 503 | * |
||
| 504 | * @param callable|null $comparator Accepts two values to be compared. |
||
| 505 | */ |
||
| 506 | View Code Duplication | public function sort(callable $comparator = null) |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Returns a sorted copy of the map, based on an optional callable |
||
| 522 | * comparator. The map will be sorted by value. |
||
| 523 | * |
||
| 524 | * @param callable|null $comparator Accepts two values to be compared. |
||
| 525 | * |
||
| 526 | * @return Map |
||
| 527 | */ |
||
| 528 | public function sorted(callable $comparator = null): Map |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Sorts the map in-place, based on an optional callable comparator. |
||
| 537 | * |
||
| 538 | * The map will be sorted by key. |
||
| 539 | * |
||
| 540 | * @param callable|null $comparator Accepts two keys to be compared. |
||
| 541 | */ |
||
| 542 | View Code Duplication | public function ksort(callable $comparator = null) |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Returns a sorted copy of the map, based on an optional callable |
||
| 558 | * comparator. The map will be sorted by key. |
||
| 559 | * |
||
| 560 | * @param callable|null $comparator Accepts two keys to be compared. |
||
| 561 | * |
||
| 562 | * @return Map |
||
| 563 | */ |
||
| 564 | public function ksorted(callable $comparator = null): Map |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Returns the sum of all values in the map. |
||
| 573 | * |
||
| 574 | * @return int|float The sum of all the values in the map. |
||
| 575 | */ |
||
| 576 | public function sum() |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @inheritDoc |
||
| 583 | */ |
||
| 584 | public function toArray(): array |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Returns a sequence of all the associated values in the Map. |
||
| 597 | * |
||
| 598 | * @return Sequence |
||
| 599 | */ |
||
| 600 | public function values(): Sequence |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Creates a new map that contains the pairs of the current instance as well |
||
| 611 | * as the pairs of another map. |
||
| 612 | * |
||
| 613 | * @param Map $map The other map, to combine with the current instance. |
||
| 614 | * |
||
| 615 | * @return Map A new map containing all the pairs of the current |
||
| 616 | * instance as well as another map. |
||
| 617 | */ |
||
| 618 | public function union(Map $map): Map |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Creates a new map using keys of either the current instance or of another |
||
| 625 | * map, but not of both. |
||
| 626 | * |
||
| 627 | * @param Map $map |
||
| 628 | * |
||
| 629 | * @return Map A new map containing keys in the current instance as well |
||
| 630 | * as another map, but not in both. |
||
| 631 | */ |
||
| 632 | public function xor(Map $map): Map |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @inheritDoc |
||
| 641 | */ |
||
| 642 | public function getIterator() |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Returns a representation to be used for var_dump and print_r. |
||
| 651 | */ |
||
| 652 | public function __debugInfo() |
||
| 656 | |||
| 657 | /** |
||
| 658 | * @inheritdoc |
||
| 659 | */ |
||
| 660 | public function offsetSet($offset, $value) |
||
| 664 | |||
| 665 | /** |
||
| 666 | * @inheritdoc |
||
| 667 | * |
||
| 668 | * @throws OutOfBoundsException |
||
| 669 | */ |
||
| 670 | public function &offsetGet($offset) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * @inheritdoc |
||
| 683 | */ |
||
| 684 | public function offsetUnset($offset) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * @inheritdoc |
||
| 691 | */ |
||
| 692 | public function offsetExists($offset) |
||
| 696 | } |
||
| 697 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.