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) |
|
| 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 | 9 | 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) |
|
| 148 | { |
||
| 149 | // Consistency checks. |
||
| 150 | 2 | if ($this->hasAlias($type)) { |
|
| 151 | 1 | throw new MappingException("Cannot add description for '{$type}', '{$type}' is an alias"); |
|
| 152 | } |
||
| 153 | |||
| 154 | 1 | $this->addMapSubEntry('t', $type, 'desc', $description); |
|
| 155 | 1 | return $this; |
|
| 156 | } |
||
| 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) |
|
| 171 | { |
||
| 172 | 3 | $type = strtolower($type); |
|
| 173 | 3 | $alias = strtolower($alias); |
|
| 174 | |||
| 175 | // Consistency checks. |
||
| 176 | 3 | if (!$this->hasType($type)) { |
|
| 177 | 1 | throw new MappingException("Cannot set '{$alias}' as alias for '{$type}', '{$type}' not defined"); |
|
| 178 | } |
||
| 179 | 2 | if ($this->hasType($alias)) { |
|
| 180 | 2 | throw new MappingException("Cannot set '{$alias}' as alias for '{$type}', '{$alias}' is already defined as a type"); |
|
| 181 | } |
||
| 182 | |||
| 183 | 1 | $this->addMapSubEntry('t', $type, 'a', $alias); |
|
| 184 | 1 | $this->addMapSubEntry('a', $alias, 't', $type); |
|
| 185 | 1 | return $this; |
|
| 186 | } |
||
| 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) |
|
| 201 | { |
||
| 202 | 6 | $type = strtolower($type); |
|
| 203 | 6 | $extension = strtolower($extension); |
|
| 204 | |||
| 205 | // Consistency checks. |
||
| 206 | 6 | if ($this->hasAlias($type)) { |
|
| 207 | 1 | throw new MappingException("Cannot map '{$extension}' to '{$type}', '{$type}' is an alias"); |
|
| 208 | } |
||
| 209 | |||
| 210 | // Add entry to 't'. |
||
| 211 | 5 | $this->addMapSubEntry('t', $type, 'e', $extension); |
|
| 212 | |||
| 213 | // Add entry to 'e'. |
||
| 214 | 5 | $this->addMapSubEntry('e', $extension, 't', $type); |
|
| 215 | |||
| 216 | 5 | return $this; |
|
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Gets the aliases of a MIME type. |
||
| 221 | * |
||
| 222 | * @param string $type The type to be found. |
||
| 223 | * |
||
| 224 | * @return string[] The mapped aliases. |
||
| 225 | */ |
||
| 226 | 1 | public function getTypeAliases($type) |
|
| 227 | { |
||
| 228 | 1 | $res = $this->getMapSubEntry('t', $type, 'a'); |
|
| 229 | 1 | return $res ?: []; |
|
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Gets the content of an entry from the 't' array. |
||
| 234 | * |
||
| 235 | * @param string $type The type to be found. |
||
| 236 | * |
||
| 237 | * @return string[] The mapped file extensions. |
||
| 238 | */ |
||
| 239 | 5 | public function getTypeExtensions($type) |
|
| 244 | |||
| 245 | /** |
||
| 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 | 3 | public function setTypeDefaultExtension($type, $extension) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Removes the entire mapping of a type. |
||
| 264 | * |
||
| 265 | * @param string $type |
||
| 266 | * A MIME type. |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | * true if the mapping was removed, false if the type was not present. |
||
| 270 | */ |
||
| 271 | 1 | public function removeType($type) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Removes a MIME type alias. |
||
| 297 | * |
||
| 298 | * @param string $type |
||
| 299 | * A MIME type. |
||
| 300 | * @param string $alias |
||
| 301 | * The alias to be removed. |
||
| 302 | * |
||
| 303 | * @return bool |
||
| 304 | * true if the alias was removed, false if the alias was not present. |
||
| 305 | */ |
||
| 306 | 1 | 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 | * The file extension to be removed. |
||
| 324 | * |
||
| 325 | * @return bool |
||
| 326 | * true if the mapping was removed, false if the mapping was not present. |
||
| 327 | */ |
||
| 328 | 1 | 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 | 9 | public function getExtensionTypes($extension) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Changes the default MIME type for a file extension. |
||
| 354 | * |
||
| 355 | * @param string $extension |
||
| 356 | * A file extension. |
||
| 357 | * @param string $type |
||
| 358 | * A MIME type. |
||
| 359 | * |
||
| 360 | * @throws MappingException if no mapping found. |
||
| 361 | * |
||
| 362 | * @return $this |
||
| 363 | */ |
||
| 364 | 3 | 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 | * |
||
| 377 | * @return array |
||
| 378 | * The list of the entries. |
||
| 379 | */ |
||
| 380 | 7 | protected function listEntries($entry, $match = null) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * 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 | 18 | 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 | 12 | 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 | 5 | 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 | 1 | 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 | 6 | protected function setValueAsDefault($entry, $entry_key, $sub_entry, $value) |
|
| 565 | } |
||
| 566 |