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 | 16 | public function __construct(string $html = '') |
|
| 45 | |||
| 46 | /** |
||
| 47 | * @param string $html |
||
| 48 | * @return MessageBodyCollection |
||
| 49 | */ |
||
| 50 | 6 | public function withHtml(string $html): self |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $html |
||
| 60 | * @return MessageBodyCollection |
||
| 61 | */ |
||
| 62 | 3 | public function withHtmlAndNoGeneratedAlternativeText(string $html): self |
|
| 68 | |||
| 69 | /** |
||
| 70 | * @param AlternativeText $text |
||
| 71 | * @return MessageBodyCollection |
||
| 72 | */ |
||
| 73 | 4 | public function withAlternativeText(AlternativeText $text): self |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @param PartInterface $part |
||
| 82 | * @return MessageBodyCollection |
||
| 83 | */ |
||
| 84 | 7 | public function withAttachment(PartInterface $part): self |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param EmbeddedImage $embeddedImage |
||
| 106 | * @return MessageBodyCollection |
||
| 107 | */ |
||
| 108 | 5 | public function withEmbeddedImage(EmbeddedImage $embeddedImage): self |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | 4 | public function getHtml(): string |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @return AlternativeText |
||
| 125 | */ |
||
| 126 | 4 | public function getText(): AlternativeText |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @return array|PartInterface[] |
||
| 133 | */ |
||
| 134 | 4 | public function getAttachments(): iterable |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @return array|PartInterface[] |
||
| 141 | */ |
||
| 142 | 4 | public function getEmbeddedImages(): iterable |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @return MessageInterface |
||
| 149 | */ |
||
| 150 | 8 | public function createMessage(): MessageInterface |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param MessageInterface $message |
||
| 157 | * @return MessageInterface |
||
| 158 | */ |
||
| 159 | 1 | public function attachToMessage(MessageInterface $message): MessageInterface |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @return PartInterface |
||
| 175 | */ |
||
| 176 | 8 | private function createMessageRoot(): PartInterface |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @return PartInterface |
||
| 192 | */ |
||
| 193 | 8 | private function createMessageHumanReadable(): PartInterface |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @return PartInterface |
||
| 209 | */ |
||
| 210 | 8 | private function createMessageText(): PartInterface |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param MessageInterface $message |
||
| 234 | * @return MessageBodyCollection |
||
| 235 | */ |
||
| 236 | 5 | public static function fromMessage(MessageInterface $message): MessageBodyCollection |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @param MessageInterface $message |
||
| 243 | * @return MessageBodyCollection |
||
| 244 | */ |
||
| 245 | 5 | private function extract(MessageInterface $message): self |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @param MultiPartInterface $parts |
||
| 267 | */ |
||
| 268 | 3 | private function extractFromMimePart(MultiPartInterface $parts): void |
|
| 303 | } |
||
| 304 |