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 | * Convenience method to parse a handle or string into a Message without |
||
| 71 | * requiring including MailMimeParser, instantiating it, and calling parse. |
||
| 72 | * |
||
| 73 | * @param resource|string $handleOrString the resource handle to the input |
||
| 74 | * stream of the mime message, or a string containing a mime message |
||
| 75 | */ |
||
| 76 | 1 | public static function from($handleOrString) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Constructs a Message. |
||
| 84 | * |
||
| 85 | * @param HeaderFactory $headerFactory |
||
| 86 | * @param MimePartFactory $mimePartFactory |
||
| 87 | */ |
||
| 88 | 80 | public function __construct(HeaderFactory $headerFactory, MimePartFactory $mimePartFactory) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Returns the unique object ID registered with the PartStreamRegistry |
||
| 97 | * service object. |
||
| 98 | * |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | 75 | public function getObjectId() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Loops through the parts parents to find if it's an alternative part or |
||
| 108 | * an attachment. |
||
| 109 | * |
||
| 110 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 111 | * @return boolean true if its been added |
||
| 112 | */ |
||
| 113 | 21 | private function addToAlternativeContentPartFromParsed(MimePart $part) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Returns true if the $part should be assigned as this message's main |
||
| 135 | * content part. |
||
| 136 | * |
||
| 137 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | 76 | private function addContentPartFromParsed(MimePart $part) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Either adds the passed part to $this->textPart if its content type is |
||
| 158 | * text/plain, to $this->htmlPart if it's text/html, or adds the part to the |
||
| 159 | * parts array otherwise. |
||
| 160 | * |
||
| 161 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 162 | */ |
||
| 163 | 78 | public function addPart(MimePart $part) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Returns the content part (or null) for the passed mime type looking at |
||
| 180 | * the assigned content part, and if it's a multipart/alternative part, |
||
| 181 | * looking to find an alternative part of the passed mime type. |
||
| 182 | * |
||
| 183 | * @param string $mimeType |
||
| 184 | * @return \ZBateson\MailMimeParser\MimePart or null if not available |
||
| 185 | */ |
||
| 186 | 69 | protected function getContentPartByMimeType($mimeType) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Sets the content of the message to the content of the passed part, for a |
||
| 202 | * message with a multipart/alternative content type where the other part |
||
| 203 | * has been removed, and this is the only remaining part. |
||
| 204 | * |
||
| 205 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 206 | */ |
||
| 207 | 1 | private function overrideAlternativeMessageContentFromContentPart(MimePart $part) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Removes the passed MimePart as a content part. If there's a remaining |
||
| 230 | * part, either sets the content on this message if the message itself is a |
||
| 231 | * multipart/alternative message, or overrides the contentPart with the |
||
| 232 | * remaining part. |
||
| 233 | * |
||
| 234 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 235 | */ |
||
| 236 | 2 | private function removePartFromAlternativeContentPart(MimePart $part) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Loops over children of the content part looking for a part with the |
||
| 251 | * passed mime type, then proceeds to remove it by calling |
||
| 252 | * removePartFromAlternativeContentPart. |
||
| 253 | * |
||
| 254 | * @param string $contentType |
||
| 255 | * @return boolean true on success |
||
| 256 | */ |
||
| 257 | 2 | private function removeContentPartFromAlternative($contentType) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Removes the content part of the message with the passed mime type. If |
||
| 272 | * there is a remaining content part and it is an alternative part of the |
||
| 273 | * main message, the content part is moved to the message part. |
||
| 274 | * |
||
| 275 | * If the content part is part of an alternative part beneath the message, |
||
| 276 | * the alternative part is replaced by the remaining content part. |
||
| 277 | * |
||
| 278 | * @param string $contentType |
||
| 279 | * @return boolean true on success |
||
| 280 | */ |
||
| 281 | 2 | protected function removeContentPart($contentType) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Returns the text part (or null if none is set.) |
||
| 300 | * |
||
| 301 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 302 | */ |
||
| 303 | 59 | public function getTextPart() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Returns the HTML part (or null if none is set.) |
||
| 310 | * |
||
| 311 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 312 | */ |
||
| 313 | 32 | public function getHtmlPart() |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Returns an open resource handle for the passed string or resource handle. |
||
| 320 | * |
||
| 321 | * For a string, creates a php://temp stream and returns it. |
||
| 322 | * |
||
| 323 | * @param resource|string $stringOrHandle |
||
| 324 | * @return resource |
||
| 325 | */ |
||
| 326 | 4 | private function getHandleForStringOrHandle($stringOrHandle) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Creates and returns a unique boundary. |
||
| 340 | * |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | 12 | private function getUniqueBoundary() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Creates a unique mime boundary and assigns it to the passed part's |
||
| 350 | * Content-Type header with the passed mime type. |
||
| 351 | * |
||
| 352 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 353 | * @param string $mimeType |
||
| 354 | */ |
||
| 355 | 6 | private function setMimeHeaderBoundaryOnPart(MimePart $part, $mimeType) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Sets this message to be a multipart/alternative message, making space for |
||
| 366 | * another alternative content part. |
||
| 367 | * |
||
| 368 | * Creates a content part and assigns the content stream from the message to |
||
| 369 | * that newly created part. |
||
| 370 | */ |
||
| 371 | 1 | private function setMessageAsAlternative() |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Creates a new mime part as a multipart/alternative, assigning it to |
||
| 389 | * $this->contentPart. Adds the current contentPart below the newly created |
||
| 390 | * alternative part. |
||
| 391 | */ |
||
| 392 | 2 | private function createAlternativeContentPart() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Copies Content-Type and Content-Transfer-Encoding headers from the $from |
||
| 405 | * header into the $to header. If the Content-Type header isn't defined in |
||
| 406 | * $from, defaults to text/plain and quoted-printable. |
||
| 407 | * |
||
| 408 | * @param \ZBateson\MailMimeParser\MimePart $from |
||
| 409 | * @param \ZBateson\MailMimeParser\MimePart $to |
||
| 410 | */ |
||
| 411 | 6 | private function copyTypeHeadersFromPartToPart(MimePart $from, MimePart $to) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Creates a new content part from the passed part, allowing the part to be |
||
| 428 | * used for something else (e.g. changing a non-mime message to a multipart |
||
| 429 | * mime message). |
||
| 430 | */ |
||
| 431 | 4 | private function createNewContentPartFromPart(MimePart $part) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Creates a new part out of the current contentPart and sets the message's |
||
| 442 | * type to be multipart/mixed. |
||
| 443 | */ |
||
| 444 | 4 | private function setMessageAsMixed() |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Updates parents of the contentPart and any children, and sets |
||
| 455 | * $this->contentPart to the passed $messagePart if the $this->contentPart |
||
| 456 | * is set to $this |
||
| 457 | * |
||
| 458 | * @param \ZBateson\MailMimeParser\MimePart $messagePart |
||
| 459 | */ |
||
| 460 | 3 | private function updateContentPartForSignedMessage(MimePart $messagePart) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * This function makes space by moving the main message part down one level. |
||
| 478 | * |
||
| 479 | * The content-type and content-transfer-encoding headers are copied from |
||
| 480 | * this message to the newly created part, the resource handle is moved and |
||
| 481 | * detached, any attachments and content parts with parents set to this |
||
| 482 | * message get their parents set to the newly created part. |
||
| 483 | */ |
||
| 484 | 3 | private function makeSpaceForMultipartSignedMessage() |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Creates and returns a new MimePart for the signature part of a |
||
| 503 | * multipart/signed message and assigns it to $this->signedSignaturePart. |
||
| 504 | * |
||
| 505 | * @param string $protocol |
||
| 506 | * @param string $body |
||
| 507 | */ |
||
| 508 | 5 | public function createSignaturePart($body) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Creates a multipart/mixed MimePart assigns it to $this->signedMixedPart |
||
| 523 | * if the message contains attachments. |
||
| 524 | * |
||
| 525 | * @param array $parts |
||
| 526 | */ |
||
| 527 | 9 | private function createMultipartMixedForSignedMessage() |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Loops over parts of this message and sets the content-transfer-encoding |
||
| 547 | * header to quoted-printable for text/* mime parts, and to base64 |
||
| 548 | * otherwise for parts that are '8bit' encoded. |
||
| 549 | * |
||
| 550 | * Used for multipart/signed messages which doesn't support 8bit transfer |
||
| 551 | * encodings. |
||
| 552 | */ |
||
| 553 | 5 | private function overwrite8bitContentEncoding() |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Ensures a non-text part comes first in a signed multipart/alternative |
||
| 568 | * message as some clients seem to prefer the first content part if the |
||
| 569 | * client doesn't understand multipart/signed. |
||
| 570 | */ |
||
| 571 | 5 | private function ensureHtmlPartFirstForSignedMessage() |
|
| 585 | |||
| 586 | /** |
||
| 587 | * Turns the message into a multipart/signed message, moving the actual |
||
| 588 | * message into a child part, sets the content-type of the main message to |
||
| 589 | * multipart/signed and adds a signature part as well. |
||
| 590 | * |
||
| 591 | * @param string $micalg The Message Integrity Check algorithm being used |
||
| 592 | * @param string $protocol The mime-type of the signature body |
||
| 593 | * @param string $body The signature signed according to the value of |
||
| 594 | * $protocol |
||
| 595 | */ |
||
| 596 | 5 | public function setAsMultipartSigned($micalg, $protocol) |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Returns the signed part or null if not set. |
||
| 615 | * |
||
| 616 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 617 | */ |
||
| 618 | 9 | public function getSignaturePart() |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Enforces the message to be a mime message for a non-mime (e.g. uuencoded |
||
| 625 | * or unspecified) message. If the message has uuencoded attachments, sets |
||
| 626 | * up the message as a multipart/mixed message and creates a content part. |
||
| 627 | */ |
||
| 628 | 6 | private function enforceMime() |
|
| 639 | |||
| 640 | /** |
||
| 641 | * Creates a new content part for the passed mimeType and charset, making |
||
| 642 | * space by creating a multipart/alternative if needed |
||
| 643 | * |
||
| 644 | * @param string $mimeType |
||
| 645 | * @param string $charset |
||
| 646 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 647 | */ |
||
| 648 | 3 | private function createContentPartForMimeType($mimeType, $charset) |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Either creates a mime part or sets the existing mime part with the passed |
||
| 676 | * mimeType to $strongOrHandle. |
||
| 677 | * |
||
| 678 | * @param string $mimeType |
||
| 679 | * @param string|resource $stringOrHandle |
||
| 680 | * @param string $charset |
||
| 681 | */ |
||
| 682 | 3 | protected function setContentPartForMimeType($mimeType, $stringOrHandle, $charset) |
|
| 698 | |||
| 699 | /** |
||
| 700 | * Sets the text/plain part of the message to the passed $stringOrHandle, |
||
| 701 | * either creating a new part if one doesn't exist for text/plain, or |
||
| 702 | * assigning the value of $stringOrHandle to an existing text/plain part. |
||
| 703 | * |
||
| 704 | * The optional $charset parameter is the charset for saving to. |
||
| 705 | * $stringOrHandle is expected to be in UTF-8. |
||
| 706 | * |
||
| 707 | * @param string|resource $stringOrHandle |
||
| 708 | * @param string $charset |
||
| 709 | */ |
||
| 710 | 1 | public function setTextPart($stringOrHandle, $charset = null) |
|
| 714 | |||
| 715 | /** |
||
| 716 | * Sets the text/html part of the message to the passed $stringOrHandle, |
||
| 717 | * either creating a new part if one doesn't exist for text/html, or |
||
| 718 | * assigning the value of $stringOrHandle to an existing text/html part. |
||
| 719 | * |
||
| 720 | * The optional $charset parameter is the charset for saving to. |
||
| 721 | * $stringOrHandle is expected to be in UTF-8. |
||
| 722 | * |
||
| 723 | * @param string|resource $stringOrHandle |
||
| 724 | * @param string $charset |
||
| 725 | */ |
||
| 726 | 3 | public function setHtmlPart($stringOrHandle, $charset = null) |
|
| 730 | |||
| 731 | /** |
||
| 732 | * Removes the text part of the message if one exists. Returns true on |
||
| 733 | * success. |
||
| 734 | * |
||
| 735 | * @return bool true on success |
||
| 736 | */ |
||
| 737 | 1 | public function removeTextPart() |
|
| 741 | |||
| 742 | /** |
||
| 743 | * Removes the html part of the message if one exists. Returns true on |
||
| 744 | * success. |
||
| 745 | * |
||
| 746 | * @return bool true on success |
||
| 747 | */ |
||
| 748 | 1 | public function removeHtmlPart() |
|
| 752 | |||
| 753 | /** |
||
| 754 | * Returns the non-content part at the given 0-based index, or null if none |
||
| 755 | * is set. |
||
| 756 | * |
||
| 757 | * @param int $index |
||
| 758 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 759 | */ |
||
| 760 | 6 | public function getAttachmentPart($index) |
|
| 767 | |||
| 768 | /** |
||
| 769 | * Returns all attachment parts. |
||
| 770 | * |
||
| 771 | * @return \ZBateson\MailMimeParser\MimePart[] |
||
| 772 | */ |
||
| 773 | 40 | public function getAllAttachmentParts() |
|
| 777 | |||
| 778 | /** |
||
| 779 | * Returns the number of attachments available. |
||
| 780 | * |
||
| 781 | * @return int |
||
| 782 | */ |
||
| 783 | 40 | public function getAttachmentCount() |
|
| 787 | |||
| 788 | /** |
||
| 789 | * Removes the attachment with the given index |
||
| 790 | * |
||
| 791 | * @param int $index |
||
| 792 | */ |
||
| 793 | 2 | public function removeAttachmentPart($index) |
|
| 799 | |||
| 800 | /** |
||
| 801 | * Creates and returns a MimePart for use with a new attachment part being |
||
| 802 | * created. |
||
| 803 | * |
||
| 804 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 805 | */ |
||
| 806 | 2 | protected function createPartForAttachment() |
|
| 820 | |||
| 821 | /** |
||
| 822 | * Adds an attachment part for the passed raw data string or handle and |
||
| 823 | * given parameters. |
||
| 824 | * |
||
| 825 | * @param string|handle $stringOrHandle |
||
| 826 | * @param strubg $mimeType |
||
| 827 | * @param string $filename |
||
| 828 | * @param string $disposition |
||
| 829 | */ |
||
| 830 | 1 | public function addAttachmentPart($stringOrHandle, $mimeType, $filename = null, $disposition = 'attachment') |
|
| 844 | |||
| 845 | /** |
||
| 846 | * Adds an attachment part using the passed file. |
||
| 847 | * |
||
| 848 | * Essentially creates a file stream and uses it. |
||
| 849 | * |
||
| 850 | * @param string $file |
||
| 851 | * @param string $mimeType |
||
| 852 | * @param string $filename |
||
| 853 | * @param string $disposition |
||
| 854 | */ |
||
| 855 | 2 | public function addAttachmentPartFromFile($file, $mimeType, $filename = null, $disposition = 'attachment') |
|
| 870 | |||
| 871 | /** |
||
| 872 | * Returns a resource handle where the text content can be read or null if |
||
| 873 | * unavailable. |
||
| 874 | * |
||
| 875 | * @return resource |
||
| 876 | */ |
||
| 877 | 55 | public function getTextStream() |
|
| 885 | |||
| 886 | /** |
||
| 887 | * Returns the text content as a string. |
||
| 888 | * |
||
| 889 | * Reads the entire stream content into a string and returns it. Returns |
||
| 890 | * null if the message doesn't have a text part. |
||
| 891 | * |
||
| 892 | * @return string |
||
| 893 | */ |
||
| 894 | public function getTextContent() |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Returns a resource handle where the HTML content can be read or null if |
||
| 905 | * unavailable. |
||
| 906 | * |
||
| 907 | * @return resource |
||
| 908 | */ |
||
| 909 | 26 | public function getHtmlStream() |
|
| 917 | |||
| 918 | /** |
||
| 919 | * Returns the HTML content as a string. |
||
| 920 | * |
||
| 921 | * Reads the entire stream content into a string and returns it. Returns |
||
| 922 | * null if the message doesn't have an HTML part. |
||
| 923 | * |
||
| 924 | * @return string |
||
| 925 | */ |
||
| 926 | public function getHtmlContent() |
||
| 934 | |||
| 935 | /** |
||
| 936 | * Returns true if either a Content-Type or Mime-Version header are defined |
||
| 937 | * in this Message. |
||
| 938 | * |
||
| 939 | * @return bool |
||
| 940 | */ |
||
| 941 | 75 | public function isMime() |
|
| 947 | |||
| 948 | /** |
||
| 949 | * Writes out a mime boundary to the passed $handle |
||
| 950 | * |
||
| 951 | * @param resource $handle |
||
| 952 | * @param string $boundary |
||
| 953 | * @param bool $isEnd |
||
| 954 | */ |
||
| 955 | 48 | private function writeBoundary($handle, $boundary, $isEnd = false) |
|
| 964 | |||
| 965 | /** |
||
| 966 | * Writes out any necessary boundaries for the given $part if required based |
||
| 967 | * on its $parent and $boundaryParent. |
||
| 968 | * |
||
| 969 | * Also writes out end boundaries for the previous part if applicable. |
||
| 970 | * |
||
| 971 | * @param resource $handle |
||
| 972 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 973 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
| 974 | * @param \ZBateson\MailMimeParser\MimePart $boundaryParent |
||
| 975 | * @param string $boundary |
||
| 976 | */ |
||
| 977 | 48 | private function writePartBoundaries($handle, MimePart $part, MimePart $parent, MimePart &$boundaryParent, $boundary) |
|
| 990 | |||
| 991 | /** |
||
| 992 | * Writes out the passed mime part, writing out any necessary mime |
||
| 993 | * boundaries. |
||
| 994 | * |
||
| 995 | * @param resource $handle |
||
| 996 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 997 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
| 998 | * @param \ZBateson\MailMimeParser\MimePart $boundaryParent |
||
| 999 | */ |
||
| 1000 | 71 | private function writePartTo($handle, MimePart $part, MimePart $parent, MimePart &$boundaryParent) |
|
| 1014 | |||
| 1015 | /** |
||
| 1016 | * Either returns $this for a non-text, non-html part, or returns |
||
| 1017 | * $this->contentPart. |
||
| 1018 | * |
||
| 1019 | * Note that if Content-Disposition is set on the passed part, $this is |
||
| 1020 | * always returned. |
||
| 1021 | * |
||
| 1022 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 1023 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 1024 | */ |
||
| 1025 | 71 | private function getWriteParentForPart(MimePart $part) |
|
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Loops over parts of the message and writes them as an email to the |
||
| 1039 | * provided $handle. |
||
| 1040 | * |
||
| 1041 | * The function rewrites mime parts in a multipart-mime message to be either |
||
| 1042 | * alternatives of text/plain and text/html, or attachments because |
||
| 1043 | * MailMimeParser doesn't currently maintain the structure of the original |
||
| 1044 | * message. This means other alternative parts would be dropped to |
||
| 1045 | * attachments, and multipart/related parts are completely ignored. |
||
| 1046 | * |
||
| 1047 | * @param resource $handle the handle to write out to |
||
| 1048 | * @param Iterator $partsIter an Iterator for parts to save |
||
| 1049 | * @param \ZBateson\MailMimeParser\MimePart $curParent the current parent |
||
| 1050 | */ |
||
| 1051 | 71 | protected function writePartsTo($handle, Iterator $partsIter, MimePart $curParent) |
|
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Saves the message as a MIME message to the passed resource handle. |
||
| 1067 | * |
||
| 1068 | * The saved message is not guaranteed to be the same as the parsed message. |
||
| 1069 | * Namely, for mime messages anything that is not text/html or text/plain |
||
| 1070 | * will be moved into parts under the main 'message' as attachments, other |
||
| 1071 | * alternative parts are dropped, and multipart/related parts are ignored |
||
| 1072 | * (their contents are either moved under a multipart/alternative part or as |
||
| 1073 | * attachments below the main multipart/mixed message). |
||
| 1074 | * |
||
| 1075 | * @param resource $handle |
||
| 1076 | */ |
||
| 1077 | 71 | public function save($handle) |
|
| 1104 | |||
| 1105 | /** |
||
| 1106 | * Writes out the content of the message into a string and returns it. |
||
| 1107 | * |
||
| 1108 | * @return string |
||
| 1109 | */ |
||
| 1110 | 5 | private function getSignableBodyFromParts(array $parts) |
|
| 1128 | |||
| 1129 | /** |
||
| 1130 | * Returns the content part of a signed message for a signature to be |
||
| 1131 | * calculated on the message. |
||
| 1132 | * |
||
| 1133 | * @return string |
||
| 1134 | */ |
||
| 1135 | 5 | public function getSignableBody() |
|
| 1154 | |||
| 1155 | /** |
||
| 1156 | * Shortcut to call Message::save with a php://temp stream and return the |
||
| 1157 | * written email message as a string. |
||
| 1158 | * |
||
| 1159 | * @return string |
||
| 1160 | */ |
||
| 1161 | public function __toString() |
||
| 1170 | } |
||
| 1171 |
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: