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 |
||
19 | class Mailbox implements IMailBox { |
||
20 | |||
21 | /** |
||
22 | * @var Horde_Imap_Client_Socket |
||
23 | */ |
||
24 | protected $conn; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private $attributes; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $specialRole; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $displayName; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $delimiter; |
||
45 | |||
46 | /** |
||
47 | * @var Horde_Imap_Client_Mailbox |
||
48 | */ |
||
49 | protected $mailBox; |
||
50 | |||
51 | /** |
||
52 | * @param Horde_Imap_Client_Socket $conn |
||
53 | * @param Horde_Imap_Client_Mailbox $mailBox |
||
54 | * @param array $attributes |
||
55 | * @param string $delimiter |
||
56 | */ |
||
57 | 5 | public function __construct($conn, $mailBox, $attributes, $delimiter='/') { |
|
68 | |||
69 | public function getMessages($from = 0, $count = 2, $filter = '') { |
||
70 | if ($filter instanceof Horde_Imap_Client_Search_Query) { |
||
|
|||
71 | $query = $filter; |
||
72 | } else { |
||
73 | $query = new Horde_Imap_Client_Search_Query(); |
||
74 | if ($filter) { |
||
75 | $query->text($filter, false); |
||
76 | } |
||
77 | } |
||
78 | if ($this->getSpecialRole() !== 'trash') { |
||
79 | $query->flag(Horde_Imap_Client::FLAG_DELETED, false); |
||
80 | } |
||
81 | $result = $this->conn->search($this->mailBox, $query, ['sort' => [Horde_Imap_Client::SORT_DATE]]); |
||
82 | $ids = array_reverse($result['match']->ids); |
||
83 | if ($from >= 0 && $count >= 0) { |
||
84 | $ids = array_slice($ids, $from, $count); |
||
85 | } |
||
86 | $ids = new \Horde_Imap_Client_Ids($ids, false); |
||
87 | |||
88 | $headers = []; |
||
89 | |||
90 | $fetch_query = new \Horde_Imap_Client_Fetch_Query(); |
||
91 | $fetch_query->envelope(); |
||
92 | $fetch_query->flags(); |
||
93 | $fetch_query->size(); |
||
94 | $fetch_query->uid(); |
||
95 | $fetch_query->imapDate(); |
||
96 | $fetch_query->structure(); |
||
97 | |||
98 | $headers = array_merge($headers, [ |
||
99 | 'importance', |
||
100 | 'list-post', |
||
101 | 'x-priority' |
||
102 | ]); |
||
103 | $headers[] = 'content-type'; |
||
104 | |||
105 | $fetch_query->headers('imp', $headers, [ |
||
106 | 'cache' => true, |
||
107 | 'peek' => true |
||
108 | ]); |
||
109 | |||
110 | $options = ['ids' => $ids]; |
||
111 | // $list is an array of Horde_Imap_Client_Data_Fetch objects. |
||
112 | $headers = $this->conn->fetch($this->mailBox, $fetch_query, $options); |
||
113 | |||
114 | ob_start(); // fix for Horde warnings |
||
115 | $messages = []; |
||
116 | foreach ($headers->ids() as $message_id) { |
||
117 | $header = $headers[$message_id]; |
||
118 | $message = new IMAPMessage($this->conn, $this->mailBox, $message_id, $header); |
||
119 | $messages[] = $message->getListArray(); |
||
120 | } |
||
121 | ob_get_clean(); |
||
122 | |||
123 | // sort by time |
||
124 | usort($messages, function($a, $b) { |
||
125 | return $a['dateInt'] < $b['dateInt']; |
||
126 | }); |
||
127 | |||
128 | return $messages; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @return array |
||
133 | */ |
||
134 | 3 | public function attributes() { |
|
137 | |||
138 | /** |
||
139 | * @param string $messageId |
||
140 | * @param bool $loadHtmlMessageBody |
||
141 | * @return IMAPMessage |
||
142 | */ |
||
143 | public function getMessage($messageId, $loadHtmlMessageBody = false) { |
||
146 | |||
147 | /** |
||
148 | * @param int $flags |
||
149 | * @return array |
||
150 | */ |
||
151 | 12 | public function getStatus($flags = \Horde_Imap_Client::STATUS_ALL) { |
|
154 | |||
155 | /** |
||
156 | * @return int |
||
157 | */ |
||
158 | 3 | public function getTotalMessages() { |
|
159 | 3 | $status = $this->getStatus(\Horde_Imap_Client::STATUS_MESSAGES); |
|
160 | return (int) $status['messages']; |
||
161 | } |
||
162 | |||
163 | 5 | protected function makeDisplayName() { |
|
176 | |||
177 | 5 | public function getFolderId() { |
|
180 | |||
181 | /** |
||
182 | * @return string |
||
183 | */ |
||
184 | 3 | public function getParent() { |
|
194 | |||
195 | /** |
||
196 | * @return string |
||
197 | */ |
||
198 | 4 | public function getSpecialRole() { |
|
201 | |||
202 | /** |
||
203 | * @return string |
||
204 | */ |
||
205 | 4 | public function getDisplayName() { |
|
208 | |||
209 | /** |
||
210 | * @param string $displayName |
||
211 | */ |
||
212 | 4 | public function setDisplayName($displayName) { |
|
215 | |||
216 | /** |
||
217 | * @param integer $accountId |
||
218 | * @return array |
||
219 | */ |
||
220 | 3 | public function getListArray($accountId, $status = null) { |
|
262 | /** |
||
263 | * Get the special use role of the mailbox |
||
264 | * |
||
265 | * This method reads the attributes sent by the server |
||
266 | * |
||
267 | */ |
||
268 | 5 | protected function getSpecialRoleFromAttributes() { |
|
305 | |||
306 | /** |
||
307 | * Assign a special role to this mailbox based on its name |
||
308 | */ |
||
309 | 5 | protected function guessSpecialRole() { |
|
332 | |||
333 | /** |
||
334 | * @param int $messageId |
||
335 | * @param string $attachmentId |
||
336 | * @return Attachment |
||
337 | */ |
||
338 | public function getAttachment($messageId, $attachmentId) { |
||
341 | |||
342 | /** |
||
343 | * @param string $rawBody |
||
344 | * @param array $flags |
||
345 | */ |
||
346 | public function saveMessage($rawBody, $flags = []) { |
||
347 | |||
348 | $this->conn->append($this->mailBox, [ |
||
349 | [ |
||
350 | 'data' => $rawBody, |
||
351 | 'flags' => $flags |
||
352 | ] |
||
353 | ]); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Save draft |
||
358 | * |
||
359 | * @param string $rawBody |
||
360 | * @return int UID of the saved draft |
||
361 | */ |
||
362 | public function saveDraft($rawBody) { |
||
375 | |||
376 | /** |
||
377 | * @param int $uid |
||
378 | * @param string $flag |
||
379 | * @param boolean $add |
||
380 | */ |
||
381 | public function setMessageFlag($uid, $flag, $add) { |
||
392 | |||
393 | /** |
||
394 | * @param $fromUid |
||
395 | * @param $toUid |
||
396 | * @return array |
||
397 | */ |
||
398 | public function getMessagesSince($fromUid, $toUid) { |
||
399 | $query = new Horde_Imap_Client_Search_Query(); |
||
400 | $query->ids(new Horde_Imap_Client_Ids("$fromUid:$toUid")); |
||
401 | return $this->getMessages(-1, -1, $query); |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * @return Horde_Imap_Client_Mailbox |
||
406 | */ |
||
407 | public function getHordeMailBox() { |
||
410 | |||
411 | } |
||
412 |