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 | 88 | public function __construct(HeaderFactory $headerFactory) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Closes the attached resource handle. |
||
| 71 | */ |
||
| 72 | 88 | 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 | 80 | public function addPart(MimePart $part) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Unregisters the child part from this part. |
||
| 96 | * |
||
| 97 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 98 | */ |
||
| 99 | 8 | public function removePart(MimePart $part) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Returns the non-text, non-HTML part at the given 0-based index, or null |
||
| 114 | * if none is set. |
||
| 115 | * |
||
| 116 | * @param int $index |
||
| 117 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 118 | */ |
||
| 119 | 3 | public function getPart($index) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Returns all attachment parts. |
||
| 129 | * |
||
| 130 | * @return \ZBateson\MailMimeParser\MimePart[] |
||
| 131 | */ |
||
| 132 | 25 | public function getAllParts() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Returns the number of attachments available. |
||
| 139 | * |
||
| 140 | * @return int |
||
| 141 | */ |
||
| 142 | 1 | public function getPartCount() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Returns the part associated with the passed mime type if it exists. |
||
| 149 | * |
||
| 150 | * @param string $mimeType |
||
| 151 | * @return \ZBateson\MailMimeParser\MimePart or null |
||
| 152 | */ |
||
| 153 | 18 | public function getPartByMimeType($mimeType) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Returns true if there's a content stream associated with the part. |
||
| 164 | * |
||
| 165 | * @return boolean |
||
| 166 | */ |
||
| 167 | 78 | public function hasContent() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Returns true if this part's mime type is multipart/* |
||
| 177 | * |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | 80 | public function isMultiPart() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Returns true if this part's mime type is text/plain, text/html or has a |
||
| 190 | * text/* and has a defined 'charset' attribute. |
||
| 191 | * |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | 80 | public function isTextPart() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Attaches the resource handle for the part's content. The attached handle |
||
| 209 | * is closed when the MimePart object is destroyed. |
||
| 210 | * |
||
| 211 | * @param resource $contentHandle |
||
| 212 | */ |
||
| 213 | 82 | public function attachContentResourceHandle($contentHandle) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * |
||
| 223 | */ |
||
| 224 | 11 | protected function detachContentResourceHandle() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Sets the content of the part to the passed string (effectively creates |
||
| 231 | * a php://temp stream with the passed content and calls |
||
| 232 | * attachContentResourceHandle with the opened stream). |
||
| 233 | * |
||
| 234 | * @param string $string |
||
| 235 | */ |
||
| 236 | 8 | public function setContent($string) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Returns the resource stream handle for the part's content or null if not |
||
| 246 | * set. rewind() is called on the stream before returning it. |
||
| 247 | * |
||
| 248 | * The resource is automatically closed by MimePart's destructor and should |
||
| 249 | * not be closed otherwise. |
||
| 250 | * |
||
| 251 | * @return resource |
||
| 252 | */ |
||
| 253 | 76 | public function getContentResourceHandle() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Shortcut to reading stream content and assigning it to a string. Returns |
||
| 263 | * null if the part doesn't have a content stream. |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | 3 | public function getContent() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Adds a header with the given $name and $value. |
||
| 277 | * |
||
| 278 | * Creates a new \ZBateson\MailMimeParser\Header\AbstractHeader object and |
||
| 279 | * registers it as a header. |
||
| 280 | * |
||
| 281 | * @param string $name |
||
| 282 | * @param string $value |
||
| 283 | */ |
||
| 284 | 83 | public function setRawHeader($name, $value) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Removes the header with the given name |
||
| 291 | * |
||
| 292 | * @param string $name |
||
| 293 | */ |
||
| 294 | 7 | public function removeHeader($name) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Returns the AbstractHeader object for the header with the given $name |
||
| 301 | * |
||
| 302 | * Note that mime headers aren't case sensitive. |
||
| 303 | * |
||
| 304 | * @param string $name |
||
| 305 | * @return \ZBateson\MailMimeParser\Header\AbstractHeader |
||
| 306 | */ |
||
| 307 | 85 | public function getHeader($name) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Returns the string value for the header with the given $name. |
||
| 317 | * |
||
| 318 | * Note that mime headers aren't case sensitive. |
||
| 319 | * |
||
| 320 | * @param string $name |
||
| 321 | * @param string $defaultValue |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | 82 | public function getHeaderValue($name, $defaultValue = null) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Returns the full array of headers for this part. |
||
| 335 | * |
||
| 336 | * @return \ZBateson\MailMimeParser\Header\AbstractHeader[] |
||
| 337 | */ |
||
| 338 | 1 | public function getHeaders() |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Returns a parameter of the header $header, given the parameter named |
||
| 345 | * $param. |
||
| 346 | * |
||
| 347 | * Only headers of type |
||
| 348 | * \ZBateson\MailMimeParser\Header\ParameterHeader have parameters. |
||
| 349 | * Content-Type and Content-Disposition are examples of headers with |
||
| 350 | * parameters. "Charset" is a common parameter of Content-Type. |
||
| 351 | * |
||
| 352 | * @param string $header |
||
| 353 | * @param string $param |
||
| 354 | * @param string $defaultValue |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | 82 | public function getHeaderParameter($header, $param, $defaultValue = null) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Sets the parent part. |
||
| 368 | * |
||
| 369 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 370 | */ |
||
| 371 | 79 | public function setParent(MimePart $part) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Returns this part's parent. |
||
| 378 | * |
||
| 379 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 380 | */ |
||
| 381 | 79 | public function getParent() |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Sets up a mailmimeparser-encode stream filter on the passed stream |
||
| 388 | * resource handle if applicable and returns a reference to the filter. |
||
| 389 | * |
||
| 390 | * @param resource $handle |
||
| 391 | * @return resource a reference to the appended stream filter or null |
||
| 392 | */ |
||
| 393 | 77 | private function setCharsetStreamFilterOnStream($handle) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Appends a stream filter the passed resource handle based on the type of |
||
| 411 | * encoding for the current mime part. |
||
| 412 | * |
||
| 413 | * Unfortunately PHP seems to error out allocating memory for |
||
| 414 | * stream_filter_make_writable in Base64EncodeStreamFilter using |
||
| 415 | * STREAM_FILTER_WRITE, and HHVM doesn't seem to remove the filter properly |
||
| 416 | * for STREAM_FILTER_READ, so the function appends a read filter on |
||
| 417 | * $fromHandle if running through 'php', and a write filter on $toHandle if |
||
| 418 | * using HHVM. |
||
| 419 | * |
||
| 420 | * @param resource $fromHandle |
||
| 421 | * @param resource $toHandle |
||
| 422 | * @param \ZBateson\MailMimeParser\Stream\StreamLeftover $leftovers |
||
| 423 | * @return resource the stream filter |
||
| 424 | */ |
||
| 425 | 77 | private function setTransferEncodingFilterOnStream($fromHandle, $toHandle, StreamLeftover $leftovers) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Returns true if the content-transfer-encoding header of the current part |
||
| 465 | * is set to 'x-uuencode'. |
||
| 466 | * |
||
| 467 | * @return bool |
||
| 468 | */ |
||
| 469 | private function isUUEncoded() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Filters out single line feed (CR or LF) characters from text input and |
||
| 477 | * replaces them with CRLF, assigning the result to $read. Also trims out |
||
| 478 | * any starting and ending CRLF characters in the stream. |
||
| 479 | * |
||
| 480 | * @param string $read the read string, and where the result will be written |
||
| 481 | * to |
||
| 482 | * @param bool $first set to true if this is the first set of read |
||
| 483 | * characters from the stream (ltrims CRLF) |
||
| 484 | * @param string $lastChars contains any CRLF characters from the last $read |
||
| 485 | * line if it ended with a CRLF (because they're trimmed from the |
||
| 486 | * end, and get prepended to $read). |
||
| 487 | */ |
||
| 488 | 71 | private function filterTextBeforeCopying(&$read, &$first, &$lastChars) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * Copies the content of the $fromHandle stream into the $toHandle stream, |
||
| 506 | * maintaining the current read position in $fromHandle and writing |
||
| 507 | * uuencode headers. |
||
| 508 | * |
||
| 509 | * @param resource $fromHandle |
||
| 510 | * @param resource $toHandle |
||
| 511 | */ |
||
| 512 | 77 | private function copyContentStream($fromHandle, $toHandle) |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Writes out headers and follows them with an empty line. |
||
| 531 | * |
||
| 532 | * @param resource $handle |
||
| 533 | */ |
||
| 534 | 77 | protected function writeHeadersTo($handle) |
|
| 541 | |||
| 542 | /** |
||
| 543 | * Writes out the content portion of the mime part based on the headers that |
||
| 544 | * are set on the part, taking care of character/content-transfer encoding. |
||
| 545 | * |
||
| 546 | * @param resource $handle |
||
| 547 | */ |
||
| 548 | 77 | protected function writeContentTo($handle) |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Writes out the MimePart to the passed resource. |
||
| 568 | * |
||
| 569 | * Takes care of character and content transfer encoding on the output based |
||
| 570 | * on what headers are set. |
||
| 571 | * |
||
| 572 | * @param resource $handle |
||
| 573 | */ |
||
| 574 | 51 | protected function writeTo($handle) |
|
| 579 | } |
||
| 580 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.