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 | 50 | ||
| 53 | /** |
||
| 54 | 50 | * Optional MIME parameters |
|
| 55 | 43 | * |
|
| 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 | 50 | * |
|
| 65 | * @param string $type MIME type |
||
| 66 | */ |
||
| 67 | 50 | public function __construct($type) |
|
| 71 | |||
| 72 | 46 | /** |
|
| 73 | 1 | * Parse a mime-type and set the class variables. |
|
| 74 | * |
||
| 75 | * @param string $type MIME type to parse |
||
| 76 | 45 | * |
|
| 77 | 45 | * @return void |
|
| 78 | 45 | */ |
|
| 79 | 43 | protected function parse($type) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Does this type have any parameters? |
||
| 120 | * |
||
| 121 | * @return boolean true if type has parameters, false otherwise |
||
| 122 | */ |
||
| 123 | public function hasParameters() |
||
| 127 | 50 | ||
| 128 | 50 | /** |
|
| 129 | 50 | * Get a MIME type's parameters |
|
| 130 | 50 | * |
|
| 131 | 50 | * @return TypeParameter[] Type's parameters |
|
| 132 | */ |
||
| 133 | 50 | public function getParameters() |
|
| 137 | 47 | ||
| 138 | 4 | /** |
|
| 139 | 2 | * Get a MIME type's parameter |
|
| 140 | * |
||
| 141 | 2 | * @param string $name Parameter name |
|
| 142 | * |
||
| 143 | 4 | * @return TypeParameter|null |
|
| 144 | 47 | */ |
|
| 145 | 4 | public function getParameter($name) |
|
| 149 | 47 | ||
| 150 | 20 | /** |
|
| 151 | 20 | * Get a MIME type's media. |
|
| 152 | 20 | * |
|
| 153 | * Note: 'media' refers to the portion before the first slash. |
||
| 154 | 47 | * |
|
| 155 | 20 | * @return string Type's media. |
|
| 156 | 47 | */ |
|
| 157 | 9 | public function getMedia() |
|
| 161 | 8 | ||
| 162 | /** |
||
| 163 | 9 | * Get a MIME type's media comment. |
|
| 164 | * |
||
| 165 | * @return string Type's media comment. |
||
| 166 | 47 | */ |
|
| 167 | 47 | public function getMediaComment() |
|
| 171 | |||
| 172 | /** |
||
| 173 | 50 | * Get a MIME type's subtype. |
|
| 174 | 2 | * |
|
| 175 | * @return string Type's subtype, null if invalid mime type. |
||
| 176 | */ |
||
| 177 | public function getSubType() |
||
| 181 | 49 | ||
| 182 | /** |
||
| 183 | * Get a MIME type's subtype comment. |
||
| 184 | * |
||
| 185 | * @return string Type's subtype comment, null if invalid mime type. |
||
| 186 | */ |
||
| 187 | public function getSubTypeComment() |
||
| 191 | |||
| 192 | 26 | /** |
|
| 193 | * Create a textual MIME type from object values |
||
| 194 | 26 | * |
|
| 195 | 26 | * This function performs the opposite function of parse(). |
|
| 196 | * |
||
| 197 | * @param int $format The format of the output string. |
||
| 198 | * |
||
| 199 | * @return string MIME type string |
||
| 200 | */ |
||
| 201 | public function toString($format = Type::FULL_TEXT) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Is this type experimental? |
||
| 221 | * |
||
| 222 | 28 | * Note: Experimental types are denoted by a leading 'x-' in the media or |
|
| 223 | * subtype, e.g. text/x-vcard or x-world/x-vrml. |
||
| 224 | 28 | * |
|
| 225 | * @return boolean true if type is experimental, false otherwise |
||
| 226 | */ |
||
| 227 | public function isExperimental() |
||
| 234 | 29 | ||
| 235 | /** |
||
| 236 | * Is this a vendor MIME type? |
||
| 237 | * |
||
| 238 | * Note: Vendor types are denoted with a leading 'vnd. in the subtype. |
||
| 239 | * |
||
| 240 | * @return boolean true if type is a vendor type, false otherwise |
||
| 241 | */ |
||
| 242 | public function isVendor() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Is this a wildcard type? |
||
| 252 | * |
||
| 253 | * @return boolean true if type is a wildcard, false otherwise |
||
| 254 | */ |
||
| 255 | public function isWildcard() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Perform a wildcard match on a MIME type |
||
| 266 | 28 | * |
|
| 267 | * Example: |
||
| 268 | 28 | * $type = new Type('image/png'); |
|
| 269 | * $type->wildcardMatch('image/*'); |
||
| 270 | * |
||
| 271 | * @param string $card Wildcard to check against |
||
| 272 | * |
||
| 273 | * @return boolean true if there was a match, false otherwise |
||
| 274 | */ |
||
| 275 | public function wildcardMatch($card) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Add a parameter to this type |
||
| 296 | * |
||
| 297 | * @param string $name Parameter name |
||
| 298 | 30 | * @param string $value Parameter value |
|
| 299 | * @param string $comment Comment for this parameter |
||
| 300 | 30 | * |
|
| 301 | * @return void |
||
| 302 | */ |
||
| 303 | 30 | public function addParameter($name, $value, $comment = null) |
|
| 307 | |||
| 308 | /** |
||
| 309 | 30 | * Remove a parameter from this type |
|
| 310 | * |
||
| 311 | * @param string $name Parameter name |
||
| 312 | * |
||
| 313 | * @return void |
||
| 314 | */ |
||
| 315 | public function removeParameter($name) |
||
| 319 | |||
| 320 | 1 | public function getDefaultExtension($strict = true) |
|
| 325 | 1 | ||
| 326 | public function getExtensions($strict = true) |
||
| 340 | } |
||
| 341 |