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 Type 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 Type, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Type |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Short format [e.g. image/jpeg] for strings. |
||
| 12 | */ |
||
| 13 | const SHORT_TEXT = 0; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Full format [e.g. image/jpeg; p="1"] for strings. |
||
| 17 | */ |
||
| 18 | const FULL_TEXT = 1; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Full format with comments [e.g. image/jpeg; p="1" (comment)] for strings. |
||
| 22 | */ |
||
| 23 | const FULL_TEXT_WITH_COMMENTS = 2; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The MIME media type. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $media; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The MIME media type comment. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $mediaComment; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The MIME media sub-type. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $subType; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The MIME media sub-type comment. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $subTypeComment; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Optional MIME parameters |
||
| 55 | * |
||
| 56 | * @var TypeParameter[] |
||
| 57 | */ |
||
| 58 | protected $parameters = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Constructor. |
||
| 62 | * |
||
| 63 | * The type string will be parsed and the appropriate class vars set. |
||
| 64 | * |
||
| 65 | * @param string $type MIME type |
||
| 66 | */ |
||
| 67 | 54 | public function __construct($type) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Parse a mime-type and set the class variables. |
||
| 74 | * |
||
| 75 | * @param string $type MIME type to parse |
||
| 76 | * |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | 54 | protected function parse($type) |
|
| 80 | { |
||
| 81 | // Media and SubType are separated by a slash '/'. |
||
| 82 | 54 | $media = TypeParser::parseStringPart($type, 0, '/'); |
|
| 83 | |||
| 84 | 53 | if (!$media['string']) { |
|
| 85 | 3 | throw new MalformedTypeException('Media type not found'); |
|
| 86 | } |
||
| 87 | 50 | if (!$media['delimiter_matched']) { |
|
| 88 | 1 | throw new MalformedTypeException('Slash \'/\' to separate media type and subtype not found'); |
|
| 89 | } |
||
| 90 | |||
| 91 | 49 | $this->media = strtolower($media['string']); |
|
| 92 | 49 | $this->mediaComment = $media['comment']; |
|
| 93 | |||
| 94 | // SubType and Parameters are separated by semicolons ';'. |
||
| 95 | 49 | $sub = TypeParser::parseStringPart($type, $media['end_offset'] + 1, ';'); |
|
| 96 | |||
| 97 | 48 | if (!$sub['string']) { |
|
| 98 | 1 | throw new MalformedTypeException('Media subtype not found'); |
|
| 99 | } |
||
| 100 | |||
| 101 | 47 | $this->subType = strtolower($sub['string']); |
|
| 102 | 47 | $this->subTypeComment = $sub['comment']; |
|
| 103 | |||
| 104 | // Loops through the parameter. |
||
| 105 | 47 | while ($sub['delimiter_matched']) { |
|
| 106 | 26 | $sub = TypeParser::parseStringPart($type, $sub['end_offset'] + 1, ';'); |
|
| 107 | 26 | $tmp = explode('=', $sub['string'], 2); |
|
| 108 | 26 | $p_name = trim($tmp[0]); |
|
| 109 | 26 | $p_val = trim($tmp[1]); |
|
| 110 | 26 | if ($p_val[0] == '"' && $p_val[strlen($p_val) - 1] == '"') { |
|
| 111 | $p_val = substr($p_val, 1, -1); |
||
| 112 | } |
||
| 113 | 26 | $p_val = str_replace('\\"', '"', $p_val); |
|
| 114 | 26 | $this->addParameter($p_name, $p_val, $sub['comment']); |
|
| 115 | } |
||
| 116 | 47 | } |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Does this type have any parameters? |
||
| 120 | * |
||
| 121 | * @return boolean true if type has parameters, false otherwise |
||
| 122 | */ |
||
| 123 | 28 | public function hasParameters() |
|
| 124 | { |
||
| 125 | 28 | return (bool) $this->parameters; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get a MIME type's parameters |
||
| 130 | * |
||
| 131 | * @return TypeParameter[] Type's parameters |
||
| 132 | */ |
||
| 133 | 29 | public function getParameters() |
|
| 134 | { |
||
| 135 | 29 | return $this->parameters; |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get a MIME type's parameter |
||
| 140 | * |
||
| 141 | * @param string $name Parameter name |
||
| 142 | * |
||
| 143 | * @return TypeParameter|null |
||
| 144 | */ |
||
| 145 | 23 | public function getParameter($name) |
|
| 146 | { |
||
| 147 | 23 | return isset($this->parameters[$name]) ? $this->parameters[$name] : null; |
|
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Gets a MIME type's media. |
||
| 152 | * |
||
| 153 | * Note: 'media' refers to the portion before the first slash. |
||
| 154 | * |
||
| 155 | * @return string Type's media. |
||
| 156 | */ |
||
| 157 | 41 | public function getMedia() |
|
| 158 | { |
||
| 159 | 41 | return $this->media; |
|
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Gets a MIME type's media comment. |
||
| 164 | * |
||
| 165 | * @return string Type's media comment. |
||
| 166 | */ |
||
| 167 | 28 | public function getMediaComment() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Sets a MIME type's media comment. |
||
| 174 | * |
||
| 175 | * @param string Type's media comment. |
||
| 176 | * |
||
| 177 | 42 | * @return $this |
|
| 178 | */ |
||
| 179 | 42 | public function setMediaComment($comment) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Gets a MIME type's subtype. |
||
| 187 | 28 | * |
|
| 188 | * @return string Type's subtype, null if invalid mime type. |
||
| 189 | 28 | */ |
|
| 190 | public function getSubType() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Gets a MIME type's subtype comment. |
||
| 197 | * |
||
| 198 | * @return string Type's subtype comment, null if invalid mime type. |
||
| 199 | */ |
||
| 200 | public function getSubTypeComment() |
||
| 201 | 41 | { |
|
| 202 | return $this->subTypeComment; |
||
| 203 | 41 | } |
|
| 204 | 41 | ||
| 205 | 2 | /** |
|
| 206 | * Sets a MIME type's subtype comment. |
||
| 207 | 41 | * |
|
| 208 | 41 | * @param string Type's subtype comment. |
|
| 209 | 5 | * |
|
| 210 | * @return $this |
||
| 211 | 41 | */ |
|
| 212 | 23 | public function getSubTypeComment($comment) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Create a textual MIME type from object values |
||
| 220 | * |
||
| 221 | * This function performs the opposite function of parse(). |
||
| 222 | * |
||
| 223 | * @param int $format The format of the output string. |
||
| 224 | * |
||
| 225 | * @return string MIME type string |
||
| 226 | */ |
||
| 227 | 1 | public function toString($format = Type::FULL_TEXT) |
|
| 244 | 1 | ||
| 245 | 1 | /** |
|
| 246 | * Is this type experimental? |
||
| 247 | 1 | * |
|
| 248 | * Note: Experimental types are denoted by a leading 'x-' in the media or |
||
| 249 | * subtype, e.g. text/x-vcard or x-world/x-vrml. |
||
| 250 | * |
||
| 251 | * @return boolean true if type is experimental, false otherwise |
||
| 252 | */ |
||
| 253 | public function isExperimental() |
||
| 260 | 9 | ||
| 261 | /** |
||
| 262 | * Is this a vendor MIME type? |
||
| 263 | * |
||
| 264 | * Note: Vendor types are denoted with a leading 'vnd. in the subtype. |
||
| 265 | * |
||
| 266 | * @return boolean true if type is a vendor type, false otherwise |
||
| 267 | */ |
||
| 268 | public function isVendor() |
||
| 275 | |||
| 276 | 1 | /** |
|
| 277 | * Is this a wildcard type? |
||
| 278 | 1 | * |
|
| 279 | 1 | * @return boolean true if type is a wildcard, false otherwise. |
|
| 280 | */ |
||
| 281 | public function isWildcard() |
||
| 288 | 1 | ||
| 289 | /** |
||
| 290 | * Is this an alias? |
||
| 291 | * |
||
| 292 | * @return boolean true if type is an alias, false otherwise. |
||
| 293 | */ |
||
| 294 | public function isAlias() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Perform a wildcard match on a MIME type |
||
| 307 | * |
||
| 308 | * Example: |
||
| 309 | * $type = new Type('image/png'); |
||
| 310 | * $type->wildcardMatch('image/*'); |
||
| 311 | * |
||
| 312 | 1 | * @param string $wildcard Wildcard to check against |
|
| 313 | * |
||
| 314 | 1 | * @return boolean true if there was a match, false otherwise |
|
| 315 | 1 | */ |
|
| 316 | public function wildcardMatch($wildcard) |
||
| 332 | 7 | ||
| 333 | /** |
||
| 334 | 7 | * Add a parameter to this type |
|
| 335 | 4 | * |
|
| 336 | * @param string $name Parameter name |
||
| 337 | 3 | * @param string $value Parameter value |
|
| 338 | * @param string $comment Comment for this parameter |
||
| 339 | * |
||
| 340 | 7 | * @return void |
|
| 341 | 5 | */ |
|
| 342 | 4 | public function addParameter($name, $value, $comment = null) |
|
| 346 | |||
| 347 | /** |
||
| 348 | 3 | * Remove a parameter from this type |
|
| 349 | * |
||
| 350 | * @param string $name Parameter name |
||
| 351 | * |
||
| 352 | * @return void |
||
| 353 | */ |
||
| 354 | public function removeParameter($name) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Builds a list of MIME types. |
||
| 361 | * |
||
| 362 | * If the current type is a wildcard, than all the types matching the |
||
| 363 | * wildcard will be returned. |
||
| 364 | * |
||
| 365 | * @param bool $strict |
||
| 366 | 6 | * (Optional) if true a MappingException is thrown when no type is |
|
| 367 | * found, if false it returns an empty array as a default. |
||
| 368 | 6 | * Defaults to true. |
|
| 369 | 6 | * |
|
| 370 | * @throws MappingException if no mapping found and $strict is true. |
||
| 371 | * |
||
| 372 | 6 | * @return string[] |
|
| 373 | 6 | */ |
|
| 374 | 6 | public function buildTypesList($strict = true) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Returns all the aliases related to the MIME type(s). |
||
| 405 | * |
||
| 406 | * If the current type is a wildcard, than all aliases of all the |
||
| 407 | * types matching the wildcard will be returned. |
||
| 408 | * |
||
| 409 | * @param bool $strict |
||
| 410 | * (Optional) if true a MappingException is thrown when no mapping is |
||
| 411 | * found, if false it returns an empty array as a default. |
||
| 412 | * Defaults to true. |
||
| 413 | * |
||
| 414 | * @throws MappingException if no mapping found and $strict is true. |
||
| 415 | * |
||
| 416 | * @return string[] |
||
| 417 | */ |
||
| 418 | View Code Duplication | public function getAliases($strict = true) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Returns the MIME type's preferred file extension. |
||
| 436 | * |
||
| 437 | * @param bool $strict |
||
| 438 | * (Optional) if true a MappingException is thrown when no mapping is |
||
| 439 | * found, if false it returns null as a default. |
||
| 440 | * Defaults to true. |
||
| 441 | * |
||
| 442 | * @throws MappingException if no mapping found and $strict is true. |
||
| 443 | * |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | public function getDefaultExtension($strict = true) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Returns all the file extensions related to the MIME type(s). |
||
| 470 | * |
||
| 471 | * If the current type is a wildcard, than all extensions of all the |
||
| 472 | * types matching the wildcard will be returned. |
||
| 473 | * |
||
| 474 | * @param bool $strict |
||
| 475 | * (Optional) if true a MappingException is thrown when no mapping is |
||
| 476 | * found, if false it returns an empty array as a default. |
||
| 477 | * Defaults to true. |
||
| 478 | * |
||
| 479 | * @throws MappingException if no mapping found and $strict is true. |
||
| 480 | * |
||
| 481 | * @return string[] |
||
| 482 | */ |
||
| 483 | View Code Duplication | public function getExtensions($strict = true) |
|
| 498 | } |
||
| 499 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: