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 | 32 | 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 | 7 | 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 | 3 | 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 | public function hasExtension($extension) |
||
| 98 | 7 | ||
| 99 | /** |
||
| 100 | 7 | * Lists all the MIME types defined in the map. |
|
| 101 | * |
||
| 102 | 7 | * @param string $match (Optional) a match wildcard to limit the list. |
|
| 103 | 3 | * |
|
| 104 | * @return string[] |
||
| 105 | 4 | */ |
|
| 106 | 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 | 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 | public function listExtensions($match = null) |
||
| 134 | 8 | ||
| 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 | 1 | * |
|
| 145 | * @return $this |
||
| 146 | 1 | */ |
|
| 147 | public function addTypeDescription($type, $description) |
||
| 157 | |||
| 158 | 10 | /** |
|
| 159 | 10 | * 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 | public function addTypeAlias($type, $alias) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Adds a type-to-extension mapping. |
||
| 190 | * |
||
| 191 | * @param string $type |
||
| 192 | 5 | * A MIME type. |
|
| 193 | * @param string $extension |
||
| 194 | 5 | * A file extension. |
|
| 195 | 5 | * |
|
| 196 | * @throws MappingException if $type is an alias. |
||
| 197 | 4 | * |
|
| 198 | 4 | * @return $this |
|
| 199 | */ |
||
| 200 | public function addTypeExtensionMapping($type, $extension) |
||
| 218 | |||
| 219 | /** |
||
| 220 | 1 | * Gets the aliases of a MIME type. |
|
| 221 | 1 | * |
|
| 222 | * @param string $type The type to be found. |
||
| 223 | * |
||
| 224 | * @return string[] The mapped aliases. |
||
| 225 | 1 | */ |
|
| 226 | 1 | public function getTypeAliases($type) |
|
| 231 | 1 | ||
| 232 | /** |
||
| 233 | * Gets the content of an entry from the 't' array. |
||
| 234 | 1 | * |
|
| 235 | 1 | * @param string $type The type to be found. |
|
| 236 | * |
||
| 237 | * @return string[] The mapped file extensions. |
||
| 238 | 1 | */ |
|
| 239 | 1 | public function getTypeExtensions($type) |
|
| 244 | |||
| 245 | 1 | /** |
|
| 246 | * Changes the default extension for a MIME type. |
||
| 247 | * |
||
| 248 | * @param string $type |
||
| 249 | * A MIME type. |
||
| 250 | * @param string $extension |
||
| 251 | * A file extension. |
||
| 252 | * |
||
| 253 | * @throws MappingException if no mapping found. |
||
| 254 | * |
||
| 255 | * @return $this |
||
| 256 | */ |
||
| 257 | public function setTypeDefaultExtension($type, $extension) |
||
| 261 | |||
| 262 | 6 | /** |
|
| 263 | * Removes the entire mapping of a type. |
||
| 264 | * |
||
| 265 | 6 | * @param string $type |
|
| 266 | 2 | * A MIME type. |
|
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | * true if the mapping was removed, false if the type was not present. |
||
| 270 | 4 | */ |
|
| 271 | 4 | public function removeType($type) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Removes a MIME type alias. |
||
| 297 | * |
||
| 298 | 5 | * @param string $type |
|
| 299 | * A MIME type. |
||
| 300 | 5 | * @param string $alias |
|
| 301 | 5 | * The alias to be removed. |
|
| 302 | * |
||
| 303 | * @return bool |
||
| 304 | 5 | * true if the alias was removed, false if the alias was not present. |
|
| 305 | */ |
||
| 306 | public function removeTypeAlias($type, $alias) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Removes a type-to-extension mapping. |
||
| 319 | * |
||
| 320 | * @param string $type |
||
| 321 | * A MIME type. |
||
| 322 | * @param string $extension |
||
| 323 | 1 | * The file extension to be removed. |
|
| 324 | * |
||
| 325 | 1 | * @return bool |
|
| 326 | 1 | * true if the mapping was removed, false if the mapping was not present. |
|
| 327 | */ |
||
| 328 | public function removeTypeExtensionMapping($type, $extension) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Gets the content of an entry from the 'extensions' array. |
||
| 341 | * |
||
| 342 | * @param string $extension The extension to be found. |
||
| 343 | * |
||
| 344 | * @return string[] The mapped MIME types. |
||
| 345 | */ |
||
| 346 | 1 | public function getExtensionTypes($extension) |
|
| 351 | 1 | ||
| 352 | 1 | /** |
|
| 353 | * Changes the default MIME type for a file extension. |
||
| 354 | * |
||
| 355 | * @param string $extension |
||
| 356 | * A file extension. |
||
| 357 | 1 | * @param string $type |
|
| 358 | 1 | * A MIME type. |
|
| 359 | * |
||
| 360 | * @throws MappingException if no mapping found. |
||
| 361 | 1 | * |
|
| 362 | * @return $this |
||
| 363 | */ |
||
| 364 | public function setExtensionDefaultType($extension, $type) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Gets a list of entries of the map. |
||
| 371 | * |
||
| 372 | * @param string $entry |
||
| 373 | * The main array entry. |
||
| 374 | * @param string $match |
||
| 375 | * (Optional) a match wildcard to limit the list. |
||
| 376 | 3 | * |
|
| 377 | * @return array |
||
| 378 | 3 | * The list of the entries. |
|
| 379 | 3 | */ |
|
| 380 | protected function listEntries($entry, $match = null) |
||
| 399 | 3 | ||
| 400 | /** |
||
| 401 | 3 | * Gets the content of an entry of the map. |
|
| 402 | * |
||
| 403 | * @param string $entry |
||
| 404 | * The main array entry. |
||
| 405 | * @param string $entry_key |
||
| 406 | * The main entry value. |
||
| 407 | * |
||
| 408 | * @return mixed|null |
||
| 409 | * The value of the entry, or null if missing. |
||
| 410 | */ |
||
| 411 | protected function getMapEntry($entry, $entry_key) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Gets the content of a subentry of the map. |
||
| 420 | * |
||
| 421 | * @param string $entry |
||
| 422 | * The main array entry. |
||
| 423 | * @param string $entry_key |
||
| 424 | * The main entry value. |
||
| 425 | * @param string $sub_entry |
||
| 426 | * The sub entry. |
||
| 427 | * |
||
| 428 | * @return mixed|null |
||
| 429 | * The value of the entry, or null if missing. |
||
| 430 | */ |
||
| 431 | protected function getMapSubEntry($entry, $entry_key, $sub_entry) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Adds an entry to the map. |
||
| 441 | * |
||
| 442 | * Checks that no duplicate entries are made. |
||
| 443 | * |
||
| 444 | * @param string $entry |
||
| 445 | * The main array entry. |
||
| 446 | * @param string $entry_key |
||
| 447 | * The main entry value. |
||
| 448 | * @param string $sub_entry |
||
| 449 | * The sub entry. |
||
| 450 | * @param string $value |
||
| 451 | * The value to add. |
||
| 452 | * |
||
| 453 | * @return $this |
||
| 454 | */ |
||
| 455 | protected function addMapSubEntry($entry, $entry_key, $sub_entry, $value) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Removes an entry from the map. |
||
| 472 | * |
||
| 473 | * @param string $entry |
||
| 474 | * The main array entry. |
||
| 475 | * @param string $entry_key |
||
| 476 | * The main entry value. |
||
| 477 | * @param string $sub_entry |
||
| 478 | * The sub entry. |
||
| 479 | * @param string $value |
||
| 480 | * The value to remove. |
||
| 481 | * |
||
| 482 | * @return bool |
||
| 483 | * true if the entry was removed, false if the entry was not present. |
||
| 484 | */ |
||
| 485 | protected function removeMapSubEntry($entry, $entry_key, $sub_entry, $value) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Sets a value as the default for an entry. |
||
| 522 | * |
||
| 523 | * @param string $entry |
||
| 524 | * The main array entry. |
||
| 525 | * @param string $entry_key |
||
| 526 | * The main entry value. |
||
| 527 | * @param string $sub_entry |
||
| 528 | * The sub entry. |
||
| 529 | * @param string $value |
||
| 530 | * The value to add. |
||
| 531 | * |
||
| 532 | * @throws MappingException if no mapping found. |
||
| 533 | * |
||
| 534 | * @return $this |
||
| 535 | */ |
||
| 536 | protected function setValueAsDefault($entry, $entry_key, $sub_entry, $value) |
||
| 565 | } |
||
| 566 |