Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Determines if a MIME type exists. |
||
| 78 | * |
||
| 79 | * @param string $type The type to be found. |
||
| 80 | * |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | 9 | public function hasType($type) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Determines if a MIME type alias exists. |
||
| 91 | * |
||
| 92 | * @param string $alias The alias to be found. |
||
| 93 | * |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | 5 | public function hasAlias($alias) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Determines if an entry exists from the 'extensions' array. |
||
| 103 | * |
||
| 104 | * @param string $extension The extension to be found. |
||
| 105 | * |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | 1 | public function hasExtension($extension) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Lists all the MIME types defined in the map. |
||
| 115 | * |
||
| 116 | * @param string $match (Optional) a match wildcard to limit the list. |
||
| 117 | * |
||
| 118 | * @return string[] |
||
| 119 | */ |
||
| 120 | 7 | public function listTypes($match = null) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Lists all the MIME types aliases defined in the map. |
||
| 128 | * |
||
| 129 | * @param string $match (Optional) a match wildcard to limit the list. |
||
| 130 | * |
||
| 131 | * @return string[] |
||
| 132 | */ |
||
| 133 | public function listAliases($match = null) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Lists all the extensions defined in the map. |
||
| 140 | * |
||
| 141 | * @param string $match (Optional) a match wildcard to limit the list. |
||
| 142 | * |
||
| 143 | * @return string[] |
||
| 144 | */ |
||
| 145 | 3 | public function listExtensions($match = null) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Adds a description of a MIME type. |
||
| 152 | * |
||
| 153 | * @param string $type |
||
| 154 | * A MIME type. |
||
| 155 | * @param string $description |
||
| 156 | * The description of the MIME type. |
||
| 157 | * |
||
| 158 | * @throws MappingException if $type is an alias. |
||
| 159 | * |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | 1 | public function addTypeDescription($type, $description) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Adds an alias of a MIME type. |
||
| 175 | * |
||
| 176 | * @param string $type |
||
| 177 | * A MIME type. |
||
| 178 | * @param string $alias |
||
| 179 | * An alias of $type. |
||
| 180 | * |
||
| 181 | * @throws MappingException if no $type is found. |
||
| 182 | * |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | 1 | public function addTypeAlias($type, $alias) |
|
| 186 | { |
||
| 187 | 1 | $type = strtolower($type); |
|
| 188 | 1 | $alias = strtolower($alias); |
|
| 189 | |||
| 190 | // Consistency checks. |
||
| 191 | 1 | if (!$this->hasType($type)) { |
|
| 192 | throw new MappingException("Cannot set '{$alias}' as alias for '{$type}', '{$type}' not defined"); |
||
| 193 | } |
||
| 194 | 1 | if ($this->hasType($alias)) { |
|
| 195 | throw new MappingException("Cannot set '{$alias}' as alias for '{$type}', '{$alias}' is already defined as a type"); |
||
| 196 | } |
||
| 197 | |||
| 198 | 1 | $this->addMapSubEntry('t', $type, 'a', $alias); |
|
| 199 | 1 | $this->addMapSubEntry('a', $alias, 't', $type); |
|
| 200 | 1 | return $this; |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Adds a type-to-extension mapping. |
||
| 205 | * |
||
| 206 | * @param string $type |
||
| 207 | * A MIME type. |
||
| 208 | * @param string $extension |
||
| 209 | * A file extension. |
||
| 210 | * |
||
| 211 | * @throws MappingException if $type is an alias. |
||
| 212 | * |
||
| 213 | * @return $this |
||
| 214 | */ |
||
| 215 | 5 | public function addTypeExtensionMapping($type, $extension) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Gets the aliases of a MIME type. |
||
| 236 | * |
||
| 237 | * @param string $type The type to be found. |
||
| 238 | * |
||
| 239 | * @return string[] The mapped aliases. |
||
| 240 | */ |
||
| 241 | 1 | public function getTypeAliases($type) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Gets the content of an entry from the 't' array. |
||
| 249 | * |
||
| 250 | * @param string $type The type to be found. |
||
| 251 | * |
||
| 252 | * @return string[] The mapped file extensions. |
||
| 253 | */ |
||
| 254 | 5 | public function getTypeExtensions($type) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Changes the default extension for a MIME type. |
||
| 263 | * |
||
| 264 | * @param string $type |
||
| 265 | * A MIME type. |
||
| 266 | * @param string $extension |
||
| 267 | * A file extension. |
||
| 268 | * |
||
| 269 | * @throws MappingException if no mapping found. |
||
| 270 | * |
||
| 271 | * @return $this |
||
| 272 | */ |
||
| 273 | 3 | public function setTypeDefaultExtension($type, $extension) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Removes the entire mapping of a type. |
||
| 280 | * |
||
| 281 | * @param string $type |
||
| 282 | * A MIME type. |
||
| 283 | * |
||
| 284 | * @return bool |
||
| 285 | * true if the mapping was removed, false if the type was not present. |
||
| 286 | */ |
||
| 287 | 1 | public function removeType($type) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Removes a MIME type alias. |
||
| 311 | * |
||
| 312 | * @param string $type |
||
| 313 | * A MIME type. |
||
| 314 | * @param string $alias |
||
| 315 | * The alias to be removed. |
||
| 316 | * |
||
| 317 | * @return bool |
||
| 318 | * true if the alias was removed, false if the alias was not present. |
||
| 319 | */ |
||
| 320 | public function removeTypeAlias($type, $alias) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Removes a type-to-extension mapping. |
||
| 333 | * |
||
| 334 | * @param string $type |
||
| 335 | * A MIME type. |
||
| 336 | * @param string $extension |
||
| 337 | * The file extension to be removed. |
||
| 338 | * |
||
| 339 | * @return bool |
||
| 340 | * true if the mapping was removed, false if the mapping was not present. |
||
| 341 | */ |
||
| 342 | 1 | public function removeTypeExtensionMapping($type, $extension) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Gets the content of an entry from the 'extensions' array. |
||
| 355 | * |
||
| 356 | * @param string $extension The extension to be found. |
||
| 357 | * |
||
| 358 | * @return string[] The mapped MIME types. |
||
| 359 | */ |
||
| 360 | 9 | public function getExtensionTypes($extension) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Changes the default MIME type for a file extension. |
||
| 368 | * |
||
| 369 | * @param string $extension |
||
| 370 | * A file extension. |
||
| 371 | * @param string $type |
||
| 372 | * A MIME type. |
||
| 373 | * |
||
| 374 | * @throws MappingException if no mapping found. |
||
| 375 | * |
||
| 376 | * @return $this |
||
| 377 | */ |
||
| 378 | 3 | public function setExtensionDefaultType($extension, $type) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Gets a list of entries of the map. |
||
| 385 | * |
||
| 386 | * @param string $entry |
||
| 387 | * The main array entry. |
||
| 388 | * @param string $match |
||
| 389 | * (Optional) a match wildcard to limit the list. |
||
| 390 | * |
||
| 391 | * @return mixed|null |
||
| 392 | * The value of the entry, or null if missing. |
||
| 393 | */ |
||
| 394 | 7 | protected function listEntries($entry, $match = null) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Gets the content of an entry of the map. |
||
| 411 | * |
||
| 412 | * @param string $entry |
||
| 413 | * The main array entry. |
||
| 414 | * @param string $entry_key |
||
| 415 | * The main entry value. |
||
| 416 | * |
||
| 417 | * @return mixed|null |
||
| 418 | * The value of the entry, or null if missing. |
||
| 419 | */ |
||
| 420 | 13 | protected function getMapEntry($entry, $entry_key) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Removes an entry from the map. |
||
| 429 | * |
||
| 430 | * @param string $entry |
||
| 431 | * The main array entry. |
||
| 432 | * @param string $entry_key |
||
| 433 | * The main entry value. |
||
| 434 | * |
||
| 435 | * @return bool |
||
| 436 | * true if the entry was removed, false if the entry was not present. |
||
| 437 | */ |
||
| 438 | 1 | protected function removeMapEntry($entry, $entry_key) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Gets the content of a subentry of the map. |
||
| 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 | * |
||
| 464 | * @return mixed|null |
||
| 465 | * The value of the entry, or null if missing. |
||
| 466 | */ |
||
| 467 | 12 | protected function getMapSubEntry($entry, $entry_key, $sub_entry) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Adds an entry to the map. |
||
| 477 | * |
||
| 478 | * Checks that no duplicate entries are made. |
||
| 479 | * |
||
| 480 | * @param string $entry |
||
| 481 | * The main array entry. |
||
| 482 | * @param string $entry_key |
||
| 483 | * The main entry value. |
||
| 484 | * @param string $sub_entry |
||
| 485 | * The sub entry. |
||
| 486 | * @param string $value |
||
| 487 | * The value to add. |
||
| 488 | * |
||
| 489 | * @return $this |
||
| 490 | */ |
||
| 491 | 5 | protected function addMapSubEntry($entry, $entry_key, $sub_entry, $value) |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Removes an entry from the map. |
||
| 508 | * |
||
| 509 | * @param string $entry |
||
| 510 | * The main array entry. |
||
| 511 | * @param string $entry_key |
||
| 512 | * The main entry value. |
||
| 513 | * @param string $sub_entry |
||
| 514 | * The sub entry. |
||
| 515 | * @param string $value |
||
| 516 | * The value to remove. |
||
| 517 | * |
||
| 518 | * @return bool |
||
| 519 | * true if the entry was removed, false if the entry was not present. |
||
| 520 | */ |
||
| 521 | 1 | protected function removeMapSubEntry($entry, $entry_key, $sub_entry, $value) |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Sets a value as the default for an entry. |
||
| 558 | * |
||
| 559 | * @param string $entry |
||
| 560 | * The main array entry. |
||
| 561 | * @param string $entry_key |
||
| 562 | * The main entry value. |
||
| 563 | * @param string $sub_entry |
||
| 564 | * The sub entry. |
||
| 565 | * @param string $value |
||
| 566 | * The value to add. |
||
| 567 | * |
||
| 568 | * @throws MappingException if no mapping found. |
||
| 569 | * |
||
| 570 | * @return $this |
||
| 571 | */ |
||
| 572 | 6 | protected function setValueAsDefault($entry, $entry_key, $sub_entry, $value) |
|
| 601 | } |
||
| 602 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.