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 | 37 | 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 | 25 | 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 | 6 | 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 | 8 | private function newReply(MessageInterface $originalMessage, array $replyRecipientHeaderNames): MessageInterface |
|
261 | |||
262 | /** |
||
263 | * @param MessageInterface $originalMessage |
||
264 | * @return MessageInterface |
||
265 | */ |
||
266 | 3 | public function asForwardTo(MessageInterface $originalMessage): MessageInterface |
|
278 | |||
279 | /** |
||
280 | * @return PartInterface |
||
281 | */ |
||
282 | 25 | private function createMessageRoot(): PartInterface |
|
295 | |||
296 | /** |
||
297 | * @return PartInterface |
||
298 | */ |
||
299 | 25 | private function createMessageHumanReadable(): PartInterface |
|
312 | |||
313 | /** |
||
314 | * @return PartInterface |
||
315 | */ |
||
316 | 25 | private function createMessageText(): PartInterface |
|
337 | |||
338 | /** |
||
339 | * @param MessageInterface $message |
||
340 | * @return MessageBodyCollection |
||
341 | */ |
||
342 | 18 | public static function extract(MessageInterface $message): MessageBodyCollection |
|
365 | |||
366 | /** |
||
367 | * @param MultiPartInterface $parts |
||
368 | */ |
||
369 | 14 | private function extractFromMimePart(MultiPartInterface $parts): void |
|
404 | |||
405 | /** |
||
406 | * @param MessageInterface $message |
||
407 | * @return string |
||
408 | */ |
||
409 | 12 | private function extractSubject(MessageInterface $message): string |
|
417 | |||
418 | /** |
||
419 | * @param HeaderInterface $header |
||
420 | * @return HeaderInterface |
||
421 | */ |
||
422 | 5 | private function determineReplyHeader(HeaderInterface $header): HeaderInterface |
|
431 | |||
432 | /** |
||
433 | * @param MessageInterface $originalMessage |
||
434 | * @return MessageInterface |
||
435 | */ |
||
436 | 11 | private function createReferencedMessage(MessageInterface $originalMessage): MessageInterface |
|
458 | |||
459 | /** |
||
460 | * @param PartInterface $part |
||
461 | * @return StreamInterface |
||
462 | */ |
||
463 | 14 | private static function decodeBodyPart(PartInterface $part): StreamInterface |
|
484 | |||
485 | /** |
||
486 | * @param MessageInterface $message |
||
487 | * @return StreamInterface |
||
488 | */ |
||
489 | 4 | private static function decodeMessageBody(MessageInterface $message): StreamInterface |
|
510 | } |
||
511 |
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: