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 BounceMailHandler 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 BounceMailHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class BounceMailHandler |
||
32 | { |
||
33 | const SECONDS_TIMEOUT = 6000; |
||
34 | |||
35 | const VERBOSE_DEBUG = 3; // detailed report plus debug info |
||
36 | |||
37 | const VERBOSE_QUIET = 0; // suppress output |
||
38 | |||
39 | const VERBOSE_REPORT = 2; // detailed report |
||
40 | |||
41 | const VERBOSE_SIMPLE = 1; // simple report |
||
42 | |||
43 | /** |
||
44 | * mail-server |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | public $mailhost = 'localhost'; |
||
49 | |||
50 | /** |
||
51 | * the username of mailbox |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | public $mailboxUserName = ''; |
||
56 | |||
57 | /** |
||
58 | * the password needed to access mailbox |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | public $mailboxPassword = ''; |
||
63 | |||
64 | /** |
||
65 | * the last error msg |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | public $errorMessage = ''; |
||
70 | |||
71 | /** |
||
72 | * maximum limit messages processed in one batch |
||
73 | * |
||
74 | * @var int |
||
75 | */ |
||
76 | public $maxMessages = 3000; |
||
77 | |||
78 | /** |
||
79 | * callback Action function name the function that handles the bounce mail. Parameters: |
||
80 | * |
||
81 | * int $msgnum the message number returned by Bounce Mail Handler |
||
82 | * string $bounce_type the bounce type: |
||
83 | * 'antispam', |
||
84 | * 'autoreply', |
||
85 | * 'concurrent', |
||
86 | * 'content_reject', |
||
87 | * 'command_reject', |
||
88 | * 'internal_error', |
||
89 | * 'defer', |
||
90 | * 'delayed' |
||
91 | * => |
||
92 | * array( |
||
93 | * 'remove' => 0, |
||
94 | * 'bounce_type' => 'temporary' |
||
95 | * ), |
||
96 | * 'dns_loop', |
||
97 | * 'dns_unknown', |
||
98 | * 'full', |
||
99 | * 'inactive', |
||
100 | * 'latin_only', |
||
101 | * 'other', |
||
102 | * 'oversize', |
||
103 | * 'outofoffice', |
||
104 | * 'unknown', |
||
105 | * 'unrecognized', |
||
106 | * 'user_reject', |
||
107 | * 'warning' |
||
108 | * string $email the target email address |
||
109 | * string $subject the subject, ignore now |
||
110 | * string $xheader the XBounceHeader from the mail |
||
111 | * 1 or 0 $remove delete status, 0 is not deleted, 1 is deleted |
||
112 | * string $rule_no bounce mail detect rule no. |
||
113 | * string $rule_cat bounce mail detect rule category |
||
114 | * int $totalFetched total number of messages in the mailbox |
||
115 | * |
||
116 | * @var mixed |
||
117 | */ |
||
118 | public $actionFunction = 'callbackAction'; |
||
119 | |||
120 | /** |
||
121 | * Callback custom body rules |
||
122 | * ``` |
||
123 | * function customBodyRulesCallback( $result, $body, $structure, $debug ) |
||
124 | * { |
||
125 | * return $result; |
||
126 | * } |
||
127 | * ``` |
||
128 | * |
||
129 | * @var callable|null |
||
130 | */ |
||
131 | public $customBodyRulesCallback; |
||
132 | |||
133 | /** |
||
134 | * Callback custom DSN (Delivery Status Notification) rules |
||
135 | * ``` |
||
136 | * function customDSNRulesCallback( $result, $dsnMsg, $dsnReport, $debug ) |
||
137 | * { |
||
138 | * return $result; |
||
139 | * } |
||
140 | * ``` |
||
141 | * |
||
142 | * @var callable|null |
||
143 | */ |
||
144 | public $customDSNRulesCallback; |
||
145 | |||
146 | /** |
||
147 | * test-mode, if true will not delete messages |
||
148 | * |
||
149 | * @var bool |
||
150 | */ |
||
151 | public $testMode = false; |
||
152 | |||
153 | /** |
||
154 | * purge the unknown messages (or not) |
||
155 | * |
||
156 | * @var bool |
||
157 | */ |
||
158 | public $purgeUnprocessed = false; |
||
159 | |||
160 | /** |
||
161 | * control the debug output, default is VERBOSE_SIMPLE |
||
162 | * |
||
163 | * @var int |
||
164 | */ |
||
165 | public $verbose = self::VERBOSE_SIMPLE; |
||
166 | |||
167 | /** |
||
168 | * control the failed DSN rules output |
||
169 | * |
||
170 | * @var bool |
||
171 | */ |
||
172 | public $debugDsnRule = false; |
||
173 | |||
174 | /** |
||
175 | * control the failed BODY rules output |
||
176 | * |
||
177 | * @var bool |
||
178 | */ |
||
179 | public $debugBodyRule = false; |
||
180 | |||
181 | /** |
||
182 | * Control the method to process the mail header |
||
183 | * if set true, uses the imap_fetchstructure function |
||
184 | * otherwise, detect message type directly from headers, |
||
185 | * a bit faster than imap_fetchstructure function and take less resources. |
||
186 | * |
||
187 | * however - the difference is negligible |
||
188 | * |
||
189 | * @var bool |
||
190 | */ |
||
191 | public $useFetchstructure = true; |
||
192 | |||
193 | /** |
||
194 | * If disableDelete is equal to true, it will disable the delete function. |
||
195 | * |
||
196 | * @var bool |
||
197 | */ |
||
198 | public $disableDelete = false; |
||
199 | |||
200 | /** |
||
201 | * defines new line ending |
||
202 | * |
||
203 | * @var string |
||
204 | */ |
||
205 | public $bmhNewLine = "<br />\n"; |
||
206 | |||
207 | /** |
||
208 | * defines port number, default is '143', other common choices are '110' (pop3), '993' (gmail) |
||
209 | * |
||
210 | * @var int |
||
211 | */ |
||
212 | public $port = 143; |
||
213 | |||
214 | /** |
||
215 | * defines service, default is 'imap', choice includes 'pop3' |
||
216 | * |
||
217 | * @var string |
||
218 | */ |
||
219 | public $service = 'imap'; |
||
220 | |||
221 | /** |
||
222 | * defines service option, default is 'notls', other choices are 'tls', 'ssl' |
||
223 | * |
||
224 | * @var string |
||
225 | */ |
||
226 | public $serviceOption = 'notls'; |
||
227 | |||
228 | /** |
||
229 | * mailbox type, default is 'INBOX', other choices are (Tasks, Spam, Replies, etc.) |
||
230 | * |
||
231 | * @var string |
||
232 | */ |
||
233 | public $boxname = 'INBOX'; |
||
234 | |||
235 | /** |
||
236 | * determines if soft bounces will be moved to another mailbox folder |
||
237 | * |
||
238 | * @var bool |
||
239 | */ |
||
240 | public $moveSoft = false; |
||
241 | |||
242 | /** |
||
243 | * mailbox folder to move soft bounces to, default is 'soft' |
||
244 | * |
||
245 | * @var string |
||
246 | */ |
||
247 | public $softMailbox = 'INBOX.soft'; |
||
248 | |||
249 | /** |
||
250 | * determines if hard bounces will be moved to another mailbox folder |
||
251 | * |
||
252 | * NOTE: If true, this will disable delete and perform a move operation instead |
||
253 | * |
||
254 | * @var bool |
||
255 | */ |
||
256 | public $moveHard = false; |
||
257 | |||
258 | /** |
||
259 | * mailbox folder to move hard bounces to, default is 'hard' |
||
260 | * |
||
261 | * @var string |
||
262 | */ |
||
263 | public $hardMailbox = 'INBOX.hard'; |
||
264 | |||
265 | /* |
||
266 | * Mailbox folder to move unprocessed mails |
||
267 | * @var string |
||
268 | */ |
||
269 | public $unprocessedBox = 'INBOX.unprocessed'; |
||
270 | |||
271 | /** |
||
272 | * deletes messages globally prior to date in variable |
||
273 | * |
||
274 | * NOTE: excludes any message folder that includes 'sent' in mailbox name |
||
275 | * format is same as MySQL: 'yyyy-mm-dd' |
||
276 | * if variable is blank, will not process global delete |
||
277 | * |
||
278 | * @var string |
||
279 | */ |
||
280 | public $deleteMsgDate = ''; |
||
281 | |||
282 | /** |
||
283 | * (internal variable) |
||
284 | * |
||
285 | * The resource handler for the opened mailbox (POP3/IMAP/NNTP/etc.) |
||
286 | * |
||
287 | * @var resource |
||
288 | */ |
||
289 | protected $mailboxLink = false; |
||
290 | |||
291 | /** |
||
292 | * Holds Bounce Mail Handler version. |
||
293 | * |
||
294 | * @var string |
||
295 | */ |
||
296 | private $version = '6.0-dev'; |
||
297 | |||
298 | /** |
||
299 | * @return string |
||
300 | */ |
||
301 | public function getVersion(): string |
||
305 | |||
306 | /** |
||
307 | * Function to delete messages in a mailbox, based on date |
||
308 | 1 | * |
|
309 | * NOTE: this is global ... will affect all mailboxes except any that have 'sent' in the mailbox name |
||
310 | */ |
||
311 | 1 | public function globalDelete(): bool |
|
370 | |||
371 | /** |
||
372 | * Function to determine if a particular value is found in a imap_fetchstructure key. |
||
373 | * |
||
374 | * @param array $currParameters imap_fetstructure parameters |
||
375 | * @param string $varKey imap_fetstructure key |
||
376 | * @param string $varValue value to check for |
||
377 | * |
||
378 | * @return bool |
||
379 | */ |
||
380 | public function isParameter(array $currParameters, string $varKey, string $varValue): bool |
||
394 | |||
395 | /** |
||
396 | * Function to check if a mailbox exists - if not found, it will create it. |
||
397 | * |
||
398 | * @param string $mailbox the mailbox name, must be in 'INBOX.checkmailbox' format |
||
399 | * @param bool $create whether or not to create the checkmailbox if not found, defaults to true |
||
400 | * |
||
401 | * @return bool |
||
402 | */ |
||
403 | public function mailboxExist(string $mailbox, bool $create = true): bool |
||
462 | |||
463 | 3 | /** |
|
464 | * open a mail box in local file system |
||
465 | * |
||
466 | * @param string $filePath The local mailbox file path |
||
467 | 3 | * |
|
468 | * @return bool |
||
469 | */ |
||
470 | public function openLocal(string $filePath): bool |
||
491 | 1 | ||
492 | 1 | /** |
|
493 | * open a mail box |
||
494 | * |
||
495 | 1 | * @return bool |
|
496 | */ |
||
497 | public function openMailbox(): bool |
||
532 | |||
533 | /** |
||
534 | * output additional msg for debug |
||
535 | 3 | * |
|
536 | * @param mixed $msg if not given, output the last error msg |
||
537 | 3 | * @param int $verboseLevel the output level of this message |
|
538 | */ |
||
539 | public function output($msg = '', int $verboseLevel = self::VERBOSE_SIMPLE) |
||
549 | |||
550 | /** |
||
551 | * Function to process each individual message. |
||
552 | * |
||
553 | * @param int $pos message number |
||
554 | * @param string $type DNS or BODY type |
||
555 | * @param int $totalFetched total number of messages in mailbox |
||
556 | * |
||
557 | * @return array|false <p>"$result"-array or false</p> |
||
558 | */ |
||
559 | public function processBounce(int $pos, string $type, int $totalFetched) |
||
732 | |||
733 | 1 | /** |
|
734 | 1 | * process the messages in a mailbox |
|
735 | 1 | * |
|
736 | * @param bool|int $max $max maximum limit messages processed in one batch, |
||
737 | * if not given uses the property $maxMessages |
||
738 | * |
||
739 | 1 | * @return bool |
|
740 | 1 | */ |
|
741 | 1 | public function processMailbox($max = false): bool |
|
946 | } |
||
947 |
If you suppress an error, we recommend checking for the error condition explicitly: