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 | * The MIME media type. |
||
| 12 | * |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | protected $media; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The MIME media type comment. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $mediaComment; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The MIME media sub-type. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $subType; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The MIME media sub-type comment. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $subTypeComment; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Optional MIME parameters |
||
| 40 | * |
||
| 41 | * @var TypeParameter[] |
||
| 42 | */ |
||
| 43 | protected $parameters = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor. |
||
| 47 | * |
||
| 48 | * The type string will be parsed and the appropriate class vars set. |
||
| 49 | * |
||
| 50 | * @param string $type MIME type |
||
| 51 | */ |
||
| 52 | 47 | public function __construct($type) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Parse a mime-type and set the class variables. |
||
| 59 | * |
||
| 60 | * @param string $type MIME type to parse |
||
| 61 | * |
||
| 62 | * @return void |
||
| 63 | */ |
||
| 64 | 47 | protected function parse($type) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Parse the sub-type part of the type and set the class variables. |
||
| 83 | * |
||
| 84 | * @param string $sub_type |
||
| 85 | * |
||
| 86 | * @return void |
||
| 87 | */ |
||
| 88 | 42 | protected function parseSubType($sub_type) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Parses a part of the content MIME type string. |
||
| 111 | * |
||
| 112 | * Splits string and comment until a delimiter is found. |
||
| 113 | * |
||
| 114 | * @param string $string Input string. |
||
| 115 | * @param int $offset Offset to start parsing from. |
||
| 116 | * @param string $delimiter Stop parsing when delimiter found. |
||
| 117 | * |
||
| 118 | * @return array An array with the following keys: |
||
| 119 | * 'string' - the uncommented part of $string |
||
| 120 | * 'comment' - the comment part of $string |
||
| 121 | * 'delimiter_matched' - true if a $delimiter stopped the parsing, false |
||
| 122 | * otherwise |
||
| 123 | * 'end_offset' - the last position parsed in $string. |
||
| 124 | */ |
||
| 125 | 47 | protected function parseStringPart($string, $offset, $delimiter) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Get a parameter attribute (e.g. name) |
||
| 187 | * |
||
| 188 | * @param string $param MIME type parameter |
||
| 189 | * |
||
| 190 | * @return string Attribute name |
||
| 191 | */ |
||
| 192 | 26 | public static function getAttribute($param) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Get a parameter value |
||
| 200 | * |
||
| 201 | * @param string $param MIME type parameter |
||
| 202 | * |
||
| 203 | * @return string Value |
||
| 204 | */ |
||
| 205 | 26 | public static function getValue($param) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Does this type have any parameters? |
||
| 219 | * |
||
| 220 | * @return boolean true if type has parameters, false otherwise |
||
| 221 | */ |
||
| 222 | 28 | public function hasParameters() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Get a MIME type's parameters |
||
| 229 | * |
||
| 230 | * @return TypeParameter[] Type's parameters |
||
| 231 | */ |
||
| 232 | 29 | public function getParameters() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Get a MIME type's parameter |
||
| 239 | * |
||
| 240 | * @param string $name Parameter name |
||
| 241 | * |
||
| 242 | * @return TypeParameter|null |
||
| 243 | */ |
||
| 244 | 23 | public function getParameter($name) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Get a MIME type's media. |
||
| 251 | * |
||
| 252 | * Note: 'media' refers to the portion before the first slash. |
||
| 253 | * |
||
| 254 | * @return string Type's media. |
||
| 255 | */ |
||
| 256 | 34 | public function getMedia() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Get a MIME type's media comment. |
||
| 263 | * |
||
| 264 | * @return string Type's media comment. |
||
| 265 | */ |
||
| 266 | 28 | public function getMediaComment() |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Get a MIME type's subtype. |
||
| 273 | * |
||
| 274 | * @return string Type's subtype, null if invalid mime type. |
||
| 275 | */ |
||
| 276 | 35 | public function getSubType() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Get a MIME type's subtype comment. |
||
| 283 | * |
||
| 284 | * @return string Type's subtype comment, null if invalid mime type. |
||
| 285 | */ |
||
| 286 | 28 | public function getSubTypeComment() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Create a textual MIME type from object values |
||
| 293 | * |
||
| 294 | * This function performs the opposite function of parse(). |
||
| 295 | * |
||
| 296 | * @return string MIME type string |
||
| 297 | */ |
||
| 298 | 30 | public function toString() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Is this type experimental? |
||
| 314 | * |
||
| 315 | * Note: Experimental types are denoted by a leading 'x-' in the media or |
||
| 316 | * subtype, e.g. text/x-vcard or x-world/x-vrml. |
||
| 317 | * |
||
| 318 | * @return boolean true if type is experimental, false otherwise |
||
| 319 | */ |
||
| 320 | 1 | public function isExperimental() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Is this a vendor MIME type? |
||
| 330 | * |
||
| 331 | * Note: Vendor types are denoted with a leading 'vnd. in the subtype. |
||
| 332 | * |
||
| 333 | * @return boolean true if type is a vendor type, false otherwise |
||
| 334 | */ |
||
| 335 | 1 | public function isVendor() |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Is this a wildcard type? |
||
| 345 | * |
||
| 346 | * @return boolean true if type is a wildcard, false otherwise |
||
| 347 | */ |
||
| 348 | 3 | public function isWildcard() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Perform a wildcard match on a MIME type |
||
| 359 | * |
||
| 360 | * Example: |
||
| 361 | * $type = new Type('image/png'); |
||
| 362 | * $type->wildcardMatch('image/*'); |
||
| 363 | * |
||
| 364 | * @param string $card Wildcard to check against |
||
| 365 | * |
||
| 366 | * @return boolean true if there was a match, false otherwise |
||
| 367 | */ |
||
| 368 | 2 | public function wildcardMatch($card) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Add a parameter to this type |
||
| 389 | * |
||
| 390 | * @param string $name Parameter name |
||
| 391 | * @param string $value Parameter value |
||
| 392 | * @param string $comment Comment for this parameter |
||
| 393 | * |
||
| 394 | * @return void |
||
| 395 | */ |
||
| 396 | 26 | public function addParameter($name, $value, $comment = null) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Remove a parameter from this type |
||
| 403 | * |
||
| 404 | * @param string $name Parameter name |
||
| 405 | * |
||
| 406 | * @return void |
||
| 407 | */ |
||
| 408 | 1 | public function removeParameter($name) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Return default file extension. |
||
| 415 | * |
||
| 416 | * @return string A file extension without leading period. |
||
| 417 | */ |
||
| 418 | 2 | public function getDefaultExtension() |
|
| 429 | } |
||
| 430 |