Complex classes like MailReader 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 MailReader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class MailReader |
||
6 | { |
||
7 | |||
8 | /** |
||
9 | * @var array |
||
10 | */ |
||
11 | private $settings = ['server' => '', 'username' => '', 'password' => '']; |
||
12 | |||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | private $messages = []; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $mailbox = 'INBOX'; |
||
22 | |||
23 | /** |
||
24 | * @var resource |
||
25 | */ |
||
26 | private $conn; |
||
27 | |||
28 | |||
29 | /** |
||
30 | * When this class is destructed we |
||
31 | * might want to close the connection. |
||
32 | */ |
||
33 | public function __destruct() |
||
37 | |||
38 | |||
39 | /** |
||
40 | * Open a connection to a mail server. |
||
41 | * |
||
42 | * @param array $credentials |
||
43 | * |
||
44 | * @return bool |
||
45 | * @throws \Exception |
||
46 | */ |
||
47 | public function connect($credentials = []) |
||
48 | { |
||
49 | if ($diff = array_diff(array_keys($this->settings), array_keys($credentials))) { |
||
50 | throw new \Exception("Missing credentials, the following fields are missing ".implode('/', $diff)); |
||
51 | } |
||
52 | |||
53 | $this->settings = array_merge($this->settings, $credentials); |
||
54 | |||
55 | if (isset($this->settings['port']) === false) { |
||
56 | $this->settings['port'] = 143; |
||
57 | } |
||
58 | |||
59 | $this->conn = @imap_open('{'.$this->settings['server'].':'.$this->settings['port'].'/notls}', |
||
60 | $this->settings['username'], $this->settings['password']); |
||
61 | |||
62 | if ($this->conn == false) { |
||
63 | throw new \Exception("Could not connect or authorize to ".$this->settings['server'].':'.$this->settings['port']); |
||
64 | } |
||
65 | |||
66 | return ($this->conn != null); |
||
67 | } |
||
68 | |||
69 | |||
70 | /** |
||
71 | * Close the connection to the mail server. |
||
72 | */ |
||
73 | private function close() |
||
74 | { |
||
75 | if ($this->conn) { |
||
76 | imap_close($this->conn); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | |||
81 | /** |
||
82 | * @param string $mailbox |
||
83 | * |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function setMailbox($mailbox = 'INBOX') |
||
87 | { |
||
88 | |||
89 | if ($mailbox == 'INBOX') { |
||
90 | $mailbox = "{".$this->settings['server']."}INBOX"; |
||
91 | } else { |
||
92 | $mailbox = "{".$this->settings['server']."}INBOX.".imap_utf7_encode($mailbox); |
||
93 | } |
||
94 | |||
95 | $result = false; |
||
96 | |||
97 | if ($this->conn) { |
||
98 | $result = imap_reopen($this->conn, $mailbox); |
||
99 | $this->mailbox = $mailbox; |
||
100 | } |
||
101 | |||
102 | return $result; |
||
103 | } |
||
104 | |||
105 | |||
106 | /** |
||
107 | * Return the name of the current mailbox. |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getMailbox() |
||
112 | { |
||
113 | $mailbox = str_replace("{".$this->settings['server']."}", '', $this->mailbox); |
||
114 | $mailbox = (substr($mailbox, 0, 6) == 'INBOX.') ? substr($mailbox, -6) : $mailbox; |
||
115 | |||
116 | return $mailbox; |
||
117 | } |
||
118 | |||
119 | |||
120 | /** |
||
121 | * Mark a given message as read. |
||
122 | * |
||
123 | * @param string $index |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | public function markMessageAsRead($index = '') |
||
128 | { |
||
129 | return imap_setflag_full($this->conn, $index, "\\Seen"); |
||
130 | } |
||
131 | |||
132 | |||
133 | /** |
||
134 | * Filter unread message sent to $to |
||
135 | * |
||
136 | * @param string $to |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | public function filterUnReadMessagesTo($to = '') |
||
141 | { |
||
142 | $filteredResult = $this->filterTo($to); |
||
143 | $filteredMessages = []; |
||
144 | |||
145 | if (is_array($filteredResult) && count($filteredResult) > 0) { |
||
146 | foreach ($filteredResult as $message) { |
||
147 | if (isset($message['header'])) { |
||
148 | $header = $message['header']; |
||
149 | if ($header->Unseen == 'U') { |
||
150 | $filteredMessages[] = $message; |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | |||
156 | return $filteredMessages; |
||
157 | } |
||
158 | |||
159 | |||
160 | /** |
||
161 | * Filter message sent to $to |
||
162 | * |
||
163 | * @param string $to |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | public function filterTo($to = '') |
||
185 | |||
186 | |||
187 | /** |
||
188 | * Create a mailbox (folder) |
||
189 | * |
||
190 | * @param string $name |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function createMailbox($name = '') |
||
204 | |||
205 | |||
206 | /** |
||
207 | * Subscribe to a mailbox. |
||
208 | * |
||
209 | * @param string $name |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | public function subscribeMailbox($name = '') { |
||
221 | |||
222 | /** |
||
223 | * This function will be deleted in version 1.3 |
||
224 | * This function is deprecated because of the naming |
||
225 | * inconsistency. |
||
226 | * |
||
227 | * @param string $name |
||
228 | * |
||
229 | * @deprecated |
||
230 | * @return bool |
||
231 | */ |
||
232 | public function removeMailbox($name = '') |
||
236 | |||
237 | |||
238 | /** |
||
239 | * Delete a mailbox (folder) |
||
240 | * |
||
241 | * @param string $name |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | public function deleteMailbox($name = '') |
||
255 | |||
256 | |||
257 | |||
258 | |||
259 | /** |
||
260 | * Check to see if a given mailbox (folder) exists on the server. |
||
261 | * |
||
262 | * @param string $name |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | public function mailboxExists($name = '') |
||
278 | |||
279 | |||
280 | /** |
||
281 | * Return all mailboxes (folders) on the server. |
||
282 | * |
||
283 | * @return array |
||
284 | */ |
||
285 | public function getMailboxes() |
||
289 | |||
290 | |||
291 | /** |
||
292 | * Return all mailboxes (folders) on the server. This function removes the |
||
293 | * mailbox prefixes like {server}INBOX.<name here> |
||
294 | * |
||
295 | * @return array |
||
296 | */ |
||
297 | public function getPrettyMailboxes() |
||
316 | |||
317 | |||
318 | /** |
||
319 | * Rename a mailbox (folder) |
||
320 | * |
||
321 | * @param string $from |
||
322 | * @param string $to |
||
323 | * |
||
324 | * @return bool |
||
325 | */ |
||
326 | public function renameMailbox($from = '', $to = '') |
||
338 | |||
339 | |||
340 | /** |
||
341 | * Move a given message at $index to a given mailbox (folder). |
||
342 | * |
||
343 | * @param int $index |
||
344 | * @param string $to |
||
345 | * |
||
346 | * @return bool |
||
347 | */ |
||
348 | public function moveMessage($index = -1, $to = '') |
||
375 | |||
376 | |||
377 | |||
378 | /** |
||
379 | * Delete a given email message. The param $index |
||
380 | * is bases of the [index] field on an email array. |
||
381 | * |
||
382 | * @param int $index |
||
383 | * |
||
384 | * @return bool |
||
385 | */ |
||
386 | public function deleteMessage($index = -1) |
||
399 | |||
400 | |||
401 | /** |
||
402 | * Return message information without reading it from the server |
||
403 | * |
||
404 | * @param string $id |
||
405 | * |
||
406 | * @return mixed |
||
407 | */ |
||
408 | public function getMessageInformation($id = '') |
||
420 | |||
421 | /** |
||
422 | * Return a message based on its index in the mailbox. |
||
423 | * |
||
424 | * @param string $id |
||
425 | * |
||
426 | * @return mixed |
||
427 | */ |
||
428 | public function getMessage($id = '') |
||
447 | |||
448 | |||
449 | /** |
||
450 | * Retrieve a list of message in the mailbox. |
||
451 | * |
||
452 | * @return array |
||
453 | */ |
||
454 | public function readMailbox() |
||
470 | } |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.