Complex classes like Mailbox 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 Mailbox, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class Mailbox implements IMailBox { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var Horde_Imap_Client_Socket |
||
| 47 | */ |
||
| 48 | protected $conn; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $attributes; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | private $specialRole; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | private $displayName; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $delimiter; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var Horde_Imap_Client_Mailbox |
||
| 72 | */ |
||
| 73 | protected $mailBox; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param Horde_Imap_Client_Socket $conn |
||
| 77 | * @param Horde_Imap_Client_Mailbox $mailBox |
||
| 78 | * @param array $attributes |
||
| 79 | * @param string $delimiter |
||
| 80 | */ |
||
| 81 | 12 | public function __construct($conn, $mailBox, $attributes, $delimiter='/') { |
|
| 82 | 12 | $this->conn = $conn; |
|
| 83 | 12 | $this->mailBox = $mailBox; |
|
| 84 | 12 | $this->attributes = $attributes; |
|
| 85 | 12 | $this->delimiter = $delimiter; |
|
| 86 | 12 | $this->getSpecialRoleFromAttributes(); |
|
| 87 | 12 | if ($this->specialRole === null) { |
|
| 88 | 12 | $this->guessSpecialRole(); |
|
| 89 | 12 | } |
|
| 90 | 12 | $this->makeDisplayName(); |
|
| 91 | 12 | } |
|
| 92 | |||
| 93 | 6 | private function getSearchIds($from, $count, $filter) { |
|
| 94 | 6 | if ($filter instanceof Horde_Imap_Client_Search_Query) { |
|
|
|
|||
| 95 | 3 | $query = $filter; |
|
| 96 | 3 | } else { |
|
| 97 | 3 | $query = new Horde_Imap_Client_Search_Query(); |
|
| 98 | 3 | if ($filter) { |
|
| 99 | $query->text($filter, false); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | 6 | if ($this->getSpecialRole() !== 'trash') { |
|
| 103 | 6 | $query->flag(Horde_Imap_Client::FLAG_DELETED, false); |
|
| 104 | 6 | } |
|
| 105 | 6 | $result = $this->conn->search($this->mailBox, $query, ['sort' => [Horde_Imap_Client::SORT_DATE]]); |
|
| 106 | 6 | $ids = array_reverse($result['match']->ids); |
|
| 107 | 6 | if ($from >= 0 && $count >= 0) { |
|
| 108 | 3 | $ids = array_slice($ids, $from, $count); |
|
| 109 | 3 | } |
|
| 110 | 6 | return new \Horde_Imap_Client_Ids($ids, false); |
|
| 111 | } |
||
| 112 | |||
| 113 | private function getFetchIds($from, $count) { |
||
| 114 | $q = new Horde_Imap_Client_Fetch_Query(); |
||
| 115 | $q->uid(); |
||
| 116 | $q->imapDate(); |
||
| 117 | |||
| 118 | $result = $this->conn->fetch($this->mailBox, $q); |
||
| 119 | $uidMap = []; |
||
| 120 | foreach ($result as $r) { |
||
| 121 | $uidMap[$r->getUid()] = $r->getImapDate()->getTimeStamp(); |
||
| 122 | } |
||
| 123 | // sort by time |
||
| 124 | uasort($uidMap, function($a, $b) { |
||
| 125 | return $a < $b; |
||
| 126 | }); |
||
| 127 | if ($from >= 0 && $count >= 0) { |
||
| 128 | $uidMap = array_slice($uidMap, $from, $count, true); |
||
| 129 | } |
||
| 130 | return new \Horde_Imap_Client_Ids(array_keys($uidMap), false); |
||
| 131 | } |
||
| 132 | |||
| 133 | 6 | public function getMessages($from = 0, $count = 2, $filter = '') { |
|
| 134 | 6 | if (!$this->conn->capability->query('SORT') && (is_null($filter) || $filter === '')) { |
|
| 135 | $ids = $this->getFetchIds($from, $count); |
||
| 136 | } else { |
||
| 137 | 6 | $ids = $this->getSearchIds($from, $count, $filter); |
|
| 138 | } |
||
| 139 | |||
| 140 | 6 | $headers = []; |
|
| 141 | |||
| 142 | 6 | $fetch_query = new Horde_Imap_Client_Fetch_Query(); |
|
| 143 | 6 | $fetch_query->envelope(); |
|
| 144 | 6 | $fetch_query->flags(); |
|
| 145 | 6 | $fetch_query->size(); |
|
| 146 | 6 | $fetch_query->uid(); |
|
| 147 | 6 | $fetch_query->imapDate(); |
|
| 148 | 6 | $fetch_query->structure(); |
|
| 149 | |||
| 150 | 6 | $headers = array_merge($headers, [ |
|
| 151 | 6 | 'importance', |
|
| 152 | 6 | 'list-post', |
|
| 153 | 'x-priority' |
||
| 154 | 6 | ]); |
|
| 155 | 6 | $headers[] = 'content-type'; |
|
| 156 | |||
| 157 | 6 | $fetch_query->headers('imp', $headers, [ |
|
| 158 | 6 | 'cache' => true, |
|
| 159 | 'peek' => true |
||
| 160 | 6 | ]); |
|
| 161 | |||
| 162 | 6 | $options = ['ids' => $ids]; |
|
| 163 | // $list is an array of Horde_Imap_Client_Data_Fetch objects. |
||
| 164 | 6 | $headers = $this->conn->fetch($this->mailBox, $fetch_query, $options); |
|
| 165 | |||
| 166 | 6 | ob_start(); // fix for Horde warnings |
|
| 167 | 6 | $messages = []; |
|
| 168 | 6 | foreach ($headers->ids() as $message_id) { |
|
| 169 | 6 | $header = $headers[$message_id]; |
|
| 170 | 6 | $message = new IMAPMessage($this->conn, $this->mailBox, $message_id, $header); |
|
| 171 | 6 | $messages[] = $message->getListArray(); |
|
| 172 | 6 | } |
|
| 173 | 6 | ob_get_clean(); |
|
| 174 | |||
| 175 | // sort by time |
||
| 176 | usort($messages, function($a, $b) { |
||
| 177 | return $a['dateInt'] < $b['dateInt']; |
||
| 178 | 6 | }); |
|
| 179 | |||
| 180 | 6 | return $messages; |
|
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | 3 | public function attributes() { |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param string $messageId |
||
| 192 | * @param bool $loadHtmlMessageBody |
||
| 193 | * @return IMAPMessage |
||
| 194 | */ |
||
| 195 | public function getMessage($messageId, $loadHtmlMessageBody = false) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param int $flags |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | 12 | public function getStatus($flags = \Horde_Imap_Client::STATUS_ALL) { |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @return int |
||
| 209 | */ |
||
| 210 | 3 | public function getTotalMessages() { |
|
| 214 | |||
| 215 | 12 | protected function makeDisplayName() { |
|
| 228 | |||
| 229 | 13 | public function getFolderId() { |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | 6 | public function getParent() { |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | 9 | public function getSpecialRole() { |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | 6 | public function getDisplayName() { |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $displayName |
||
| 263 | */ |
||
| 264 | 6 | public function setDisplayName($displayName) { |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @param integer $accountId |
||
| 270 | * @return array |
||
| 271 | */ |
||
| 272 | 6 | public function getListArray($accountId, $status = null) { |
|
| 273 | 6 | $displayName = $this->getDisplayName(); |
|
| 274 | try { |
||
| 275 | 6 | if (is_null($status)) { |
|
| 276 | 3 | $status = $this->getStatus(); |
|
| 277 | 3 | } |
|
| 278 | 6 | $total = $status['messages']; |
|
| 279 | 6 | $specialRole = $this->getSpecialRole(); |
|
| 280 | 6 | $unseen = ($specialRole === 'trash') ? 0 : $status['unseen']; |
|
| 281 | 6 | $isEmpty = ($total === 0); |
|
| 282 | 6 | $noSelect = in_array('\\noselect', $this->attributes); |
|
| 283 | 6 | $parentId = $this->getParent(); |
|
| 284 | 6 | $parentId = ($parentId !== null) ? base64_encode($parentId) : null; |
|
| 285 | return [ |
||
| 286 | 6 | 'id' => base64_encode($this->getFolderId()), |
|
| 287 | 6 | 'parent' => $parentId, |
|
| 288 | 6 | 'name' => $displayName, |
|
| 289 | 6 | 'specialRole' => $specialRole, |
|
| 290 | 6 | 'unseen' => $unseen, |
|
| 291 | 6 | 'total' => $total, |
|
| 292 | 6 | 'isEmpty' => $isEmpty, |
|
| 293 | 6 | 'accountId' => $accountId, |
|
| 294 | 6 | 'noSelect' => $noSelect, |
|
| 295 | 6 | 'uidvalidity' => $status['uidvalidity'], |
|
| 296 | 6 | 'uidnext' => $status['uidnext'], |
|
| 297 | 6 | 'delimiter' => $this->delimiter |
|
| 298 | 6 | ]; |
|
| 299 | } catch (Horde_Imap_Client_Exception $e) { |
||
| 300 | return [ |
||
| 301 | 'id' => base64_encode($this->getFolderId()), |
||
| 302 | 'parent' => null, |
||
| 303 | 'name' => $displayName, |
||
| 304 | 'specialRole' => null, |
||
| 305 | 'unseen' => 0, |
||
| 306 | 'total' => 0, |
||
| 307 | 'error' => $e->getMessage(), |
||
| 308 | 'isEmpty' => true, |
||
| 309 | 'accountId' => $accountId, |
||
| 310 | 'noSelect' => true |
||
| 311 | ]; |
||
| 312 | } |
||
| 313 | } |
||
| 314 | /** |
||
| 315 | * Get the special use role of the mailbox |
||
| 316 | * |
||
| 317 | * This method reads the attributes sent by the server |
||
| 318 | * |
||
| 319 | */ |
||
| 320 | 12 | protected function getSpecialRoleFromAttributes() { |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Assign a special role to this mailbox based on its name |
||
| 360 | */ |
||
| 361 | 12 | protected function guessSpecialRole() { |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @param int $messageId |
||
| 387 | * @param string $attachmentId |
||
| 388 | * @return Attachment |
||
| 389 | */ |
||
| 390 | public function getAttachment($messageId, $attachmentId) { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param string $rawBody |
||
| 396 | * @param array $flags |
||
| 397 | */ |
||
| 398 | 6 | public function saveMessage($rawBody, $flags = []) { |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Save draft |
||
| 410 | * |
||
| 411 | * @param string $rawBody |
||
| 412 | * @return int UID of the saved draft |
||
| 413 | */ |
||
| 414 | public function saveDraft($rawBody) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param int $uid |
||
| 430 | * @param string $flag |
||
| 431 | * @param boolean $add |
||
| 432 | */ |
||
| 433 | public function setMessageFlag($uid, $flag, $add) { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param $fromUid |
||
| 447 | * @param $toUid |
||
| 448 | * @return array |
||
| 449 | */ |
||
| 450 | 3 | public function getMessagesSince($fromUid, $toUid) { |
|
| 455 | |||
| 456 | /** |
||
| 457 | * @return Horde_Imap_Client_Mailbox |
||
| 458 | */ |
||
| 459 | public function getHordeMailBox() { |
||
| 462 | |||
| 463 | } |
||
| 464 |