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 | private const DEFAULT_CHARSET = '<meta http-equiv="Content-Type" content="text/html; charset=%s"/>'; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $html = ''; |
||
33 | |||
34 | /** |
||
35 | * @var AlternativeText |
||
36 | */ |
||
37 | private $text; |
||
38 | |||
39 | /** |
||
40 | * @var array|PartInterface[] |
||
41 | */ |
||
42 | private $attachments = []; |
||
43 | |||
44 | /** |
||
45 | * @var array|PartInterface[] |
||
46 | */ |
||
47 | private $embedImages = []; |
||
48 | |||
49 | /** |
||
50 | * @param string $html |
||
51 | */ |
||
52 | 39 | public function __construct(string $html = '') |
|
57 | |||
58 | /** |
||
59 | * @param string $html |
||
60 | * @return MessageBodyCollection |
||
61 | */ |
||
62 | 6 | public function withHtml(string $html): self |
|
69 | |||
70 | /** |
||
71 | * @param string $html |
||
72 | * @return MessageBodyCollection |
||
73 | */ |
||
74 | 18 | public function withHtmlAndNoGeneratedAlternativeText(string $html): self |
|
80 | |||
81 | /** |
||
82 | * @param AlternativeText $text |
||
83 | * @return MessageBodyCollection |
||
84 | */ |
||
85 | 19 | public function withAlternativeText(AlternativeText $text): self |
|
91 | |||
92 | /** |
||
93 | * @param PartInterface $part |
||
94 | * @return MessageBodyCollection |
||
95 | */ |
||
96 | 10 | public function withAttachment(PartInterface $part): self |
|
115 | |||
116 | /** |
||
117 | * @param EmbeddedImage $embeddedImage |
||
118 | * @return MessageBodyCollection |
||
119 | */ |
||
120 | 5 | public function withEmbeddedImage(EmbeddedImage $embeddedImage): self |
|
126 | |||
127 | /** |
||
128 | * @param MessageInterface $message |
||
129 | * @return MessageBodyCollection |
||
130 | */ |
||
131 | 1 | public function withAttachedMessage(MessageInterface $message): self |
|
145 | |||
146 | /** |
||
147 | * @param MessageInterface $message |
||
148 | * @param QuotationInterface $quotation |
||
149 | * @return MessageBodyCollection |
||
150 | */ |
||
151 | 7 | public function withQuotedMessage(MessageInterface $message, QuotationInterface $quotation): self |
|
155 | |||
156 | /** |
||
157 | * @return string |
||
158 | */ |
||
159 | 19 | public function getHtml(): string |
|
163 | |||
164 | /** |
||
165 | * @return AlternativeText |
||
166 | */ |
||
167 | 19 | public function getText(): AlternativeText |
|
171 | |||
172 | /** |
||
173 | * @return array|PartInterface[] |
||
174 | */ |
||
175 | 4 | public function getAttachments(): iterable |
|
179 | |||
180 | /** |
||
181 | * @return array|PartInterface[] |
||
182 | */ |
||
183 | 4 | public function getEmbeddedImages(): iterable |
|
187 | |||
188 | /** |
||
189 | * @return MessageInterface |
||
190 | */ |
||
191 | 25 | public function createMessage(): MessageInterface |
|
195 | |||
196 | /** |
||
197 | * @param MessageInterface $message |
||
198 | * @return MessageInterface |
||
199 | */ |
||
200 | 1 | public function attachToMessage(MessageInterface $message): MessageInterface |
|
213 | |||
214 | /** |
||
215 | * @param MessageInterface $originalMessage |
||
216 | * @return MessageInterface |
||
217 | */ |
||
218 | 6 | public function inReplyTo(MessageInterface $originalMessage): MessageInterface |
|
225 | |||
226 | /** |
||
227 | * @param MessageInterface $originalMessage |
||
228 | * @return MessageInterface |
||
229 | */ |
||
230 | 2 | public function inReplyToAll(MessageInterface $originalMessage): MessageInterface |
|
237 | |||
238 | /** |
||
239 | * @param MessageInterface $originalMessage |
||
240 | * @param array<int, string> $replyRecipientHeaderNames |
||
241 | * @return MessageInterface |
||
242 | */ |
||
243 | 8 | private function newReply(MessageInterface $originalMessage, array $replyRecipientHeaderNames): MessageInterface |
|
263 | |||
264 | /** |
||
265 | * @param MessageInterface $originalMessage |
||
266 | * @return MessageInterface |
||
267 | */ |
||
268 | 3 | public function asForwardTo(MessageInterface $originalMessage): MessageInterface |
|
280 | |||
281 | /** |
||
282 | * @return PartInterface |
||
283 | */ |
||
284 | 25 | private function createMessageRoot(): PartInterface |
|
297 | |||
298 | /** |
||
299 | * @return PartInterface |
||
300 | */ |
||
301 | 25 | private function createMessageHumanReadable(): PartInterface |
|
314 | |||
315 | /** |
||
316 | * @return PartInterface |
||
317 | */ |
||
318 | 25 | private function createMessageText(): PartInterface |
|
339 | |||
340 | /** |
||
341 | * @param MessageInterface $message |
||
342 | * @return MessageBodyCollection |
||
343 | */ |
||
344 | 20 | public static function extract(MessageInterface $message): MessageBodyCollection |
|
377 | |||
378 | /** |
||
379 | * @param MultiPartInterface $parts |
||
380 | */ |
||
381 | 16 | private function extractFromMimePart(MultiPartInterface $parts): void |
|
382 | { |
||
383 | 16 | foreach ($parts->getParts() as $part) { |
|
384 | 16 | $header = $part->getHeader('Content-Type'); |
|
385 | 16 | $contentType = $header->getValue()->getRaw(); |
|
386 | |||
387 | try { |
||
388 | 16 | $charset = $header->getValue()->getParameter('charset')->getValue(); |
|
389 | 2 | } catch (\UnexpectedValueException $e) { |
|
390 | 2 | $charset = ''; |
|
391 | } |
||
392 | |||
393 | 16 | try { |
|
394 | $disposition = $part->getHeader('Content-Disposition')->getValue()->getRaw(); |
||
395 | 16 | } catch (\UnexpectedValueException $e) { |
|
396 | 16 | $disposition = 'inline'; |
|
397 | 16 | } |
|
398 | |||
399 | if ($contentType === 'text/html' && $disposition === 'inline') { |
||
400 | 16 | $this->html = self::ensureHtmlCharset( |
|
401 | (string)new MimeBodyDecodedStream($part), |
||
402 | $charset |
||
403 | 16 | ); |
|
404 | 16 | continue; |
|
405 | 16 | } |
|
406 | |||
407 | if ($contentType === 'text/plain' && $disposition === 'inline') { |
||
408 | 2 | $this->text = AlternativeText::fromEncodedText((string)new MimeBodyDecodedStream($part), $charset); |
|
409 | 2 | continue; |
|
410 | } |
||
411 | 2 | ||
412 | 2 | if ($disposition === 'attachment') { |
|
413 | 2 | $this->attachments[] = $part; |
|
414 | continue; |
||
415 | } |
||
416 | 2 | ||
417 | 2 | if ($disposition === 'inline' && \substr($contentType, 0, 6) === 'image/' && $part->hasHeader('Content-ID')) { |
|
418 | 2 | $this->embedImages[] = $part; |
|
419 | continue; |
||
420 | } |
||
421 | |||
422 | 2 | if ($part instanceof MultiPartInterface) { |
|
423 | 2 | $this->extractFromMimePart($part); |
|
424 | } |
||
425 | } |
||
426 | 16 | } |
|
427 | |||
428 | /** |
||
429 | * @param MessageInterface $message |
||
430 | * @return string |
||
431 | */ |
||
432 | 12 | private function extractSubject(MessageInterface $message): string |
|
440 | |||
441 | /** |
||
442 | * @param HeaderInterface $header |
||
443 | * @return HeaderInterface |
||
444 | */ |
||
445 | 5 | private function determineReplyHeader(HeaderInterface $header): HeaderInterface |
|
454 | |||
455 | /** |
||
456 | * @param MessageInterface $originalMessage |
||
457 | * @return MessageInterface |
||
458 | */ |
||
459 | 11 | private function createReferencedMessage(MessageInterface $originalMessage): MessageInterface |
|
481 | |||
482 | /** |
||
483 | * @param MessageInterface $message |
||
484 | * @return StreamInterface |
||
485 | */ |
||
486 | 4 | private static function decodeMessageBody(MessageInterface $message): StreamInterface |
|
507 | |||
508 | /** |
||
509 | * @param string $html |
||
510 | * @param string $charset |
||
511 | * @return string |
||
512 | */ |
||
513 | 39 | private static function ensureHtmlCharset(string $html, string $charset = 'UTF-8'): string |
|
535 | } |
||
536 |
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: