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 |
||
26 | final class MessageBodyCollection |
||
27 | { |
||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $html = ''; |
||
32 | |||
33 | /** |
||
34 | * @var AlternativeText |
||
35 | */ |
||
36 | private $text; |
||
37 | |||
38 | /** |
||
39 | * @var array|PartInterface[] |
||
40 | */ |
||
41 | private $attachments = []; |
||
42 | |||
43 | /** |
||
44 | * @var array|PartInterface[] |
||
45 | */ |
||
46 | private $embedImages = []; |
||
47 | |||
48 | /** |
||
49 | * @param string $html |
||
50 | */ |
||
51 | 37 | public function __construct(string $html = '') |
|
56 | |||
57 | /** |
||
58 | * @param string $html |
||
59 | * @return MessageBodyCollection |
||
60 | */ |
||
61 | 6 | public function withHtml(string $html): self |
|
68 | |||
69 | /** |
||
70 | * @param string $html |
||
71 | * @return MessageBodyCollection |
||
72 | */ |
||
73 | 16 | public function withHtmlAndNoGeneratedAlternativeText(string $html): self |
|
79 | |||
80 | /** |
||
81 | * @param AlternativeText $text |
||
82 | * @return MessageBodyCollection |
||
83 | */ |
||
84 | 17 | public function withAlternativeText(AlternativeText $text): self |
|
90 | |||
91 | /** |
||
92 | * @param PartInterface $part |
||
93 | * @return MessageBodyCollection |
||
94 | */ |
||
95 | 10 | public function withAttachment(PartInterface $part): self |
|
114 | |||
115 | /** |
||
116 | * @param EmbeddedImage $embeddedImage |
||
117 | * @return MessageBodyCollection |
||
118 | */ |
||
119 | 5 | public function withEmbeddedImage(EmbeddedImage $embeddedImage): self |
|
125 | |||
126 | /** |
||
127 | * @param MessageInterface $message |
||
128 | * @return MessageBodyCollection |
||
129 | */ |
||
130 | 1 | public function withAttachedMessage(MessageInterface $message): self |
|
144 | |||
145 | /** |
||
146 | * @param MessageInterface $message |
||
147 | * @param QuotationInterface $quotation |
||
148 | * @return MessageBodyCollection |
||
149 | */ |
||
150 | 7 | public function withQuotedMessage(MessageInterface $message, QuotationInterface $quotation): self |
|
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | 17 | public function getHtml(): string |
|
162 | |||
163 | /** |
||
164 | * @return AlternativeText |
||
165 | */ |
||
166 | 17 | public function getText(): AlternativeText |
|
170 | |||
171 | /** |
||
172 | * @return array|PartInterface[] |
||
173 | */ |
||
174 | 4 | public function getAttachments(): iterable |
|
178 | |||
179 | /** |
||
180 | * @return array|PartInterface[] |
||
181 | */ |
||
182 | 4 | public function getEmbeddedImages(): iterable |
|
186 | |||
187 | /** |
||
188 | * @return MessageInterface |
||
189 | */ |
||
190 | 25 | public function createMessage(): MessageInterface |
|
194 | |||
195 | /** |
||
196 | * @param MessageInterface $message |
||
197 | * @return MessageInterface |
||
198 | */ |
||
199 | 1 | public function attachToMessage(MessageInterface $message): MessageInterface |
|
212 | |||
213 | /** |
||
214 | * @param MessageInterface $originalMessage |
||
215 | * @return MessageInterface |
||
216 | */ |
||
217 | 6 | public function inReplyTo(MessageInterface $originalMessage): MessageInterface |
|
224 | |||
225 | /** |
||
226 | * @param MessageInterface $originalMessage |
||
227 | * @return MessageInterface |
||
228 | */ |
||
229 | 2 | public function inReplyToAll(MessageInterface $originalMessage): MessageInterface |
|
236 | |||
237 | /** |
||
238 | * @param MessageInterface $originalMessage |
||
239 | * @param array<int, string> $replyRecipientHeaderNames |
||
240 | * @return MessageInterface |
||
241 | */ |
||
242 | 8 | private function newReply(MessageInterface $originalMessage, array $replyRecipientHeaderNames): MessageInterface |
|
262 | |||
263 | /** |
||
264 | * @param MessageInterface $originalMessage |
||
265 | * @return MessageInterface |
||
266 | */ |
||
267 | 3 | public function asForwardTo(MessageInterface $originalMessage): MessageInterface |
|
279 | |||
280 | /** |
||
281 | * @return PartInterface |
||
282 | */ |
||
283 | 25 | private function createMessageRoot(): PartInterface |
|
296 | |||
297 | /** |
||
298 | * @return PartInterface |
||
299 | */ |
||
300 | 25 | private function createMessageHumanReadable(): PartInterface |
|
313 | |||
314 | /** |
||
315 | * @return PartInterface |
||
316 | */ |
||
317 | 25 | private function createMessageText(): PartInterface |
|
338 | |||
339 | /** |
||
340 | * @param MessageInterface $message |
||
341 | * @return MessageBodyCollection |
||
342 | */ |
||
343 | 18 | public static function extract(MessageInterface $message): MessageBodyCollection |
|
366 | |||
367 | /** |
||
368 | * @param MultiPartInterface $parts |
||
369 | */ |
||
370 | 14 | private function extractFromMimePart(MultiPartInterface $parts): void |
|
405 | |||
406 | /** |
||
407 | * @param MessageInterface $message |
||
408 | * @return string |
||
409 | */ |
||
410 | 12 | private function extractSubject(MessageInterface $message): string |
|
418 | |||
419 | /** |
||
420 | * @param HeaderInterface $header |
||
421 | * @return HeaderInterface |
||
422 | */ |
||
423 | 5 | private function determineReplyHeader(HeaderInterface $header): HeaderInterface |
|
432 | |||
433 | /** |
||
434 | * @param MessageInterface $originalMessage |
||
435 | * @return MessageInterface |
||
436 | */ |
||
437 | 11 | private function createReferencedMessage(MessageInterface $originalMessage): MessageInterface |
|
459 | |||
460 | /** |
||
461 | * @param MessageInterface $message |
||
462 | * @return StreamInterface |
||
463 | */ |
||
464 | 4 | private static function decodeMessageBody(MessageInterface $message): StreamInterface |
|
485 | } |
||
486 |
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: