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 | 81 | 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 | 76 | 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 | 77 | 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 | 79 | 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 | 70 | 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 | 60 | 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 | 13 | 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 and Content-Transfer-Encoding headers from the $from |
||
409 | * header into the $to header. If the Content-Type header isn't defined in |
||
410 | * $from, defaults to text/plain and quoted-printable. |
||
411 | * |
||
412 | * @param \ZBateson\MailMimeParser\MimePart $from |
||
413 | * @param \ZBateson\MailMimeParser\MimePart $to |
||
414 | */ |
||
415 | 6 | private function copyTypeHeadersFromPartToPart(MimePart $from, MimePart $to) |
|
429 | |||
430 | /** |
||
431 | * Creates a new content part from the passed part, allowing the part to be |
||
432 | * used for something else (e.g. changing a non-mime message to a multipart |
||
433 | * mime message). |
||
434 | */ |
||
435 | 4 | private function createNewContentPartFromPart(MimePart $part) |
|
443 | |||
444 | /** |
||
445 | * Creates a new part out of the current contentPart and sets the message's |
||
446 | * type to be multipart/mixed. |
||
447 | */ |
||
448 | 4 | private function setMessageAsMixed() |
|
456 | |||
457 | /** |
||
458 | * Updates parents of the contentPart and any children, and sets |
||
459 | * $this->contentPart to the passed $messagePart if the $this->contentPart |
||
460 | * is set to $this |
||
461 | * |
||
462 | * @param \ZBateson\MailMimeParser\MimePart $messagePart |
||
463 | */ |
||
464 | 3 | private function updateContentPartForSignedMessage(MimePart $messagePart) |
|
479 | |||
480 | /** |
||
481 | * This function makes space by moving the main message part down one level. |
||
482 | * |
||
483 | * The content-type and content-transfer-encoding headers are copied from |
||
484 | * this message to the newly created part, the resource handle is moved and |
||
485 | * detached, any attachments and content parts with parents set to this |
||
486 | * message get their parents set to the newly created part. |
||
487 | */ |
||
488 | 3 | private function makeSpaceForMultipartSignedMessage() |
|
504 | |||
505 | /** |
||
506 | * Creates and returns a new MimePart for the signature part of a |
||
507 | * multipart/signed message and assigns it to $this->signedSignaturePart. |
||
508 | * |
||
509 | * @param string $protocol |
||
510 | * @param string $body |
||
511 | */ |
||
512 | 5 | public function createSignaturePart($body) |
|
524 | |||
525 | /** |
||
526 | * Creates a multipart/mixed MimePart assigns it to $this->signedMixedPart |
||
527 | * if the message contains attachments. |
||
528 | * |
||
529 | * @param array $parts |
||
530 | */ |
||
531 | 9 | private function createMultipartMixedForSignedMessage() |
|
548 | |||
549 | /** |
||
550 | * Loops over parts of this message and sets the content-transfer-encoding |
||
551 | * header to quoted-printable for text/* mime parts, and to base64 |
||
552 | * otherwise for parts that are '8bit' encoded. |
||
553 | * |
||
554 | * Used for multipart/signed messages which doesn't support 8bit transfer |
||
555 | * encodings. |
||
556 | */ |
||
557 | 5 | private function overwrite8bitContentEncoding() |
|
569 | |||
570 | /** |
||
571 | * Ensures a non-text part comes first in a signed multipart/alternative |
||
572 | * message as some clients seem to prefer the first content part if the |
||
573 | * client doesn't understand multipart/signed. |
||
574 | */ |
||
575 | 5 | private function ensureHtmlPartFirstForSignedMessage() |
|
589 | |||
590 | /** |
||
591 | * Turns the message into a multipart/signed message, moving the actual |
||
592 | * message into a child part, sets the content-type of the main message to |
||
593 | * multipart/signed and adds a signature part as well. |
||
594 | * |
||
595 | * @param string $micalg The Message Integrity Check algorithm being used |
||
596 | * @param string $protocol The mime-type of the signature body |
||
597 | * @param string $body The signature signed according to the value of |
||
598 | * $protocol |
||
599 | */ |
||
600 | 5 | public function setAsMultipartSigned($micalg, $protocol) |
|
617 | |||
618 | /** |
||
619 | * Returns the signed part or null if not set. |
||
620 | * |
||
621 | * @return \ZBateson\MailMimeParser\MimePart |
||
622 | */ |
||
623 | 9 | public function getSignaturePart() |
|
627 | |||
628 | /** |
||
629 | * Enforces the message to be a mime message for a non-mime (e.g. uuencoded |
||
630 | * or unspecified) message. If the message has uuencoded attachments, sets |
||
631 | * up the message as a multipart/mixed message and creates a content part. |
||
632 | */ |
||
633 | 7 | private function enforceMime() |
|
644 | |||
645 | /** |
||
646 | * Creates a new content part for the passed mimeType and charset, making |
||
647 | * space by creating a multipart/alternative if needed |
||
648 | * |
||
649 | * @param string $mimeType |
||
650 | * @param string $charset |
||
651 | * @return \ZBateson\MailMimeParser\MimePart |
||
652 | */ |
||
653 | 4 | private function createContentPartForMimeType($mimeType, $charset) |
|
678 | |||
679 | /** |
||
680 | * Either creates a mime part or sets the existing mime part with the passed |
||
681 | * mimeType to $strongOrHandle. |
||
682 | * |
||
683 | * @param string $mimeType |
||
684 | * @param string|resource $stringOrHandle |
||
685 | * @param string $charset |
||
686 | */ |
||
687 | 4 | protected function setContentPartForMimeType($mimeType, $stringOrHandle, $charset) |
|
703 | |||
704 | /** |
||
705 | * Sets the text/plain part of the message to the passed $stringOrHandle, |
||
706 | * either creating a new part if one doesn't exist for text/plain, or |
||
707 | * assigning the value of $stringOrHandle to an existing text/plain part. |
||
708 | * |
||
709 | * The optional $charset parameter is the charset for saving to. |
||
710 | * $stringOrHandle is expected to be in UTF-8. |
||
711 | * |
||
712 | * @param string|resource $stringOrHandle |
||
713 | * @param string $charset |
||
714 | */ |
||
715 | 1 | public function setTextPart($stringOrHandle, $charset = null) |
|
719 | |||
720 | /** |
||
721 | * Sets the text/html part of the message to the passed $stringOrHandle, |
||
722 | * either creating a new part if one doesn't exist for text/html, or |
||
723 | * assigning the value of $stringOrHandle to an existing text/html 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 | 4 | public function setHtmlPart($stringOrHandle, $charset = null) |
|
735 | |||
736 | /** |
||
737 | * Removes the text part of the message if one exists. Returns true on |
||
738 | * success. |
||
739 | * |
||
740 | * @return bool true on success |
||
741 | */ |
||
742 | 2 | public function removeTextPart() |
|
746 | |||
747 | /** |
||
748 | * Removes the html part of the message if one exists. Returns true on |
||
749 | * success. |
||
750 | * |
||
751 | * @return bool true on success |
||
752 | */ |
||
753 | 1 | public function removeHtmlPart() |
|
757 | |||
758 | /** |
||
759 | * Returns the non-content part at the given 0-based index, or null if none |
||
760 | * is set. |
||
761 | * |
||
762 | * @param int $index |
||
763 | * @return \ZBateson\MailMimeParser\MimePart |
||
764 | */ |
||
765 | 6 | public function getAttachmentPart($index) |
|
772 | |||
773 | /** |
||
774 | * Returns all attachment parts. |
||
775 | * |
||
776 | * @return \ZBateson\MailMimeParser\MimePart[] |
||
777 | */ |
||
778 | 40 | public function getAllAttachmentParts() |
|
782 | |||
783 | /** |
||
784 | * Returns the number of attachments available. |
||
785 | * |
||
786 | * @return int |
||
787 | */ |
||
788 | 40 | public function getAttachmentCount() |
|
792 | |||
793 | /** |
||
794 | * Removes the attachment with the given index |
||
795 | * |
||
796 | * @param int $index |
||
797 | */ |
||
798 | 2 | public function removeAttachmentPart($index) |
|
804 | |||
805 | /** |
||
806 | * Creates and returns a MimePart for use with a new attachment part being |
||
807 | * created. |
||
808 | * |
||
809 | * @return \ZBateson\MailMimeParser\MimePart |
||
810 | */ |
||
811 | 2 | protected function createPartForAttachment() |
|
825 | |||
826 | /** |
||
827 | * Adds an attachment part for the passed raw data string or handle and |
||
828 | * given parameters. |
||
829 | * |
||
830 | * @param string|handle $stringOrHandle |
||
831 | * @param strubg $mimeType |
||
832 | * @param string $filename |
||
833 | * @param string $disposition |
||
834 | */ |
||
835 | 1 | public function addAttachmentPart($stringOrHandle, $mimeType, $filename = null, $disposition = 'attachment') |
|
849 | |||
850 | /** |
||
851 | * Adds an attachment part using the passed file. |
||
852 | * |
||
853 | * Essentially creates a file stream and uses it. |
||
854 | * |
||
855 | * @param string $file |
||
856 | * @param string $mimeType |
||
857 | * @param string $filename |
||
858 | * @param string $disposition |
||
859 | */ |
||
860 | 2 | public function addAttachmentPartFromFile($file, $mimeType, $filename = null, $disposition = 'attachment') |
|
875 | |||
876 | /** |
||
877 | * Returns a resource handle where the text content can be read or null if |
||
878 | * unavailable. |
||
879 | * |
||
880 | * @return resource |
||
881 | */ |
||
882 | 56 | public function getTextStream() |
|
890 | |||
891 | /** |
||
892 | * Returns the text content as a string. |
||
893 | * |
||
894 | * Reads the entire stream content into a string and returns it. Returns |
||
895 | * null if the message doesn't have a text part. |
||
896 | * |
||
897 | * @return string |
||
898 | */ |
||
899 | 1 | public function getTextContent() |
|
907 | |||
908 | /** |
||
909 | * Returns a resource handle where the HTML content can be read or null if |
||
910 | * unavailable. |
||
911 | * |
||
912 | * @return resource |
||
913 | */ |
||
914 | 27 | public function getHtmlStream() |
|
922 | |||
923 | /** |
||
924 | * Returns the HTML content as a string. |
||
925 | * |
||
926 | * Reads the entire stream content into a string and returns it. Returns |
||
927 | * null if the message doesn't have an HTML part. |
||
928 | * |
||
929 | * @return string |
||
930 | */ |
||
931 | public function getHtmlContent() |
||
939 | |||
940 | /** |
||
941 | * Returns true if either a Content-Type or Mime-Version header are defined |
||
942 | * in this Message. |
||
943 | * |
||
944 | * @return bool |
||
945 | */ |
||
946 | 76 | public function isMime() |
|
952 | |||
953 | /** |
||
954 | * Writes out a mime boundary to the passed $handle |
||
955 | * |
||
956 | * @param resource $handle |
||
957 | * @param string $boundary |
||
958 | * @param bool $isEnd |
||
959 | */ |
||
960 | 48 | private function writeBoundary($handle, $boundary, $isEnd = false) |
|
974 | |||
975 | /** |
||
976 | * Writes out any necessary boundaries for the given $part if required based |
||
977 | * on its $parent and $boundaryParent. |
||
978 | * |
||
979 | * Also writes out end boundaries for the previous part if applicable. |
||
980 | * |
||
981 | * @param resource $handle |
||
982 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
983 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
984 | * @param \ZBateson\MailMimeParser\MimePart $boundaryParent |
||
985 | * @param string $boundary |
||
986 | */ |
||
987 | 48 | private function writePartBoundaries($handle, MimePart $part, MimePart $parent, MimePart &$boundaryParent, $boundary) |
|
1000 | |||
1001 | /** |
||
1002 | * Writes out the passed mime part, writing out any necessary mime |
||
1003 | * boundaries. |
||
1004 | * |
||
1005 | * @param resource $handle |
||
1006 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
1007 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
1008 | * @param \ZBateson\MailMimeParser\MimePart $boundaryParent |
||
1009 | */ |
||
1010 | 72 | private function writePartTo($handle, MimePart $part, MimePart $parent, MimePart &$boundaryParent) |
|
1028 | |||
1029 | /** |
||
1030 | * Either returns $this for a non-text, non-html part, or returns |
||
1031 | * $this->contentPart. |
||
1032 | * |
||
1033 | * Note that if Content-Disposition is set on the passed part, $this is |
||
1034 | * always returned. |
||
1035 | * |
||
1036 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
1037 | * @return \ZBateson\MailMimeParser\MimePart |
||
1038 | */ |
||
1039 | 72 | private function getWriteParentForPart(MimePart $part) |
|
1050 | |||
1051 | /** |
||
1052 | * Loops over parts of the message and writes them as an email to the |
||
1053 | * provided $handle. |
||
1054 | * |
||
1055 | * The function rewrites mime parts in a multipart-mime message to be either |
||
1056 | * alternatives of text/plain and text/html, or attachments because |
||
1057 | * MailMimeParser doesn't currently maintain the structure of the original |
||
1058 | * message. This means other alternative parts would be dropped to |
||
1059 | * attachments, and multipart/related parts are completely ignored. |
||
1060 | * |
||
1061 | * @param resource $handle the handle to write out to |
||
1062 | * @param Iterator $partsIter an Iterator for parts to save |
||
1063 | * @param \ZBateson\MailMimeParser\MimePart $curParent the current parent |
||
1064 | */ |
||
1065 | 72 | protected function writePartsTo($handle, Iterator $partsIter, MimePart $curParent) |
|
1079 | |||
1080 | /** |
||
1081 | * Saves the message as a MIME message to the passed resource handle. |
||
1082 | * |
||
1083 | * The saved message is not guaranteed to be the same as the parsed message. |
||
1084 | * Namely, for mime messages anything that is not text/html or text/plain |
||
1085 | * will be moved into parts under the main 'message' as attachments, other |
||
1086 | * alternative parts are dropped, and multipart/related parts are ignored |
||
1087 | * (their contents are either moved under a multipart/alternative part or as |
||
1088 | * attachments below the main multipart/mixed message). |
||
1089 | * |
||
1090 | * @param resource $handle |
||
1091 | */ |
||
1092 | 72 | public function save($handle) |
|
1119 | |||
1120 | /** |
||
1121 | * Writes out the content of the message into a string and returns it. |
||
1122 | * |
||
1123 | * @return string |
||
1124 | */ |
||
1125 | 5 | private function getSignableBodyFromParts(array $parts) |
|
1143 | |||
1144 | /** |
||
1145 | * Returns the content part of a signed message for a signature to be |
||
1146 | * calculated on the message. |
||
1147 | * |
||
1148 | * @return string |
||
1149 | */ |
||
1150 | 5 | public function getSignableBody() |
|
1169 | |||
1170 | /** |
||
1171 | * Shortcut to call Message::save with a php://temp stream and return the |
||
1172 | * written email message as a string. |
||
1173 | * |
||
1174 | * @return string |
||
1175 | */ |
||
1176 | public function __toString() |
||
1185 | } |
||
1186 |
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: