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 |
||
| 8 | class MapHandler |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Mapping between file extensions and MIME types. |
||
| 12 | * |
||
| 13 | * The array has two main keys, 'types' that maps MIME types to file |
||
| 14 | * extensions, and 'extensions' that map file extensions to MIME types. |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected $map; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Constructor. |
||
| 22 | * |
||
| 23 | * @param array $map |
||
| 24 | * (Optional) The mapping to be used for this instance. If null, the |
||
| 25 | * default map will be used. |
||
| 26 | */ |
||
| 27 | 17 | public function __construct(array $map = null) |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Returns the map associated with this instance. |
||
| 38 | * |
||
| 39 | * @return array |
||
| 40 | */ |
||
| 41 | 13 | public function get() |
|
| 45 | |||
| 46 | /** |
||
| 47 | * Sorts the map. |
||
| 48 | * |
||
| 49 | * @return $this |
||
| 50 | */ |
||
| 51 | 1 | public function sort() |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Adds an entry to the map. |
||
| 60 | * |
||
| 61 | * Checks that no duplicate entries are made. |
||
| 62 | * |
||
| 63 | * @param string $key |
||
| 64 | * The main array key. |
||
| 65 | * @param string $entry |
||
| 66 | * The sub array key. |
||
| 67 | * @param string $value |
||
| 68 | * The value to add. |
||
| 69 | * |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | 2 | protected function addMapEntry($key, $entry, $value) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Removes an entry from the map. |
||
| 86 | * |
||
| 87 | * @param string $key |
||
| 88 | * The main array key. |
||
| 89 | * @param string $entry |
||
| 90 | * The sub array key. |
||
| 91 | * @param string $value |
||
| 92 | * The value. |
||
| 93 | * |
||
| 94 | * @return bool |
||
| 95 | * true if the entry was removed, false if the entry was not present. |
||
| 96 | */ |
||
| 97 | 1 | protected function removeMapEntry($key, $entry, $value) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Adds a type-to-extension mapping. |
||
| 130 | * |
||
| 131 | * @param string $type |
||
| 132 | * A MIME type. |
||
| 133 | * @param string $extension |
||
| 134 | * A file extension. |
||
| 135 | * |
||
| 136 | * @return $this |
||
| 137 | */ |
||
| 138 | 2 | public function addMapping($type, $extension) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Removes a type-to-extension mapping. |
||
| 154 | * |
||
| 155 | * @param string $type |
||
| 156 | * A MIME type. |
||
| 157 | * @param string $extension |
||
| 158 | * A file extension. |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | * true if the mapping was removed, false if the mapping was not present. |
||
| 162 | */ |
||
| 163 | 1 | public function removeMapping($type, $extension) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Removes the entire mapping of a type. |
||
| 179 | * |
||
| 180 | * @param string $type |
||
| 181 | * A MIME type. |
||
| 182 | * |
||
| 183 | * @return bool |
||
| 184 | * true if the mapping was removed, false if the type was not present. |
||
| 185 | */ |
||
| 186 | 1 | public function removeType($type) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Changes the default extension for a MIME type. |
||
| 206 | * |
||
| 207 | * @param string $type |
||
| 208 | * A MIME type. |
||
| 209 | * @param string $extension |
||
| 210 | * A file extension. |
||
| 211 | * |
||
| 212 | * @throws MappingException if no mapping found. |
||
| 213 | * |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | 3 | View Code Duplication | public function setTypeDefaultExtension($type, $extension) |
| 238 | |||
| 239 | /** |
||
| 240 | * Changes the default MIME type for a file extension. |
||
| 241 | * |
||
| 242 | * @param string $extension |
||
| 243 | * A file extension. |
||
| 244 | * @param string $type |
||
| 245 | * A MIME type. |
||
| 246 | * |
||
| 247 | * @throws MappingException if no mapping found. |
||
| 248 | * |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | 3 | View Code Duplication | public function setExtensionDefaultType($extension, $type) |
| 273 | } |
||
| 274 |
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.