Complex classes like AbstractMap 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 AbstractMap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | abstract class AbstractMap |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Returns the singleton. |
||
| 16 | * |
||
| 17 | * @return string |
||
| 18 | */ |
||
| 19 | 39 | public static function getInstance() |
|
| 26 | |||
| 27 | /** |
||
| 28 | * Returns this file's full qualified filename. |
||
| 29 | * |
||
| 30 | * @return string |
||
| 31 | */ |
||
| 32 | 1 | public function getFileName() |
|
| 33 | { |
||
| 34 | 1 | throw new \LogicException(__METHOD__ . ' is not implemented in ' . get_called_class()); |
|
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Gets the map array. |
||
| 39 | * |
||
| 40 | * @return array |
||
| 41 | */ |
||
| 42 | 5 | public function getMapArray() |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Sorts the map. |
||
| 49 | * |
||
| 50 | * @return $this |
||
| 51 | */ |
||
| 52 | 6 | public function sort() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Determines if a MIME type exists. |
||
| 65 | * |
||
| 66 | * @param string $type The type to be found. |
||
| 67 | * |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | 11 | public function hasType($type) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Determines if a MIME type alias exists. |
||
| 77 | * |
||
| 78 | * @param string $alias The alias to be found. |
||
| 79 | * |
||
| 80 | * @return bool |
||
| 81 | */ |
||
| 82 | 10 | public function hasAlias($alias) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Determines if an entry exists from the 'extensions' array. |
||
| 89 | * |
||
| 90 | * @param string $extension The extension to be found. |
||
| 91 | * |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | 1 | public function hasExtension($extension) |
|
| 95 | { |
||
| 96 | 1 | return (bool) $this->getMapEntry('e', $extension); |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Lists all the MIME types defined in the map. |
||
| 101 | * |
||
| 102 | * @param string $match (Optional) a match wildcard to limit the list. |
||
| 103 | * |
||
| 104 | * @return string[] |
||
| 105 | */ |
||
| 106 | 7 | public function listTypes($match = null) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Lists all the MIME types aliases defined in the map. |
||
| 113 | * |
||
| 114 | * @param string $match (Optional) a match wildcard to limit the list. |
||
| 115 | * |
||
| 116 | * @return string[] |
||
| 117 | */ |
||
| 118 | 2 | public function listAliases($match = null) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Lists all the extensions defined in the map. |
||
| 125 | * |
||
| 126 | * @param string $match (Optional) a match wildcard to limit the list. |
||
| 127 | * |
||
| 128 | * @return string[] |
||
| 129 | */ |
||
| 130 | 3 | public function listExtensions($match = null) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Adds a description of a MIME type. |
||
| 137 | * |
||
| 138 | * @param string $type |
||
| 139 | * A MIME type. |
||
| 140 | * @param string $description |
||
| 141 | * The description of the MIME type. |
||
| 142 | * |
||
| 143 | * @throws MappingException if $type is an alias. |
||
| 144 | * |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | 2 | public function addTypeDescription($type, $description) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Adds an alias of a MIME type. |
||
| 160 | * |
||
| 161 | * @param string $type |
||
| 162 | * A MIME type. |
||
| 163 | * @param string $alias |
||
| 164 | * An alias of $type. |
||
| 165 | * |
||
| 166 | * @throws MappingException if no $type is found. |
||
| 167 | * |
||
| 168 | * @return $this |
||
| 169 | */ |
||
| 170 | 3 | public function addTypeAlias($type, $alias) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Adds a type-to-extension mapping. |
||
| 190 | * |
||
| 191 | * @param string $type |
||
| 192 | * A MIME type. |
||
| 193 | * @param string $extension |
||
| 194 | * A file extension. |
||
| 195 | * |
||
| 196 | * @throws MappingException if $type is an alias. |
||
| 197 | * |
||
| 198 | * @return $this |
||
| 199 | */ |
||
| 200 | 6 | public function addTypeExtensionMapping($type, $extension) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Gets the descriptions of a MIME type. |
||
| 221 | * |
||
| 222 | * @param string $type The type to be found. |
||
| 223 | * |
||
| 224 | * @return string[] The mapped descriptions. |
||
| 225 | */ |
||
| 226 | 1 | public function getTypeDescriptions($type) |
|
| 227 | { |
||
| 228 | 1 | $res = $this->getMapSubEntry('t', $type, 'desc'); |
|
| 229 | 1 | return $res ?: []; |
|
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Gets the aliases of a MIME type. |
||
| 234 | * |
||
| 235 | * @param string $type The type to be found. |
||
| 236 | * |
||
| 237 | * @return string[] The mapped aliases. |
||
| 238 | */ |
||
| 239 | 1 | public function getTypeAliases($type) |
|
| 240 | { |
||
| 241 | 1 | $res = $this->getMapSubEntry('t', $type, 'a'); |
|
| 242 | 1 | return $res ?: []; |
|
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Gets the content of an entry from the 't' array. |
||
| 247 | * |
||
| 248 | * @param string $type The type to be found. |
||
| 249 | * |
||
| 250 | * @return string[] The mapped file extensions. |
||
| 251 | */ |
||
| 252 | 5 | public function getTypeExtensions($type) |
|
| 253 | { |
||
| 254 | 5 | $res = $this->getMapSubEntry('t', $type, 'e'); |
|
| 255 | 5 | return $res ?: []; |
|
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Changes the default extension for a MIME type. |
||
| 260 | * |
||
| 261 | * @param string $type |
||
| 262 | * A MIME type. |
||
| 263 | * @param string $extension |
||
| 264 | * A file extension. |
||
| 265 | * |
||
| 266 | * @throws MappingException if no mapping found. |
||
| 267 | * |
||
| 268 | * @return $this |
||
| 269 | */ |
||
| 270 | 3 | public function setTypeDefaultExtension($type, $extension) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Removes the entire mapping of a type. |
||
| 277 | * |
||
| 278 | * @param string $type |
||
| 279 | * A MIME type. |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | * true if the mapping was removed, false if the type was not present. |
||
| 283 | */ |
||
| 284 | 1 | public function removeType($type) |
|
| 285 | { |
||
| 286 | 1 | $type = strtolower($type); |
|
| 287 | |||
| 288 | // Return false if type is not found. |
||
| 289 | 1 | if (!$this->hasType($type)) { |
|
| 290 | 1 | return false; |
|
| 291 | } |
||
| 292 | |||
| 293 | // Loop through all the associated extensions and remove them. |
||
| 294 | 1 | foreach ($this->getTypeExtensions($type) as $extension) { |
|
| 295 | 1 | $this->removeTypeExtensionMapping($type, $extension); |
|
| 296 | } |
||
| 297 | |||
| 298 | // Loop through all the associated aliases and remove them. |
||
| 299 | 1 | foreach ($this->getTypeAliases($type) as $alias) { |
|
| 300 | 1 | $this->removeTypeAlias($type, $alias); |
|
| 301 | } |
||
| 302 | |||
| 303 | 1 | unset(static::$map['t'][$type]); |
|
| 304 | |||
| 305 | 1 | return true; |
|
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Removes a MIME type alias. |
||
| 310 | * |
||
| 311 | * @param string $type |
||
| 312 | * A MIME type. |
||
| 313 | * @param string $alias |
||
| 314 | * The alias to be removed. |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | * true if the alias was removed, false if the alias was not present. |
||
| 318 | */ |
||
| 319 | 1 | public function removeTypeAlias($type, $alias) |
|
| 320 | { |
||
| 321 | 1 | $type = strtolower($type); |
|
| 322 | 1 | $alias = strtolower($alias); |
|
| 323 | |||
| 324 | 1 | $type_ret = $this->removeMapSubEntry('t', $type, 'a', $alias); |
|
| 325 | 1 | $alias_ret = $this->removeMapSubEntry('a', $alias, 't', $type); |
|
| 326 | |||
| 327 | 1 | return $type_ret && $alias_ret; |
|
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Removes a type-to-extension mapping. |
||
| 332 | * |
||
| 333 | * @param string $type |
||
| 334 | * A MIME type. |
||
| 335 | * @param string $extension |
||
| 336 | * The file extension to be removed. |
||
| 337 | * |
||
| 338 | * @return bool |
||
| 339 | * true if the mapping was removed, false if the mapping was not present. |
||
| 340 | */ |
||
| 341 | 1 | public function removeTypeExtensionMapping($type, $extension) |
|
| 342 | { |
||
| 343 | 1 | $type = strtolower($type); |
|
| 344 | 1 | $extension = strtolower($extension); |
|
| 345 | |||
| 346 | 1 | $type_ret = $this->removeMapSubEntry('t', $type, 'e', $extension); |
|
| 347 | 1 | $extension_ret = $this->removeMapSubEntry('e', $extension, 't', $type); |
|
| 348 | |||
| 349 | 1 | return $type_ret && $extension_ret; |
|
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Gets the content of an entry from the 'extensions' array. |
||
| 354 | * |
||
| 355 | * @param string $extension The extension to be found. |
||
| 356 | * |
||
| 357 | * @return string[] The mapped MIME types. |
||
| 358 | */ |
||
| 359 | 9 | public function getExtensionTypes($extension) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Changes the default MIME type for a file extension. |
||
| 367 | * |
||
| 368 | * @param string $extension |
||
| 369 | * A file extension. |
||
| 370 | * @param string $type |
||
| 371 | * A MIME type. |
||
| 372 | * |
||
| 373 | * @throws MappingException if no mapping found. |
||
| 374 | * |
||
| 375 | * @return $this |
||
| 376 | */ |
||
| 377 | 3 | public function setExtensionDefaultType($extension, $type) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Gets a list of entries of the map. |
||
| 384 | * |
||
| 385 | * @param string $entry |
||
| 386 | * The main array entry. |
||
| 387 | * @param string $match |
||
| 388 | * (Optional) a match wildcard to limit the list. |
||
| 389 | * |
||
| 390 | * @return array |
||
| 391 | * The list of the entries. |
||
| 392 | */ |
||
| 393 | 7 | protected function listEntries($entry, $match = null) |
|
| 394 | { |
||
| 395 | 7 | $entry = strtolower($entry); |
|
| 396 | |||
| 397 | 7 | if (!isset(static::$map[$entry])) { |
|
| 398 | 1 | return []; |
|
| 399 | } |
||
| 400 | |||
| 401 | 7 | $list = array_keys(static::$map[$entry]); |
|
| 402 | |||
| 403 | 7 | if (is_null($match)) { |
|
| 404 | 3 | return $list; |
|
| 405 | } else { |
||
| 406 | 4 | $re = strtr($match, ['/' => '\\/', '*' => '.*']); |
|
| 407 | return array_filter($list, function ($v) use ($re) { |
||
| 408 | 4 | return preg_match("/$re/", $v) === 1; |
|
| 409 | 4 | }); |
|
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Gets the content of an entry of the map. |
||
| 415 | * |
||
| 416 | * @param string $entry |
||
| 417 | * The main array entry. |
||
| 418 | * @param string $entry_key |
||
| 419 | * The main entry value. |
||
| 420 | * |
||
| 421 | * @return mixed|null |
||
| 422 | * The value of the entry, or null if missing. |
||
| 423 | */ |
||
| 424 | 19 | protected function getMapEntry($entry, $entry_key) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Gets the content of a subentry of the map. |
||
| 433 | * |
||
| 434 | * @param string $entry |
||
| 435 | * The main array entry. |
||
| 436 | * @param string $entry_key |
||
| 437 | * The main entry value. |
||
| 438 | * @param string $sub_entry |
||
| 439 | * The sub entry. |
||
| 440 | * |
||
| 441 | * @return mixed|null |
||
| 442 | * The value of the entry, or null if missing. |
||
| 443 | */ |
||
| 444 | 13 | protected function getMapSubEntry($entry, $entry_key, $sub_entry) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Adds an entry to the map. |
||
| 454 | * |
||
| 455 | * Checks that no duplicate entries are made. |
||
| 456 | * |
||
| 457 | * @param string $entry |
||
| 458 | * The main array entry. |
||
| 459 | * @param string $entry_key |
||
| 460 | * The main entry value. |
||
| 461 | * @param string $sub_entry |
||
| 462 | * The sub entry. |
||
| 463 | * @param string $value |
||
| 464 | * The value to add. |
||
| 465 | * |
||
| 466 | * @return $this |
||
| 467 | */ |
||
| 468 | 5 | protected function addMapSubEntry($entry, $entry_key, $sub_entry, $value) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Removes an entry from the map. |
||
| 485 | * |
||
| 486 | * @param string $entry |
||
| 487 | * The main array entry. |
||
| 488 | * @param string $entry_key |
||
| 489 | * The main entry value. |
||
| 490 | * @param string $sub_entry |
||
| 491 | * The sub entry. |
||
| 492 | * @param string $value |
||
| 493 | * The value to remove. |
||
| 494 | * |
||
| 495 | * @return bool |
||
| 496 | * true if the entry was removed, false if the entry was not present. |
||
| 497 | */ |
||
| 498 | 1 | protected function removeMapSubEntry($entry, $entry_key, $sub_entry, $value) |
|
| 499 | { |
||
| 500 | 1 | $entry = strtolower($entry); |
|
| 501 | 1 | $entry_key = strtolower($entry_key); |
|
| 502 | 1 | $sub_entry = strtolower($sub_entry); |
|
| 503 | |||
| 504 | // Return false if no entry. |
||
| 505 | 1 | if (!isset(static::$map[$entry][$entry_key][$sub_entry])) { |
|
| 506 | 1 | return false; |
|
| 507 | } |
||
| 508 | |||
| 509 | // Return false if no value. |
||
| 510 | 1 | $k = array_search($value, static::$map[$entry][$entry_key][$sub_entry]); |
|
| 511 | 1 | if ($k === false) { |
|
| 512 | 1 | return false; |
|
| 513 | } |
||
| 514 | |||
| 515 | // Remove the map entry. |
||
| 516 | 1 | unset(static::$map[$entry][$entry_key][$sub_entry][$k]); |
|
| 517 | |||
| 518 | // Remove the entry itself if no more values. |
||
| 519 | 1 | if (empty(static::$map[$entry][$entry_key][$sub_entry])) { |
|
| 520 | 1 | unset(static::$map[$entry][$entry_key][$sub_entry]); |
|
| 521 | } else { |
||
| 522 | // Resequence the remaining values. |
||
| 523 | 1 | $tmp = []; |
|
| 524 | 1 | foreach (static::$map[$entry][$entry_key][$sub_entry] as $v) { |
|
| 525 | 1 | $tmp[] = $v; |
|
| 526 | } |
||
| 527 | 1 | static::$map[$entry][$entry_key][$sub_entry] = $tmp; |
|
| 528 | } |
||
| 529 | |||
| 530 | 1 | return true; |
|
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Sets a value as the default for an entry. |
||
| 535 | * |
||
| 536 | * @param string $entry |
||
| 537 | * The main array entry. |
||
| 538 | * @param string $entry_key |
||
| 539 | * The main entry value. |
||
| 540 | * @param string $sub_entry |
||
| 541 | * The sub entry. |
||
| 542 | * @param string $value |
||
| 543 | * The value to add. |
||
| 544 | * |
||
| 545 | * @throws MappingException if no mapping found. |
||
| 546 | * |
||
| 547 | * @return $this |
||
| 548 | */ |
||
| 549 | 6 | protected function setValueAsDefault($entry, $entry_key, $sub_entry, $value) |
|
| 578 | } |
||
| 579 |