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 |
||
| 13 | final class Map implements MapInterface |
||
| 14 | { |
||
| 15 | use Type; |
||
| 16 | |||
| 17 | private $keyType; |
||
| 18 | private $valueType; |
||
| 19 | private $keySpecification; |
||
| 20 | private $valueSpecification; |
||
| 21 | private $keys; |
||
| 22 | private $values; |
||
| 23 | private $pairs; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * {@inheritdoc} |
||
| 27 | */ |
||
| 28 | 80 | public function __construct(string $keyType, string $valueType) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | 34 | public function keyType(): Str |
|
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritdoc} |
||
| 49 | */ |
||
| 50 | 32 | public function valueType(): Str |
|
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | 16 | public function size(): int |
|
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | 6 | public function count(): int |
|
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | 2 | public function current() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | 2 | public function key() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | 2 | public function next() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | 2 | public function rewind() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | 2 | public function valid() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | 2 | public function offsetExists($offset): bool |
|
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | 8 | public function offsetGet($offset) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | 2 | public function offsetSet($offset, $value) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | 2 | public function offsetUnset($offset) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | 66 | public function put($key, $value): MapInterface |
|
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritdoc} |
||
| 178 | */ |
||
| 179 | 32 | public function get($key) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritdoc} |
||
| 192 | */ |
||
| 193 | 14 | public function contains($key): bool |
|
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | */ |
||
| 201 | 14 | public function clear(): MapInterface |
|
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritdoc} |
||
| 213 | */ |
||
| 214 | 4 | public function equals(MapInterface $map): bool |
|
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritdoc} |
||
| 225 | */ |
||
| 226 | 2 | public function filter(callable $predicate): MapInterface |
|
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritdoc} |
||
| 243 | */ |
||
| 244 | public function foreach(callable $function): MapInterface |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | 4 | public function groupBy(callable $discriminator): MapInterface |
|
| 257 | { |
||
| 258 | 4 | if ($this->size() === 0) { |
|
| 259 | 2 | throw new GroupEmptyMapException; |
|
| 260 | } |
||
| 261 | |||
| 262 | 2 | $map = null; |
|
| 263 | |||
| 264 | 2 | foreach ($this->pairs as $pair) { |
|
| 265 | 2 | $key = $discriminator($pair->key(), $pair->value()); |
|
| 266 | |||
| 267 | 2 | if ($map === null) { |
|
| 268 | 2 | $map = new self( |
|
| 269 | 2 | $this->determineType($key), |
|
| 270 | 2 | MapInterface::class |
|
| 271 | ); |
||
| 272 | } |
||
| 273 | |||
| 274 | 2 | if ($map->contains($key)) { |
|
| 275 | 2 | $map = $map->put( |
|
| 276 | $key, |
||
| 277 | 2 | $map->get($key)->put( |
|
| 278 | 2 | $pair->key(), |
|
| 279 | 2 | $pair->value() |
|
| 280 | ) |
||
| 281 | ); |
||
| 282 | } else { |
||
| 283 | 2 | $map = $map->put( |
|
| 284 | $key, |
||
| 285 | 2 | $this->clear()->put( |
|
| 286 | 2 | $pair->key(), |
|
| 287 | 2 | $pair->value() |
|
| 288 | ) |
||
| 289 | ); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | 2 | return $map; |
|
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritdoc} |
||
| 298 | */ |
||
| 299 | 18 | public function keys(): SetInterface |
|
| 310 | |||
| 311 | /** |
||
| 312 | * {@inheritdoc} |
||
| 313 | */ |
||
| 314 | 14 | public function values(): StreamInterface |
|
| 325 | |||
| 326 | /** |
||
| 327 | * {@inheritdoc} |
||
| 328 | */ |
||
| 329 | 6 | public function map(callable $function): MapInterface |
|
| 352 | |||
| 353 | /** |
||
| 354 | * {@inheritdoc} |
||
| 355 | */ |
||
| 356 | 2 | public function join(string $separator): Str |
|
| 360 | |||
| 361 | /** |
||
| 362 | * {@inheritdoc} |
||
| 363 | */ |
||
| 364 | 2 | public function remove($key): MapInterface |
|
| 387 | |||
| 388 | /** |
||
| 389 | * {@inheritdoc} |
||
| 390 | */ |
||
| 391 | 4 | public function merge(MapInterface $map): MapInterface |
|
| 392 | { |
||
| 393 | if ( |
||
| 394 | 4 | !$this->keyType()->equals($map->keyType()) || |
|
| 395 | 3 | !$this->valueType()->equals($map->valueType()) |
|
| 396 | ) { |
||
| 397 | 2 | throw new InvalidArgumentException( |
|
| 398 | 2 | 'The 2 maps does not reference the same types' |
|
| 399 | ); |
||
| 400 | } |
||
| 401 | |||
| 402 | 2 | return $map->reduce( |
|
| 403 | $this, |
||
| 404 | 2 | function(self $carry, $key, $value): self { |
|
| 405 | 2 | return $carry->put($key, $value); |
|
| 406 | 2 | } |
|
| 407 | ); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * {@inheritdoc} |
||
| 412 | */ |
||
| 413 | 2 | public function partition(callable $predicate): MapInterface |
|
| 435 | |||
| 436 | /** |
||
| 437 | * {@inheritdoc} |
||
| 438 | */ |
||
| 439 | 6 | public function reduce($carry, callable $reducer) |
|
| 447 | } |
||
| 448 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: