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 = []) |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * Close the connection to the mail server. |
||
| 72 | */ |
||
| 73 | private function close() |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $mailbox |
||
| 83 | * |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | public function setMailbox($mailbox = 'INBOX') |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Return the name of the current mailbox. |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function getMailbox() |
||
| 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 = '') |
||
| 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 = '') |
||
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * Filter message sent to $to |
||
| 160 | * |
||
| 161 | * @param string $to |
||
| 162 | * |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | public function filterTo($to = '') |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * Create a mailbox (folder) |
||
| 187 | * |
||
| 188 | * @param string $name |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | public function createMailbox($name = '') |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * Remove a mailbox (folder) |
||
| 206 | * |
||
| 207 | * @param string $name |
||
| 208 | * |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | public function removeMailbox($name = '') |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * Check to see if a given mailbox (folder) exists on the server. |
||
| 225 | * |
||
| 226 | * @param string $name |
||
| 227 | * |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public function mailboxExists($name = '') |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * Return all mailboxes (folders) on the server. |
||
| 246 | * |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | public function getMailboxes() |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * Return all mailboxes (folders) on the server. This function removes the |
||
| 257 | * mailbox prefixes like {server}INBOX.<name here> |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | public function getPrettyMailboxes() |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * Rename a mailbox (folder) |
||
| 284 | * |
||
| 285 | * @param string $from |
||
| 286 | * @param string $to |
||
| 287 | * |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | public function renameMailbox($from = '', $to = '') |
||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * Move a given message at $index to a given mailbox (folder). |
||
| 306 | * |
||
| 307 | * @param int $index |
||
| 308 | * @param string $to |
||
| 309 | * |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | public function moveMessage($index = -1, $to = '') |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * Delete a given email message. The param $index |
||
| 331 | * is bases of the [index] field on an email array. |
||
| 332 | * |
||
| 333 | * @param int $index |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | public function deleteMessage($index = -1) |
||
| 350 | |||
| 351 | |||
| 352 | /** |
||
| 353 | * Return a message based on its index in the mailbox. |
||
| 354 | * |
||
| 355 | * @param string $id |
||
| 356 | * |
||
| 357 | * @return mixed |
||
| 358 | */ |
||
| 359 | public function getMessage($id = '') |
||
| 369 | |||
| 370 | |||
| 371 | /** |
||
| 372 | * Retrieve a list of message in the mailbox. |
||
| 373 | * |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | public function readMailbox() |
||
| 392 | } |