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 | ||
| 15 | final class MessageBodyCollection | ||
| 16 | { | ||
| 17 | /** | ||
| 18 | * @var string | ||
| 19 | */ | ||
| 20 | private $html = ''; | ||
| 21 | |||
| 22 | /** | ||
| 23 | * @var AlternativeText | ||
| 24 | */ | ||
| 25 | private $text; | ||
| 26 | |||
| 27 | /** | ||
| 28 | * @var array|PartInterface[] | ||
| 29 | */ | ||
| 30 | private $attachments = []; | ||
| 31 | |||
| 32 | /** | ||
| 33 | * @var array|PartInterface[] | ||
| 34 | */ | ||
| 35 | private $embedImages = []; | ||
| 36 | |||
| 37 | /** | ||
| 38 | * @param string $html | ||
| 39 | */ | ||
| 40 | public function __construct(string $html = '') | ||
| 45 | |||
| 46 | /** | ||
| 47 | * @param string $html | ||
| 48 | * @return MessageBodyCollection | ||
| 49 | */ | ||
| 50 | public function withHtml(string $html): self | ||
| 57 | |||
| 58 | /** | ||
| 59 | * @param string $html | ||
| 60 | * @return MessageBodyCollection | ||
| 61 | */ | ||
| 62 | public function withHtmlAndNoGeneratedAlternativeText(string $html): self | ||
| 68 | |||
| 69 | /** | ||
| 70 | * @param AlternativeText $text | ||
| 71 | * @return MessageBodyCollection | ||
| 72 | */ | ||
| 73 | public function withAlternativeText(AlternativeText $text): self | ||
| 79 | |||
| 80 | /** | ||
| 81 | * @param PartInterface $part | ||
| 82 | * @return MessageBodyCollection | ||
| 83 | */ | ||
| 84 | public function withAttachment(PartInterface $part): self | ||
| 103 | |||
| 104 | /** | ||
| 105 | * @param EmbeddedImage $embeddedImage | ||
| 106 | * @return MessageBodyCollection | ||
| 107 | */ | ||
| 108 | public function withEmbeddedImage(EmbeddedImage $embeddedImage): self | ||
| 114 | |||
| 115 | /** | ||
| 116 | * @return string | ||
| 117 | */ | ||
| 118 | public function getHtml(): string | ||
| 122 | |||
| 123 | /** | ||
| 124 | * @return AlternativeText | ||
| 125 | */ | ||
| 126 | public function getText(): AlternativeText | ||
| 130 | |||
| 131 | /** | ||
| 132 | * @return array|PartInterface[] | ||
| 133 | */ | ||
| 134 | public function getAttachments(): iterable | ||
| 138 | |||
| 139 | /** | ||
| 140 | * @return array|PartInterface[] | ||
| 141 | */ | ||
| 142 | public function getEmbeddedImages(): iterable | ||
| 146 | |||
| 147 | /** | ||
| 148 | * @return MessageInterface | ||
| 149 | */ | ||
| 150 | public function createMessage(): MessageInterface | ||
| 154 | |||
| 155 | /** | ||
| 156 | * @param MessageInterface $message | ||
| 157 | * @return MessageInterface | ||
| 158 | */ | ||
| 159 | public function attachToMessage(MessageInterface $message): MessageInterface | ||
| 172 | |||
| 173 | /** | ||
| 174 | * @return PartInterface | ||
| 175 | */ | ||
| 176 | private function createMessageRoot(): PartInterface | ||
| 189 | |||
| 190 | /** | ||
| 191 | * @return PartInterface | ||
| 192 | */ | ||
| 193 | private function createMessageHumanReadable(): PartInterface | ||
| 206 | |||
| 207 | /** | ||
| 208 | * @return PartInterface | ||
| 209 | */ | ||
| 210 | private function createMessageText(): PartInterface | ||
| 231 | |||
| 232 | /** | ||
| 233 | * @param MessageInterface $message | ||
| 234 | * @return MessageBodyCollection | ||
| 235 | */ | ||
| 236 | public static function extract(MessageInterface $message): MessageBodyCollection | ||
| 257 | |||
| 258 | /** | ||
| 259 | * @param MultiPartInterface $parts | ||
| 260 | */ | ||
| 261 | private function extractFromMimePart(MultiPartInterface $parts): void | ||
| 296 | } | ||
| 297 |