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 | protected $charset = 'utf-8'; |
||
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | 1 | public function getCharset() |
|
123 | |||
124 | /** |
||
125 | * @inheritdoc |
||
126 | */ |
||
127 | 1 | public function setCharset($charset) |
|
131 | |||
132 | /** |
||
133 | * @inheritdoc |
||
134 | */ |
||
135 | 1 | public function getFrom() |
|
139 | |||
140 | /** |
||
141 | * @inheritdoc |
||
142 | */ |
||
143 | 3 | public function setFrom($from) |
|
148 | |||
149 | /** |
||
150 | * @inheritdoc |
||
151 | */ |
||
152 | 3 | public function getTo() |
|
156 | |||
157 | /** |
||
158 | * @inheritdoc |
||
159 | */ |
||
160 | 3 | public function setTo($to) |
|
168 | |||
169 | /** |
||
170 | * @inheritdoc |
||
171 | */ |
||
172 | 1 | public function getReplyTo() |
|
176 | |||
177 | /** |
||
178 | * @inheritdoc |
||
179 | */ |
||
180 | 1 | public function setReplyTo($replyTo) |
|
185 | |||
186 | /** |
||
187 | * @inheritdoc |
||
188 | */ |
||
189 | 1 | public function getCc() |
|
193 | |||
194 | /** |
||
195 | * @inheritdoc |
||
196 | */ |
||
197 | 1 | public function setCc($cc) |
|
205 | |||
206 | /** |
||
207 | * @inheritdoc |
||
208 | */ |
||
209 | 1 | public function getBcc() |
|
213 | |||
214 | /** |
||
215 | * @inheritdoc |
||
216 | */ |
||
217 | 1 | public function setBcc($bcc) |
|
225 | |||
226 | /** |
||
227 | * @inheritdoc |
||
228 | */ |
||
229 | 3 | public function getSubject() |
|
233 | |||
234 | /** |
||
235 | * @inheritdoc |
||
236 | */ |
||
237 | 2 | public function setSubject($subject) |
|
242 | |||
243 | /** |
||
244 | * @return string|null text body of the message |
||
245 | * @since XXX |
||
246 | */ |
||
247 | 1 | public function getTextBody() |
|
251 | |||
252 | /** |
||
253 | * @inheritdoc |
||
254 | */ |
||
255 | 2 | public function setTextBody($text) |
|
260 | |||
261 | /** |
||
262 | * @return string|null html body of the message |
||
263 | * @since XXX |
||
264 | */ |
||
265 | 1 | public function getHtmlBody() |
|
269 | |||
270 | /** |
||
271 | * @inheritdoc |
||
272 | */ |
||
273 | 1 | public function setHtmlBody($html) |
|
278 | |||
279 | /** |
||
280 | * @return string tag associated to the email |
||
281 | * @since XXX |
||
282 | */ |
||
283 | 1 | public function getTag() |
|
287 | |||
288 | /** |
||
289 | * @param string $tag tag which should be associated to the email |
||
290 | * @return $this |
||
291 | * @since XXX |
||
292 | */ |
||
293 | 1 | public function setTag($tag) |
|
298 | |||
299 | /** |
||
300 | * @param bool $trackOpens define if mail should be tracked |
||
301 | * @return $this |
||
302 | * @since XXX |
||
303 | */ |
||
304 | 1 | public function setTrackOpens($trackOpens) |
|
309 | |||
310 | /** |
||
311 | * @return bool tracking status |
||
312 | * @since XXX |
||
313 | */ |
||
314 | 1 | public function getTrackOpens() |
|
318 | |||
319 | /** |
||
320 | * @param integer $templateId template Id used. in this case, Subject / HtmlBody / TextBody are discarded |
||
321 | * @return $this |
||
322 | * @since XXX |
||
323 | */ |
||
324 | 2 | public function setTemplateId($templateId) |
|
329 | |||
330 | /** |
||
331 | * @return integer|null current templateId |
||
332 | * @since XXX |
||
333 | */ |
||
334 | 1 | public function getTemplateId() |
|
338 | |||
339 | /** |
||
340 | * @param array $templateModel model associated with the template |
||
341 | * @return $this |
||
342 | * @since XXX |
||
343 | */ |
||
344 | 2 | public function setTemplateModel($templateModel) |
|
349 | |||
350 | /** |
||
351 | * @return array current template model |
||
352 | * @since XXX |
||
353 | */ |
||
354 | 1 | public function getTemplateModel() |
|
358 | |||
359 | /** |
||
360 | * @param bool $inlineCss define if css should be inlined |
||
361 | * @return $this |
||
362 | * @since XXX |
||
363 | */ |
||
364 | 1 | public function setInlineCss($inlineCss) |
|
369 | |||
370 | /** |
||
371 | * @return bool define if css should be inlined |
||
372 | * @since XXX |
||
373 | */ |
||
374 | 1 | public function getInlineCss() |
|
378 | |||
379 | /** |
||
380 | * @param array $header add custom header to the mail |
||
381 | * @since XXX |
||
382 | */ |
||
383 | 1 | public function addHeader($header) |
|
387 | |||
388 | /** |
||
389 | * @return array|null headers which should be added to the mail |
||
390 | * @since XXX |
||
391 | */ |
||
392 | 1 | public function getHeaders() |
|
396 | |||
397 | /** |
||
398 | * @return array|null list of attachments |
||
399 | * @since XXX |
||
400 | */ |
||
401 | 1 | public function getAttachments() |
|
405 | |||
406 | /** |
||
407 | * @inheritdoc |
||
408 | */ |
||
409 | 1 | public function attach($fileName, array $options = []) |
|
427 | |||
428 | /** |
||
429 | * @inheritdoc |
||
430 | */ |
||
431 | 2 | public function attachContent($content, array $options = []) |
|
449 | |||
450 | /** |
||
451 | * @inheritdoc |
||
452 | */ |
||
453 | 1 | public function embed($fileName, array $options = []) |
|
472 | |||
473 | /** |
||
474 | * @inheritdoc |
||
475 | */ |
||
476 | 2 | public function embedContent($content, array $options = []) |
|
495 | |||
496 | /** |
||
497 | * @inheritdoc |
||
498 | * @todo make real serialization to make message compliant with PostmarkAPI |
||
499 | */ |
||
500 | public function toString() |
||
504 | |||
505 | |||
506 | /** |
||
507 | * @param array|string $emailsData email can be defined as string. In this case no transformation is done |
||
508 | * or as an array ['[email protected]', '[email protected]' => 'Email 2'] |
||
509 | * @return string|null |
||
510 | * @since XXX |
||
511 | */ |
||
512 | 1 | public static function stringifyEmails($emailsData) |
|
534 | |||
535 | |||
536 | } |