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 |
||
| 41 | class Mailbox implements IMailBox { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Horde_Imap_Client_Socket |
||
| 45 | */ |
||
| 46 | protected $conn; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private $attributes; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $specialRole; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private $displayName; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private $delimiter; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var Horde_Imap_Client_Mailbox |
||
| 70 | */ |
||
| 71 | protected $mailBox; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param Horde_Imap_Client_Socket $conn |
||
| 75 | * @param Horde_Imap_Client_Mailbox $mailBox |
||
| 76 | * @param array $attributes |
||
| 77 | * @param string $delimiter |
||
| 78 | */ |
||
| 79 | 12 | public function __construct($conn, $mailBox, $attributes, $delimiter='/') { |
|
| 90 | |||
| 91 | 6 | public function getMessages($from = 0, $count = 2, $filter = '') { |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | 3 | public function attributes() { |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $messageId |
||
| 162 | * @param bool $loadHtmlMessageBody |
||
| 163 | * @return IMAPMessage |
||
| 164 | */ |
||
| 165 | public function getMessage($messageId, $loadHtmlMessageBody = false) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param int $flags |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | 12 | public function getStatus($flags = \Horde_Imap_Client::STATUS_ALL) { |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @return int |
||
| 179 | */ |
||
| 180 | 3 | public function getTotalMessages() { |
|
| 184 | |||
| 185 | 12 | protected function makeDisplayName() { |
|
| 186 | 12 | $parts = explode($this->delimiter, $this->mailBox->utf8, 2); |
|
| 187 | |||
| 188 | 12 | if (count($parts) > 1) { |
|
| 189 | $displayName = $parts[1]; |
||
| 190 | 12 | } elseif (strtolower($this->mailBox->utf8) === 'inbox') { |
|
| 191 | 6 | $displayName = 'Inbox'; |
|
| 192 | 6 | } else { |
|
| 193 | 12 | $displayName = $this->mailBox->utf8; |
|
| 194 | } |
||
| 195 | |||
| 196 | 12 | $this->displayName = $displayName; |
|
| 197 | 12 | } |
|
| 198 | |||
| 199 | 13 | public function getFolderId() { |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | 6 | public function getParent() { |
|
| 207 | 6 | $folderId = $this->getFolderId(); |
|
| 208 | 6 | $parts = explode($this->delimiter, $folderId, 2); |
|
| 209 | |||
| 210 | 6 | if (count($parts) > 1) { |
|
| 211 | return $parts[0]; |
||
| 212 | } |
||
| 213 | |||
| 214 | 6 | return null; |
|
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | 9 | public function getSpecialRole() { |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | 6 | public function getDisplayName() { |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $displayName |
||
| 233 | */ |
||
| 234 | 6 | public function setDisplayName($displayName) { |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @param integer $accountId |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | 6 | public function getListArray($accountId, $status = null) { |
|
| 243 | 6 | $displayName = $this->getDisplayName(); |
|
| 244 | try { |
||
| 245 | 6 | if (is_null($status)) { |
|
| 246 | 3 | $status = $this->getStatus(); |
|
| 247 | 3 | } |
|
| 248 | 6 | $total = $status['messages']; |
|
| 249 | 6 | $specialRole = $this->getSpecialRole(); |
|
| 250 | 6 | $unseen = ($specialRole === 'trash') ? 0 : $status['unseen']; |
|
| 251 | 6 | $isEmpty = ($total === 0); |
|
| 252 | 6 | $noSelect = in_array('\\noselect', $this->attributes); |
|
| 253 | 6 | $parentId = $this->getParent(); |
|
| 254 | 6 | $parentId = ($parentId !== null) ? base64_encode($parentId) : null; |
|
| 255 | return [ |
||
| 256 | 6 | 'id' => base64_encode($this->getFolderId()), |
|
| 257 | 6 | 'parent' => $parentId, |
|
| 258 | 6 | 'name' => $displayName, |
|
| 259 | 6 | 'specialRole' => $specialRole, |
|
| 260 | 6 | 'unseen' => $unseen, |
|
| 261 | 6 | 'total' => $total, |
|
| 262 | 6 | 'isEmpty' => $isEmpty, |
|
| 263 | 6 | 'accountId' => $accountId, |
|
| 264 | 6 | 'noSelect' => $noSelect, |
|
| 265 | 6 | 'uidvalidity' => $status['uidvalidity'], |
|
| 266 | 6 | 'uidnext' => $status['uidnext'], |
|
| 267 | 6 | 'delimiter' => $this->delimiter |
|
| 268 | 6 | ]; |
|
| 269 | } catch (\Horde_Imap_Client_Exception $e) { |
||
| 270 | return [ |
||
| 271 | 'id' => base64_encode($this->getFolderId()), |
||
| 272 | 'parent' => null, |
||
| 273 | 'name' => $displayName, |
||
| 274 | 'specialRole' => null, |
||
| 275 | 'unseen' => 0, |
||
| 276 | 'total' => 0, |
||
| 277 | 'error' => $e->getMessage(), |
||
| 278 | 'isEmpty' => true, |
||
| 279 | 'accountId' => $accountId, |
||
| 280 | 'noSelect' => true |
||
| 281 | ]; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | /** |
||
| 285 | * Get the special use role of the mailbox |
||
| 286 | * |
||
| 287 | * This method reads the attributes sent by the server |
||
| 288 | * |
||
| 289 | */ |
||
| 290 | 12 | protected function getSpecialRoleFromAttributes() { |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Assign a special role to this mailbox based on its name |
||
| 330 | */ |
||
| 331 | 12 | protected function guessSpecialRole() { |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @param int $messageId |
||
| 357 | * @param string $attachmentId |
||
| 358 | * @return Attachment |
||
| 359 | */ |
||
| 360 | public function getAttachment($messageId, $attachmentId) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param string $rawBody |
||
| 366 | * @param array $flags |
||
| 367 | */ |
||
| 368 | 6 | public function saveMessage($rawBody, $flags = []) { |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Save draft |
||
| 380 | * |
||
| 381 | * @param string $rawBody |
||
| 382 | * @return int UID of the saved draft |
||
| 383 | */ |
||
| 384 | public function saveDraft($rawBody) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param int $uid |
||
| 400 | * @param string $flag |
||
| 401 | * @param boolean $add |
||
| 402 | */ |
||
| 403 | public function setMessageFlag($uid, $flag, $add) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param $fromUid |
||
| 417 | * @param $toUid |
||
| 418 | * @return array |
||
| 419 | */ |
||
| 420 | 3 | public function getMessagesSince($fromUid, $toUid) { |
|
| 425 | |||
| 426 | /** |
||
| 427 | * @return Horde_Imap_Client_Mailbox |
||
| 428 | */ |
||
| 429 | public function getHordeMailBox() { |
||
| 432 | |||
| 433 | } |
||
| 434 |