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 | 80 | 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 | 75 | 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 | 76 | 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 | 78 | 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 | 69 | 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 | 1 | private function overrideAlternativeMessageContentFromContentPart(MimePart $part) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Removes the passed MimePart as a content part. If there's a remaining |
||
| 236 | * part, either sets the content on this message if the message itself is a |
||
| 237 | * multipart/alternative message, or overrides the contentPart with the |
||
| 238 | * remaining part. |
||
| 239 | * |
||
| 240 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 241 | */ |
||
| 242 | 2 | private function removePartFromAlternativeContentPart(MimePart $part) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Loops over children of the content part looking for a part with the |
||
| 257 | * passed mime type, then proceeds to remove it by calling |
||
| 258 | * removePartFromAlternativeContentPart. |
||
| 259 | * |
||
| 260 | * @param string $contentType |
||
| 261 | * @return boolean true on success |
||
| 262 | */ |
||
| 263 | 2 | private function removeContentPartFromAlternative($contentType) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Removes the content part of the message with the passed mime type. If |
||
| 278 | * there is a remaining content part and it is an alternative part of the |
||
| 279 | * main message, the content part is moved to the message part. |
||
| 280 | * |
||
| 281 | * If the content part is part of an alternative part beneath the message, |
||
| 282 | * the alternative part is replaced by the remaining content part. |
||
| 283 | * |
||
| 284 | * @param string $contentType |
||
| 285 | * @return boolean true on success |
||
| 286 | */ |
||
| 287 | 2 | protected function removeContentPart($contentType) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Returns the text part (or null if none is set.) |
||
| 306 | * |
||
| 307 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 308 | */ |
||
| 309 | 59 | public function getTextPart() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Returns the HTML part (or null if none is set.) |
||
| 316 | * |
||
| 317 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 318 | */ |
||
| 319 | 32 | public function getHtmlPart() |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Returns an open resource handle for the passed string or resource handle. |
||
| 326 | * |
||
| 327 | * For a string, creates a php://temp stream and returns it. |
||
| 328 | * |
||
| 329 | * @param resource|string $stringOrHandle |
||
| 330 | * @return resource |
||
| 331 | */ |
||
| 332 | 4 | private function getHandleForStringOrHandle($stringOrHandle) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Creates and returns a unique boundary. |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | 12 | private function getUniqueBoundary() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Creates a unique mime boundary and assigns it to the passed part's |
||
| 356 | * Content-Type header with the passed mime type. |
||
| 357 | * |
||
| 358 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 359 | * @param string $mimeType |
||
| 360 | */ |
||
| 361 | 6 | private function setMimeHeaderBoundaryOnPart(MimePart $part, $mimeType) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Sets this message to be a multipart/alternative message, making space for |
||
| 372 | * another alternative content part. |
||
| 373 | * |
||
| 374 | * Creates a content part and assigns the content stream from the message to |
||
| 375 | * that newly created part. |
||
| 376 | */ |
||
| 377 | 1 | private function setMessageAsAlternative() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Creates a new mime part as a multipart/alternative, assigning it to |
||
| 395 | * $this->contentPart. Adds the current contentPart below the newly created |
||
| 396 | * alternative part. |
||
| 397 | */ |
||
| 398 | 2 | private function createAlternativeContentPart() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Copies Content-Type and Content-Transfer-Encoding headers from the $from |
||
| 411 | * header into the $to header. If the Content-Type header isn't defined in |
||
| 412 | * $from, defaults to text/plain and quoted-printable. |
||
| 413 | * |
||
| 414 | * @param \ZBateson\MailMimeParser\MimePart $from |
||
| 415 | * @param \ZBateson\MailMimeParser\MimePart $to |
||
| 416 | */ |
||
| 417 | 6 | private function copyTypeHeadersFromPartToPart(MimePart $from, MimePart $to) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Creates a new content part from the passed part, allowing the part to be |
||
| 434 | * used for something else (e.g. changing a non-mime message to a multipart |
||
| 435 | * mime message). |
||
| 436 | */ |
||
| 437 | 4 | private function createNewContentPartFromPart(MimePart $part) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Creates a new part out of the current contentPart and sets the message's |
||
| 448 | * type to be multipart/mixed. |
||
| 449 | */ |
||
| 450 | 4 | private function setMessageAsMixed() |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Updates parents of the contentPart and any children, and sets |
||
| 461 | * $this->contentPart to the passed $messagePart if the $this->contentPart |
||
| 462 | * is set to $this |
||
| 463 | * |
||
| 464 | * @param \ZBateson\MailMimeParser\MimePart $messagePart |
||
| 465 | */ |
||
| 466 | 3 | private function updateContentPartForSignedMessage(MimePart $messagePart) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * This function makes space by moving the main message part down one level. |
||
| 484 | * |
||
| 485 | * The content-type and content-transfer-encoding headers are copied from |
||
| 486 | * this message to the newly created part, the resource handle is moved and |
||
| 487 | * detached, any attachments and content parts with parents set to this |
||
| 488 | * message get their parents set to the newly created part. |
||
| 489 | */ |
||
| 490 | 3 | private function makeSpaceForMultipartSignedMessage() |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Creates and returns a new MimePart for the signature part of a |
||
| 509 | * multipart/signed message and assigns it to $this->signedSignaturePart. |
||
| 510 | * |
||
| 511 | * @param string $protocol |
||
| 512 | * @param string $body |
||
| 513 | */ |
||
| 514 | 5 | public function createSignaturePart($body) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Creates a multipart/mixed MimePart assigns it to $this->signedMixedPart |
||
| 529 | * if the message contains attachments. |
||
| 530 | * |
||
| 531 | * @param array $parts |
||
| 532 | */ |
||
| 533 | 9 | private function createMultipartMixedForSignedMessage() |
|
| 550 | |||
| 551 | /** |
||
| 552 | * Loops over parts of this message and sets the content-transfer-encoding |
||
| 553 | * header to quoted-printable for text/* mime parts, and to base64 |
||
| 554 | * otherwise for parts that are '8bit' encoded. |
||
| 555 | * |
||
| 556 | * Used for multipart/signed messages which doesn't support 8bit transfer |
||
| 557 | * encodings. |
||
| 558 | */ |
||
| 559 | 5 | private function overwrite8bitContentEncoding() |
|
| 571 | |||
| 572 | /** |
||
| 573 | * Ensures a non-text part comes first in a signed multipart/alternative |
||
| 574 | * message as some clients seem to prefer the first content part if the |
||
| 575 | * client doesn't understand multipart/signed. |
||
| 576 | */ |
||
| 577 | 5 | private function ensureHtmlPartFirstForSignedMessage() |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Turns the message into a multipart/signed message, moving the actual |
||
| 594 | * message into a child part, sets the content-type of the main message to |
||
| 595 | * multipart/signed and adds a signature part as well. |
||
| 596 | * |
||
| 597 | * @param string $micalg The Message Integrity Check algorithm being used |
||
| 598 | * @param string $protocol The mime-type of the signature body |
||
| 599 | * @param string $body The signature signed according to the value of |
||
| 600 | * $protocol |
||
| 601 | */ |
||
| 602 | 5 | public function setAsMultipartSigned($micalg, $protocol) |
|
| 619 | |||
| 620 | /** |
||
| 621 | * Returns the signed part or null if not set. |
||
| 622 | * |
||
| 623 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 624 | */ |
||
| 625 | 9 | public function getSignaturePart() |
|
| 629 | |||
| 630 | /** |
||
| 631 | * Enforces the message to be a mime message for a non-mime (e.g. uuencoded |
||
| 632 | * or unspecified) message. If the message has uuencoded attachments, sets |
||
| 633 | * up the message as a multipart/mixed message and creates a content part. |
||
| 634 | */ |
||
| 635 | 6 | private function enforceMime() |
|
| 646 | |||
| 647 | /** |
||
| 648 | * Creates a new content part for the passed mimeType and charset, making |
||
| 649 | * space by creating a multipart/alternative if needed |
||
| 650 | * |
||
| 651 | * @param string $mimeType |
||
| 652 | * @param string $charset |
||
| 653 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 654 | */ |
||
| 655 | 3 | private function createContentPartForMimeType($mimeType, $charset) |
|
| 680 | |||
| 681 | /** |
||
| 682 | * Either creates a mime part or sets the existing mime part with the passed |
||
| 683 | * mimeType to $strongOrHandle. |
||
| 684 | * |
||
| 685 | * @param string $mimeType |
||
| 686 | * @param string|resource $stringOrHandle |
||
| 687 | * @param string $charset |
||
| 688 | */ |
||
| 689 | 3 | protected function setContentPartForMimeType($mimeType, $stringOrHandle, $charset) |
|
| 705 | |||
| 706 | /** |
||
| 707 | * Sets the text/plain part of the message to the passed $stringOrHandle, |
||
| 708 | * either creating a new part if one doesn't exist for text/plain, or |
||
| 709 | * assigning the value of $stringOrHandle to an existing text/plain part. |
||
| 710 | * |
||
| 711 | * The optional $charset parameter is the charset for saving to. |
||
| 712 | * $stringOrHandle is expected to be in UTF-8. |
||
| 713 | * |
||
| 714 | * @param string|resource $stringOrHandle |
||
| 715 | * @param string $charset |
||
| 716 | */ |
||
| 717 | 1 | public function setTextPart($stringOrHandle, $charset = null) |
|
| 721 | |||
| 722 | /** |
||
| 723 | * Sets the text/html part of the message to the passed $stringOrHandle, |
||
| 724 | * either creating a new part if one doesn't exist for text/html, or |
||
| 725 | * assigning the value of $stringOrHandle to an existing text/html part. |
||
| 726 | * |
||
| 727 | * The optional $charset parameter is the charset for saving to. |
||
| 728 | * $stringOrHandle is expected to be in UTF-8. |
||
| 729 | * |
||
| 730 | * @param string|resource $stringOrHandle |
||
| 731 | * @param string $charset |
||
| 732 | */ |
||
| 733 | 3 | public function setHtmlPart($stringOrHandle, $charset = null) |
|
| 737 | |||
| 738 | /** |
||
| 739 | * Removes the text part of the message if one exists. Returns true on |
||
| 740 | * success. |
||
| 741 | * |
||
| 742 | * @return bool true on success |
||
| 743 | */ |
||
| 744 | 1 | public function removeTextPart() |
|
| 748 | |||
| 749 | /** |
||
| 750 | * Removes the html part of the message if one exists. Returns true on |
||
| 751 | * success. |
||
| 752 | * |
||
| 753 | * @return bool true on success |
||
| 754 | */ |
||
| 755 | 1 | public function removeHtmlPart() |
|
| 759 | |||
| 760 | /** |
||
| 761 | * Returns the non-content part at the given 0-based index, or null if none |
||
| 762 | * is set. |
||
| 763 | * |
||
| 764 | * @param int $index |
||
| 765 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 766 | */ |
||
| 767 | 6 | public function getAttachmentPart($index) |
|
| 774 | |||
| 775 | /** |
||
| 776 | * Returns all attachment parts. |
||
| 777 | * |
||
| 778 | * @return \ZBateson\MailMimeParser\MimePart[] |
||
| 779 | */ |
||
| 780 | 40 | public function getAllAttachmentParts() |
|
| 784 | |||
| 785 | /** |
||
| 786 | * Returns the number of attachments available. |
||
| 787 | * |
||
| 788 | * @return int |
||
| 789 | */ |
||
| 790 | 40 | public function getAttachmentCount() |
|
| 794 | |||
| 795 | /** |
||
| 796 | * Removes the attachment with the given index |
||
| 797 | * |
||
| 798 | * @param int $index |
||
| 799 | */ |
||
| 800 | 2 | public function removeAttachmentPart($index) |
|
| 806 | |||
| 807 | /** |
||
| 808 | * Creates and returns a MimePart for use with a new attachment part being |
||
| 809 | * created. |
||
| 810 | * |
||
| 811 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 812 | */ |
||
| 813 | 2 | protected function createPartForAttachment() |
|
| 827 | |||
| 828 | /** |
||
| 829 | * Adds an attachment part for the passed raw data string or handle and |
||
| 830 | * given parameters. |
||
| 831 | * |
||
| 832 | * @param string|handle $stringOrHandle |
||
| 833 | * @param strubg $mimeType |
||
| 834 | * @param string $filename |
||
| 835 | * @param string $disposition |
||
| 836 | */ |
||
| 837 | 1 | public function addAttachmentPart($stringOrHandle, $mimeType, $filename = null, $disposition = 'attachment') |
|
| 851 | |||
| 852 | /** |
||
| 853 | * Adds an attachment part using the passed file. |
||
| 854 | * |
||
| 855 | * Essentially creates a file stream and uses it. |
||
| 856 | * |
||
| 857 | * @param string $file |
||
| 858 | * @param string $mimeType |
||
| 859 | * @param string $filename |
||
| 860 | * @param string $disposition |
||
| 861 | */ |
||
| 862 | 2 | public function addAttachmentPartFromFile($file, $mimeType, $filename = null, $disposition = 'attachment') |
|
| 877 | |||
| 878 | /** |
||
| 879 | * Returns a resource handle where the text content can be read or null if |
||
| 880 | * unavailable. |
||
| 881 | * |
||
| 882 | * @return resource |
||
| 883 | */ |
||
| 884 | 55 | public function getTextStream() |
|
| 892 | |||
| 893 | /** |
||
| 894 | * Returns the text content as a string. |
||
| 895 | * |
||
| 896 | * Reads the entire stream content into a string and returns it. Returns |
||
| 897 | * null if the message doesn't have a text part. |
||
| 898 | * |
||
| 899 | * @return string |
||
| 900 | */ |
||
| 901 | public function getTextContent() |
||
| 909 | |||
| 910 | /** |
||
| 911 | * Returns a resource handle where the HTML content can be read or null if |
||
| 912 | * unavailable. |
||
| 913 | * |
||
| 914 | * @return resource |
||
| 915 | */ |
||
| 916 | 26 | public function getHtmlStream() |
|
| 924 | |||
| 925 | /** |
||
| 926 | * Returns the HTML content as a string. |
||
| 927 | * |
||
| 928 | * Reads the entire stream content into a string and returns it. Returns |
||
| 929 | * null if the message doesn't have an HTML part. |
||
| 930 | * |
||
| 931 | * @return string |
||
| 932 | */ |
||
| 933 | public function getHtmlContent() |
||
| 941 | |||
| 942 | /** |
||
| 943 | * Returns true if either a Content-Type or Mime-Version header are defined |
||
| 944 | * in this Message. |
||
| 945 | * |
||
| 946 | * @return bool |
||
| 947 | */ |
||
| 948 | 75 | public function isMime() |
|
| 954 | |||
| 955 | /** |
||
| 956 | * Writes out a mime boundary to the passed $handle |
||
| 957 | * |
||
| 958 | * @param resource $handle |
||
| 959 | * @param string $boundary |
||
| 960 | * @param bool $isEnd |
||
| 961 | */ |
||
| 962 | 48 | private function writeBoundary($handle, $boundary, $isEnd = false) |
|
| 976 | |||
| 977 | /** |
||
| 978 | * Writes out any necessary boundaries for the given $part if required based |
||
| 979 | * on its $parent and $boundaryParent. |
||
| 980 | * |
||
| 981 | * Also writes out end boundaries for the previous part if applicable. |
||
| 982 | * |
||
| 983 | * @param resource $handle |
||
| 984 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 985 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
| 986 | * @param \ZBateson\MailMimeParser\MimePart $boundaryParent |
||
| 987 | * @param string $boundary |
||
| 988 | */ |
||
| 989 | 48 | private function writePartBoundaries($handle, MimePart $part, MimePart $parent, MimePart &$boundaryParent, $boundary) |
|
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Writes out the passed mime part, writing out any necessary mime |
||
| 1005 | * boundaries. |
||
| 1006 | * |
||
| 1007 | * @param resource $handle |
||
| 1008 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 1009 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
| 1010 | * @param \ZBateson\MailMimeParser\MimePart $boundaryParent |
||
| 1011 | */ |
||
| 1012 | 71 | private function writePartTo($handle, MimePart $part, MimePart $parent, MimePart &$boundaryParent) |
|
| 1030 | |||
| 1031 | /** |
||
| 1032 | * Either returns $this for a non-text, non-html part, or returns |
||
| 1033 | * $this->contentPart. |
||
| 1034 | * |
||
| 1035 | * Note that if Content-Disposition is set on the passed part, $this is |
||
| 1036 | * always returned. |
||
| 1037 | * |
||
| 1038 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
| 1039 | * @return \ZBateson\MailMimeParser\MimePart |
||
| 1040 | */ |
||
| 1041 | 71 | private function getWriteParentForPart(MimePart $part) |
|
| 1052 | |||
| 1053 | /** |
||
| 1054 | * Loops over parts of the message and writes them as an email to the |
||
| 1055 | * provided $handle. |
||
| 1056 | * |
||
| 1057 | * The function rewrites mime parts in a multipart-mime message to be either |
||
| 1058 | * alternatives of text/plain and text/html, or attachments because |
||
| 1059 | * MailMimeParser doesn't currently maintain the structure of the original |
||
| 1060 | * message. This means other alternative parts would be dropped to |
||
| 1061 | * attachments, and multipart/related parts are completely ignored. |
||
| 1062 | * |
||
| 1063 | * @param resource $handle the handle to write out to |
||
| 1064 | * @param Iterator $partsIter an Iterator for parts to save |
||
| 1065 | * @param \ZBateson\MailMimeParser\MimePart $curParent the current parent |
||
| 1066 | */ |
||
| 1067 | 71 | protected function writePartsTo($handle, Iterator $partsIter, MimePart $curParent) |
|
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Saves the message as a MIME message to the passed resource handle. |
||
| 1084 | * |
||
| 1085 | * The saved message is not guaranteed to be the same as the parsed message. |
||
| 1086 | * Namely, for mime messages anything that is not text/html or text/plain |
||
| 1087 | * will be moved into parts under the main 'message' as attachments, other |
||
| 1088 | * alternative parts are dropped, and multipart/related parts are ignored |
||
| 1089 | * (their contents are either moved under a multipart/alternative part or as |
||
| 1090 | * attachments below the main multipart/mixed message). |
||
| 1091 | * |
||
| 1092 | * @param resource $handle |
||
| 1093 | */ |
||
| 1094 | 71 | public function save($handle) |
|
| 1121 | |||
| 1122 | /** |
||
| 1123 | * Writes out the content of the message into a string and returns it. |
||
| 1124 | * |
||
| 1125 | * @return string |
||
| 1126 | */ |
||
| 1127 | 5 | private function getSignableBodyFromParts(array $parts) |
|
| 1145 | |||
| 1146 | /** |
||
| 1147 | * Returns the content part of a signed message for a signature to be |
||
| 1148 | * calculated on the message. |
||
| 1149 | * |
||
| 1150 | * @return string |
||
| 1151 | */ |
||
| 1152 | 5 | public function getSignableBody() |
|
| 1171 | |||
| 1172 | /** |
||
| 1173 | * Shortcut to call Message::save with a php://temp stream and return the |
||
| 1174 | * written email message as a string. |
||
| 1175 | * |
||
| 1176 | * @return string |
||
| 1177 | */ |
||
| 1178 | public function __toString() |
||
| 1187 | } |
||
| 1188 |
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: