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:
| 1 | <?php |
||
| 12 | abstract class AbstractMap |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Returns the singleton. |
||
| 16 | * |
||
| 17 | * @return string |
||
| 18 | */ |
||
| 19 | 24 | public static function getInstance() |
|
| 26 | |||
| 27 | /** |
||
| 28 | * Returns this file's full qualified filename. |
||
| 29 | * |
||
| 30 | * @return string |
||
| 31 | */ |
||
| 32 | public function getFileName() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Gets the map array. |
||
| 39 | * |
||
| 40 | * @return array |
||
| 41 | */ |
||
| 42 | 3 | public function getMapArray() |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Sorts the map. |
||
| 49 | * |
||
| 50 | * @return $this |
||
| 51 | */ |
||
| 52 | 1 | public function sort() |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Lists all the MIME types defined in the map. |
||
| 61 | * |
||
| 62 | * @param string $match (Optional) a match wildcard to limit the list. |
||
| 63 | * |
||
| 64 | * @return string[] |
||
| 65 | */ |
||
| 66 | 6 | View Code Duplication | public function listTypes($match = null) |
| 79 | |||
| 80 | /** |
||
| 81 | * Lists all the extensions defined in the map. |
||
| 82 | * |
||
| 83 | * @param string $match (Optional) a match wildcard to limit the list. |
||
| 84 | * |
||
| 85 | * @return string[] |
||
| 86 | */ |
||
| 87 | 2 | View Code Duplication | public function listExtensions($match = null) |
| 100 | |||
| 101 | /** |
||
| 102 | * Determines if an entry exists form the 'types' array. |
||
| 103 | * |
||
| 104 | * @param string $type The type to be found. |
||
| 105 | * |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | 6 | public function hasType($type) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Gets the content of an entry from the 'types' array. |
||
| 115 | * |
||
| 116 | * @param string $type The extension to be found. |
||
| 117 | * |
||
| 118 | * @return string[] The mapped file extensions. |
||
| 119 | */ |
||
| 120 | 6 | public function getType($type) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Determines if an entry exists form the 'extensions' array. |
||
| 128 | * |
||
| 129 | * @param string $extension The extension to be found. |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | public function hasExtension($extension) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Gets the content of an entry from the 'extensions' array. |
||
| 140 | * |
||
| 141 | * @param string $extension The extension to be found. |
||
| 142 | * |
||
| 143 | * @return string[] The mapped MIME types. |
||
| 144 | */ |
||
| 145 | 9 | public function getExtension($extension) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Gets the content of an entry of the map. |
||
| 153 | * |
||
| 154 | * @param string $key |
||
| 155 | * The main array key. |
||
| 156 | * @param string $entry |
||
| 157 | * The sub array key. |
||
| 158 | * |
||
| 159 | * @return mixed|null |
||
| 160 | * The value of the entry, or null if missing. |
||
| 161 | */ |
||
| 162 | 13 | protected function getMapEntry($key, $entry) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Adds an entry to the map. |
||
| 169 | * |
||
| 170 | * Checks that no duplicate entries are made. |
||
| 171 | * |
||
| 172 | * @param string $key |
||
| 173 | * The main array key. |
||
| 174 | * @param string $entry |
||
| 175 | * The sub array key. |
||
| 176 | * @param string $value |
||
| 177 | * The value to add. |
||
| 178 | * |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | 4 | protected function addMapEntry($key, $entry, $value) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Removes an entry from the map. |
||
| 195 | * |
||
| 196 | * @param string $key |
||
| 197 | * The main array key. |
||
| 198 | * @param string $entry |
||
| 199 | * The sub array key. |
||
| 200 | * @param string $value |
||
| 201 | * The value. |
||
| 202 | * |
||
| 203 | * @return bool |
||
| 204 | * true if the entry was removed, false if the entry was not present. |
||
| 205 | */ |
||
| 206 | 1 | protected function removeMapEntry($key, $entry, $value) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Sets a value as the default for an entry. |
||
| 239 | * |
||
| 240 | * @param string $key |
||
| 241 | * The main array key. |
||
| 242 | * @param string $entry |
||
| 243 | * The sub array key. |
||
| 244 | * @param string $value |
||
| 245 | * The value. |
||
| 246 | * |
||
| 247 | * @throws MappingException if no mapping found. |
||
| 248 | * |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | 6 | protected function setValueAsDefault($key, $entry, $value) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Adds a type-to-extension mapping. |
||
| 279 | * |
||
| 280 | * @param string $type |
||
| 281 | * A MIME type. |
||
| 282 | * @param string $extension |
||
| 283 | * A file extension. |
||
| 284 | * |
||
| 285 | * @return $this |
||
| 286 | */ |
||
| 287 | 4 | public function addMapping($type, $extension) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Removes a type-to-extension mapping. |
||
| 303 | * |
||
| 304 | * @param string $type |
||
| 305 | * A MIME type. |
||
| 306 | * @param string $extension |
||
| 307 | * A file extension. |
||
| 308 | * |
||
| 309 | * @return bool |
||
| 310 | * true if the mapping was removed, false if the mapping was not present. |
||
| 311 | */ |
||
| 312 | 1 | public function removeMapping($type, $extension) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Removes the entire mapping of a type. |
||
| 328 | * |
||
| 329 | * @param string $type |
||
| 330 | * A MIME type. |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | * true if the mapping was removed, false if the type was not present. |
||
| 334 | */ |
||
| 335 | 1 | public function removeType($type) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Changes the default extension for a MIME type. |
||
| 355 | * |
||
| 356 | * @param string $type |
||
| 357 | * A MIME type. |
||
| 358 | * @param string $extension |
||
| 359 | * A file extension. |
||
| 360 | * |
||
| 361 | * @throws MappingException if no mapping found. |
||
| 362 | * |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | 3 | public function setTypeDefaultExtension($type, $extension) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Changes the default MIME type for a file extension. |
||
| 375 | * |
||
| 376 | * @param string $extension |
||
| 377 | * A file extension. |
||
| 378 | * @param string $type |
||
| 379 | * A MIME type. |
||
| 380 | * |
||
| 381 | * @throws MappingException if no mapping found. |
||
| 382 | * |
||
| 383 | * @return $this |
||
| 384 | */ |
||
| 385 | 3 | public function setExtensionDefaultType($extension, $type) |
|
| 392 | } |
||
| 393 |
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.