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:
| 1 | <?php |
||
| 18 | class HashMap extends AbstractMap |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * The mapping keys |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $keys = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The mapping values |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $values = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Constructor |
||
| 36 | * |
||
| 37 | * @param array $map |
||
| 38 | */ |
||
| 39 | 12 | public function __construct(array $map = []) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Removes all mappings from map |
||
| 50 | * |
||
| 51 | * @return void |
||
| 52 | */ |
||
| 53 | 1 | public function clear() |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Returns true if the key exists in the lookup table |
||
| 61 | * |
||
| 62 | * @param mixed $key |
||
| 63 | * @return bool |
||
| 64 | */ |
||
| 65 | 29 | public function containsKey($key): bool |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Returns true if the value exists in the map |
||
| 72 | * |
||
| 73 | * @param mixed $value |
||
| 74 | * @return bool |
||
| 75 | */ |
||
| 76 | 2 | public function containsValue($value): bool |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Return a set representation of map |
||
| 83 | * |
||
| 84 | * If a set is passed in, that set will be populated, otherwise |
||
| 85 | * a default set will be used. |
||
| 86 | * |
||
| 87 | * @param SetInterface $set |
||
| 88 | * @return SetInterface |
||
| 89 | */ |
||
| 90 | 9 | public function entrySet(SetInterface $set = null): SetInterface |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Get the value at the specified key |
||
| 102 | * |
||
| 103 | * @param mixed $key |
||
| 104 | * @return mixed |
||
| 105 | * @throws \OutOfRangeException if the key doesn't exist |
||
| 106 | */ |
||
| 107 | 7 | public function get($key) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Returns true if the map is empty |
||
| 119 | * |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | 2 | public function isEmpty(): bool |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Returns a set of they keys in the map |
||
| 129 | * |
||
| 130 | * If a set is passed in, that set will be populated, otherwise |
||
| 131 | * a default set will be used. |
||
| 132 | * |
||
| 133 | * @param SetInterface $set |
||
| 134 | * @return SetInterface |
||
| 135 | */ |
||
| 136 | 2 | public function keySet(SetInterface $set = null): SetInterface |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Returns the previous value or null if there was no value |
||
| 149 | * |
||
| 150 | * @param mixed $key |
||
| 151 | * @param mixed $value |
||
| 152 | * @return mixed |
||
| 153 | */ |
||
| 154 | 28 | public function put($key, $value) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Remove the mapping for the key and returns the previous value |
||
| 171 | * or null |
||
| 172 | * |
||
| 173 | * @param mixed $key |
||
| 174 | * @return mixed |
||
| 175 | */ |
||
| 176 | 4 | public function remove($key) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Returns the number of mappings in the map |
||
| 191 | * |
||
| 192 | * @return int |
||
| 193 | */ |
||
| 194 | 5 | public function count(): int |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the keys as a collection |
||
| 201 | * |
||
| 202 | * @param CollectionInterface $collection |
||
| 203 | * @return CollectionInterface |
||
| 204 | */ |
||
| 205 | 13 | View Code Duplication | public function keys(CollectionInterface $collection = null): CollectionInterface |
| 215 | |||
| 216 | /** |
||
| 217 | * Returns the values as a collection |
||
| 218 | * |
||
| 219 | * @param CollectionInterface $collection |
||
| 220 | * @return CollectionInterface |
||
| 221 | */ |
||
| 222 | 2 | View Code Duplication | public function values(CollectionInterface $collection = null): CollectionInterface |
| 232 | |||
| 233 | /** |
||
| 234 | * Filter the collection using closure |
||
| 235 | * |
||
| 236 | * The closure will get passed a [@see MapEntry]. Returning true from the |
||
| 237 | * closure will include that entry in the new map. |
||
| 238 | * |
||
| 239 | * @param callable $filter |
||
| 240 | * @return MapInterface |
||
| 241 | */ |
||
| 242 | 1 | public function filter(callable $filter): MapInterface |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Generate a hashcode for a php value |
||
| 258 | * |
||
| 259 | * @param mixed $value |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | 29 | protected function hashCode($value): string |
|
| 274 | } |
||
| 275 |