Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Archangel 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 Archangel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Archangel |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * These variables are set with setter methods below |
||
20 | */ |
||
21 | private $subject; |
||
22 | private $reply_to; |
||
|
|||
23 | private $plain_message; |
||
24 | private $html_message; |
||
25 | |||
26 | /** |
||
27 | * Holder for error handling and validation |
||
28 | */ |
||
29 | private $passed_validation = TRUE; |
||
30 | private $validation_error = array(); |
||
31 | |||
32 | /** |
||
33 | * Holders for some of the more list-y variable handling |
||
34 | */ |
||
35 | private $to_array = array(); |
||
36 | private $cc_array = array(); |
||
37 | private $bcc_array = array(); |
||
38 | private $header_array = array(); |
||
39 | private $attachment_array = array(); |
||
40 | |||
41 | /** |
||
42 | * Static pieces that really don't need to change |
||
43 | */ |
||
44 | private static $MAILER = 'PHP/%s'; |
||
45 | private static $LINE_BREAK = "\r\n"; |
||
46 | private static $BOUNDARY_FORMAT = 'PHP-mixed-%s'; |
||
47 | private static $BOUNDARY_SALT = 'Boundary Salt'; |
||
48 | private static $ALTERNATIVE_BOUNDARY_FORMAT = 'PHP-alternative-%s'; |
||
49 | private static $ALTERNATIVE_BOUNDARY_SALT = 'Alternative Boundary Salt'; |
||
50 | |||
51 | /** |
||
52 | * Standard constructor, sets some of the base (unchanging) fields |
||
53 | */ |
||
54 | public function __construct() |
||
58 | |||
59 | /** |
||
60 | * Setter method for adding recipients |
||
61 | * This class only sends a single email, so all recipients can see each other |
||
62 | * |
||
63 | * @param string $address email address for the recipient |
||
64 | * @param string $title name of the recipient (optional) |
||
65 | * @return object instantiated $this |
||
66 | */ |
||
67 | View Code Duplication | public function addTo($address, $title = '') |
|
74 | |||
75 | /** |
||
76 | * Setter method for adding cc recipients |
||
77 | * |
||
78 | * @param string $address email address for the cc recipient |
||
79 | * @param string $title name of the cc recipient (optional) |
||
80 | * @return object instantiated $this |
||
81 | */ |
||
82 | View Code Duplication | public function addCC($address, $title = '') |
|
89 | |||
90 | /** |
||
91 | * Setter method for adding bcc recipients |
||
92 | * |
||
93 | * @param string $address email address for the bcc recipient |
||
94 | * @param string $title name of the bcc recipient (optional) |
||
95 | * @return object instantiated $this |
||
96 | */ |
||
97 | View Code Duplication | public function addBCC($address, $title = '') |
|
104 | |||
105 | /** |
||
106 | * Setter method for setting the single 'from' field |
||
107 | * |
||
108 | * @param string $address email address for the sender |
||
109 | * @param string $title name of the sender (optional) |
||
110 | * @return object instantiated $this |
||
111 | */ |
||
112 | View Code Duplication | public function setFrom($address, $title = '') |
|
119 | |||
120 | /** |
||
121 | * Setter method for setting the single 'reply-to' field |
||
122 | * |
||
123 | * @param string $address email address for the reply-to |
||
124 | * @param string $title name of the reply-to (optional) |
||
125 | * @return object instantiated $this |
||
126 | */ |
||
127 | View Code Duplication | public function setReplyTo($address, $title = '') |
|
134 | |||
135 | /** |
||
136 | * Setter method for setting a subject |
||
137 | * |
||
138 | * @param string $subject subject for the email |
||
139 | * @return object instantiated $this |
||
140 | */ |
||
141 | public function setSubject($subject) |
||
148 | |||
149 | /** |
||
150 | * Setter method for the plain text message |
||
151 | * |
||
152 | * @param string $message the plain-text message |
||
153 | * @return object insantiated $this |
||
154 | */ |
||
155 | public function setPlainMessage($message) |
||
161 | |||
162 | /** |
||
163 | * Setter method for the html message |
||
164 | * |
||
165 | * @param string $message the html message |
||
166 | * @return object insantiated $this |
||
167 | */ |
||
168 | public function setHTMLMessage($message) |
||
174 | |||
175 | /** |
||
176 | * Setter method for adding attachments |
||
177 | * |
||
178 | * @param string $path the full path of the attachment |
||
179 | * @param string $type mime type of the file |
||
180 | * @param string $title the title of the attachment (optional) |
||
181 | * @return object insantiated $this |
||
182 | */ |
||
183 | public function addAttachment($path, $type, $title = '') |
||
192 | |||
193 | /** |
||
194 | * The executing step, the actual sending of the email |
||
195 | * First checks to make sure the minimum fields are set (returns false if they are not) |
||
196 | * Second it attempts to send the mail with php's mail() (returns false if it fails) |
||
197 | * |
||
198 | * return boolean whether or not the email was valid & sent |
||
199 | */ |
||
200 | public function send() |
||
215 | |||
216 | /** |
||
217 | * Main instantiator for the class |
||
218 | * |
||
219 | * @return object instantiated $this |
||
220 | */ |
||
221 | public static function instance() |
||
225 | |||
226 | /** |
||
227 | * Private call to check the minimum required fields |
||
228 | * |
||
229 | * @return boolean whether or not the email meets the minimum required fields |
||
230 | */ |
||
231 | private function check_required_fields() |
||
241 | |||
242 | /** |
||
243 | * Private function to collect the recipients from to_array |
||
244 | * |
||
245 | * @return string comma-separated lit of recipients |
||
246 | */ |
||
247 | private function get_to() |
||
251 | |||
252 | /** |
||
253 | * Long, nasty creater of the actual message, with all the multipart logic you'd never want to see |
||
254 | * |
||
255 | * @return string email message |
||
256 | */ |
||
257 | private function get_message() |
||
326 | |||
327 | /** |
||
328 | * Private holder for the boundry logic |
||
329 | * Not called/created unless it's needed |
||
330 | * |
||
331 | * @return string boundary |
||
332 | */ |
||
333 | private $boundary; |
||
334 | private function get_boundary() |
||
340 | |||
341 | /** |
||
342 | * Private holder for the alternative boundry logic |
||
343 | * Not called/created unless it's needed |
||
344 | * |
||
345 | * @return string alternative boundary |
||
346 | */ |
||
347 | private $alternative_boundary; |
||
348 | private function get_alternative_boundary() |
||
354 | |||
355 | /** |
||
356 | * Fetcher for the additional headers needed for multipart emails |
||
357 | * |
||
358 | * @return string headers needed for multipart |
||
359 | */ |
||
360 | private function get_additional_headers() |
||
386 | |||
387 | /** |
||
388 | * File reader for attachments |
||
389 | * |
||
390 | * @return string binary representation of file, base64'd |
||
391 | */ |
||
392 | private function get_attachment_content($attachment) |
||
402 | |||
403 | /** |
||
404 | * stub for email address checking |
||
405 | */ |
||
406 | private function is_valid_email_address($string) |
||
413 | |||
414 | /** |
||
415 | * stub for email title checking |
||
416 | */ |
||
417 | private function is_valid_email_title($string) |
||
421 | |||
422 | /** |
||
423 | * stub for subject checking |
||
424 | */ |
||
425 | private function is_valid_subject($string) |
||
432 | |||
433 | /** |
||
434 | * holder for all validation fails |
||
435 | */ |
||
436 | private function fail_validation($message) |
||
443 | |||
444 | public function get_validation_errors() |
||
448 | |||
449 | } |
||
450 |
This check marks private properties in classes that are never used. Those properties can be removed.