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 |
||
37 | class Message extends BaseMessage |
||
38 | { |
||
39 | /** |
||
40 | * @var string|array from |
||
41 | */ |
||
42 | protected $from; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $to = []; |
||
48 | |||
49 | /** |
||
50 | * @var string|array reply to |
||
51 | */ |
||
52 | protected $replyTo; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $cc = []; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $bcc = []; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $subject; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $textBody; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $htmlBody; |
||
78 | |||
79 | /** |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $attachments = []; |
||
83 | |||
84 | /** |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $tag; |
||
88 | |||
89 | /** |
||
90 | * @var bool |
||
91 | */ |
||
92 | protected $trackOpens = true; |
||
93 | |||
94 | /** |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $headers = []; |
||
98 | |||
99 | /** |
||
100 | * @var integer |
||
101 | */ |
||
102 | protected $templateId; |
||
103 | |||
104 | /** |
||
105 | * @var array |
||
106 | */ |
||
107 | protected $templateModel; |
||
108 | |||
109 | /** |
||
110 | * @var bool |
||
111 | */ |
||
112 | protected $inlineCss = true; |
||
113 | |||
114 | /** |
||
115 | * @inheritdoc |
||
116 | */ |
||
117 | 1 | public function getCharset() |
|
121 | |||
122 | /** |
||
123 | * @inheritdoc |
||
124 | */ |
||
125 | 1 | public function setCharset($charset) |
|
129 | |||
130 | /** |
||
131 | * @inheritdoc |
||
132 | */ |
||
133 | 1 | public function getFrom() |
|
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | */ |
||
141 | 3 | public function setFrom($from) |
|
146 | |||
147 | /** |
||
148 | * @inheritdoc |
||
149 | */ |
||
150 | 3 | public function getTo() |
|
154 | |||
155 | /** |
||
156 | * @inheritdoc |
||
157 | */ |
||
158 | 3 | public function setTo($to) |
|
166 | |||
167 | /** |
||
168 | * @inheritdoc |
||
169 | */ |
||
170 | 1 | public function getReplyTo() |
|
174 | |||
175 | /** |
||
176 | * @inheritdoc |
||
177 | */ |
||
178 | 1 | public function setReplyTo($replyTo) |
|
183 | |||
184 | /** |
||
185 | * @inheritdoc |
||
186 | */ |
||
187 | 1 | public function getCc() |
|
191 | |||
192 | /** |
||
193 | * @inheritdoc |
||
194 | */ |
||
195 | 1 | public function setCc($cc) |
|
203 | |||
204 | /** |
||
205 | * @inheritdoc |
||
206 | */ |
||
207 | 1 | public function getBcc() |
|
211 | |||
212 | /** |
||
213 | * @inheritdoc |
||
214 | */ |
||
215 | 1 | public function setBcc($bcc) |
|
223 | |||
224 | /** |
||
225 | * @inheritdoc |
||
226 | */ |
||
227 | 3 | public function getSubject() |
|
231 | |||
232 | /** |
||
233 | * @inheritdoc |
||
234 | */ |
||
235 | 2 | public function setSubject($subject) |
|
240 | |||
241 | /** |
||
242 | * @return string|null text body of the message |
||
243 | * @since XXX |
||
244 | */ |
||
245 | 1 | public function getTextBody() |
|
249 | |||
250 | /** |
||
251 | * @inheritdoc |
||
252 | */ |
||
253 | 2 | public function setTextBody($text) |
|
258 | |||
259 | /** |
||
260 | * @return string|null html body of the message |
||
261 | * @since XXX |
||
262 | */ |
||
263 | 1 | public function getHtmlBody() |
|
267 | |||
268 | /** |
||
269 | * @inheritdoc |
||
270 | */ |
||
271 | 1 | public function setHtmlBody($html) |
|
276 | |||
277 | /** |
||
278 | * @return string tag associated to the email |
||
279 | * @since XXX |
||
280 | */ |
||
281 | 1 | public function getTag() |
|
285 | |||
286 | /** |
||
287 | * @param string $tag tag which should be associated to the email |
||
288 | * @return $this |
||
289 | * @since XXX |
||
290 | */ |
||
291 | 1 | public function setTag($tag) |
|
296 | |||
297 | /** |
||
298 | * @param bool $trackOpens define if mail should be tracked |
||
299 | * @return $this |
||
300 | * @since XXX |
||
301 | */ |
||
302 | 1 | public function setTrackOpens($trackOpens) |
|
307 | |||
308 | /** |
||
309 | * @return bool tracking status |
||
310 | * @since XXX |
||
311 | */ |
||
312 | 1 | public function getTrackOpens() |
|
316 | |||
317 | /** |
||
318 | * @param integer $templateId template Id used. in this case, Subject / HtmlBody / TextBody are discarded |
||
319 | * @return $this |
||
320 | * @since XXX |
||
321 | */ |
||
322 | 2 | public function setTemplateId($templateId) |
|
327 | |||
328 | /** |
||
329 | * @return integer|null current templateId |
||
330 | * @since XXX |
||
331 | */ |
||
332 | 1 | public function getTemplateId() |
|
336 | |||
337 | /** |
||
338 | * @param array $templateModel model associated with the template |
||
339 | * @return $this |
||
340 | * @since XXX |
||
341 | */ |
||
342 | 2 | public function setTemplateModel($templateModel) |
|
347 | |||
348 | /** |
||
349 | * @return array current template model |
||
350 | * @since XXX |
||
351 | */ |
||
352 | 1 | public function getTemplateModel() |
|
356 | |||
357 | /** |
||
358 | * @param bool $inlineCss define if css should be inlined |
||
359 | * @return $this |
||
360 | * @since XXX |
||
361 | */ |
||
362 | 1 | public function setInlineCss($inlineCss) |
|
367 | |||
368 | /** |
||
369 | * @return bool define if css should be inlined |
||
370 | * @since XXX |
||
371 | */ |
||
372 | 1 | public function getInlineCss() |
|
376 | |||
377 | /** |
||
378 | * @param array $header add custom header to the mail |
||
379 | * @since XXX |
||
380 | */ |
||
381 | 1 | public function addHeader($header) |
|
385 | |||
386 | /** |
||
387 | * @return array|null headers which should be added to the mail |
||
388 | * @since XXX |
||
389 | */ |
||
390 | 1 | public function getHeaders() |
|
394 | |||
395 | /** |
||
396 | * @return array|null list of attachments |
||
397 | * @since XXX |
||
398 | */ |
||
399 | 1 | public function getAttachments() |
|
403 | |||
404 | /** |
||
405 | * @inheritdoc |
||
406 | */ |
||
407 | 1 | public function attach($fileName, array $options = []) |
|
425 | |||
426 | /** |
||
427 | * @inheritdoc |
||
428 | */ |
||
429 | 2 | public function attachContent($content, array $options = []) |
|
447 | |||
448 | /** |
||
449 | * @inheritdoc |
||
450 | */ |
||
451 | 1 | public function embed($fileName, array $options = []) |
|
470 | |||
471 | /** |
||
472 | * @inheritdoc |
||
473 | */ |
||
474 | 2 | public function embedContent($content, array $options = []) |
|
493 | |||
494 | /** |
||
495 | * @inheritdoc |
||
496 | * @todo make real serialization to make message compliant with PostmarkAPI |
||
497 | */ |
||
498 | public function toString() |
||
502 | |||
503 | |||
504 | /** |
||
505 | * @param array|string $emailsData email can be defined as string. In this case no transformation is done |
||
506 | * or as an array ['[email protected]', '[email protected]' => 'Email 2'] |
||
507 | * @return string|null |
||
508 | * @since XXX |
||
509 | */ |
||
510 | 1 | public static function stringifyEmails($emailsData) |
|
532 | |||
533 | |||
534 | } |