| Total Complexity | 52 |
| Total Lines | 422 |
| Duplicated Lines | 0 % |
| Coverage | 98.18% |
| Changes | 0 | ||
Complex classes like ObjectKeys 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 ObjectKeys, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | final class ObjectKeys implements MapInterface |
||
| 24 | { |
||
| 25 | private $keyType; |
||
| 26 | private $valueType; |
||
| 27 | private $keySpecification; |
||
| 28 | private $valueSpecification; |
||
| 29 | private $values; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * {@inheritdoc} |
||
| 33 | */ |
||
| 34 | 62 | public function __construct(string $keyType, string $valueType) |
|
| 35 | { |
||
| 36 | 62 | $this->keySpecification = Type::of($keyType); |
|
| 37 | |||
| 38 | 62 | if (!$this->keySpecification instanceof ClassType) { |
|
| 39 | 2 | throw new LogicException; |
|
| 40 | } |
||
| 41 | |||
| 42 | 60 | $this->valueSpecification = Type::of($valueType); |
|
| 43 | 60 | $this->keyType = new Str($keyType); |
|
| 44 | 60 | $this->valueType = new Str($valueType); |
|
| 45 | 60 | $this->values = new \SplObjectStorage; |
|
| 46 | 60 | } |
|
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | 16 | public function keyType(): Str |
|
| 52 | { |
||
| 53 | 16 | return $this->keyType; |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | 14 | public function valueType(): Str |
|
| 60 | { |
||
| 61 | 14 | return $this->valueType; |
|
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritdoc} |
||
| 66 | */ |
||
| 67 | 14 | public function size(): int |
|
| 68 | { |
||
| 69 | 14 | return $this->values->count(); |
|
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | */ |
||
| 75 | public function count(): int |
||
| 76 | { |
||
| 77 | return $this->size(); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * {@inheritdoc} |
||
| 82 | */ |
||
| 83 | 2 | public function current() |
|
| 84 | { |
||
| 85 | 2 | return $this->get($this->key()); |
|
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritdoc} |
||
| 90 | */ |
||
| 91 | 2 | public function key() |
|
| 92 | { |
||
| 93 | 2 | return $this->values->current(); |
|
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | 2 | public function next() |
|
| 100 | { |
||
| 101 | 2 | $this->values->next(); |
|
| 102 | 2 | } |
|
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | */ |
||
| 107 | 42 | public function rewind() |
|
| 108 | { |
||
| 109 | 42 | $this->values->rewind(); |
|
| 110 | 42 | } |
|
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | 2 | public function valid() |
|
| 116 | { |
||
| 117 | 2 | return $this->values->valid(); |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritdoc} |
||
| 122 | */ |
||
| 123 | 24 | public function offsetExists($offset): bool |
|
| 124 | { |
||
| 125 | 24 | if (!is_object($offset)) { |
|
| 126 | 2 | return false; |
|
| 127 | } |
||
| 128 | |||
| 129 | 24 | return $this->values->offsetExists($offset); |
|
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | 4 | public function offsetGet($offset) |
|
| 136 | { |
||
| 137 | 4 | return $this->get($offset); |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | 2 | public function offsetSet($offset, $value) |
|
| 144 | { |
||
| 145 | 2 | throw new LogicException('You can\'t modify a map'); |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | 2 | public function offsetUnset($offset) |
|
| 152 | { |
||
| 153 | 2 | throw new LogicException('You can\'t modify a map'); |
|
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | 48 | public function put($key, $value): MapInterface |
|
| 160 | { |
||
| 161 | 48 | $this->keySpecification->validate($key); |
|
| 162 | 48 | $this->valueSpecification->validate($value); |
|
| 163 | |||
| 164 | 42 | $map = clone $this; |
|
| 165 | 42 | $map->values = clone $this->values; |
|
| 166 | 42 | $map->values[$key] = $value; |
|
| 167 | 42 | $map->rewind(); |
|
| 168 | |||
| 169 | 42 | return $map; |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | 18 | public function get($key) |
|
| 176 | { |
||
| 177 | 18 | if (!$this->offsetExists($key)) { |
|
| 178 | 4 | throw new ElementNotFoundException; |
|
| 179 | } |
||
| 180 | |||
| 181 | 14 | return $this->values->offsetGet($key); |
|
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritdoc} |
||
| 186 | */ |
||
| 187 | 10 | public function contains($key): bool |
|
| 188 | { |
||
| 189 | 10 | return $this->offsetExists($key); |
|
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * {@inheritdoc} |
||
| 194 | */ |
||
| 195 | 14 | public function clear(): MapInterface |
|
| 196 | { |
||
| 197 | 14 | $map = clone $this; |
|
| 198 | 14 | $map->values = new \SplObjectStorage; |
|
| 199 | |||
| 200 | 14 | return $map; |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritdoc} |
||
| 205 | */ |
||
| 206 | 4 | public function equals(MapInterface $map): bool |
|
| 207 | { |
||
| 208 | 4 | if ($map->size() !== $this->size()) { |
|
| 209 | 2 | return false; |
|
| 210 | } |
||
| 211 | |||
| 212 | 4 | foreach ($this->values as $k) { |
|
| 213 | 4 | $v = $this->values[$k]; |
|
| 214 | |||
| 215 | 4 | if (!$map->contains($k)) { |
|
| 216 | return false; |
||
| 217 | } |
||
| 218 | |||
| 219 | 4 | if ($map->get($k) !== $v) { |
|
| 220 | 4 | return false; |
|
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | 2 | return true; |
|
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * {@inheritdoc} |
||
| 229 | */ |
||
| 230 | 2 | public function filter(callable $predicate): MapInterface |
|
| 231 | { |
||
| 232 | 2 | $map = $this->clear(); |
|
| 233 | |||
| 234 | 2 | foreach ($this->values as $k) { |
|
| 235 | 2 | $v = $this->values[$k]; |
|
| 236 | |||
| 237 | 2 | if ($predicate($k, $v) === true) { |
|
| 238 | 2 | $map->values[$k] = $v; |
|
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | 2 | $map->rewind(); |
|
| 243 | |||
| 244 | 2 | return $map; |
|
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * {@inheritdoc} |
||
| 249 | */ |
||
| 250 | 2 | public function foreach(callable $function): MapInterface |
|
| 251 | { |
||
| 252 | 2 | foreach ($this->values as $k) { |
|
| 253 | 2 | $v = $this->values[$k]; |
|
| 254 | |||
| 255 | 2 | $function($k, $v); |
|
| 256 | } |
||
| 257 | |||
| 258 | 2 | return $this; |
|
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | */ |
||
| 264 | 4 | public function groupBy(callable $discriminator): MapInterface |
|
| 265 | { |
||
| 266 | 4 | if ($this->size() === 0) { |
|
| 267 | 2 | throw new GroupEmptyMapException; |
|
| 268 | } |
||
| 269 | |||
| 270 | 2 | $map = null; |
|
| 271 | |||
| 272 | 2 | foreach ($this->values as $k) { |
|
| 273 | 2 | $v = $this->values[$k]; |
|
| 274 | |||
| 275 | 2 | $key = $discriminator($k, $v); |
|
| 276 | |||
| 277 | 2 | if ($map === null) { |
|
| 278 | 2 | $map = new Map( |
|
| 279 | 2 | Type::determine($key), |
|
| 280 | 2 | MapInterface::class |
|
| 281 | ); |
||
| 282 | } |
||
| 283 | |||
| 284 | 2 | if ($map->contains($key)) { |
|
| 285 | 2 | $map = $map->put( |
|
| 286 | 2 | $key, |
|
| 287 | 2 | $map->get($key)->put($k, $v) |
|
| 288 | ); |
||
| 289 | } else { |
||
| 290 | 2 | $map = $map->put( |
|
| 291 | 2 | $key, |
|
| 292 | 2 | $this->clear()->put($k, $v) |
|
| 293 | ); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | 2 | return $map; |
|
|
1 ignored issue
–
show
|
|||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | 12 | public function keys(): SetInterface |
|
| 304 | { |
||
| 305 | 12 | return $this->reduce( |
|
| 306 | 12 | Set::of((string) $this->keyType), |
|
| 307 | 12 | static function(SetInterface $keys, $key): SetInterface { |
|
| 308 | 12 | return $keys->add($key); |
|
| 309 | 12 | } |
|
| 310 | ); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * {@inheritdoc} |
||
| 315 | */ |
||
| 316 | 14 | public function values(): StreamInterface |
|
| 317 | { |
||
| 318 | 14 | return $this->reduce( |
|
| 319 | 14 | Stream::of((string) $this->valueType), |
|
| 320 | 14 | static function(StreamInterface $values, $key, $value): StreamInterface { |
|
| 321 | 14 | return $values->add($value); |
|
| 322 | 14 | } |
|
| 323 | ); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * {@inheritdoc} |
||
| 328 | */ |
||
| 329 | 6 | public function map(callable $function): MapInterface |
|
| 330 | { |
||
| 331 | 6 | $map = $this->clear(); |
|
| 332 | |||
| 333 | 6 | foreach ($this->values as $k) { |
|
| 334 | 6 | $v = $this->values[$k]; |
|
| 335 | |||
| 336 | 6 | $return = $function($k, $v); |
|
| 337 | |||
| 338 | 6 | if ($return instanceof Pair) { |
|
| 339 | 4 | $this->keySpecification->validate($return->key()); |
|
| 340 | |||
| 341 | 2 | $key = $return->key(); |
|
| 342 | 2 | $value = $return->value(); |
|
| 343 | } else { |
||
| 344 | 4 | $key = $k; |
|
| 345 | 4 | $value = $return; |
|
| 346 | } |
||
| 347 | |||
| 348 | 4 | $this->valueSpecification->validate($value); |
|
| 349 | |||
| 350 | 2 | $map->values[$key] = $value; |
|
| 351 | } |
||
| 352 | |||
| 353 | 2 | $map->rewind(); |
|
| 354 | |||
| 355 | 2 | return $map; |
|
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * {@inheritdoc} |
||
| 360 | */ |
||
| 361 | 2 | public function join(string $separator): Str |
|
| 362 | { |
||
| 363 | 2 | return $this->values()->join($separator); |
|
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * {@inheritdoc} |
||
| 368 | */ |
||
| 369 | 2 | public function remove($key): MapInterface |
|
| 370 | { |
||
| 371 | 2 | if (!$this->contains($key)) { |
|
| 372 | 2 | return $this; |
|
| 373 | } |
||
| 374 | |||
| 375 | 2 | $map = clone $this; |
|
| 376 | 2 | $map->values = clone $this->values; |
|
| 377 | 2 | $map->values->detach($key); |
|
| 378 | 2 | $map->rewind(); |
|
| 379 | |||
| 380 | 2 | return $map; |
|
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * {@inheritdoc} |
||
| 385 | */ |
||
| 386 | 4 | public function merge(MapInterface $map): MapInterface |
|
| 387 | { |
||
| 388 | if ( |
||
| 389 | 4 | !$this->keyType()->equals($map->keyType()) || |
|
| 390 | 4 | !$this->valueType()->equals($map->valueType()) |
|
| 391 | ) { |
||
| 392 | 2 | throw new InvalidArgumentException( |
|
| 393 | 2 | 'The 2 maps does not reference the same types' |
|
| 394 | ); |
||
| 395 | } |
||
| 396 | |||
| 397 | 2 | return $map->reduce( |
|
| 398 | 2 | $this, |
|
| 399 | 2 | function(self $carry, $key, $value): self { |
|
| 400 | 2 | return $carry->put($key, $value); |
|
| 401 | 2 | } |
|
| 402 | ); |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * {@inheritdoc} |
||
| 407 | */ |
||
| 408 | 2 | public function partition(callable $predicate): MapInterface |
|
| 409 | { |
||
| 410 | 2 | $truthy = $this->clear(); |
|
| 411 | 2 | $falsy = $this->clear(); |
|
| 412 | |||
| 413 | 2 | foreach ($this->values as $k) { |
|
| 414 | 2 | $v = $this->values[$k]; |
|
| 415 | |||
| 416 | 2 | $return = $predicate($k, $v); |
|
| 417 | |||
| 418 | 2 | if ($return === true) { |
|
| 419 | 2 | $truthy->values[$k] = $v; |
|
| 420 | } else { |
||
| 421 | 2 | $falsy->values[$k] = $v; |
|
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | 2 | $truthy->rewind(); |
|
| 426 | 2 | $falsy->rewind(); |
|
| 427 | |||
| 428 | 2 | return Map::of('bool', MapInterface::class) |
|
| 429 | 2 | (true, $truthy) |
|
| 430 | 2 | (false, $falsy); |
|
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * {@inheritdoc} |
||
| 435 | */ |
||
| 436 | 16 | public function reduce($carry, callable $reducer) |
|
| 445 | } |
||
| 446 | } |
||
| 447 |