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 from |
||
41 | */ |
||
42 | protected $from; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $to = []; |
||
48 | |||
49 | /** |
||
50 | * @var string 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 string |
||
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) |
|
163 | |||
164 | /** |
||
165 | * @inheritdoc |
||
166 | */ |
||
167 | 1 | public function getReplyTo() |
|
171 | |||
172 | /** |
||
173 | * @inheritdoc |
||
174 | */ |
||
175 | 1 | public function setReplyTo($replyTo) |
|
180 | |||
181 | /** |
||
182 | * @inheritdoc |
||
183 | */ |
||
184 | 1 | public function getCc() |
|
188 | |||
189 | /** |
||
190 | * @inheritdoc |
||
191 | */ |
||
192 | 1 | public function setCc($cc) |
|
197 | |||
198 | /** |
||
199 | * @inheritdoc |
||
200 | */ |
||
201 | 1 | public function getBcc() |
|
205 | |||
206 | /** |
||
207 | * @inheritdoc |
||
208 | */ |
||
209 | 1 | public function setBcc($bcc) |
|
214 | |||
215 | /** |
||
216 | * @inheritdoc |
||
217 | */ |
||
218 | 3 | public function getSubject() |
|
222 | |||
223 | /** |
||
224 | * @inheritdoc |
||
225 | */ |
||
226 | 2 | public function setSubject($subject) |
|
231 | |||
232 | /** |
||
233 | * @return string|null text body of the message |
||
234 | * @since XXX |
||
235 | */ |
||
236 | 1 | public function getTextBody() |
|
240 | |||
241 | /** |
||
242 | * @inheritdoc |
||
243 | */ |
||
244 | 2 | public function setTextBody($text) |
|
249 | |||
250 | /** |
||
251 | * @return string|null html body of the message |
||
252 | * @since XXX |
||
253 | */ |
||
254 | 1 | public function getHtmlBody() |
|
258 | |||
259 | /** |
||
260 | * @inheritdoc |
||
261 | */ |
||
262 | 1 | public function setHtmlBody($html) |
|
267 | |||
268 | /** |
||
269 | * @return string tag associated to the email |
||
270 | * @since XXX |
||
271 | */ |
||
272 | 1 | public function getTag() |
|
276 | |||
277 | /** |
||
278 | * @param string $tag tag which should be associated to the email |
||
279 | * @return $this |
||
280 | * @since XXX |
||
281 | */ |
||
282 | 1 | public function setTag($tag) |
|
287 | |||
288 | /** |
||
289 | * @param bool $trackOpens define if mail should be tracked |
||
290 | * @return $this |
||
291 | * @since XXX |
||
292 | */ |
||
293 | 1 | public function setTrackOpens($trackOpens) |
|
298 | |||
299 | /** |
||
300 | * @return bool tracking status |
||
301 | * @since XXX |
||
302 | */ |
||
303 | 1 | public function getTrackOpens() |
|
307 | |||
308 | /** |
||
309 | * @param integer $templateId template Id used. in this case, Subject / HtmlBody / TextBody are discarded |
||
310 | * @return $this |
||
311 | * @since XXX |
||
312 | */ |
||
313 | 2 | public function setTemplateId($templateId) |
|
318 | |||
319 | /** |
||
320 | * @return integer|null current templateId |
||
321 | * @since XXX |
||
322 | */ |
||
323 | 1 | public function getTemplateId() |
|
327 | |||
328 | /** |
||
329 | * @param array $templateModel model associated with the template |
||
330 | * @return $this |
||
331 | * @since XXX |
||
332 | */ |
||
333 | 2 | public function setTemplateModel($templateModel) |
|
338 | |||
339 | /** |
||
340 | * @return array current template model |
||
341 | * @since XXX |
||
342 | */ |
||
343 | 1 | public function getTemplateModel() |
|
347 | |||
348 | /** |
||
349 | * @param bool $inlineCss define if css should be inlined |
||
350 | * @return $this |
||
351 | * @since XXX |
||
352 | */ |
||
353 | 1 | public function setInlineCss($inlineCss) |
|
358 | |||
359 | /** |
||
360 | * @return bool define if css should be inlined |
||
361 | * @since XXX |
||
362 | */ |
||
363 | 1 | public function getInlineCss() |
|
367 | |||
368 | /** |
||
369 | * @param array $header add custom header to the mail |
||
370 | * @since XXX |
||
371 | */ |
||
372 | 1 | public function addHeader($header) |
|
376 | |||
377 | /** |
||
378 | * @return array|null headers which should be added to the mail |
||
379 | * @since XXX |
||
380 | */ |
||
381 | 1 | public function getHeaders() |
|
385 | |||
386 | /** |
||
387 | * @return array|null list of attachments |
||
388 | * @since XXX |
||
389 | */ |
||
390 | 1 | public function getAttachments() |
|
394 | |||
395 | /** |
||
396 | * @inheritdoc |
||
397 | */ |
||
398 | 1 | public function attach($fileName, array $options = []) |
|
414 | |||
415 | /** |
||
416 | * @inheritdoc |
||
417 | */ |
||
418 | 2 | public function attachContent($content, array $options = []) |
|
434 | |||
435 | /** |
||
436 | * @inheritdoc |
||
437 | */ |
||
438 | 1 | public function embed($fileName, array $options = []) |
|
455 | |||
456 | /** |
||
457 | * @inheritdoc |
||
458 | */ |
||
459 | 2 | public function embedContent($content, array $options = []) |
|
476 | |||
477 | /** |
||
478 | * @inheritdoc |
||
479 | * @todo make real serialization to make message compliant with PostmarkAPI |
||
480 | */ |
||
481 | public function toString() |
||
485 | |||
486 | |||
487 | /** |
||
488 | * @param array|string $emailsData email can be defined as string. In this case no transformation is done |
||
489 | * or as an array ['[email protected]', '[email protected]' => 'Email 2'] |
||
490 | * @return string|null |
||
491 | * @since XXX |
||
492 | */ |
||
493 | 3 | private function extractEmails($emailsData) |
|
515 | |||
516 | |||
517 | } |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.