Complex classes like MimePart 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 MimePart, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class MimePart |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var \ZBateson\MailMimeParser\Header\HeaderFactory the HeaderFactory |
||
| 28 | * object used for created headers |
||
| 29 | */ |
||
| 30 | protected $headerFactory; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var \ZBateson\MailMimeParser\Header\AbstractHeader[] array of header |
||
| 34 | * objects |
||
| 35 | */ |
||
| 36 | protected $headers; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \ZBateson\MailMimeParser\MimePart parent part |
||
| 40 | */ |
||
| 41 | protected $parent; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var resource the content's resource handle |
||
| 45 | */ |
||
| 46 | protected $handle; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var \ZBateson\MailMimeParser\MimePart[] array of parts in this message |
||
| 50 | */ |
||
| 51 | protected $parts = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \ZBateson\MailMimeParser\MimePart[] Maps mime types to parts for |
||
| 55 | * looking up in getPartByMimeType |
||
| 56 | */ |
||
| 57 | protected $mimeToPart = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Sets up class dependencies. |
||
| 61 | * |
||
| 62 | * @param HeaderFactory $headerFactory |
||
| 63 | */ |
||
| 64 | 82 | public function __construct(HeaderFactory $headerFactory) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Closes the attached resource handle. |
||
| 71 | */ |
||
| 72 | 82 | public function __destruct() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Adds the passed part to the parts array, and registers non-attachment/ |
||
| 81 | * non-multipart parts by their content type. |
||
| 82 | * |
||
| 83 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 84 | */ |
||
| 85 | 74 | public function addPart(MimePart $part) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Unregisters the child part from this part. |
||
| 96 | * |
||
| 97 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 98 | */ |
||
| 99 | 7 | public function removePart(MimePart $part) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the non-text, non-HTML part at the given 0-based index, or null |
||
| 113 | * if none is set. |
||
| 114 | * |
||
| 115 | * @param int $index |
||
| 116 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 117 | */ |
||
| 118 | 2 | public function getPart($index) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Returns all attachment parts. |
||
| 128 | * |
||
| 129 | * @return \ZBateson\MailMimeParser\MimePart[] |
||
| 130 | */ |
||
| 131 | 22 | public function getAllParts() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Returns the number of attachments available. |
||
| 138 | * |
||
| 139 | * @return int |
||
| 140 | */ |
||
| 141 | 1 | public function getPartCount() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Returns the part associated with the passed mime type if it exists. |
||
| 148 | * |
||
| 149 | * @param string $mimeType |
||
| 150 | * @return \ZBateson\MailMimeParser\MimePart or null |
||
| 151 | */ |
||
| 152 | 18 | public function getPartByMimeType($mimeType) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Returns true if there's a content stream associated with the part. |
||
| 163 | * |
||
| 164 | * @return boolean |
||
| 165 | */ |
||
| 166 | 72 | public function hasContent() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Returns true if this part's mime type is multipart/* |
||
| 176 | * |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | 74 | public function isMultiPart() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Returns true if this part's mime type is text/* |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | 71 | public function isTextPart() |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Attaches the resource handle for the part's content. The attached handle |
||
| 202 | * is closed when the MimePart object is destroyed. |
||
| 203 | * |
||
| 204 | * @param resource $contentHandle |
||
| 205 | */ |
||
| 206 | 76 | public function attachContentResourceHandle($contentHandle) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * |
||
| 216 | */ |
||
| 217 | 8 | protected function detachContentResourceHandle() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Sets the content of the part to the passed string (effectively creates |
||
| 224 | * a php://temp stream with the passed content and calls |
||
| 225 | * attachContentResourceHandle with the opened stream). |
||
| 226 | * |
||
| 227 | * @param string $string |
||
| 228 | */ |
||
| 229 | 6 | public function setContent($string) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Returns the resource stream handle for the part's content. |
||
| 239 | * |
||
| 240 | * The resource is automatically closed by MimePart's destructor and should |
||
| 241 | * not be closed otherwise. |
||
| 242 | * |
||
| 243 | * @return resource |
||
| 244 | */ |
||
| 245 | 72 | public function getContentResourceHandle() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Shortcut to reading stream content and assigning it to a string. Returns |
||
| 252 | * null if the part doesn't have a content stream. |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | 2 | public function getContent() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Adds a header with the given $name and $value. |
||
| 266 | * |
||
| 267 | * Creates a new \ZBateson\MailMimeParser\Header\AbstractHeader object and |
||
| 268 | * registers it as a header. |
||
| 269 | * |
||
| 270 | * @param string $name |
||
| 271 | * @param string $value |
||
| 272 | */ |
||
| 273 | 77 | public function setRawHeader($name, $value) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Removes the header with the given name |
||
| 280 | * |
||
| 281 | * @param string $name |
||
| 282 | */ |
||
| 283 | 5 | public function removeHeader($name) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Returns the AbstractHeader object for the header with the given $name |
||
| 290 | * |
||
| 291 | * Note that mime headers aren't case sensitive. |
||
| 292 | * |
||
| 293 | * @param string $name |
||
| 294 | * @return \ZBateson\MailMimeParser\Header\AbstractHeader |
||
| 295 | */ |
||
| 296 | 79 | public function getHeader($name) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Returns the string value for the header with the given $name. |
||
| 306 | * |
||
| 307 | * Note that mime headers aren't case sensitive. |
||
| 308 | * |
||
| 309 | * @param string $name |
||
| 310 | * @param string $defaultValue |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | 76 | public function getHeaderValue($name, $defaultValue = null) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Returns the full array of headers for this part. |
||
| 324 | * |
||
| 325 | * @return \ZBateson\MailMimeParser\Header\AbstractHeader[] |
||
| 326 | */ |
||
| 327 | 1 | public function getHeaders() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Returns a parameter of the header $header, given the parameter named |
||
| 334 | * $param. |
||
| 335 | * |
||
| 336 | * Only headers of type |
||
| 337 | * \ZBateson\MailMimeParser\Header\ParameterHeader have parameters. |
||
| 338 | * Content-Type and Content-Disposition are examples of headers with |
||
| 339 | * parameters. "Charset" is a common parameter of Content-Type. |
||
| 340 | * |
||
| 341 | * @param string $header |
||
| 342 | * @param string $param |
||
| 343 | * @param string $defaultValue |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | 76 | public function getHeaderParameter($header, $param, $defaultValue = null) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Sets the parent part. |
||
| 357 | * |
||
| 358 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 359 | */ |
||
| 360 | 73 | public function setParent(MimePart $part) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Returns this part's parent. |
||
| 367 | * |
||
| 368 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 369 | */ |
||
| 370 | 73 | public function getParent() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Sets up a mailmimeparser-encode stream filter on the passed stream |
||
| 377 | * resource handle if applicable and returns a reference to the filter. |
||
| 378 | * |
||
| 379 | * @param resource $handle |
||
| 380 | * @return resource a reference to the appended stream filter or null |
||
| 381 | */ |
||
| 382 | 71 | private function setCharsetStreamFilterOnStream($handle) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Appends a stream filter the passed resource handle based on the type of |
||
| 401 | * encoding for the current mime part. |
||
| 402 | * |
||
| 403 | * Unfortunately PHP seems to error out allocating memory for |
||
| 404 | * stream_filter_make_writable in Base64EncodeStreamFilter using |
||
| 405 | * STREAM_FILTER_WRITE, and HHVM doesn't seem to remove the filter properly |
||
| 406 | * for STREAM_FILTER_READ, so the function appends a read filter on |
||
| 407 | * $fromHandle if running through 'php', and a write filter on $toHandle if |
||
| 408 | * using HHVM. |
||
| 409 | * |
||
| 410 | * @param resource $fromHandle |
||
| 411 | * @param resource $toHandle |
||
| 412 | * @param \ZBateson\MailMimeParser\Stream\StreamLeftover $leftovers |
||
| 413 | * @return resource the stream filter |
||
| 414 | */ |
||
| 415 | 71 | private function setTransferEncodingFilterOnStream($fromHandle, $toHandle, StreamLeftover $leftovers) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Returns true if the content-transfer-encoding header of the current part |
||
| 455 | * is set to 'x-uuencode'. |
||
| 456 | * |
||
| 457 | * @return bool |
||
| 458 | */ |
||
| 459 | private function isUUEncoded() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Filters out single line feed (CR or LF) characters from text input and |
||
| 467 | * replaces them with CRLF, assigning the result to $read. Also trims out |
||
| 468 | * any starting and ending CRLF characters in the stream. |
||
| 469 | * |
||
| 470 | * @param string $read the read string, and where the result will be written |
||
| 471 | * to |
||
| 472 | * @param bool $first set to true if this is the first set of read |
||
| 473 | * characters from the stream (ltrims CRLF) |
||
| 474 | * @param string $lastChars contains any CRLF characters from the last $read |
||
| 475 | * line if it ended with a CRLF (because they're trimmed from the |
||
| 476 | * end, and get prepended to $read). |
||
| 477 | */ |
||
| 478 | 69 | private function filterTextBeforeCopying(&$read, &$first, &$lastChars) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Copies the content of the $fromHandle stream into the $toHandle stream, |
||
| 496 | * maintaining the current read position in $fromHandle and writing |
||
| 497 | * uuencode headers. |
||
| 498 | * |
||
| 499 | * @param resource $fromHandle |
||
| 500 | * @param resource $toHandle |
||
| 501 | */ |
||
| 502 | 71 | private function copyContentStream($fromHandle, $toHandle) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Writes out headers and follows them with an empty line. |
||
| 521 | * |
||
| 522 | * @param resource $handle |
||
| 523 | */ |
||
| 524 | 71 | protected function writeHeadersTo($handle) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Writes out the content portion of the mime part based on the headers that |
||
| 534 | * are set on the part, taking care of character/content-transfer encoding. |
||
| 535 | * |
||
| 536 | * @param resource $handle |
||
| 537 | */ |
||
| 538 | 71 | protected function writeContentTo($handle) |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Writes out the MimePart to the passed resource. |
||
| 558 | * |
||
| 559 | * Takes care of character and content transfer encoding on the output based |
||
| 560 | * on what headers are set. |
||
| 561 | * |
||
| 562 | * @param resource $handle |
||
| 563 | */ |
||
| 564 | 48 | protected function writeTo($handle) |
|
| 569 | } |
||
| 570 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.