Complex classes like Email 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 Email, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Email extends \SerializableObject |
||
20 | { |
||
21 | /** The email's sender */ |
||
22 | protected $sender; |
||
23 | /** An array of receipients */ |
||
24 | protected $to; |
||
25 | /** An array of CC receipients */ |
||
26 | protected $cc; |
||
27 | /** An array of BCC receipients */ |
||
28 | protected $bcc; |
||
29 | /** The email's reply to address */ |
||
30 | protected $replyTo; |
||
31 | /** The subject of the email */ |
||
32 | protected $subject; |
||
33 | /** The email's HTML body */ |
||
34 | protected $htmlBody; |
||
35 | /** The email's plain text body */ |
||
36 | protected $textBody; |
||
37 | /** An array of attachements for the email */ |
||
38 | protected $attachments; |
||
39 | |||
40 | /** |
||
41 | * Initialize a new email |
||
42 | */ |
||
43 | public function __construct() |
||
55 | |||
56 | /** |
||
57 | * Who is this email going to be sent from |
||
58 | * |
||
59 | * This will return a string either in the format of 'email@address' or 'name <email@address>' |
||
60 | * |
||
61 | * @return string The sender of the email |
||
62 | */ |
||
63 | public function getFromAddress() |
||
71 | |||
72 | /** |
||
73 | * Who is this email going to |
||
74 | * |
||
75 | * @return array The recipients of the email |
||
76 | */ |
||
77 | public function getToAddresses() |
||
81 | |||
82 | /** |
||
83 | * Who is this email going to (CC) |
||
84 | * |
||
85 | * @return array The recipients of the email |
||
86 | */ |
||
87 | public function getCCAddresses() |
||
91 | |||
92 | /** |
||
93 | * Who is this email going to (BCC) |
||
94 | * |
||
95 | * @return array The recipients of the email |
||
96 | */ |
||
97 | public function getBCCAddresses() |
||
101 | |||
102 | /** |
||
103 | * Who should a recipient reply to? |
||
104 | * |
||
105 | * @return string The reply to address of the email |
||
106 | */ |
||
107 | public function getReplyTo() |
||
115 | |||
116 | /** |
||
117 | * What is the email's subject? |
||
118 | * |
||
119 | * @return string The email's subject |
||
120 | */ |
||
121 | public function getSubject() |
||
125 | |||
126 | /** |
||
127 | * What should a user with an HTML capable email client see? |
||
128 | * |
||
129 | * @return string The email in HTML form |
||
130 | */ |
||
131 | public function getHTMLBody() |
||
135 | |||
136 | /** |
||
137 | * What should a user with a plain text only email client see? |
||
138 | * |
||
139 | * @return string The email in ASCII form |
||
140 | */ |
||
141 | public function getTextBody() |
||
145 | |||
146 | /** |
||
147 | * Create the address string given an email address and if specified a name |
||
148 | * |
||
149 | * @param string $email The email address |
||
150 | * @param string $name The name to associate with the address |
||
151 | * |
||
152 | * @return string The email address and name format |
||
153 | */ |
||
154 | protected function constructEmailAddressString($email, $name = false) |
||
162 | |||
163 | /** |
||
164 | * Set the address the email should be sent from |
||
165 | * |
||
166 | * @param string $email The email address to send from |
||
167 | * @param string $name The name to associate with the from address |
||
168 | */ |
||
169 | public function setFromAddress($email, $name = false) |
||
173 | |||
174 | /** |
||
175 | * Add a new address to the To: line |
||
176 | * |
||
177 | * @param string $email The email address to send to |
||
178 | * @param string $name The name to associate with the address |
||
179 | */ |
||
180 | public function addToAddress($email, $name = false) |
||
184 | |||
185 | /** |
||
186 | * Add a new address to the CC: line |
||
187 | * |
||
188 | * @param string $email The email address to send to |
||
189 | * @param string $name The name to associate with the address |
||
190 | */ |
||
191 | public function addCCAddress($email, $name = false) |
||
195 | |||
196 | /** |
||
197 | * Add a new address to the BCC: line |
||
198 | * |
||
199 | * @param string $email The email address to send to |
||
200 | * @param string $name The name to associate with the address |
||
201 | */ |
||
202 | public function addBCCAddress($email, $name = false) |
||
206 | |||
207 | /** |
||
208 | * Add an address to the inidicated list |
||
209 | * |
||
210 | * @param array $list The list to add the address to |
||
211 | * @param string $email The email address to send to |
||
212 | * @param string $name The name to associate with the address |
||
213 | */ |
||
214 | protected function addAddress(&$list, $email, $name = false) |
||
218 | |||
219 | /** |
||
220 | * Set the address a recipient should reply to |
||
221 | * |
||
222 | * @param string $email The email address to reply to |
||
223 | * @param string $name The name to associate with the from address |
||
224 | */ |
||
225 | public function setReplyTo($email, $name = false) |
||
229 | |||
230 | /** |
||
231 | * Set the subject line for the email |
||
232 | * |
||
233 | * @param string $subject The email's new subject line |
||
234 | */ |
||
235 | public function setSubject($subject) |
||
239 | |||
240 | /** |
||
241 | * Set the HTML body for the email |
||
242 | * |
||
243 | * @param string $body The email's new HTML body |
||
244 | */ |
||
245 | public function setHTMLBody($body) |
||
249 | |||
250 | /** |
||
251 | * Set the plain text for the email |
||
252 | * |
||
253 | * @param string $body The email's new plain text body |
||
254 | */ |
||
255 | public function setTextBody($body) |
||
259 | |||
260 | /** |
||
261 | * Append a string to the HTML Body |
||
262 | * |
||
263 | * @param string $body The string to append to the HTML body |
||
264 | */ |
||
265 | public function appendToHTMLBody($body) |
||
269 | |||
270 | /** |
||
271 | * Append a string to the Plain Text Body |
||
272 | * |
||
273 | * @param string $body The string to append to the plain text body |
||
274 | */ |
||
275 | public function appendToTextBody($body) |
||
279 | |||
280 | /** |
||
281 | * Add an attachment from a memory buffer |
||
282 | * |
||
283 | * @param string $name The file name for the attachment to be sent as |
||
284 | * @param string $buffer The attachment as a binary string |
||
285 | * @param string $mimeType The MIME type to send the attachment as |
||
286 | */ |
||
287 | public function addAttachmentFromBuffer($name, $buffer, $mimeType = 'application/octet-stream') |
||
291 | |||
292 | /** |
||
293 | * Add an attachment from a file on the local disk |
||
294 | * |
||
295 | * @param string $filename The file name and path on the local disk |
||
296 | * @param string $name The file name for the attachment to be sent as |
||
297 | */ |
||
298 | public function addAttachmentFromFile($filename, $name = false) |
||
315 | |||
316 | /** |
||
317 | * Does this email have an attachment? |
||
318 | * |
||
319 | * @return boolean True if the email has an attachment. False otherwise |
||
320 | */ |
||
321 | public function hasAttachments() |
||
325 | |||
326 | protected function addBodyIfPresent($body, $encoding) |
||
327 | { |
||
328 | $rawMessage = ''; |
||
329 | if($body !== false && strlen($body) > 0) |
||
330 | { |
||
331 | $rawMessage .= "\n--alt-{$boundary}\n"; |
||
|
|||
332 | $rawMessage .= "Content-Type: $encoding\n\n"; |
||
333 | $rawMessage .= $body."\n"; |
||
334 | } |
||
335 | return $rawMessage; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Serialize the message to a raw MIME encoded format suitable for sending over SMTP |
||
340 | * |
||
341 | * @return string A text version of the message suitable for sending over SMTP |
||
342 | */ |
||
343 | public function getRawMessage() |
||
344 | { |
||
345 | $boundary = uniqid(rand(), true); |
||
346 | $rawMessage = 'To: '.$this->encodeRecipients($this->getToAddresses())."\n"; |
||
347 | $from = $this->getFromAddress(); |
||
348 | if($from === false) |
||
349 | { |
||
350 | throw new \Exception('Message must have a from address'); |
||
351 | } |
||
352 | $rawMessage .= 'From: '.$this->encodeRecipients($from)."\n"; |
||
353 | if(!empty($this->cc)) |
||
354 | { |
||
355 | $rawMessage .= 'CC: '.$this->encodeRecipients($this->getCCAddresses())."\n"; |
||
356 | } |
||
357 | if(!empty($this->bcc)) |
||
358 | { |
||
359 | $rawMessage .= 'BCC: '.$this->encodeRecipients($this->getBCCAddresses())."\n"; |
||
360 | } |
||
361 | $rawMessage .= 'Subject: '.$this->getSubject()."\n"; |
||
362 | $rawMessage .= 'MIME-Version: 1.0'."\n"; |
||
363 | $rawMessage .= 'Content-type: Multipart/Mixed; boundary="'.$boundary.'"'."\n"; |
||
364 | $rawMessage .= "\n--{$boundary}\n"; |
||
365 | $rawMessage .= 'Content-type: Multipart/Alternative; boundary="alt-'.$boundary.'"'."\n"; |
||
366 | $rawMessage .= $this->addBodyIfPresent($this->getTextBody(), 'text/plain'); |
||
367 | $rawMessage .= $this->addBodyIfPresent($this->getHTMLBody(), 'text/html; charset="UTF-8"'); |
||
368 | $rawMessage .= "\n--alt-{$boundary}--\n"; |
||
369 | foreach($this->attachments as $attachment) |
||
370 | { |
||
371 | $rawMessage .= "\n--{$boundary}\n"; |
||
372 | $rawMessage .= 'Content-Type: '.$attachment['mimeType'].'; name="'.$attachment['name']."\"\n"; |
||
373 | $rawMessage .= 'Content-Disposition: attachment'."\n"; |
||
374 | $rawMessage .= 'Content-Transfer-Encoding: base64'."\n\n"; |
||
375 | $rawMessage .= chunk_split(base64_encode($attachment['data']), 76, "\n")."\n"; |
||
376 | } |
||
377 | $rawMessage .= "\n--{$boundary}--\n"; |
||
378 | return $rawMessage; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Serialize a recipient so that it can be sent over SMTP |
||
383 | * |
||
384 | * @param string $recipient The recipient in the format 'name <email@address>' |
||
385 | * |
||
386 | * @return string A text version of the recipient name and address suitable for sending over SMTP |
||
387 | */ |
||
388 | public function encodeRecipients($recipient) |
||
400 | } |
||
401 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
402 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.