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 | 33 | 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 | 9 | public function hasType($type) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Determines if a MIME type alias exists. |
||
| 78 | * |
||
| 79 | * @param string $alias The alias to be found. |
||
| 80 | * |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | 6 | public function hasAlias($alias) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Determines if an entry exists from the 'extensions' array. |
||
| 90 | * |
||
| 91 | * @param string $extension The extension to be found. |
||
| 92 | * |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | 1 | public function hasExtension($extension) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Lists all the MIME types defined in the map. |
||
| 102 | * |
||
| 103 | * @param string $match (Optional) a match wildcard to limit the list. |
||
| 104 | * |
||
| 105 | * @return string[] |
||
| 106 | */ |
||
| 107 | 7 | public function listTypes($match = null) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Lists all the MIME types aliases defined in the map. |
||
| 115 | * |
||
| 116 | * @param string $match (Optional) a match wildcard to limit the list. |
||
| 117 | * |
||
| 118 | * @return string[] |
||
| 119 | */ |
||
| 120 | 2 | public function listAliases($match = null) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Lists all the extensions defined in the map. |
||
| 127 | * |
||
| 128 | * @param string $match (Optional) a match wildcard to limit the list. |
||
| 129 | * |
||
| 130 | * @return string[] |
||
| 131 | */ |
||
| 132 | 3 | public function listExtensions($match = null) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Adds a description of a MIME type. |
||
| 139 | * |
||
| 140 | * @param string $type |
||
| 141 | * A MIME type. |
||
| 142 | * @param string $description |
||
| 143 | * The description of the MIME type. |
||
| 144 | * |
||
| 145 | * @throws MappingException if $type is an alias. |
||
| 146 | * |
||
| 147 | * @return $this |
||
| 148 | */ |
||
| 149 | 1 | public function addTypeDescription($type, $description) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Adds an alias of a MIME type. |
||
| 162 | * |
||
| 163 | * @param string $type |
||
| 164 | * A MIME type. |
||
| 165 | * @param string $alias |
||
| 166 | * An alias of $type. |
||
| 167 | * |
||
| 168 | * @throws MappingException if no $type is found. |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | 1 | public function addTypeAlias($type, $alias) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Adds a type-to-extension mapping. |
||
| 192 | * |
||
| 193 | * @param string $type |
||
| 194 | * A MIME type. |
||
| 195 | * @param string $extension |
||
| 196 | * A file extension. |
||
| 197 | * |
||
| 198 | * @throws MappingException if $type is an alias. |
||
| 199 | * |
||
| 200 | * @return $this |
||
| 201 | */ |
||
| 202 | 5 | public function addTypeExtensionMapping($type, $extension) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Gets the aliases of a MIME type. |
||
| 223 | * |
||
| 224 | * @param string $type The type to be found. |
||
| 225 | * |
||
| 226 | * @return string[] The mapped aliases. |
||
| 227 | */ |
||
| 228 | 1 | public function getTypeAliases($type) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Gets the content of an entry from the 't' array. |
||
| 236 | * |
||
| 237 | * @param string $type The type to be found. |
||
| 238 | * |
||
| 239 | * @return string[] The mapped file extensions. |
||
| 240 | */ |
||
| 241 | 5 | public function getTypeExtensions($type) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Changes the default extension for a MIME type. |
||
| 250 | * |
||
| 251 | * @param string $type |
||
| 252 | * A MIME type. |
||
| 253 | * @param string $extension |
||
| 254 | * A file extension. |
||
| 255 | * |
||
| 256 | * @throws MappingException if no mapping found. |
||
| 257 | * |
||
| 258 | * @return $this |
||
| 259 | */ |
||
| 260 | 3 | public function setTypeDefaultExtension($type, $extension) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Removes the entire mapping of a type. |
||
| 267 | * |
||
| 268 | * @param string $type |
||
| 269 | * A MIME type. |
||
| 270 | * |
||
| 271 | * @return bool |
||
| 272 | * true if the mapping was removed, false if the type was not present. |
||
| 273 | */ |
||
| 274 | 1 | public function removeType($type) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Removes a MIME type alias. |
||
| 298 | * |
||
| 299 | * @param string $type |
||
| 300 | * A MIME type. |
||
| 301 | * @param string $alias |
||
| 302 | * The alias to be removed. |
||
| 303 | * |
||
| 304 | * @return bool |
||
| 305 | * true if the alias was removed, false if the alias was not present. |
||
| 306 | */ |
||
| 307 | 1 | public function removeTypeAlias($type, $alias) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Removes a type-to-extension mapping. |
||
| 320 | * |
||
| 321 | * @param string $type |
||
| 322 | * A MIME type. |
||
| 323 | * @param string $extension |
||
| 324 | * The file extension to be removed. |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | * true if the mapping was removed, false if the mapping was not present. |
||
| 328 | */ |
||
| 329 | 1 | public function removeTypeExtensionMapping($type, $extension) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Gets the content of an entry from the 'extensions' array. |
||
| 342 | * |
||
| 343 | * @param string $extension The extension to be found. |
||
| 344 | * |
||
| 345 | * @return string[] The mapped MIME types. |
||
| 346 | */ |
||
| 347 | 9 | public function getExtensionTypes($extension) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Changes the default MIME type for a file extension. |
||
| 355 | * |
||
| 356 | * @param string $extension |
||
| 357 | * A file extension. |
||
| 358 | * @param string $type |
||
| 359 | * A MIME type. |
||
| 360 | * |
||
| 361 | * @throws MappingException if no mapping found. |
||
| 362 | * |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | 3 | public function setExtensionDefaultType($extension, $type) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Gets a list of entries of the map. |
||
| 372 | * |
||
| 373 | * @param string $entry |
||
| 374 | * The main array entry. |
||
| 375 | * @param string $match |
||
| 376 | * (Optional) a match wildcard to limit the list. |
||
| 377 | * |
||
| 378 | * @return array |
||
| 379 | * The list of the entries. |
||
| 380 | */ |
||
| 381 | 7 | protected function listEntries($entry, $match = null) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Gets the content of an entry of the map. |
||
| 403 | * |
||
| 404 | * @param string $entry |
||
| 405 | * The main array entry. |
||
| 406 | * @param string $entry_key |
||
| 407 | * The main entry value. |
||
| 408 | * |
||
| 409 | * @return mixed|null |
||
| 410 | * The value of the entry, or null if missing. |
||
| 411 | */ |
||
| 412 | 14 | protected function getMapEntry($entry, $entry_key) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Removes an entry from the map. |
||
| 421 | * |
||
| 422 | * @param string $entry |
||
| 423 | * The main array entry. |
||
| 424 | * @param string $entry_key |
||
| 425 | * The main entry value. |
||
| 426 | * |
||
| 427 | * @return bool |
||
| 428 | * true if the entry was removed, false if the entry was not present. |
||
| 429 | */ |
||
| 430 | 1 | protected function removeMapEntry($entry, $entry_key) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Gets the content of a subentry of the map. |
||
| 448 | * |
||
| 449 | * @param string $entry |
||
| 450 | * The main array entry. |
||
| 451 | * @param string $entry_key |
||
| 452 | * The main entry value. |
||
| 453 | * @param string $sub_entry |
||
| 454 | * The sub entry. |
||
| 455 | * |
||
| 456 | * @return mixed|null |
||
| 457 | * The value of the entry, or null if missing. |
||
| 458 | */ |
||
| 459 | 12 | protected function getMapSubEntry($entry, $entry_key, $sub_entry) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Adds an entry to the map. |
||
| 469 | * |
||
| 470 | * Checks that no duplicate entries are made. |
||
| 471 | * |
||
| 472 | * @param string $entry |
||
| 473 | * The main array entry. |
||
| 474 | * @param string $entry_key |
||
| 475 | * The main entry value. |
||
| 476 | * @param string $sub_entry |
||
| 477 | * The sub entry. |
||
| 478 | * @param string $value |
||
| 479 | * The value to add. |
||
| 480 | * |
||
| 481 | * @return $this |
||
| 482 | */ |
||
| 483 | 5 | protected function addMapSubEntry($entry, $entry_key, $sub_entry, $value) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Removes an entry from the map. |
||
| 500 | * |
||
| 501 | * @param string $entry |
||
| 502 | * The main array entry. |
||
| 503 | * @param string $entry_key |
||
| 504 | * The main entry value. |
||
| 505 | * @param string $sub_entry |
||
| 506 | * The sub entry. |
||
| 507 | * @param string $value |
||
| 508 | * The value to remove. |
||
| 509 | * |
||
| 510 | * @return bool |
||
| 511 | * true if the entry was removed, false if the entry was not present. |
||
| 512 | */ |
||
| 513 | 1 | protected function removeMapSubEntry($entry, $entry_key, $sub_entry, $value) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Sets a value as the default for an entry. |
||
| 550 | * |
||
| 551 | * @param string $entry |
||
| 552 | * The main array entry. |
||
| 553 | * @param string $entry_key |
||
| 554 | * The main entry value. |
||
| 555 | * @param string $sub_entry |
||
| 556 | * The sub entry. |
||
| 557 | * @param string $value |
||
| 558 | * The value to add. |
||
| 559 | * |
||
| 560 | * @throws MappingException if no mapping found. |
||
| 561 | * |
||
| 562 | * @return $this |
||
| 563 | */ |
||
| 564 | 6 | protected function setValueAsDefault($entry, $entry_key, $sub_entry, $value) |
|
| 593 | } |
||
| 594 |