Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like IMAPMessage 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 IMAPMessage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class IMAPMessage implements IMessage { |
||
| 40 | |||
| 41 | use ConvertAddresses; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string[] |
||
| 45 | */ |
||
| 46 | private $attachmentsToIgnore = ['signature.asc', 'smime.p7s']; |
||
| 47 | |||
| 48 | /** @var string */ |
||
| 49 | private $uid; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param \Horde_Imap_Client_Socket|null $conn |
||
| 53 | * @param \Horde_Imap_Client_Mailbox $mailBox |
||
| 54 | * @param integer $messageId |
||
| 55 | * @param \Horde_Imap_Client_Data_Fetch|null $fetch |
||
| 56 | * @param boolean $loadHtmlMessage |
||
| 57 | * @param Html|null $htmlService |
||
| 58 | */ |
||
| 59 | 3 | public function __construct($conn, $mailBox, $messageId, $fetch=null, |
|
| 60 | $loadHtmlMessage=false, $htmlService = null) { |
||
| 61 | 3 | $this->conn = $conn; |
|
| 62 | 3 | $this->mailBox = $mailBox; |
|
| 63 | 3 | $this->messageId = $messageId; |
|
| 64 | 3 | $this->loadHtmlMessage = $loadHtmlMessage; |
|
| 65 | |||
| 66 | 3 | $this->htmlService = $htmlService; |
|
|
|
|||
| 67 | 3 | if (is_null($htmlService)) { |
|
| 68 | 2 | $urlGenerator = \OC::$server->getURLGenerator(); |
|
| 69 | 2 | $this->htmlService = new Html($urlGenerator); |
|
| 70 | 2 | } |
|
| 71 | |||
| 72 | 3 | if ($fetch === null) { |
|
| 73 | 1 | $this->loadMessageBodies(); |
|
| 74 | 1 | } else { |
|
| 75 | 2 | $this->fetch = $fetch; |
|
| 76 | } |
||
| 77 | 3 | } |
|
| 78 | |||
| 79 | // output all the following: |
||
| 80 | public $header = null; |
||
| 81 | public $htmlMessage = ''; |
||
| 82 | public $plainMessage = ''; |
||
| 83 | public $attachments = []; |
||
| 84 | private $loadHtmlMessage = false; |
||
| 85 | private $hasHtmlMessage = false; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var \Horde_Imap_Client_Socket |
||
| 89 | */ |
||
| 90 | 1 | private $conn; |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @var \Horde_Imap_Client_Mailbox |
||
| 94 | */ |
||
| 95 | private $mailBox; |
||
| 96 | private $messageId; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var \Horde_Imap_Client_Data_Fetch |
||
| 100 | */ |
||
| 101 | private $fetch; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return int |
||
| 105 | */ |
||
| 106 | 1 | public function getUid() { |
|
| 112 | |||
| 113 | 1 | public function setUid($uid) { |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | public function getFlags() { |
||
| 125 | $flags = $this->fetch->getFlags(); |
||
| 126 | return [ |
||
| 127 | 'unseen' => !in_array(Horde_Imap_Client::FLAG_SEEN, $flags), |
||
| 128 | 'flagged' => in_array(Horde_Imap_Client::FLAG_FLAGGED, $flags), |
||
| 129 | 'answered' => in_array(Horde_Imap_Client::FLAG_ANSWERED, $flags), |
||
| 130 | 'deleted' => in_array(Horde_Imap_Client::FLAG_DELETED, $flags), |
||
| 131 | 'draft' => in_array(Horde_Imap_Client::FLAG_DRAFT, $flags), |
||
| 132 | 'forwarded' => in_array(Horde_Imap_Client::FLAG_FORWARDED, $flags), |
||
| 133 | 'hasAttachments' => $this->hasAttachments($this->fetch->getStructure()) |
||
| 134 | ]; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param array $flags |
||
| 139 | */ |
||
| 140 | 1 | public function setFlags(array $flags) { |
|
| 141 | // TODO: implement |
||
| 142 | throw new Exception('Not implemented'); |
||
| 143 | 1 | } |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @return \Horde_Imap_Client_Data_Envelope |
||
| 147 | */ |
||
| 148 | 2 | public function getEnvelope() { |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | 1 | public function getFromEmail() { |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | 1 | public function getFrom() { |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $from |
||
| 172 | * @throws Exception |
||
| 173 | */ |
||
| 174 | public function setFrom($from) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | public function getFromList() { |
||
| 182 | $e = $this->getEnvelope(); |
||
| 183 | return $this->convertAddressList($e->from); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function getToEmail() { |
||
| 190 | $e = $this->getEnvelope(); |
||
| 191 | $to = $e->to[0]; |
||
| 192 | return $to ? $to->bare_address : null; |
||
| 193 | } |
||
| 194 | |||
| 195 | public function getTo() { |
||
| 196 | $e = $this->getEnvelope(); |
||
| 197 | $to = $e->to[0]; |
||
| 198 | return $to ? $to->label : null; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param Horde_Mail_Rfc822_List $to |
||
| 203 | * @throws Exception |
||
| 204 | */ |
||
| 205 | public function setTo(Horde_Mail_Rfc822_List $to) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | View Code Duplication | public function getToList($assoc = false) { |
|
| 213 | $e = $this->getEnvelope(); |
||
| 214 | if ($assoc) { |
||
| 215 | return $this->convertAddressList($e->to); |
||
| 216 | } else { |
||
| 217 | return $this->hordeListToStringArray($e->to); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | View Code Duplication | public function getCCList($assoc = false) { |
|
| 222 | $e = $this->getEnvelope(); |
||
| 223 | if ($assoc) { |
||
| 224 | return $this->convertAddressList($e->cc); |
||
| 225 | } else { |
||
| 226 | return $this->hordeListToStringArray($e->cc); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | public function setCC(Horde_Mail_Rfc822_List $cc) { |
||
| 233 | |||
| 234 | View Code Duplication | public function getBCCList($assoc = false) { |
|
| 242 | |||
| 243 | public function setBcc(Horde_Mail_Rfc822_List $bcc) { |
||
| 246 | |||
| 247 | public function getReplyToList() { |
||
| 251 | |||
| 252 | public function setReplyTo(array $replyTo) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * on reply, fill cc with everyone from to and cc except yourself |
||
| 258 | * |
||
| 259 | * @param string $ownMail |
||
| 260 | */ |
||
| 261 | 1 | public function getReplyCcList($ownMail) { |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Get the ID if available |
||
| 273 | * |
||
| 274 | * @return int|null |
||
| 275 | */ |
||
| 276 | public function getMessageId() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getSubject() { |
||
| 285 | $e = $this->getEnvelope(); |
||
| 286 | return $e->subject; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $subject |
||
| 291 | * @throws Exception |
||
| 292 | */ |
||
| 293 | public function setSubject($subject) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return \Horde_Imap_Client_DateTime |
||
| 299 | */ |
||
| 300 | public function getSentDate() { |
||
| 301 | return $this->fetch->getImapDate(); |
||
| 302 | } |
||
| 303 | |||
| 304 | public function getSize() { |
||
| 305 | return $this->fetch->getSize(); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param \Horde_Mime_Part $part |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | private function hasAttachments($part) { |
||
| 313 | foreach($part->getParts() as $p) { |
||
| 314 | /** |
||
| 315 | * @var \Horde_Mime_Part $p |
||
| 316 | */ |
||
| 317 | $filename = $p->getName(); |
||
| 318 | |||
| 319 | if(!is_null($p->getContentId())) { |
||
| 320 | continue; |
||
| 321 | } |
||
| 322 | if(isset($filename)) { |
||
| 323 | // do not show technical attachments |
||
| 324 | if(in_array($filename, $this->attachmentsToIgnore)) { |
||
| 325 | continue; |
||
| 326 | } else { |
||
| 327 | return true; |
||
| 328 | } |
||
| 329 | } |
||
| 330 | if($this->hasAttachments($p)) { |
||
| 331 | return true; |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | return false; |
||
| 336 | } |
||
| 337 | |||
| 338 | 1 | private function loadMessageBodies() { |
|
| 390 | |||
| 391 | /** |
||
| 392 | * @param $p \Horde_Mime_Part |
||
| 393 | * @param $partNo |
||
| 394 | */ |
||
| 395 | 1 | private function getPart($p, $partNo) { |
|
| 446 | |||
| 447 | /** |
||
| 448 | * @param string $ownMail |
||
| 449 | * @param string $specialRole |
||
| 450 | */ |
||
| 451 | public function getFullMessage($ownMail, $specialRole=null) { |
||
| 480 | |||
| 481 | 1 | public function getListArray() { |
|
| 482 | $data = []; |
||
| 483 | $data['id'] = $this->getUid(); |
||
| 484 | $data['from'] = $this->getFrom(); |
||
| 485 | $data['fromEmail'] = $this->getFromEmail(); |
||
| 486 | $data['fromList'] = $this->getFromList(); |
||
| 487 | $data['to'] = $this->getTo(); |
||
| 488 | $data['toEmail'] = $this->getToEmail(); |
||
| 489 | $data['toList'] = $this->getToList(true); |
||
| 490 | $data['subject'] = $this->getSubject(); |
||
| 491 | $data['date'] = Util::formatDate($this->getSentDate()->format('U')); |
||
| 492 | $data['size'] = Util::humanFileSize($this->getSize()); |
||
| 493 | $data['flags'] = $this->getFlags(); |
||
| 494 | $data['dateInt'] = $this->getSentDate()->getTimestamp(); |
||
| 495 | $data['dateIso'] = $this->getSentDate()->format('c'); |
||
| 496 | 1 | $data['ccList'] = $this->getCCList(true); |
|
| 497 | return $data; |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @param int $accountId |
||
| 502 | * @param string $folderId |
||
| 503 | * @param int $messageId |
||
| 504 | * @param Closure $attachments |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | 1 | public function getHtmlBody($accountId, $folderId, $messageId, Closure $attachments) { |
|
| 514 | |||
| 515 | /** |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | 1 | public function getPlainBody() { |
|
| 521 | |||
| 522 | /** |
||
| 523 | * @param \Horde_Mime_Part $part |
||
| 524 | * @param int $partNo |
||
| 525 | */ |
||
| 526 | 1 | private function handleMultiPartMessage($part, $partNo) { |
|
| 533 | |||
| 534 | /** |
||
| 535 | * @param \Horde_Mime_Part $p |
||
| 536 | * @param int $partNo |
||
| 537 | */ |
||
| 538 | 1 | private function handleTextMessage($p, $partNo) { |
|
| 543 | |||
| 544 | /** |
||
| 545 | * @param \Horde_Mime_Part $p |
||
| 546 | * @param int $partNo |
||
| 547 | */ |
||
| 548 | 1 | private function handleHtmlMessage($p, $partNo) { |
|
| 555 | |||
| 556 | /** |
||
| 557 | * @param \Horde_Mime_Part $p |
||
| 558 | * @param int $partNo |
||
| 559 | * @return string |
||
| 560 | * @throws DoesNotExistException |
||
| 561 | * @throws \Exception |
||
| 562 | */ |
||
| 563 | 1 | private function loadBodyData($p, $partNo) { |
|
| 596 | |||
| 597 | public function getContent() { |
||
| 600 | |||
| 601 | public function setContent($content) { |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @return array |
||
| 607 | */ |
||
| 608 | public function getAttachments() { |
||
| 611 | |||
| 612 | /** |
||
| 613 | * @param File $file |
||
| 614 | */ |
||
| 615 | public function addAttachmentFromFiles(File $file) { |
||
| 618 | |||
| 619 | /** |
||
| 620 | * @return IMessage |
||
| 621 | */ |
||
| 622 | public function getRepliedMessage() { |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @param IMessage $message |
||
| 628 | */ |
||
| 629 | public function setRepliedMessage(IMessage $message) { |
||
| 632 | |||
| 633 | } |
||
| 634 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: