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 |
||
| 22 | final class MessageBodyCollection |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $html = ''; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var AlternativeText |
||
| 31 | */ |
||
| 32 | private $text; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array|PartInterface[] |
||
| 36 | */ |
||
| 37 | private $attachments = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array|PartInterface[] |
||
| 41 | */ |
||
| 42 | private $embedImages = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $html |
||
| 46 | */ |
||
| 47 | 27 | public function __construct(string $html = '') |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @param string $html |
||
| 55 | * @return MessageBodyCollection |
||
| 56 | */ |
||
| 57 | 6 | public function withHtml(string $html): self |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $html |
||
| 67 | * @return MessageBodyCollection |
||
| 68 | */ |
||
| 69 | 11 | public function withHtmlAndNoGeneratedAlternativeText(string $html): self |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param AlternativeText $text |
||
| 78 | * @return MessageBodyCollection |
||
| 79 | */ |
||
| 80 | 12 | public function withAlternativeText(AlternativeText $text): self |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @param PartInterface $part |
||
| 89 | * @return MessageBodyCollection |
||
| 90 | */ |
||
| 91 | 8 | public function withAttachment(PartInterface $part): self |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param EmbeddedImage $embeddedImage |
||
| 113 | * @return MessageBodyCollection |
||
| 114 | */ |
||
| 115 | 5 | public function withEmbeddedImage(EmbeddedImage $embeddedImage): self |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @param MessageInterface $message |
||
| 124 | * @return MessageBodyCollection |
||
| 125 | */ |
||
| 126 | 1 | public function withAttachedMessage(MessageInterface $message): self |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @param MessageInterface $message |
||
| 143 | * @param QuotationInterface $quotation |
||
| 144 | * @return MessageBodyCollection |
||
| 145 | */ |
||
| 146 | 7 | public function withQuotedMessage(MessageInterface $message, QuotationInterface $quotation): self |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | 12 | public function getHtml(): string |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @return AlternativeText |
||
| 161 | */ |
||
| 162 | 12 | public function getText(): AlternativeText |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @return array|PartInterface[] |
||
| 169 | */ |
||
| 170 | 4 | public function getAttachments(): iterable |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @return array|PartInterface[] |
||
| 177 | */ |
||
| 178 | 4 | public function getEmbeddedImages(): iterable |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @return MessageInterface |
||
| 185 | */ |
||
| 186 | 18 | public function createMessage(): MessageInterface |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @param MessageInterface $message |
||
| 193 | * @return MessageInterface |
||
| 194 | */ |
||
| 195 | 1 | public function attachToMessage(MessageInterface $message): MessageInterface |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @param MessageInterface $originalMessage |
||
| 211 | * @return MessageInterface |
||
| 212 | */ |
||
| 213 | 5 | public function inReplyTo(MessageInterface $originalMessage): MessageInterface |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @param MessageInterface $originalMessage |
||
| 223 | * @return MessageInterface |
||
| 224 | */ |
||
| 225 | 2 | public function inReplyToAll(MessageInterface $originalMessage): MessageInterface |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param MessageInterface $originalMessage |
||
| 235 | * @param array $replyRecipientHeaderNames |
||
| 236 | * @return MessageInterface |
||
| 237 | */ |
||
| 238 | 7 | private function newReply(MessageInterface $originalMessage, array $replyRecipientHeaderNames): MessageInterface |
|
| 252 | |||
| 253 | /** |
||
| 254 | * @param MessageInterface $originalMessage |
||
| 255 | * @return MessageInterface |
||
| 256 | */ |
||
| 257 | 1 | public function asForwardTo(MessageInterface $originalMessage): MessageInterface |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @return PartInterface |
||
| 266 | */ |
||
| 267 | 18 | private function createMessageRoot(): PartInterface |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @return PartInterface |
||
| 283 | */ |
||
| 284 | 18 | private function createMessageHumanReadable(): PartInterface |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @return PartInterface |
||
| 300 | */ |
||
| 301 | 18 | private function createMessageText(): PartInterface |
|
| 322 | |||
| 323 | /** |
||
| 324 | * @param MessageInterface $message |
||
| 325 | * @return MessageBodyCollection |
||
| 326 | */ |
||
| 327 | 13 | public static function extract(MessageInterface $message): MessageBodyCollection |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @param MultiPartInterface $parts |
||
| 351 | */ |
||
| 352 | 11 | private function extractFromMimePart(MultiPartInterface $parts): void |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @param MessageInterface $message |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | 9 | private function extractSubject(MessageInterface $message): string |
|
| 400 | |||
| 401 | /** |
||
| 402 | * @param HeaderInterface $header |
||
| 403 | * @return HeaderInterface |
||
| 404 | */ |
||
| 405 | 5 | private function determineReplyHeader(HeaderInterface $header): HeaderInterface |
|
| 414 | |||
| 415 | /** |
||
| 416 | * @param MessageInterface $originalMessage |
||
| 417 | * @return MessageInterface |
||
| 418 | */ |
||
| 419 | 8 | private function createReferencedMessage(MessageInterface $originalMessage): MessageInterface |
|
| 441 | } |
||
| 442 |