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 |
||
| 26 | class Message extends MimePart |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var string unique ID used to identify the object to |
||
| 30 | * $this->partStreamRegistry when registering the stream. The ID is |
||
| 31 | * used for opening stream parts with the mmp-mime-message "protocol". |
||
| 32 | * |
||
| 33 | * @see \ZBateson\MailMimeParser\SimpleDi::registerStreamExtensions |
||
| 34 | * @see \ZBateson\MailMimeParser\Stream\PartStream::stream_open |
||
| 35 | */ |
||
| 36 | protected $objectId; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \ZBateson\MailMimeParser\MimePart represents the content portion of |
||
| 40 | * the email message. It is assigned either a text or HTML part, or a |
||
| 41 | * MultipartAlternativePart |
||
| 42 | */ |
||
| 43 | protected $contentPart; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var \ZBateson\MailMimeParser\MimePart contains the body of the signature |
||
| 47 | * for a multipart/signed message. |
||
| 48 | */ |
||
| 49 | protected $signedSignaturePart; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \ZBateson\MailMimeParser\MimePart The mixed part for a |
||
| 53 | * multipart/signed message if the message contains attachments |
||
| 54 | */ |
||
| 55 | protected $signedMixedPart; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \ZBateson\MailMimeParser\MimePart[] array of non-content parts in |
||
| 59 | * this message |
||
| 60 | */ |
||
| 61 | protected $attachmentParts = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var \ZBateson\MailMimeParser\MimePartFactory a MimePartFactory to create |
||
| 65 | * parts for attachments/content |
||
| 66 | */ |
||
| 67 | protected $mimePartFactory; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var bool set to true if a newline should be inserted before the next |
||
| 71 | * boundary (signed messages are finicky) |
||
| 72 | */ |
||
| 73 | private $insertNewLineBeforeBoundary = false; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Convenience method to parse a handle or string into a Message without |
||
| 77 | * requiring including MailMimeParser, instantiating it, and calling parse. |
||
| 78 | * |
||
| 79 | * @param resource|string $handleOrString the resource handle to the input |
||
| 80 | * stream of the mime message, or a string containing a mime message |
||
| 81 | */ |
||
| 82 | 1 | public static function from($handleOrString) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Constructs a Message. |
||
| 90 | * |
||
| 91 | * @param HeaderFactory $headerFactory |
||
| 92 | * @param MimePartFactory $mimePartFactory |
||
| 93 | */ |
||
| 94 | 86 | public function __construct(HeaderFactory $headerFactory, MimePartFactory $mimePartFactory) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Returns the unique object ID registered with the PartStreamRegistry |
||
| 103 | * service object. |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 81 | public function getObjectId() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Loops through the parts parents to find if it's an alternative part or |
||
| 114 | * an attachment. |
||
| 115 | * |
||
| 116 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 117 | * @return boolean true if its been added |
||
| 118 | */ |
||
| 119 | 21 | private function addToAlternativeContentPartFromParsed(MimePart $part) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Returns true if the $part should be assigned as this message's main |
||
| 141 | * content part. |
||
| 142 | * |
||
| 143 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | 81 | private function addContentPartFromParsed(MimePart $part) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Either adds the passed part to $this->textPart if its content type is |
||
| 164 | * text/plain, to $this->htmlPart if it's text/html, or adds the part to the |
||
| 165 | * parts array otherwise. |
||
| 166 | * |
||
| 167 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 168 | */ |
||
| 169 | 84 | public function addPart(MimePart $part) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Returns the content part (or null) for the passed mime type looking at |
||
| 186 | * the assigned content part, and if it's a multipart/alternative part, |
||
| 187 | * looking to find an alternative part of the passed mime type. |
||
| 188 | * |
||
| 189 | * @param string $mimeType |
||
| 190 | * @return \ZBateson\MailMimeParser\MimePart or null if not available |
||
| 191 | */ |
||
| 192 | 71 | protected function getContentPartByMimeType($mimeType) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Sets the content of the message to the content of the passed part, for a |
||
| 208 | * message with a multipart/alternative content type where the other part |
||
| 209 | * has been removed, and this is the only remaining part. |
||
| 210 | * |
||
| 211 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 212 | */ |
||
| 213 | 2 | private function overrideAlternativeMessageContentFromContentPart(MimePart $part) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Removes the passed MimePart as a content part. If there's a remaining |
||
| 234 | * part, either sets the content on this message if the message itself is a |
||
| 235 | * multipart/alternative message, or overrides the contentPart with the |
||
| 236 | * remaining part. |
||
| 237 | * |
||
| 238 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 239 | */ |
||
| 240 | 3 | private function removePartFromAlternativeContentPart(MimePart $part) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Loops over children of the content part looking for a part with the |
||
| 255 | * passed mime type, then proceeds to remove it by calling |
||
| 256 | * removePartFromAlternativeContentPart. |
||
| 257 | * |
||
| 258 | * @param string $contentType |
||
| 259 | * @return boolean true on success |
||
| 260 | */ |
||
| 261 | 3 | private function removeContentPartFromAlternative($contentType) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Removes the content part of the message with the passed mime type. If |
||
| 276 | * there is a remaining content part and it is an alternative part of the |
||
| 277 | * main message, the content part is moved to the message part. |
||
| 278 | * |
||
| 279 | * If the content part is part of an alternative part beneath the message, |
||
| 280 | * the alternative part is replaced by the remaining content part. |
||
| 281 | * |
||
| 282 | * @param string $contentType |
||
| 283 | * @return boolean true on success |
||
| 284 | */ |
||
| 285 | 3 | protected function removeContentPart($contentType) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Returns the text part (or null if none is set.) |
||
| 304 | * |
||
| 305 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 306 | */ |
||
| 307 | 61 | public function getTextPart() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Returns the HTML part (or null if none is set.) |
||
| 314 | * |
||
| 315 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 316 | */ |
||
| 317 | 33 | public function getHtmlPart() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Returns an open resource handle for the passed string or resource handle. |
||
| 324 | * |
||
| 325 | * For a string, creates a php://temp stream and returns it. |
||
| 326 | * |
||
| 327 | * @param resource|string $stringOrHandle |
||
| 328 | * @return resource |
||
| 329 | */ |
||
| 330 | 5 | private function getHandleForStringOrHandle($stringOrHandle) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Creates and returns a unique boundary. |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | 15 | private function getUniqueBoundary() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Creates a unique mime boundary and assigns it to the passed part's |
||
| 354 | * Content-Type header with the passed mime type. |
||
| 355 | * |
||
| 356 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 357 | * @param string $mimeType |
||
| 358 | */ |
||
| 359 | 7 | private function setMimeHeaderBoundaryOnPart(MimePart $part, $mimeType) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Sets this message to be a multipart/alternative message, making space for |
||
| 370 | * another alternative content part. |
||
| 371 | * |
||
| 372 | * Creates a content part and assigns the content stream from the message to |
||
| 373 | * that newly created part. |
||
| 374 | */ |
||
| 375 | 2 | private function setMessageAsAlternative() |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Creates a new mime part as a multipart/alternative, assigning it to |
||
| 393 | * $this->contentPart. Adds the current contentPart below the newly created |
||
| 394 | * alternative part. |
||
| 395 | */ |
||
| 396 | 2 | private function createAlternativeContentPart() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Copies Content-Type, Content-Disposition and Content-Transfer-Encoding |
||
| 409 | * headers from the $from header into the $to header. If the Content-Type |
||
| 410 | * header isn't defined in $from, defaults to text/plain and |
||
| 411 | * quoted-printable. |
||
| 412 | * |
||
| 413 | * @param \ZBateson\MailMimeParser\MimePart $from |
||
| 414 | * @param \ZBateson\MailMimeParser\MimePart $to |
||
| 415 | */ |
||
| 416 | 8 | private function copyTypeHeadersFromPartToPart(MimePart $from, MimePart $to) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Creates a new content part from the passed part, allowing the part to be |
||
| 437 | * used for something else (e.g. changing a non-mime message to a multipart |
||
| 438 | * mime message). |
||
| 439 | */ |
||
| 440 | 4 | private function createNewContentPartFromPart(MimePart $part) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Creates a new part out of the current contentPart and sets the message's |
||
| 451 | * type to be multipart/mixed. |
||
| 452 | */ |
||
| 453 | 4 | private function setMessageAsMixed() |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Updates parents of the contentPart and any children, and sets |
||
| 464 | * $this->contentPart to the passed $messagePart if the $this->contentPart |
||
| 465 | * is set to $this |
||
| 466 | * |
||
| 467 | * @param \ZBateson\MailMimeParser\MimePart $messagePart |
||
| 468 | */ |
||
| 469 | 5 | private function updateContentPartForSignedMessage(MimePart $messagePart) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * This function makes space by moving the main message part down one level. |
||
| 494 | * |
||
| 495 | * The content-type, content-disposition and content-transfer-encoding |
||
| 496 | * headers are copied from this message to the newly created part, the |
||
| 497 | * resource handle is moved and detached, any attachments and content parts |
||
| 498 | * with parents set to this message get their parents set to the newly |
||
| 499 | * created part. |
||
| 500 | */ |
||
| 501 | 5 | private function makeSpaceForMultipartSignedMessage() |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Creates and returns a new MimePart for the signature part of a |
||
| 523 | * multipart/signed message and assigns it to $this->signedSignaturePart. |
||
| 524 | * |
||
| 525 | * @param string $protocol |
||
| 526 | * @param string $body |
||
| 527 | */ |
||
| 528 | 77 | public function createSignaturePart($body) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Creates a multipart/mixed MimePart assigns it to $this->signedMixedPart |
||
| 543 | * if the message contains attachments. |
||
| 544 | * |
||
| 545 | * @param array $parts |
||
| 546 | */ |
||
| 547 | 11 | private function createMultipartMixedForSignedMessage() |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Loops over parts of this message and sets the content-transfer-encoding |
||
| 567 | * header to quoted-printable for text/* mime parts, and to base64 |
||
| 568 | * otherwise for parts that are '8bit' encoded. |
||
| 569 | * |
||
| 570 | * Used for multipart/signed messages which doesn't support 8bit transfer |
||
| 571 | * encodings. |
||
| 572 | */ |
||
| 573 | 7 | private function overwrite8bitContentEncoding() |
|
| 585 | |||
| 586 | /** |
||
| 587 | * Ensures a non-text part comes first in a signed multipart/alternative |
||
| 588 | * message as some clients seem to prefer the first content part if the |
||
| 589 | * client doesn't understand multipart/signed. |
||
| 590 | */ |
||
| 591 | 7 | private function ensureHtmlPartFirstForSignedMessage() |
|
| 605 | |||
| 606 | /** |
||
| 607 | * Turns the message into a multipart/signed message, moving the actual |
||
| 608 | * message into a child part, sets the content-type of the main message to |
||
| 609 | * multipart/signed and adds a signature part as well. |
||
| 610 | * |
||
| 611 | * @param string $micalg The Message Integrity Check algorithm being used |
||
| 612 | * @param string $protocol The mime-type of the signature body |
||
| 613 | * @param string $body The signature signed according to the value of |
||
| 614 | * $protocol |
||
| 615 | */ |
||
| 616 | 7 | public function setAsMultipartSigned($micalg, $protocol) |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Returns the signed part or null if not set. |
||
| 636 | * |
||
| 637 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 638 | */ |
||
| 639 | 11 | public function getSignaturePart() |
|
| 643 | |||
| 644 | /** |
||
| 645 | * Enforces the message to be a mime message for a non-mime (e.g. uuencoded |
||
| 646 | * or unspecified) message. If the message has uuencoded attachments, sets |
||
| 647 | * up the message as a multipart/mixed message and creates a content part. |
||
| 648 | */ |
||
| 649 | 9 | private function enforceMime() |
|
| 660 | |||
| 661 | /** |
||
| 662 | * Creates a new content part for the passed mimeType and charset, making |
||
| 663 | * space by creating a multipart/alternative if needed |
||
| 664 | * |
||
| 665 | * @param string $mimeType |
||
| 666 | * @param string $charset |
||
| 667 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 668 | */ |
||
| 669 | 4 | private function createContentPartForMimeType($mimeType, $charset) |
|
| 694 | |||
| 695 | /** |
||
| 696 | * Either creates a mime part or sets the existing mime part with the passed |
||
| 697 | * mimeType to $strongOrHandle. |
||
| 698 | * |
||
| 699 | * @param string $mimeType |
||
| 700 | * @param string|resource $stringOrHandle |
||
| 701 | * @param string $charset |
||
| 702 | */ |
||
| 703 | 4 | protected function setContentPartForMimeType($mimeType, $stringOrHandle, $charset) |
|
| 719 | |||
| 720 | /** |
||
| 721 | * Sets the text/plain part of the message to the passed $stringOrHandle, |
||
| 722 | * either creating a new part if one doesn't exist for text/plain, or |
||
| 723 | * assigning the value of $stringOrHandle to an existing text/plain part. |
||
| 724 | * |
||
| 725 | * The optional $charset parameter is the charset for saving to. |
||
| 726 | * $stringOrHandle is expected to be in UTF-8. |
||
| 727 | * |
||
| 728 | * @param string|resource $stringOrHandle |
||
| 729 | * @param string $charset |
||
| 730 | */ |
||
| 731 | 1 | public function setTextPart($stringOrHandle, $charset = null) |
|
| 735 | |||
| 736 | /** |
||
| 737 | * Sets the text/html part of the message to the passed $stringOrHandle, |
||
| 738 | * either creating a new part if one doesn't exist for text/html, or |
||
| 739 | * assigning the value of $stringOrHandle to an existing text/html part. |
||
| 740 | * |
||
| 741 | * The optional $charset parameter is the charset for saving to. |
||
| 742 | * $stringOrHandle is expected to be in UTF-8. |
||
| 743 | * |
||
| 744 | * @param string|resource $stringOrHandle |
||
| 745 | * @param string $charset |
||
| 746 | */ |
||
| 747 | 4 | public function setHtmlPart($stringOrHandle, $charset = null) |
|
| 751 | |||
| 752 | /** |
||
| 753 | * Removes the text part of the message if one exists. Returns true on |
||
| 754 | * success. |
||
| 755 | * |
||
| 756 | * @return bool true on success |
||
| 757 | */ |
||
| 758 | 2 | public function removeTextPart() |
|
| 762 | |||
| 763 | /** |
||
| 764 | * Removes the html part of the message if one exists. Returns true on |
||
| 765 | * success. |
||
| 766 | * |
||
| 767 | * @return bool true on success |
||
| 768 | */ |
||
| 769 | 1 | public function removeHtmlPart() |
|
| 773 | |||
| 774 | /** |
||
| 775 | * Returns the non-content part at the given 0-based index, or null if none |
||
| 776 | * is set. |
||
| 777 | * |
||
| 778 | * @param int $index |
||
| 779 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 780 | */ |
||
| 781 | 7 | public function getAttachmentPart($index) |
|
| 788 | |||
| 789 | /** |
||
| 790 | * Returns all attachment parts. |
||
| 791 | * |
||
| 792 | * @return \ZBateson\MailMimeParser\MimePart[] |
||
| 793 | */ |
||
| 794 | 44 | public function getAllAttachmentParts() |
|
| 798 | |||
| 799 | /** |
||
| 800 | * Returns the number of attachments available. |
||
| 801 | * |
||
| 802 | * @return int |
||
| 803 | */ |
||
| 804 | 45 | public function getAttachmentCount() |
|
| 808 | |||
| 809 | /** |
||
| 810 | * Removes the attachment with the given index |
||
| 811 | * |
||
| 812 | * @param int $index |
||
| 813 | */ |
||
| 814 | 2 | public function removeAttachmentPart($index) |
|
| 820 | |||
| 821 | /** |
||
| 822 | * Creates and returns a MimePart for use with a new attachment part being |
||
| 823 | * created. |
||
| 824 | * |
||
| 825 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 826 | */ |
||
| 827 | 2 | protected function createPartForAttachment() |
|
| 841 | |||
| 842 | /** |
||
| 843 | * Adds an attachment part for the passed raw data string or handle and |
||
| 844 | * given parameters. |
||
| 845 | * |
||
| 846 | * @param string|handle $stringOrHandle |
||
| 847 | * @param strubg $mimeType |
||
| 848 | * @param string $filename |
||
| 849 | * @param string $disposition |
||
| 850 | */ |
||
| 851 | 1 | public function addAttachmentPart($stringOrHandle, $mimeType, $filename = null, $disposition = 'attachment') |
|
| 865 | |||
| 866 | /** |
||
| 867 | * Adds an attachment part using the passed file. |
||
| 868 | * |
||
| 869 | * Essentially creates a file stream and uses it. |
||
| 870 | * |
||
| 871 | * @param string $file |
||
| 872 | * @param string $mimeType |
||
| 873 | * @param string $filename |
||
| 874 | * @param string $disposition |
||
| 875 | */ |
||
| 876 | 2 | public function addAttachmentPartFromFile($file, $mimeType, $filename = null, $disposition = 'attachment') |
|
| 891 | |||
| 892 | /** |
||
| 893 | * Returns a resource handle where the text content can be read or null if |
||
| 894 | * unavailable. |
||
| 895 | * |
||
| 896 | * @return resource |
||
| 897 | */ |
||
| 898 | 57 | public function getTextStream() |
|
| 906 | |||
| 907 | /** |
||
| 908 | * Returns the text content as a string. |
||
| 909 | * |
||
| 910 | * Reads the entire stream content into a string and returns it. Returns |
||
| 911 | * null if the message doesn't have a text part. |
||
| 912 | * |
||
| 913 | * @return string |
||
| 914 | */ |
||
| 915 | 1 | public function getTextContent() |
|
| 923 | |||
| 924 | /** |
||
| 925 | * Returns a resource handle where the HTML content can be read or null if |
||
| 926 | * unavailable. |
||
| 927 | * |
||
| 928 | * @return resource |
||
| 929 | */ |
||
| 930 | 27 | public function getHtmlStream() |
|
| 938 | |||
| 939 | /** |
||
| 940 | * Returns the HTML content as a string. |
||
| 941 | * |
||
| 942 | * Reads the entire stream content into a string and returns it. Returns |
||
| 943 | * null if the message doesn't have an HTML part. |
||
| 944 | * |
||
| 945 | * @return string |
||
| 946 | */ |
||
| 947 | public function getHtmlContent() |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Returns true if either a Content-Type or Mime-Version header are defined |
||
| 958 | * in this Message. |
||
| 959 | * |
||
| 960 | * @return bool |
||
| 961 | */ |
||
| 962 | 81 | public function isMime() |
|
| 968 | |||
| 969 | /** |
||
| 970 | * Writes out a mime boundary to the passed $handle |
||
| 971 | * |
||
| 972 | * @param resource $handle |
||
| 973 | * @param string $boundary |
||
| 974 | * @param bool $isEnd |
||
| 975 | */ |
||
| 976 | 51 | private function writeBoundary($handle, $boundary, $isEnd = false) |
|
| 990 | |||
| 991 | /** |
||
| 992 | * Writes out any necessary boundaries for the given $part if required based |
||
| 993 | * on its $parent and $boundaryParent. |
||
| 994 | * |
||
| 995 | * Also writes out end boundaries for the previous part if applicable. |
||
| 996 | * |
||
| 997 | * @param resource $handle |
||
| 998 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 999 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
| 1000 | * @param \ZBateson\MailMimeParser\MimePart $boundaryParent |
||
| 1001 | * @param string $boundary |
||
| 1002 | */ |
||
| 1003 | 51 | private function writePartBoundaries($handle, MimePart $part, MimePart $parent, MimePart &$boundaryParent, $boundary) |
|
| 1016 | |||
| 1017 | /** |
||
| 1018 | * Writes out the passed mime part, writing out any necessary mime |
||
| 1019 | * boundaries. |
||
| 1020 | * |
||
| 1021 | * @param resource $handle |
||
| 1022 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 1023 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
| 1024 | * @param \ZBateson\MailMimeParser\MimePart $boundaryParent |
||
| 1025 | */ |
||
| 1026 | 77 | private function writePartTo($handle, MimePart $part, MimePart $parent, MimePart &$boundaryParent) |
|
| 1044 | |||
| 1045 | /** |
||
| 1046 | * Either returns $this for a non-text, non-html part, or returns |
||
| 1047 | * $this->contentPart. |
||
| 1048 | * |
||
| 1049 | * Note that if Content-Disposition is set on the passed part, $this is |
||
| 1050 | * always returned. |
||
| 1051 | * |
||
| 1052 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 1053 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 1054 | */ |
||
| 1055 | 77 | private function getWriteParentForPart(MimePart $part) |
|
| 1066 | |||
| 1067 | /** |
||
| 1068 | * Loops over parts of the message and writes them as an email to the |
||
| 1069 | * provided $handle. |
||
| 1070 | * |
||
| 1071 | * The function rewrites mime parts in a multipart-mime message to be either |
||
| 1072 | * alternatives of text/plain and text/html, or attachments because |
||
| 1073 | * MailMimeParser doesn't currently maintain the structure of the original |
||
| 1074 | * message. This means other alternative parts would be dropped to |
||
| 1075 | * attachments, and multipart/related parts are completely ignored. |
||
| 1076 | * |
||
| 1077 | * @param resource $handle the handle to write out to |
||
| 1078 | * @param Iterator $partsIter an Iterator for parts to save |
||
| 1079 | * @param \ZBateson\MailMimeParser\MimePart $curParent the current parent |
||
| 1080 | */ |
||
| 1081 | 77 | protected function writePartsTo($handle, Iterator $partsIter, MimePart $curParent) |
|
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Saves the message as a MIME message to the passed resource handle. |
||
| 1098 | * |
||
| 1099 | * The saved message is not guaranteed to be the same as the parsed message. |
||
| 1100 | * Namely, for mime messages anything that is not text/html or text/plain |
||
| 1101 | * will be moved into parts under the main 'message' as attachments, other |
||
| 1102 | * alternative parts are dropped, and multipart/related parts are ignored |
||
| 1103 | * (their contents are either moved under a multipart/alternative part or as |
||
| 1104 | * attachments below the main multipart/mixed message). |
||
| 1105 | * |
||
| 1106 | * @param resource $handle |
||
| 1107 | */ |
||
| 1108 | 77 | public function save($handle) |
|
| 1135 | |||
| 1136 | /** |
||
| 1137 | * Writes out the content of the message into a string and returns it. |
||
| 1138 | * |
||
| 1139 | * @return string |
||
| 1140 | */ |
||
| 1141 | 7 | private function getSignableBodyFromParts(array $parts) |
|
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Returns the content part of a signed message for a signature to be |
||
| 1162 | * calculated on the message. |
||
| 1163 | * |
||
| 1164 | * @return string |
||
| 1165 | */ |
||
| 1166 | 7 | public function getSignableBody() |
|
| 1185 | |||
| 1186 | /** |
||
| 1187 | * Shortcut to call Message::save with a php://temp stream and return the |
||
| 1188 | * written email message as a string. |
||
| 1189 | * |
||
| 1190 | * @return string |
||
| 1191 | */ |
||
| 1192 | public function __toString() |
||
| 1201 | } |
||
| 1202 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: