Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Message 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 Message, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Message extends MimePart |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var string unique ID used to identify the object to |
||
| 31 | * $this->partStreamRegistry when registering the stream. The ID is |
||
| 32 | * used for opening stream parts with the mmp-mime-message "protocol". |
||
| 33 | * |
||
| 34 | * @see \ZBateson\MailMimeParser\SimpleDi::registerStreamExtensions |
||
| 35 | * @see \ZBateson\MailMimeParser\Stream\PartStream::stream_open |
||
| 36 | */ |
||
| 37 | protected $objectId; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var \ZBateson\MailMimeParser\Message\MimePart represents the content portion of |
||
| 41 | * the email message. It is assigned either a text or HTML part, or a |
||
| 42 | * MultipartAlternativePart |
||
| 43 | */ |
||
| 44 | protected $contentPart; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var \ZBateson\MailMimeParser\Message\MimePart contains the body of the signature |
||
| 48 | * for a multipart/signed message. |
||
| 49 | */ |
||
| 50 | protected $signedSignaturePart; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var \ZBateson\MailMimeParser\Message\MimePart[] array of non-content parts in |
||
| 54 | * this message |
||
| 55 | */ |
||
| 56 | protected $attachmentParts = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var \ZBateson\MailMimeParser\Message\MimePartFactory a MimePartFactory to create |
||
| 60 | * parts for attachments/content |
||
| 61 | */ |
||
| 62 | protected $mimePartFactory; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var \ZBateson\MailMimeParser\Message\Writer\MessageWriter the part |
||
| 66 | * writer for this Message. The same object is assigned to $partWriter |
||
| 67 | * but as an AbstractWriter -- not really needed in PHP but helps with |
||
| 68 | * auto-complete and code analyzers. |
||
| 69 | */ |
||
| 70 | protected $messageWriter = null; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var bool set to true if a newline should be inserted before the next |
||
| 74 | * boundary (signed messages are finicky) |
||
| 75 | */ |
||
| 76 | private $insertNewLineBeforeBoundary = false; |
||
|
|
|||
| 77 | |||
| 78 | /** |
||
| 79 | * Convenience method to parse a handle or string into a Message without |
||
| 80 | * requiring including MailMimeParser, instantiating it, and calling parse. |
||
| 81 | * |
||
| 82 | * @param resource|string $handleOrString the resource handle to the input |
||
| 83 | * stream of the mime message, or a string containing a mime message |
||
| 84 | */ |
||
| 85 | 1 | public static function from($handleOrString) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Constructs a Message. |
||
| 93 | * |
||
| 94 | * @param HeaderFactory $headerFactory |
||
| 95 | * @param MessageWriter $messageWriter |
||
| 96 | * @param MimePartFactory $mimePartFactory |
||
| 97 | */ |
||
| 98 | 89 | public function __construct( |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Returns the unique object ID registered with the PartStreamRegistry |
||
| 111 | * service object. |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | 84 | public function getObjectId() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Returns true if the $part should be assigned as this message's main |
||
| 122 | * content part. |
||
| 123 | * |
||
| 124 | * @param \ZBateson\MailMimeParser\Message\MimePart $part |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | 84 | private function addContentPartFromParsed(MimePart $part) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Adds the passed part to the message with the passed position, or at the |
||
| 144 | * end if not passed. |
||
| 145 | * |
||
| 146 | * This should not be used by a user directly and will be set 'protected' in |
||
| 147 | * the future. Instead setTextPart, setHtmlPart and addAttachment should be |
||
| 148 | * used. |
||
| 149 | * |
||
| 150 | * @param \ZBateson\MailMimeParser\Message\MimePart $part |
||
| 151 | * @param int $position |
||
| 152 | */ |
||
| 153 | 87 | public function addPart(MimePart $part, $position = null) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Returns the content part (or null) for the passed mime type looking at |
||
| 169 | * the assigned content part, and if it's a multipart/alternative part, |
||
| 170 | * looking to find an alternative part of the passed mime type. |
||
| 171 | * |
||
| 172 | * @param string $mimeType |
||
| 173 | * @return \ZBateson\MailMimeParser\Message\MimePart or null if not |
||
| 174 | * available |
||
| 175 | */ |
||
| 176 | 74 | protected function getContentPartByMimeType($mimeType) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Sets the content of the message to the content of the passed part, for a |
||
| 192 | * message with a multipart/alternative content type where the other part |
||
| 193 | * has been removed, and this is the only remaining part. |
||
| 194 | * |
||
| 195 | * @param \ZBateson\MailMimeParser\Message\MimePart $part |
||
| 196 | */ |
||
| 197 | 2 | private function overrideAlternativeMessageContentFromContentPart(MimePart $part) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Removes the passed MimePart as a content part. If there's a remaining |
||
| 218 | * part, either sets the content on this message if the message itself is a |
||
| 219 | * multipart/alternative message, or overrides the contentPart with the |
||
| 220 | * remaining part. |
||
| 221 | * |
||
| 222 | * @param \ZBateson\MailMimeParser\Message\MimePart $part |
||
| 223 | */ |
||
| 224 | 3 | private function removePartFromAlternativeContentPart(MimePart $part) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Loops over children of the content part looking for a part with the |
||
| 242 | * passed mime type, then proceeds to remove it by calling |
||
| 243 | * removePartFromAlternativeContentPart. |
||
| 244 | * |
||
| 245 | * @param string $contentType |
||
| 246 | * @return boolean true on success |
||
| 247 | */ |
||
| 248 | 3 | private function removeContentPartFromAlternative($contentType) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Removes the content part of the message with the passed mime type. If |
||
| 263 | * there is a remaining content part and it is an alternative part of the |
||
| 264 | * main message, the content part is moved to the message part. |
||
| 265 | * |
||
| 266 | * If the content part is part of an alternative part beneath the message, |
||
| 267 | * the alternative part is replaced by the remaining content part. |
||
| 268 | * |
||
| 269 | * @param string $contentType |
||
| 270 | * @return boolean true on success |
||
| 271 | */ |
||
| 272 | 3 | protected function removeContentPart($contentType) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Returns the text part (or null if none is set.) |
||
| 291 | * |
||
| 292 | * @return \ZBateson\MailMimeParser\Message\MimePart |
||
| 293 | */ |
||
| 294 | 64 | public function getTextPart() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Returns the HTML part (or null if none is set.) |
||
| 301 | * |
||
| 302 | * @return \ZBateson\MailMimeParser\Message\MimePart |
||
| 303 | */ |
||
| 304 | 36 | public function getHtmlPart() |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Returns the content MimePart, which could be a text/plain, text/html or |
||
| 311 | * multipart/alternative part or null if none is set. |
||
| 312 | * |
||
| 313 | * @return \ZBateson\MailMimeParser\Message\MimePart |
||
| 314 | */ |
||
| 315 | 1 | public function getContentPart() |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Returns an open resource handle for the passed string or resource handle. |
||
| 322 | * |
||
| 323 | * For a string, creates a php://temp stream and returns it. |
||
| 324 | * |
||
| 325 | * @param resource|string $stringOrHandle |
||
| 326 | * @return resource |
||
| 327 | */ |
||
| 328 | 5 | private function getHandleForStringOrHandle($stringOrHandle) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Creates and returns a unique boundary. |
||
| 342 | * |
||
| 343 | * @param string $mimeType first 3 characters of a multipart type are used, |
||
| 344 | * e.g. REL for relative or ALT for alternative |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | 14 | private function getUniqueBoundary($mimeType) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Creates a unique mime boundary and assigns it to the passed part's |
||
| 355 | * Content-Type header with the passed mime type. |
||
| 356 | * |
||
| 357 | * @param \ZBateson\MailMimeParser\Message\MimePart $part |
||
| 358 | * @param string $mimeType |
||
| 359 | */ |
||
| 360 | 7 | private function setMimeHeaderBoundaryOnPart(MimePart $part, $mimeType) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Sets this message to be a multipart/alternative message, making space for |
||
| 371 | * another alternative content part. |
||
| 372 | * |
||
| 373 | * Creates a content part and assigns the content stream from the message to |
||
| 374 | * that newly created part. |
||
| 375 | */ |
||
| 376 | 2 | private function setMessageAsAlternative() |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Creates a new mime part as a multipart/alternative, assigning it to |
||
| 397 | * $this->contentPart. Adds the current contentPart below the newly created |
||
| 398 | * alternative part. |
||
| 399 | */ |
||
| 400 | 2 | View Code Duplication | private function createAlternativeContentPart() |
| 412 | |||
| 413 | /** |
||
| 414 | * Copies Content-Type, Content-Disposition and Content-Transfer-Encoding |
||
| 415 | * headers from the $from header into the $to header. If the Content-Type |
||
| 416 | * header isn't defined in $from, defaults to text/plain and |
||
| 417 | * quoted-printable. |
||
| 418 | * |
||
| 419 | * @param \ZBateson\MailMimeParser\Message\MimePart $from |
||
| 420 | * @param \ZBateson\MailMimeParser\Message\MimePart $to |
||
| 421 | */ |
||
| 422 | 11 | private function copyTypeHeadersFromPartToPart(MimePart $from, MimePart $to) |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Creates a new content part from the passed part, allowing the part to be |
||
| 443 | * used for something else (e.g. changing a non-mime message to a multipart |
||
| 444 | * mime message). |
||
| 445 | */ |
||
| 446 | 4 | private function createNewContentPartFromPart(MimePart $part) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Creates a new part out of the current contentPart and sets the message's |
||
| 457 | * type to be multipart/mixed. |
||
| 458 | */ |
||
| 459 | 4 | private function setMessageAsMixed() |
|
| 467 | |||
| 468 | /** |
||
| 469 | * This function makes space by moving the main message part down one level. |
||
| 470 | * |
||
| 471 | * The content-type, content-disposition and content-transfer-encoding |
||
| 472 | * headers are copied from this message to the newly created part, the |
||
| 473 | * resource handle is moved and detached, any attachments and content parts |
||
| 474 | * with parents set to this message get their parents set to the newly |
||
| 475 | * created part. |
||
| 476 | */ |
||
| 477 | 8 | private function makeSpaceForMultipartSignedMessage() |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Creates and returns a new MimePart for the signature part of a |
||
| 501 | * multipart/signed message and assigns it to $this->signedSignaturePart. |
||
| 502 | * |
||
| 503 | * @param string $body |
||
| 504 | */ |
||
| 505 | 8 | View Code Duplication | public function createSignaturePart($body) |
| 520 | |||
| 521 | /** |
||
| 522 | * Loops over parts of this message and sets the content-transfer-encoding |
||
| 523 | * header to quoted-printable for text/* mime parts, and to base64 |
||
| 524 | * otherwise for parts that are '8bit' encoded. |
||
| 525 | * |
||
| 526 | * Used for multipart/signed messages which doesn't support 8bit transfer |
||
| 527 | * encodings. |
||
| 528 | */ |
||
| 529 | 8 | private function overwrite8bitContentEncoding() |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Ensures a non-text part comes first in a signed multipart/alternative |
||
| 545 | * message as some clients seem to prefer the first content part if the |
||
| 546 | * client doesn't understand multipart/signed. |
||
| 547 | */ |
||
| 548 | 8 | private function ensureHtmlPartFirstForSignedMessage() |
|
| 562 | |||
| 563 | /** |
||
| 564 | * Turns the message into a multipart/signed message, moving the actual |
||
| 565 | * message into a child part, sets the content-type of the main message to |
||
| 566 | * multipart/signed and adds a signature part as well. |
||
| 567 | * |
||
| 568 | * @param string $micalg The Message Integrity Check algorithm being used |
||
| 569 | * @param string $protocol The mime-type of the signature body |
||
| 570 | */ |
||
| 571 | 8 | public function setAsMultipartSigned($micalg, $protocol) |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Returns the signed part or null if not set. |
||
| 590 | * |
||
| 591 | * @return \ZBateson\MailMimeParser\Message\MimePart |
||
| 592 | */ |
||
| 593 | 12 | public function getSignaturePart() |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Enforces the message to be a mime message for a non-mime (e.g. uuencoded |
||
| 600 | * or unspecified) message. If the message has uuencoded attachments, sets |
||
| 601 | * up the message as a multipart/mixed message and creates a content part. |
||
| 602 | */ |
||
| 603 | 12 | private function enforceMime() |
|
| 614 | |||
| 615 | /** |
||
| 616 | * Creates a new content part for the passed mimeType and charset, making |
||
| 617 | * space by creating a multipart/alternative if needed |
||
| 618 | * |
||
| 619 | * @param string $mimeType |
||
| 620 | * @param string $charset |
||
| 621 | * @return \ZBateson\MailMimeParser\Message\MimePart |
||
| 622 | */ |
||
| 623 | 4 | private function createContentPartForMimeType($mimeType, $charset) |
|
| 647 | |||
| 648 | /** |
||
| 649 | * Either creates a mime part or sets the existing mime part with the passed |
||
| 650 | * mimeType to $strongOrHandle. |
||
| 651 | * |
||
| 652 | * @param string $mimeType |
||
| 653 | * @param string|resource $stringOrHandle |
||
| 654 | * @param string $charset |
||
| 655 | */ |
||
| 656 | 4 | protected function setContentPartForMimeType($mimeType, $stringOrHandle, $charset) |
|
| 669 | |||
| 670 | /** |
||
| 671 | * Sets the text/plain part of the message to the passed $stringOrHandle, |
||
| 672 | * either creating a new part if one doesn't exist for text/plain, or |
||
| 673 | * assigning the value of $stringOrHandle to an existing text/plain part. |
||
| 674 | * |
||
| 675 | * The optional $charset parameter is the charset for saving to. |
||
| 676 | * $stringOrHandle is expected to be in UTF-8. |
||
| 677 | * |
||
| 678 | * @param string|resource $stringOrHandle |
||
| 679 | * @param string $charset |
||
| 680 | */ |
||
| 681 | 1 | public function setTextPart($stringOrHandle, $charset = null) |
|
| 685 | |||
| 686 | /** |
||
| 687 | * Sets the text/html part of the message to the passed $stringOrHandle, |
||
| 688 | * either creating a new part if one doesn't exist for text/html, or |
||
| 689 | * assigning the value of $stringOrHandle to an existing text/html part. |
||
| 690 | * |
||
| 691 | * The optional $charset parameter is the charset for saving to. |
||
| 692 | * $stringOrHandle is expected to be in UTF-8. |
||
| 693 | * |
||
| 694 | * @param string|resource $stringOrHandle |
||
| 695 | * @param string $charset |
||
| 696 | */ |
||
| 697 | 4 | public function setHtmlPart($stringOrHandle, $charset = null) |
|
| 701 | |||
| 702 | /** |
||
| 703 | * Removes the text part of the message if one exists. Returns true on |
||
| 704 | * success. |
||
| 705 | * |
||
| 706 | * @return bool true on success |
||
| 707 | */ |
||
| 708 | 2 | public function removeTextPart() |
|
| 712 | |||
| 713 | /** |
||
| 714 | * Removes the html part of the message if one exists. Returns true on |
||
| 715 | * success. |
||
| 716 | * |
||
| 717 | * @return bool true on success |
||
| 718 | */ |
||
| 719 | 1 | public function removeHtmlPart() |
|
| 723 | |||
| 724 | /** |
||
| 725 | * Returns the non-content part at the given 0-based index, or null if none |
||
| 726 | * is set. |
||
| 727 | * |
||
| 728 | * @param int $index |
||
| 729 | * @return \ZBateson\MailMimeParser\Message\MimePart |
||
| 730 | */ |
||
| 731 | 7 | public function getAttachmentPart($index) |
|
| 738 | |||
| 739 | /** |
||
| 740 | * Returns all attachment parts. |
||
| 741 | * |
||
| 742 | * @return \ZBateson\MailMimeParser\Message\MimePart[] |
||
| 743 | */ |
||
| 744 | 47 | public function getAllAttachmentParts() |
|
| 748 | |||
| 749 | /** |
||
| 750 | * Returns the number of attachments available. |
||
| 751 | * |
||
| 752 | * @return int |
||
| 753 | */ |
||
| 754 | 48 | public function getAttachmentCount() |
|
| 758 | |||
| 759 | /** |
||
| 760 | * Removes the attachment with the given index |
||
| 761 | * |
||
| 762 | * @param int $index |
||
| 763 | */ |
||
| 764 | 2 | public function removeAttachmentPart($index) |
|
| 770 | |||
| 771 | /** |
||
| 772 | * Creates and returns a MimePart for use with a new attachment part being |
||
| 773 | * created. |
||
| 774 | * |
||
| 775 | * @return \ZBateson\MailMimeParser\Message\MimePart |
||
| 776 | */ |
||
| 777 | 2 | protected function createPartForAttachment() |
|
| 791 | |||
| 792 | /** |
||
| 793 | * Adds an attachment part for the passed raw data string or handle and |
||
| 794 | * given parameters. |
||
| 795 | * |
||
| 796 | * @param string|handle $stringOrHandle |
||
| 797 | * @param strubg $mimeType |
||
| 798 | * @param string $filename |
||
| 799 | * @param string $disposition |
||
| 800 | */ |
||
| 801 | 1 | public function addAttachmentPart($stringOrHandle, $mimeType, $filename = null, $disposition = 'attachment') |
|
| 814 | |||
| 815 | /** |
||
| 816 | * Adds an attachment part using the passed file. |
||
| 817 | * |
||
| 818 | * Essentially creates a file stream and uses it. |
||
| 819 | * |
||
| 820 | * @param string $file |
||
| 821 | * @param string $mimeType |
||
| 822 | * @param string $filename |
||
| 823 | * @param string $disposition |
||
| 824 | */ |
||
| 825 | 2 | public function addAttachmentPartFromFile($file, $mimeType, $filename = null, $disposition = 'attachment') |
|
| 839 | |||
| 840 | /** |
||
| 841 | * Returns a resource handle where the text content can be read or null if |
||
| 842 | * unavailable. |
||
| 843 | * |
||
| 844 | * @return resource |
||
| 845 | */ |
||
| 846 | 60 | public function getTextStream() |
|
| 854 | |||
| 855 | /** |
||
| 856 | * Returns the text content as a string. |
||
| 857 | * |
||
| 858 | * Reads the entire stream content into a string and returns it. Returns |
||
| 859 | * null if the message doesn't have a text part. |
||
| 860 | * |
||
| 861 | * @return string |
||
| 862 | */ |
||
| 863 | 1 | public function getTextContent() |
|
| 871 | |||
| 872 | /** |
||
| 873 | * Returns a resource handle where the HTML content can be read or null if |
||
| 874 | * unavailable. |
||
| 875 | * |
||
| 876 | * @return resource |
||
| 877 | */ |
||
| 878 | 30 | public function getHtmlStream() |
|
| 886 | |||
| 887 | /** |
||
| 888 | * Returns the HTML content as a string. |
||
| 889 | * |
||
| 890 | * Reads the entire stream content into a string and returns it. Returns |
||
| 891 | * null if the message doesn't have an HTML part. |
||
| 892 | * |
||
| 893 | * @return string |
||
| 894 | */ |
||
| 895 | public function getHtmlContent() |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Returns true if either a Content-Type or Mime-Version header are defined |
||
| 906 | * in this Message. |
||
| 907 | * |
||
| 908 | * @return bool |
||
| 909 | */ |
||
| 910 | 84 | public function isMime() |
|
| 916 | |||
| 917 | /** |
||
| 918 | * Saves the message as a MIME message to the passed resource handle. |
||
| 919 | * |
||
| 920 | * @param resource $handle |
||
| 921 | */ |
||
| 922 | 80 | public function save($handle) |
|
| 926 | |||
| 927 | /** |
||
| 928 | * Returns the content part of a signed message for a signature to be |
||
| 929 | * calculated on the message. |
||
| 930 | * |
||
| 931 | * @return string |
||
| 932 | */ |
||
| 933 | 8 | public function getSignableBody() |
|
| 937 | |||
| 938 | /** |
||
| 939 | * Shortcut to call Message::save with a php://temp stream and return the |
||
| 940 | * written email message as a string. |
||
| 941 | * |
||
| 942 | * @return string |
||
| 943 | */ |
||
| 944 | public function __toString() |
||
| 953 | } |
||
| 954 |
This check marks private properties in classes that are never used. Those properties can be removed.