| Total Complexity | 41 |
| Total Lines | 394 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like HashMap 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.
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 HashMap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 64 | class HashMap extends BaseCollection implements |
||
| 65 | MapInterface, |
||
| 66 | MergeableInterface, |
||
| 67 | SortableInterface |
||
| 68 | { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The type of the key |
||
| 72 | * @var mixed |
||
| 73 | */ |
||
| 74 | protected $keyType; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The type of the value |
||
| 78 | * @var mixed |
||
| 79 | */ |
||
| 80 | protected $valueType; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Create new instance |
||
| 84 | * @param mixed $keyType |
||
| 85 | * @param mixed $valueType |
||
| 86 | * @param array<mixed, T> $initials |
||
| 87 | */ |
||
| 88 | public function __construct($keyType, $valueType, array $initials = []) |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * |
||
| 103 | * @param mixed $key |
||
| 104 | * @param T $value |
||
|
|
|||
| 105 | * @return void |
||
| 106 | */ |
||
| 107 | public function add($key, $value): void |
||
| 108 | { |
||
| 109 | $this->validateEntry($key, $value); |
||
| 110 | $this->data->offsetSet($key, new Pair($key, $value)); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * |
||
| 115 | * @param HashMap<T> $collection |
||
| 116 | * @return HashMap<T> |
||
| 117 | * @throws InvalidOperationException |
||
| 118 | */ |
||
| 119 | public function diff(BaseCollection $collection): BaseCollection |
||
| 120 | { |
||
| 121 | if (!$collection instanceof self) { |
||
| 122 | throw new InvalidOperationException( |
||
| 123 | 'You should only compare a Map against another Map' |
||
| 124 | ); |
||
| 125 | } |
||
| 126 | |||
| 127 | if ($this->keyType !== $collection->getKeyType()) { |
||
| 128 | throw new InvalidOperationException(sprintf( |
||
| 129 | 'The key type for this map is [%s], you cannot pass a map with [%s] as key type', |
||
| 130 | $this->keyType, |
||
| 131 | $collection->getKeyType() |
||
| 132 | )); |
||
| 133 | } |
||
| 134 | |||
| 135 | if ($this->valueType !== $collection->getValueType()) { |
||
| 136 | throw new InvalidOperationException(sprintf( |
||
| 137 | 'The value type for this map is [%s], you cannot pass a map with [%s] as value type', |
||
| 138 | $this->keyType, |
||
| 139 | $collection->getKeyType() |
||
| 140 | )); |
||
| 141 | } |
||
| 142 | |||
| 143 | $diffValues = array_udiff_uassoc( |
||
| 144 | $this->all(), |
||
| 145 | $collection->all(), |
||
| 146 | function ($a, $b) { |
||
| 147 | return $a <=> $b; |
||
| 148 | }, |
||
| 149 | function ($c, $d) { |
||
| 150 | return $c <=> $d; |
||
| 151 | } |
||
| 152 | ); |
||
| 153 | |||
| 154 | return new self($this->keyType, $this->valueType, $diffValues); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Return the type of the key |
||
| 159 | * @return mixed |
||
| 160 | */ |
||
| 161 | public function getKeyType() |
||
| 162 | { |
||
| 163 | return $this->keyType; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Return the type of the value |
||
| 168 | * @return mixed |
||
| 169 | */ |
||
| 170 | public function getValueType() |
||
| 171 | { |
||
| 172 | return $this->valueType; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * |
||
| 177 | * @param array<mixed, T> $data |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | public function fill(array $data): void |
||
| 181 | { |
||
| 182 | foreach ($data as $key => $value) { |
||
| 183 | $this->add($key, $value); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * |
||
| 189 | * @param callable $callback |
||
| 190 | * @return $this|null |
||
| 191 | */ |
||
| 192 | public function filter(callable $callback): ?self |
||
| 193 | { |
||
| 194 | $matches = []; |
||
| 195 | |||
| 196 | foreach ($this->data as $key => $value) { |
||
| 197 | $val = call_user_func($callback, $value->getKey(), $value->getValue()); |
||
| 198 | if ($val === true) { |
||
| 199 | $matches[$value->getKey()] = $value->getValue(); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | return count($matches) > 0 |
||
| 204 | ? new $this($this->keyType, $this->valueType, $matches) |
||
| 205 | : null; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * |
||
| 210 | * @param callable $callback |
||
| 211 | * @return $this|null |
||
| 212 | */ |
||
| 213 | public function map(callable $callback): ?self |
||
| 214 | { |
||
| 215 | $matches = array_map($callback, $this->all()); |
||
| 216 | |||
| 217 | return count($matches) > 0 |
||
| 218 | ? new $this($this->keyType, $this->valueType, $this->all()) |
||
| 219 | : null; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * |
||
| 224 | * @param HashMap<T> $collection |
||
| 225 | * @return bool |
||
| 226 | * @throws InvalidOperationException |
||
| 227 | */ |
||
| 228 | public function equals(BaseCollection $collection): bool |
||
| 229 | { |
||
| 230 | if (!$collection instanceof self) { |
||
| 231 | throw new InvalidOperationException( |
||
| 232 | 'You should only compare an map against another map' |
||
| 233 | ); |
||
| 234 | } |
||
| 235 | |||
| 236 | return $this->all() == $collection->all(); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * |
||
| 241 | * @param callable $callback |
||
| 242 | * @return void |
||
| 243 | */ |
||
| 244 | public function forEach(callable $callback): void |
||
| 245 | { |
||
| 246 | $data = $this->all(); |
||
| 247 | array_walk($data, $callback); |
||
| 248 | |||
| 249 | $this->initializePairs($data); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Return the value for given key |
||
| 254 | * @param mixed $key |
||
| 255 | * @return T|null |
||
| 256 | */ |
||
| 257 | public function get($key) |
||
| 258 | { |
||
| 259 | return $this->data->offsetExists($key) |
||
| 260 | ? $this->data->offsetGet($key)->getValue() |
||
| 261 | : null; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritedoc} |
||
| 266 | * @param HashMap<T> $collection |
||
| 267 | * @return HashMap<T> |
||
| 268 | */ |
||
| 269 | public function merge(BaseCollection $collection): BaseCollection |
||
| 270 | { |
||
| 271 | TypeCheck::isEqual( |
||
| 272 | $this->getKeyType(), |
||
| 273 | $collection->getKeyType(), |
||
| 274 | sprintf( |
||
| 275 | 'The new map key should be of type %s', |
||
| 276 | $this->keyType |
||
| 277 | ) |
||
| 278 | ); |
||
| 279 | |||
| 280 | TypeCheck::isEqual( |
||
| 281 | $this->getValueType(), |
||
| 282 | $collection->getValueType(), |
||
| 283 | sprintf( |
||
| 284 | 'The new map value should be of type %s', |
||
| 285 | $this->valueType |
||
| 286 | ) |
||
| 287 | ); |
||
| 288 | |||
| 289 | return new $this( |
||
| 290 | $this->keyType, |
||
| 291 | $this->valueType, |
||
| 292 | array_merge($this->all(), $collection->all()) |
||
| 293 | ); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritedoc} |
||
| 298 | */ |
||
| 299 | public function first() |
||
| 300 | { |
||
| 301 | throw new InvalidOperationException('Can not call this method in map'); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritedoc} |
||
| 306 | */ |
||
| 307 | public function last() |
||
| 308 | { |
||
| 309 | throw new InvalidOperationException('Can not call this method in map'); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * {@inheritedoc} |
||
| 314 | */ |
||
| 315 | public function remove($key): void |
||
| 316 | { |
||
| 317 | if ($this->isEmpty()) { |
||
| 318 | throw new OutOfRangeException('The collection is empty'); |
||
| 319 | } |
||
| 320 | |||
| 321 | if (!$this->data->offsetExists($key)) { |
||
| 322 | throw new OutOfRangeException(sprintf( |
||
| 323 | 'The collection key [%s] does not exists', |
||
| 324 | $key |
||
| 325 | )); |
||
| 326 | } |
||
| 327 | |||
| 328 | $this->data->offsetUnset($key); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * |
||
| 333 | * @param int $offset |
||
| 334 | * @param int|null $length |
||
| 335 | * @return HashMap<T>|null |
||
| 336 | */ |
||
| 337 | public function slice(int $offset, ?int $length = null): ?BaseCollection |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * |
||
| 352 | * @param callable $callback |
||
| 353 | * @return HashMap<T>|null |
||
| 354 | */ |
||
| 355 | public function sort(callable $callback): ?BaseCollection |
||
| 356 | { |
||
| 357 | $data = $this->all(); |
||
| 358 | |||
| 359 | return uasort($data, $callback) |
||
| 360 | ? new $this( |
||
| 361 | $this->keyType, |
||
| 362 | $this->valueType, |
||
| 363 | $data |
||
| 364 | ) |
||
| 365 | : null; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * |
||
| 370 | * @param mixed $key |
||
| 371 | * @param T $value |
||
| 372 | * @return bool |
||
| 373 | * @throws OutOfRangeException |
||
| 374 | */ |
||
| 375 | public function update($key, $value): bool |
||
| 376 | { |
||
| 377 | $this->validateEntry($key, $value); |
||
| 378 | |||
| 379 | if (!$this->data->offsetExists($key)) { |
||
| 380 | throw new OutOfRangeException(sprintf( |
||
| 381 | 'The collection key [%s] does not exists', |
||
| 382 | $key |
||
| 383 | )); |
||
| 384 | } |
||
| 385 | |||
| 386 | $this->data[$key]->setValue($value); |
||
| 387 | |||
| 388 | return $this->data[$key]->getValue() === $value; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * |
||
| 393 | * @return array<mixed, T> |
||
| 394 | */ |
||
| 395 | public function all(): array |
||
| 396 | { |
||
| 397 | $data = []; |
||
| 398 | foreach ($this->data as $pair) { |
||
| 399 | $data[$pair->getKey()] = $pair->getValue(); |
||
| 400 | } |
||
| 401 | |||
| 402 | return $data; |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | public function toJson(): string |
||
| 410 | { |
||
| 411 | /* Thank to interface JsonSerializable */ |
||
| 412 | $json = json_encode($this); |
||
| 413 | return $json === false ? '' : $json; |
||
| 414 | } |
||
| 415 | |||
| 416 | |||
| 417 | /** |
||
| 418 | * Validate the type of key and value |
||
| 419 | * @param mixed $key |
||
| 420 | * @param mixed $value |
||
| 421 | * |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | protected function validateEntry($key, $value): bool |
||
| 425 | { |
||
| 426 | TypeCheck::isValueOf( |
||
| 427 | $key, |
||
| 428 | $this->keyType, |
||
| 429 | sprintf( |
||
| 430 | 'The key type specified for this map is [%s], you cannot pass [%s]', |
||
| 431 | $this->keyType, |
||
| 432 | gettype($key) |
||
| 433 | ) |
||
| 434 | ); |
||
| 435 | |||
| 436 | TypeCheck::isValueOf( |
||
| 437 | $value, |
||
| 438 | $this->valueType, |
||
| 439 | sprintf( |
||
| 440 | 'The value type specified for this map is [%s], you cannot pass [%s]', |
||
| 441 | $this->valueType, |
||
| 442 | gettype($value) |
||
| 443 | ) |
||
| 444 | ); |
||
| 445 | |||
| 446 | return false; |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Initialize the pair values |
||
| 451 | * @param array<mixed, mixed> $data |
||
| 452 | * @return void |
||
| 453 | */ |
||
| 454 | protected function initializePairs(array $data): void |
||
| 458 | } |
||
| 459 | } |
||
| 460 | } |
||
| 461 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths