Complex classes like WritableMessage 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 WritableMessage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class WritableMessage |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var \ZBateson\MailMimeParser\Message\Part\MimePartFactory a MimePartFactory to create |
||
| 28 | * parts for attachments/content |
||
| 29 | */ |
||
| 30 | protected $mimePartFactory; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var \ZBateson\MailMimeParser\Message\Writer\MessageWriter the part |
||
| 34 | * writer for this Message. The same object is assigned to $partWriter |
||
| 35 | * but as an AbstractWriter -- not really needed in PHP but helps with |
||
| 36 | * auto-complete and code analyzers. |
||
| 37 | */ |
||
| 38 | protected $messageWriter = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Creates a unique mime boundary and assigns it to the passed part's |
||
| 42 | * Content-Type header with the passed mime type. |
||
| 43 | * |
||
| 44 | * @param \ZBateson\MailMimeParser\Message\Part\MimePart $part |
||
| 45 | * @param string $mimeType |
||
| 46 | */ |
||
| 47 | private function setMimeHeaderBoundaryOnPart(MimePart $part, $mimeType) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Sets this message to be a multipart/alternative message, making space for |
||
| 58 | * a second content part. |
||
| 59 | * |
||
| 60 | * Creates a content part and assigns the content stream from the message to |
||
| 61 | * that newly created part. |
||
| 62 | */ |
||
| 63 | private function setMessageAsAlternative() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Moves all parts under $from into this message except those with a |
||
| 80 | * content-type equal to $exceptMimeType. If the message is not a |
||
| 81 | * multipart/mixed message, it is set to multipart/mixed first. |
||
| 82 | * |
||
| 83 | * @param MimePart $from |
||
| 84 | * @param string $exceptMimeType |
||
| 85 | */ |
||
| 86 | private function moveAllPartsAsAttachmentsExcept(MimePart $from, $exceptMimeType) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns an open resource handle for the passed string or resource handle. |
||
| 107 | * |
||
| 108 | * For a string, creates a php://temp stream and returns it. |
||
| 109 | * |
||
| 110 | * @param resource|string $stringOrHandle |
||
| 111 | * @return resource |
||
| 112 | */ |
||
| 113 | private function getHandleForStringOrHandle($stringOrHandle) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Creates and returns a unique boundary. |
||
| 127 | * |
||
| 128 | * @param string $mimeType first 3 characters of a multipart type are used, |
||
| 129 | * e.g. REL for relative or ALT for alternative |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | private function getUniqueBoundary($mimeType) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns the direct child of $alternativePart containing a part of |
||
| 140 | * $mimeType. |
||
| 141 | * |
||
| 142 | * Used for alternative mime types that have a multipart/mixed or |
||
| 143 | * multipart/related child containing a content part of $mimeType, where |
||
| 144 | * the whole mixed/related part should be removed. |
||
| 145 | * |
||
| 146 | * @param string $mimeType the content-type to find below $alternativePart |
||
| 147 | * @param MimePart $alternativePart The multipart/alternative part to look |
||
| 148 | * under |
||
| 149 | * @return boolean|MimePart false if a part is not found |
||
| 150 | */ |
||
| 151 | private function getContentPartContainerFromAlternative($mimeType, MimePart $alternativePart) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Removes all parts of $mimeType from $alternativePart. |
||
| 167 | * |
||
| 168 | * If $alternativePart contains a multipart/mixed or multipart/relative part |
||
| 169 | * with other parts of different content-types, the multipart part is |
||
| 170 | * removed, and parts of different content-types can optionally be moved to |
||
| 171 | * the main message part. |
||
| 172 | * |
||
| 173 | * @param string $mimeType |
||
| 174 | * @param MimePart $alternativePart |
||
| 175 | * @param bool $keepOtherContent |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | private function removeAllContentPartsFromAlternative($mimeType, $alternativePart, $keepOtherContent) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Removes the content part of the message with the passed mime type. If |
||
| 206 | * there is a remaining content part and it is an alternative part of the |
||
| 207 | * main message, the content part is moved to the message part. |
||
| 208 | * |
||
| 209 | * If the content part is part of an alternative part beneath the message, |
||
| 210 | * the alternative part is replaced by the remaining content part, |
||
| 211 | * optionally keeping other parts if $keepOtherContent is set to true. |
||
| 212 | * |
||
| 213 | * @param string $mimeType |
||
| 214 | * @param bool $keepOtherContent |
||
| 215 | * @return boolean true on success |
||
| 216 | */ |
||
| 217 | protected function removeAllContentPartsByMimeType($mimeType, $keepOtherContent = false) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Removes the 'inline' part with the passed contentType, at the given index |
||
| 229 | * defaulting to the first |
||
| 230 | * |
||
| 231 | * @param string $contentType |
||
| 232 | * @param int $index |
||
| 233 | * @return boolean true on success |
||
| 234 | */ |
||
| 235 | protected function removePartByMimeType($mimeType, $index = 0) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Creates a new mime part as a multipart/alternative and assigns the passed |
||
| 254 | * $contentPart as a part below it before returning it. |
||
| 255 | * |
||
| 256 | * @param MimePart $contentPart |
||
| 257 | * @return MimePart the alternative part |
||
| 258 | */ |
||
| 259 | private function createAlternativeContentPart(MimePart $contentPart) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Copies type headers (Content-Type, Content-Disposition, |
||
| 271 | * Content-Transfer-Encoding) from the $from MimePart to $to. Attaches the |
||
| 272 | * content resource handle of $from to $to, and loops over child parts, |
||
| 273 | * removing them from $from and adding them to $to. |
||
| 274 | * |
||
| 275 | * @param MimePart $from |
||
| 276 | * @param MimePart $to |
||
| 277 | */ |
||
| 278 | private function movePartContentAndChildrenToPart(MimePart $from, MimePart $to) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Replaces the $part MimePart with $replacement. |
||
| 291 | * |
||
| 292 | * Essentially removes $part from its parent, and adds $replacement in its |
||
| 293 | * same position. If $part is this Message, its type headers are moved from |
||
| 294 | * this message to $replacement, the content resource is moved, and children |
||
| 295 | * are assigned to $replacement. |
||
| 296 | * |
||
| 297 | * @param MimePart $part |
||
| 298 | * @param MimePart $replacement |
||
| 299 | */ |
||
| 300 | private function replacePart(MimePart $part, MimePart $replacement) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Copies Content-Type, Content-Disposition and Content-Transfer-Encoding |
||
| 314 | * headers from the $from header into the $to header. If the Content-Type |
||
| 315 | * header isn't defined in $from, defaults to text/plain and |
||
| 316 | * quoted-printable. |
||
| 317 | * |
||
| 318 | * @param \ZBateson\MailMimeParser\Message\Part\MimePart $from |
||
| 319 | * @param \ZBateson\MailMimeParser\Message\Part\MimePart $to |
||
| 320 | */ |
||
| 321 | private function copyTypeHeadersFromPartToPart(MimePart $from, MimePart $to) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Creates a new content part from the passed part, allowing the part to be |
||
| 342 | * used for something else (e.g. changing a non-mime message to a multipart |
||
| 343 | * mime message). |
||
| 344 | * |
||
| 345 | * @param MimePart $part |
||
| 346 | * @return MimePart the newly-created MimePart |
||
| 347 | */ |
||
| 348 | private function createNewContentPartFromPart(MimePart $part) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Creates a new part out of the current contentPart and sets the message's |
||
| 359 | * type to be multipart/mixed. |
||
| 360 | */ |
||
| 361 | private function setMessageAsMixed() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * This function makes space by moving the main message part down one level. |
||
| 374 | * |
||
| 375 | * The content-type, content-disposition and content-transfer-encoding |
||
| 376 | * headers are copied from this message to the newly created part, the |
||
| 377 | * resource handle is moved and detached, any attachments and content parts |
||
| 378 | * with parents set to this message get their parents set to the newly |
||
| 379 | * created part. |
||
| 380 | */ |
||
| 381 | private function makeSpaceForMultipartSignedMessage() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Creates and returns a new MimePart for the signature part of a |
||
| 399 | * multipart/signed message |
||
| 400 | * |
||
| 401 | * @param string $body |
||
| 402 | */ |
||
| 403 | public function createSignaturePart($body) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Loops over parts of this message and sets the content-transfer-encoding |
||
| 419 | * header to quoted-printable for text/* mime parts, and to base64 |
||
| 420 | * otherwise for parts that are '8bit' encoded. |
||
| 421 | * |
||
| 422 | * Used for multipart/signed messages which doesn't support 8bit transfer |
||
| 423 | * encodings. |
||
| 424 | */ |
||
| 425 | private function overwrite8bitContentEncoding() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Ensures a non-text part comes first in a signed multipart/alternative |
||
| 444 | * message as some clients seem to prefer the first content part if the |
||
| 445 | * client doesn't understand multipart/signed. |
||
| 446 | */ |
||
| 447 | private function ensureHtmlPartFirstForSignedMessage() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Turns the message into a multipart/signed message, moving the actual |
||
| 463 | * message into a child part, sets the content-type of the main message to |
||
| 464 | * multipart/signed and adds a signature part as well. |
||
| 465 | * |
||
| 466 | * @param string $micalg The Message Integrity Check algorithm being used |
||
| 467 | * @param string $protocol The mime-type of the signature body |
||
| 468 | */ |
||
| 469 | public function setAsMultipartSigned($micalg, $protocol) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Returns the signed part or null if not set. |
||
| 489 | * |
||
| 490 | * @return \ZBateson\MailMimeParser\Message\Part\MimePart |
||
| 491 | */ |
||
| 492 | public function getSignaturePart() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Enforces the message to be a mime message for a non-mime (e.g. uuencoded |
||
| 499 | * or unspecified) message. If the message has uuencoded attachments, sets |
||
| 500 | * up the message as a multipart/mixed message and creates a content part. |
||
| 501 | */ |
||
| 502 | private function enforceMime() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Creates a multipart/related part out of 'inline' children of $parent and |
||
| 516 | * returns it. |
||
| 517 | * |
||
| 518 | * @param MimePart $parent |
||
| 519 | * @return MimePart |
||
| 520 | */ |
||
| 521 | private function createMultipartRelatedPartForInlineChildrenOf(MimePart $parent) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Finds an alternative inline part in the message and returns it if one |
||
| 535 | * exists. |
||
| 536 | * |
||
| 537 | * If the passed $mimeType is text/plain, searches for a text/html part. |
||
| 538 | * Otherwise searches for a text/plain part to return. |
||
| 539 | * |
||
| 540 | * @param string $mimeType |
||
| 541 | * @return MimeType or null if not found |
||
| 542 | */ |
||
| 543 | private function findOtherContentPartFor($mimeType) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Creates a new content part for the passed mimeType and charset, making |
||
| 560 | * space by creating a multipart/alternative if needed |
||
| 561 | * |
||
| 562 | * @param string $mimeType |
||
| 563 | * @param string $charset |
||
| 564 | * @return \ZBateson\MailMimeParser\Message\Part\MimePart |
||
| 565 | */ |
||
| 566 | private function createContentPartForMimeType($mimeType, $charset) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Either creates a mime part or sets the existing mime part with the passed |
||
| 590 | * mimeType to $strongOrHandle. |
||
| 591 | * |
||
| 592 | * @param string $mimeType |
||
| 593 | * @param string|resource $stringOrHandle |
||
| 594 | * @param string $charset |
||
| 595 | */ |
||
| 596 | protected function setContentPartForMimeType($mimeType, $stringOrHandle, $charset) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Sets the text/plain part of the message to the passed $stringOrHandle, |
||
| 611 | * either creating a new part if one doesn't exist for text/plain, or |
||
| 612 | * assigning the value of $stringOrHandle to an existing text/plain part. |
||
| 613 | * |
||
| 614 | * The optional $charset parameter is the charset for saving to. |
||
| 615 | * $stringOrHandle is expected to be in UTF-8 regardless of the target |
||
| 616 | * charset. |
||
| 617 | * |
||
| 618 | * @param string|resource $stringOrHandle |
||
| 619 | * @param string $charset |
||
| 620 | */ |
||
| 621 | public function setTextPart($stringOrHandle, $charset = 'UTF-8') |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Sets the text/html part of the message to the passed $stringOrHandle, |
||
| 628 | * either creating a new part if one doesn't exist for text/html, or |
||
| 629 | * assigning the value of $stringOrHandle to an existing text/html part. |
||
| 630 | * |
||
| 631 | * The optional $charset parameter is the charset for saving to. |
||
| 632 | * $stringOrHandle is expected to be in UTF-8 regardless of the target |
||
| 633 | * charset. |
||
| 634 | * |
||
| 635 | * @param string|resource $stringOrHandle |
||
| 636 | * @param string $charset |
||
| 637 | */ |
||
| 638 | public function setHtmlPart($stringOrHandle, $charset = 'UTF-8') |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Removes the text/plain part of the message at the passed index if one |
||
| 645 | * exists. Returns true on success. |
||
| 646 | * |
||
| 647 | * @return bool true on success |
||
| 648 | */ |
||
| 649 | public function removeTextPart($index = 0) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Removes all text/plain inline parts in this message, optionally keeping |
||
| 656 | * other inline parts as attachments on the main message (defaults to |
||
| 657 | * keeping them). |
||
| 658 | * |
||
| 659 | * @param bool $keepOtherPartsAsAttachments |
||
| 660 | * @return bool true on success |
||
| 661 | */ |
||
| 662 | public function removeAllTextParts($keepOtherPartsAsAttachments = true) |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Removes the html part of the message if one exists. Returns true on |
||
| 669 | * success. |
||
| 670 | * |
||
| 671 | * @return bool true on success |
||
| 672 | */ |
||
| 673 | public function removeHtmlPart($index = 0) |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Removes all text/html inline parts in this message, optionally keeping |
||
| 680 | * other inline parts as attachments on the main message (defaults to |
||
| 681 | * keeping them). |
||
| 682 | * |
||
| 683 | * @param bool $keepOtherPartsAsAttachments |
||
| 684 | * @return bool true on success |
||
| 685 | */ |
||
| 686 | public function removeAllHtmlParts($keepOtherPartsAsAttachments = true) |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Removes the attachment with the given index |
||
| 693 | * |
||
| 694 | * @param int $index |
||
| 695 | */ |
||
| 696 | public function removeAttachmentPart($index) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Creates and returns a MimePart for use with a new attachment part being |
||
| 704 | * created. |
||
| 705 | * |
||
| 706 | * @return \ZBateson\MailMimeParser\Message\Part\MimePart |
||
| 707 | */ |
||
| 708 | protected function createPartForAttachment() |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Adds an attachment part for the passed raw data string or handle and |
||
| 723 | * given parameters. |
||
| 724 | * |
||
| 725 | * @param string|handle $stringOrHandle |
||
| 726 | * @param strubg $mimeType |
||
| 727 | * @param string $filename |
||
| 728 | * @param string $disposition |
||
| 729 | */ |
||
| 730 | public function addAttachmentPart($stringOrHandle, $mimeType, $filename = null, $disposition = 'attachment') |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Adds an attachment part using the passed file. |
||
| 745 | * |
||
| 746 | * Essentially creates a file stream and uses it. |
||
| 747 | * |
||
| 748 | * @param string $file |
||
| 749 | * @param string $mimeType |
||
| 750 | * @param string $filename |
||
| 751 | * @param string $disposition |
||
| 752 | */ |
||
| 753 | public function addAttachmentPartFromFile($file, $mimeType, $filename = null, $disposition = 'attachment') |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Saves the message as a MIME message to the passed resource handle. |
||
| 769 | * |
||
| 770 | * @param resource $handle |
||
| 771 | */ |
||
| 772 | public function save($handle) |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Returns the content part of a signed message for a signature to be |
||
| 779 | * calculated on the message. |
||
| 780 | * |
||
| 781 | * @return string |
||
| 782 | */ |
||
| 783 | public function getSignableBody() |
||
| 787 | } |
||
| 788 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.