Complex classes like MessageBodyCollection 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 MessageBodyCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | final class MessageBodyCollection |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $html = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var AlternativeText |
||
| 34 | */ |
||
| 35 | private $text; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var array|PartInterface[] |
||
| 39 | */ |
||
| 40 | private $attachments = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array|PartInterface[] |
||
| 44 | */ |
||
| 45 | private $embedImages = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $html |
||
| 49 | */ |
||
| 50 | 34 | public function __construct(string $html = '') |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $html |
||
| 58 | * @return MessageBodyCollection |
||
| 59 | */ |
||
| 60 | 6 | public function withHtml(string $html): self |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $html |
||
| 70 | * @return MessageBodyCollection |
||
| 71 | */ |
||
| 72 | 16 | public function withHtmlAndNoGeneratedAlternativeText(string $html): self |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @param AlternativeText $text |
||
| 81 | * @return MessageBodyCollection |
||
| 82 | */ |
||
| 83 | 17 | public function withAlternativeText(AlternativeText $text): self |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @param PartInterface $part |
||
| 92 | * @return MessageBodyCollection |
||
| 93 | */ |
||
| 94 | 10 | public function withAttachment(PartInterface $part): self |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @param EmbeddedImage $embeddedImage |
||
| 116 | * @return MessageBodyCollection |
||
| 117 | */ |
||
| 118 | 5 | public function withEmbeddedImage(EmbeddedImage $embeddedImage): self |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param MessageInterface $message |
||
| 127 | * @return MessageBodyCollection |
||
| 128 | */ |
||
| 129 | 1 | public function withAttachedMessage(MessageInterface $message): self |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @param MessageInterface $message |
||
| 146 | * @param QuotationInterface $quotation |
||
| 147 | * @return MessageBodyCollection |
||
| 148 | */ |
||
| 149 | 7 | public function withQuotedMessage(MessageInterface $message, QuotationInterface $quotation): self |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | 17 | public function getHtml(): string |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @return AlternativeText |
||
| 164 | */ |
||
| 165 | 17 | public function getText(): AlternativeText |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @return array|PartInterface[] |
||
| 172 | */ |
||
| 173 | 4 | public function getAttachments(): iterable |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @return array|PartInterface[] |
||
| 180 | */ |
||
| 181 | 4 | public function getEmbeddedImages(): iterable |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @return MessageInterface |
||
| 188 | */ |
||
| 189 | 22 | public function createMessage(): MessageInterface |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @param MessageInterface $message |
||
| 196 | * @return MessageInterface |
||
| 197 | */ |
||
| 198 | 1 | public function attachToMessage(MessageInterface $message): MessageInterface |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param MessageInterface $originalMessage |
||
| 214 | * @return MessageInterface |
||
| 215 | */ |
||
| 216 | 5 | public function inReplyTo(MessageInterface $originalMessage): MessageInterface |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @param MessageInterface $originalMessage |
||
| 226 | * @return MessageInterface |
||
| 227 | */ |
||
| 228 | 2 | public function inReplyToAll(MessageInterface $originalMessage): MessageInterface |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @param MessageInterface $originalMessage |
||
| 238 | * @param array<int, string> $replyRecipientHeaderNames |
||
| 239 | * @return MessageInterface |
||
| 240 | */ |
||
| 241 | 7 | private function newReply(MessageInterface $originalMessage, array $replyRecipientHeaderNames): MessageInterface |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param MessageInterface $originalMessage |
||
| 258 | * @return MessageInterface |
||
| 259 | */ |
||
| 260 | 1 | public function asForwardTo(MessageInterface $originalMessage): MessageInterface |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @return PartInterface |
||
| 269 | */ |
||
| 270 | 22 | private function createMessageRoot(): PartInterface |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @return PartInterface |
||
| 286 | */ |
||
| 287 | 22 | private function createMessageHumanReadable(): PartInterface |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @return PartInterface |
||
| 303 | */ |
||
| 304 | 22 | private function createMessageText(): PartInterface |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param MessageInterface $message |
||
| 328 | * @return MessageBodyCollection |
||
| 329 | */ |
||
| 330 | 18 | public static function extract(MessageInterface $message): MessageBodyCollection |
|
| 353 | |||
| 354 | /** |
||
| 355 | * @param MultiPartInterface $parts |
||
| 356 | */ |
||
| 357 | 14 | private function extractFromMimePart(MultiPartInterface $parts): void |
|
| 392 | |||
| 393 | /** |
||
| 394 | * @param MessageInterface $message |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | 9 | private function extractSubject(MessageInterface $message): string |
|
| 405 | |||
| 406 | /** |
||
| 407 | * @param HeaderInterface $header |
||
| 408 | * @return HeaderInterface |
||
| 409 | */ |
||
| 410 | 5 | private function determineReplyHeader(HeaderInterface $header): HeaderInterface |
|
| 419 | |||
| 420 | /** |
||
| 421 | * @param MessageInterface $originalMessage |
||
| 422 | * @return MessageInterface |
||
| 423 | */ |
||
| 424 | 8 | private function createReferencedMessage(MessageInterface $originalMessage): MessageInterface |
|
| 446 | |||
| 447 | /** |
||
| 448 | * @param PartInterface $part |
||
| 449 | * @return StreamInterface |
||
| 450 | */ |
||
| 451 | 14 | private static function decodeBodyPart(PartInterface $part): StreamInterface |
|
| 472 | |||
| 473 | /** |
||
| 474 | * @param MessageInterface $message |
||
| 475 | * @return StreamInterface |
||
| 476 | */ |
||
| 477 | 4 | private static function decodeMessageBody(MessageInterface $message): StreamInterface |
|
| 498 | } |
||
| 499 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: