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 | 37 | 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) |
|
| 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 | 9 | 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 | 2 | public function addTypeDescription($type, $description) |
|
| 150 | { |
||
| 151 | // Consistency checks. |
||
| 152 | 2 | if ($this->hasAlias($type)) { |
|
| 153 | 1 | throw new MappingException("Cannot add description for '{$type}', '{$type}' is an alias"); |
|
| 154 | } |
||
| 155 | |||
| 156 | 1 | $this->addMapSubEntry('t', $type, 'desc', $description); |
|
| 157 | 1 | return $this; |
|
| 158 | } |
||
| 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 | 3 | public function addTypeAlias($type, $alias) |
|
| 173 | { |
||
| 174 | 3 | $type = strtolower($type); |
|
| 175 | 3 | $alias = strtolower($alias); |
|
| 176 | |||
| 177 | // Consistency checks. |
||
| 178 | 3 | if (!$this->hasType($type)) { |
|
| 179 | 1 | throw new MappingException("Cannot set '{$alias}' as alias for '{$type}', '{$type}' not defined"); |
|
| 180 | } |
||
| 181 | 2 | if ($this->hasType($alias)) { |
|
| 182 | 2 | throw new MappingException("Cannot set '{$alias}' as alias for '{$type}', '{$alias}' is already defined as a type"); |
|
| 183 | } |
||
| 184 | |||
| 185 | 1 | $this->addMapSubEntry('t', $type, 'a', $alias); |
|
| 186 | 1 | $this->addMapSubEntry('a', $alias, 't', $type); |
|
| 187 | 1 | return $this; |
|
| 188 | } |
||
| 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 | 6 | public function addTypeExtensionMapping($type, $extension) |
|
| 203 | { |
||
| 204 | 6 | $type = strtolower($type); |
|
| 205 | 6 | $extension = strtolower($extension); |
|
| 206 | |||
| 207 | // Consistency checks. |
||
| 208 | 6 | if ($this->hasAlias($type)) { |
|
| 209 | 1 | throw new MappingException("Cannot map '{$extension}' to '{$type}', '{$type}' is an alias"); |
|
| 210 | } |
||
| 211 | |||
| 212 | // Add entry to 't'. |
||
| 213 | 5 | $this->addMapSubEntry('t', $type, 'e', $extension); |
|
| 214 | |||
| 215 | // Add entry to 'e'. |
||
| 216 | 5 | $this->addMapSubEntry('e', $extension, 't', $type); |
|
| 217 | |||
| 218 | 5 | return $this; |
|
| 219 | } |
||
| 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) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Removes a MIME type alias. |
||
| 300 | * |
||
| 301 | * @param string $type |
||
| 302 | * A MIME type. |
||
| 303 | * @param string $alias |
||
| 304 | * The alias to be removed. |
||
| 305 | * |
||
| 306 | * @return bool |
||
| 307 | * true if the alias was removed, false if the alias was not present. |
||
| 308 | */ |
||
| 309 | 1 | public function removeTypeAlias($type, $alias) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Removes a type-to-extension mapping. |
||
| 322 | * |
||
| 323 | * @param string $type |
||
| 324 | * A MIME type. |
||
| 325 | * @param string $extension |
||
| 326 | * The file extension to be removed. |
||
| 327 | * |
||
| 328 | * @return bool |
||
| 329 | * true if the mapping was removed, false if the mapping was not present. |
||
| 330 | */ |
||
| 331 | 1 | public function removeTypeExtensionMapping($type, $extension) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Gets the content of an entry from the 'extensions' array. |
||
| 344 | * |
||
| 345 | * @param string $extension The extension to be found. |
||
| 346 | * |
||
| 347 | * @return string[] The mapped MIME types. |
||
| 348 | */ |
||
| 349 | 9 | public function getExtensionTypes($extension) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Changes the default MIME type for a file extension. |
||
| 357 | * |
||
| 358 | * @param string $extension |
||
| 359 | * A file extension. |
||
| 360 | * @param string $type |
||
| 361 | * A MIME type. |
||
| 362 | * |
||
| 363 | * @throws MappingException if no mapping found. |
||
| 364 | * |
||
| 365 | * @return $this |
||
| 366 | */ |
||
| 367 | 3 | public function setExtensionDefaultType($extension, $type) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Gets a list of entries of the map. |
||
| 374 | * |
||
| 375 | * @param string $entry |
||
| 376 | * The main array entry. |
||
| 377 | * @param string $match |
||
| 378 | * (Optional) a match wildcard to limit the list. |
||
| 379 | * |
||
| 380 | * @return array |
||
| 381 | * The list of the entries. |
||
| 382 | */ |
||
| 383 | 7 | protected function listEntries($entry, $match = null) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Gets the content of an entry of the map. |
||
| 405 | * |
||
| 406 | * @param string $entry |
||
| 407 | * The main array entry. |
||
| 408 | * @param string $entry_key |
||
| 409 | * The main entry value. |
||
| 410 | * |
||
| 411 | * @return mixed|null |
||
| 412 | * The value of the entry, or null if missing. |
||
| 413 | */ |
||
| 414 | 18 | protected function getMapEntry($entry, $entry_key) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Gets the content of a subentry of the map. |
||
| 423 | * |
||
| 424 | * @param string $entry |
||
| 425 | * The main array entry. |
||
| 426 | * @param string $entry_key |
||
| 427 | * The main entry value. |
||
| 428 | * @param string $sub_entry |
||
| 429 | * The sub entry. |
||
| 430 | * |
||
| 431 | * @return mixed|null |
||
| 432 | * The value of the entry, or null if missing. |
||
| 433 | */ |
||
| 434 | 12 | protected function getMapSubEntry($entry, $entry_key, $sub_entry) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Adds an entry to the map. |
||
| 444 | * |
||
| 445 | * Checks that no duplicate entries are made. |
||
| 446 | * |
||
| 447 | * @param string $entry |
||
| 448 | * The main array entry. |
||
| 449 | * @param string $entry_key |
||
| 450 | * The main entry value. |
||
| 451 | * @param string $sub_entry |
||
| 452 | * The sub entry. |
||
| 453 | * @param string $value |
||
| 454 | * The value to add. |
||
| 455 | * |
||
| 456 | * @return $this |
||
| 457 | */ |
||
| 458 | 5 | protected function addMapSubEntry($entry, $entry_key, $sub_entry, $value) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Removes an entry from the map. |
||
| 475 | * |
||
| 476 | * @param string $entry |
||
| 477 | * The main array entry. |
||
| 478 | * @param string $entry_key |
||
| 479 | * The main entry value. |
||
| 480 | * @param string $sub_entry |
||
| 481 | * The sub entry. |
||
| 482 | * @param string $value |
||
| 483 | * The value to remove. |
||
| 484 | * |
||
| 485 | * @return bool |
||
| 486 | * true if the entry was removed, false if the entry was not present. |
||
| 487 | */ |
||
| 488 | 1 | protected function removeMapSubEntry($entry, $entry_key, $sub_entry, $value) |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Sets a value as the default for an entry. |
||
| 525 | * |
||
| 526 | * @param string $entry |
||
| 527 | * The main array entry. |
||
| 528 | * @param string $entry_key |
||
| 529 | * The main entry value. |
||
| 530 | * @param string $sub_entry |
||
| 531 | * The sub entry. |
||
| 532 | * @param string $value |
||
| 533 | * The value to add. |
||
| 534 | * |
||
| 535 | * @throws MappingException if no mapping found. |
||
| 536 | * |
||
| 537 | * @return $this |
||
| 538 | */ |
||
| 539 | 6 | protected function setValueAsDefault($entry, $entry_key, $sub_entry, $value) |
|
| 568 | } |
||
| 569 |