Complex classes like Message 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 Message, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Message |
||
13 | { |
||
14 | /** |
||
15 | * @var Address[] |
||
16 | */ |
||
17 | private $to = []; |
||
18 | |||
19 | /** |
||
20 | * @var Address[] |
||
21 | */ |
||
22 | private $from = []; |
||
23 | |||
24 | /** |
||
25 | * @var Address |
||
26 | */ |
||
27 | private $sender; |
||
28 | |||
29 | /** |
||
30 | * @var Address[] |
||
31 | */ |
||
32 | private $cc = []; |
||
33 | |||
34 | /** |
||
35 | * @var Address[] |
||
36 | */ |
||
37 | private $bcc = []; |
||
38 | |||
39 | /** |
||
40 | * @var Address[] |
||
41 | */ |
||
42 | private $reply_to = []; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $subject; |
||
48 | |||
49 | /** |
||
50 | * @var DateTimeInterface |
||
51 | */ |
||
52 | private $date; |
||
53 | |||
54 | /** |
||
55 | * @var string|null |
||
56 | */ |
||
57 | private $text; |
||
58 | |||
59 | /** |
||
60 | * @var string|null |
||
61 | */ |
||
62 | private $html; |
||
63 | |||
64 | /** |
||
65 | * @var Attachment[] |
||
66 | */ |
||
67 | private $attachments = []; |
||
68 | |||
69 | /** |
||
70 | * @var InlineAttachment[] |
||
71 | */ |
||
72 | private $inline_attachments = []; |
||
73 | |||
74 | /** |
||
75 | * @var Header[] |
||
76 | */ |
||
77 | private $headers = []; |
||
78 | |||
79 | /** |
||
80 | * @param Address|Address[] $to |
||
81 | * @param Address|Address[] $from |
||
82 | * @param string $subject |
||
83 | * @param string|null $text |
||
84 | 19 | * @param string|null $html |
|
85 | */ |
||
86 | 19 | public function __construct($to, $from, $subject, $text, $html = null) |
|
95 | |||
96 | /** |
||
97 | 17 | * @return Address|Address[] |
|
98 | */ |
||
99 | 17 | public function getTo() |
|
103 | |||
104 | /** |
||
105 | * This field contains the identity of the primary recipients of the Message. |
||
106 | * |
||
107 | 19 | * @param Address|Address[] $address |
|
108 | */ |
||
109 | 19 | public function setTo($address) |
|
113 | |||
114 | /** |
||
115 | * @param Address|Address[] $address |
||
116 | */ |
||
117 | public function addTo($address) |
||
121 | |||
122 | /** |
||
123 | 17 | * @return Address|Address[] |
|
124 | */ |
||
125 | 17 | public function getFrom() |
|
129 | |||
130 | /** |
||
131 | * Specifies the author(s) of the message; that is, the mailbox(es) |
||
132 | * of the person(s) or system(s) responsible for the writing of the |
||
133 | * message. |
||
134 | * |
||
135 | 19 | * @param Address|Address[] $address |
|
136 | */ |
||
137 | 19 | public function setFrom($address) |
|
141 | |||
142 | /** |
||
143 | * @param Address|Address[] $address |
||
144 | */ |
||
145 | public function addFrom($address) |
||
149 | |||
150 | /** |
||
151 | * Return the Sender |
||
152 | * |
||
153 | 15 | * @return Address|null |
|
154 | */ |
||
155 | 15 | public function getSender() |
|
167 | |||
168 | /** |
||
169 | * Specifies the mailbox of the agent responsible for the actual transmission of the message. |
||
170 | * |
||
171 | * This field contains the authenticated identity of the "agent" (person, system or process) |
||
172 | * that sends the message. It is intended for use when the sender is *not* the author of |
||
173 | * the message, or to indicate who among a group of authors actually sent the message. |
||
174 | * |
||
175 | * If the contents of this field would be completely redundant with the "From" field, then |
||
176 | * the "Sender" field need not be present and its use is discouraged, though still permitted. |
||
177 | * |
||
178 | * In particular, the "Sender" field *must* be present if it is *not* the same as the "From" Field. |
||
179 | * |
||
180 | 2 | * @param Address|null $sender |
|
181 | */ |
||
182 | 2 | public function setSender(Address $sender) |
|
186 | |||
187 | /** |
||
188 | 17 | * @return Address|Address[] |
|
189 | */ |
||
190 | 17 | public function getCC() |
|
194 | |||
195 | /** |
||
196 | * This field contains the identity of any secondary recipients of the Message. |
||
197 | * |
||
198 | * @param Address|Address[] $address |
||
199 | */ |
||
200 | public function setCC($address) |
||
204 | |||
205 | /** |
||
206 | 2 | * @param Address|Address[] $address |
|
207 | */ |
||
208 | 2 | public function addCC($address) |
|
212 | |||
213 | /** |
||
214 | 17 | * @return Address|Address[] |
|
215 | */ |
||
216 | 17 | public function getBCC() |
|
220 | |||
221 | /** |
||
222 | * This field contains the identity of additional recipients of the message. |
||
223 | * |
||
224 | * The contents of this field are not included in copies of the Message sent to the primary |
||
225 | * or secondary recipients, e.g. the "To" and "CC" fields. |
||
226 | * |
||
227 | * Some systems may choose to include the text of the "BCC" field only in the author's copy, |
||
228 | * while others may also include it in the text sent to all those indicated in the "BCC" list. |
||
229 | * |
||
230 | * @param Address|Address[] $address |
||
231 | */ |
||
232 | public function setBCC($address) |
||
236 | |||
237 | /** |
||
238 | 2 | * @param Address|Address[] $address |
|
239 | */ |
||
240 | 2 | public function addBCC($address) |
|
244 | |||
245 | /** |
||
246 | 15 | * @return Address|Address[] |
|
247 | */ |
||
248 | 15 | public function getReplyTo() |
|
252 | |||
253 | /** |
||
254 | * @param Address|Address[] $address |
||
255 | */ |
||
256 | public function setReplyTo($address) |
||
260 | |||
261 | /** |
||
262 | * @param Address|Address[] $address |
||
263 | */ |
||
264 | public function addReplyTo($address) |
||
268 | |||
269 | /** |
||
270 | 17 | * @return string |
|
271 | */ |
||
272 | 17 | public function getSubject() |
|
276 | |||
277 | /** |
||
278 | 19 | * @param string $subject |
|
279 | */ |
||
280 | 19 | public function setSubject($subject) |
|
284 | |||
285 | /** |
||
286 | 15 | * @return DateTimeInterface |
|
287 | */ |
||
288 | 15 | public function getDate() |
|
292 | |||
293 | /** |
||
294 | 19 | * @param int|string|DateTimeInterface $date DateTime in Sender's timezone (or a UNIX integer timestamp; |
|
295 | * or a string that is compatible with the strtotime() function) |
||
296 | 19 | */ |
|
297 | 19 | public function setDate($date) |
|
311 | |||
312 | /** |
||
313 | * @return string|null plain text message body |
||
314 | 19 | */ |
|
315 | public function getText() |
||
319 | |||
320 | 19 | /** |
|
321 | 19 | * @param string|null $text plain text message body |
|
322 | * |
||
323 | * @throws InvalidArgumentException if the given message body is not valid UTF-8 |
||
324 | */ |
||
325 | public function setText($text) |
||
333 | |||
334 | /** |
||
335 | * @return string|null HTML message body |
||
336 | 19 | */ |
|
337 | public function getHTML() |
||
341 | |||
342 | 19 | /** |
|
343 | 19 | * @param string|null $html HTML message body |
|
344 | * |
||
345 | * @throws InvalidArgumentException if the given message body is not valid UTF-8 |
||
346 | */ |
||
347 | public function setHTML($html) |
||
355 | |||
356 | 7 | /** |
|
357 | * @return Attachment[] |
||
358 | 7 | */ |
|
359 | 7 | public function getAttachments() |
|
363 | |||
364 | 15 | /** |
|
365 | * @param Attachment $attachment |
||
366 | 15 | */ |
|
367 | public function addAttachment(Attachment $attachment) |
||
371 | |||
372 | /** |
||
373 | * @return InlineAttachment[] |
||
374 | */ |
||
375 | public function getInlineAttachments() |
||
379 | |||
380 | /** |
||
381 | * Add an inline Attachment, e.g. an image you wish to display in the HTML body of your Message. |
||
382 | * |
||
383 | * This method returns a URI for the inline Attachment - you should substitute a placeholder, |
||
384 | * e.g. for the `src` attribute of an `img` tag, in the body of your HTML Message content - |
||
385 | 5 | * for example: |
|
386 | * |
||
387 | 5 | * $html = '<img src="#logo-image"/>'; |
|
388 | * $uri = $message->addInlineAttachment(Attachment::fromFile(__DIR__ . '/logo.png')); |
||
389 | 5 | * $html = strtr($html, ["#logo-image" => $uri]); |
|
390 | * $message->setHTML($html); |
||
391 | 5 | * |
|
392 | * @param Attachment $attachment |
||
393 | * |
||
394 | * @return string inline Attachment URI |
||
395 | */ |
||
396 | public function addInlineAttachment(Attachment $attachment) |
||
404 | |||
405 | 16 | /** |
|
406 | * @return Header[] |
||
407 | */ |
||
408 | public function getHeaders() |
||
418 | 1 | ||
419 | 1 | /** |
|
420 | * Set a custom MIME message header - for example, you may wish to set special headers |
||
421 | * such as `Message-ID`, `X-Priority` or `X-Mailer` headers, but be aware that some |
||
422 | * headers (such as `Message-ID`) have syntax that you need to comply with. |
||
423 | * |
||
424 | * @param string $name |
||
425 | 3 | * @param string $value |
|
426 | */ |
||
427 | 3 | public function setHeader($name, $value) |
|
431 | |||
432 | /** |
||
433 | * @param string $name |
||
434 | * @param string $value |
||
435 | */ |
||
436 | public function addHeader($name, $value) |
||
440 | } |
||
441 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..