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 | 9 | public function __construct($conn, $mailBox, $messageId, $fetch=null, |
|
| 79 | |||
| 80 | // output all the following: |
||
| 81 | public $header = null; |
||
| 82 | public $htmlMessage = ''; |
||
| 83 | public $plainMessage = ''; |
||
| 84 | public $attachments = []; |
||
| 85 | private $loadHtmlMessage = false; |
||
| 86 | private $hasHtmlMessage = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var \Horde_Imap_Client_Socket |
||
| 90 | */ |
||
| 91 | 7 | private $conn; |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @var \Horde_Imap_Client_Mailbox |
||
| 95 | */ |
||
| 96 | private $mailBox; |
||
| 97 | private $messageId; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var \Horde_Imap_Client_Data_Fetch |
||
| 101 | */ |
||
| 102 | private $fetch; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return int |
||
| 106 | */ |
||
| 107 | 7 | public function getUid() { |
|
| 113 | |||
| 114 | 7 | public function setUid($uid) { |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | 7 | public function getFlags() { |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @param array $flags |
||
| 140 | */ |
||
| 141 | 7 | public function setFlags(array $flags) { |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @return \Horde_Imap_Client_Data_Envelope |
||
| 148 | */ |
||
| 149 | 8 | public function getEnvelope() { |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | 7 | public function getFromEmail() { |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | 7 | public function getFrom() { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $from |
||
| 173 | * @throws Exception |
||
| 174 | */ |
||
| 175 | public function setFrom($from) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | 6 | public function getFromList() { |
|
| 183 | 6 | $e = $this->getEnvelope(); |
|
| 184 | 6 | return $this->convertAddressList($e->from); |
|
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | 7 | public function getToEmail() { |
|
| 195 | |||
| 196 | 6 | public function getTo() { |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param Horde_Mail_Rfc822_List $to |
||
| 204 | * @throws Exception |
||
| 205 | */ |
||
| 206 | public function setTo(Horde_Mail_Rfc822_List $to) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | 6 | View Code Duplication | public function getToList($assoc = false) { |
| 221 | |||
| 222 | 6 | View Code Duplication | public function getCCList($assoc = false) { |
| 230 | |||
| 231 | public function setCC(Horde_Mail_Rfc822_List $cc) { |
||
| 234 | |||
| 235 | View Code Duplication | public function getBCCList($assoc = false) { |
|
| 243 | |||
| 244 | public function setBcc(Horde_Mail_Rfc822_List $bcc) { |
||
| 247 | |||
| 248 | public function getReplyToList() { |
||
| 252 | |||
| 253 | public function setReplyTo(array $replyTo) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * on reply, fill cc with everyone from to and cc except yourself |
||
| 259 | * |
||
| 260 | * @param string $ownMail |
||
| 261 | */ |
||
| 262 | 1 | public function getReplyCcList($ownMail) { |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Get the ID if available |
||
| 274 | * |
||
| 275 | * @return int|null |
||
| 276 | */ |
||
| 277 | public function getMessageId() { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | 6 | public function getSubject() { |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $subject |
||
| 292 | * @throws Exception |
||
| 293 | */ |
||
| 294 | public function setSubject($subject) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return \Horde_Imap_Client_DateTime |
||
| 300 | */ |
||
| 301 | 6 | public function getSentDate() { |
|
| 304 | |||
| 305 | 6 | public function getSize() { |
|
| 308 | |||
| 309 | /** |
||
| 310 | * @param \Horde_Mime_Part $part |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | 6 | private function hasAttachments($part) { |
|
| 338 | |||
| 339 | 1 | private function loadMessageBodies() { |
|
| 391 | |||
| 392 | /** |
||
| 393 | * @param $p \Horde_Mime_Part |
||
| 394 | * @param $partNo |
||
| 395 | */ |
||
| 396 | 1 | private function getPart($p, $partNo) { |
|
| 447 | |||
| 448 | /** |
||
| 449 | * @param string $ownMail |
||
| 450 | * @param string $specialRole |
||
| 451 | */ |
||
| 452 | public function getFullMessage($ownMail, $specialRole=null) { |
||
| 453 | $mailBody = $this->plainMessage; |
||
| 454 | |||
| 455 | $data = $this->getListArray(); |
||
| 456 | if ($this->hasHtmlMessage) { |
||
| 457 | $data['hasHtmlBody'] = true; |
||
| 458 | } else { |
||
| 459 | $mailBody = $this->htmlService->convertLinks($mailBody); |
||
| 460 | list($mailBody, $signature) = $this->htmlService->parseMailBody($mailBody); |
||
| 461 | $data['body'] = $specialRole === 'drafts' ? $mailBody : nl2br($mailBody); |
||
| 462 | $data['signature'] = $signature; |
||
| 463 | } |
||
| 464 | |||
| 465 | $data['attachments'] = $this->attachments; |
||
| 466 | |||
| 467 | if ($specialRole === 'sent') { |
||
| 468 | $data['replyToList'] = $this->getToList(true); |
||
| 469 | $data['replyCcList'] = $this->getCCList(true); |
||
| 470 | } else { |
||
| 471 | $data['replyToList'] = $this->getReplyToList(true); |
||
| 472 | $data['replyCcList'] = $this->getReplyCcList($ownMail); |
||
| 473 | } |
||
| 474 | return $data; |
||
| 475 | } |
||
| 476 | |||
| 477 | 6 | public function getListArray() { |
|
| 495 | |||
| 496 | /** |
||
| 497 | * @param int $accountId |
||
| 498 | * @param string $folderId |
||
| 499 | * @param int $messageId |
||
| 500 | * @param Closure $attachments |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | 1 | public function getHtmlBody($accountId, $folderId, $messageId, Closure $attachments) { |
|
| 510 | |||
| 511 | /** |
||
| 512 | * @return string |
||
| 513 | */ |
||
| 514 | 1 | public function getPlainBody() { |
|
| 517 | |||
| 518 | /** |
||
| 519 | * @param \Horde_Mime_Part $part |
||
| 520 | * @param int $partNo |
||
| 521 | */ |
||
| 522 | private function handleMultiPartMessage($part, $partNo) { |
||
| 523 | $i = 1; |
||
| 524 | foreach ($part->getParts() as $p) { |
||
| 525 | $this->getPart($p, "$partNo.$i"); |
||
| 526 | $i++; |
||
| 527 | } |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @param \Horde_Mime_Part $p |
||
| 532 | * @param int $partNo |
||
| 533 | */ |
||
| 534 | 1 | private function handleTextMessage($p, $partNo) { |
|
| 539 | |||
| 540 | /** |
||
| 541 | * @param \Horde_Mime_Part $p |
||
| 542 | * @param int $partNo |
||
| 543 | */ |
||
| 544 | 1 | private function handleHtmlMessage($p, $partNo) { |
|
| 551 | |||
| 552 | /** |
||
| 553 | * @param \Horde_Mime_Part $p |
||
| 554 | * @param int $partNo |
||
| 555 | * @return string |
||
| 556 | * @throws DoesNotExistException |
||
| 557 | * @throws \Exception |
||
| 558 | */ |
||
| 559 | 1 | private function loadBodyData($p, $partNo) { |
|
| 592 | |||
| 593 | public function getContent() { |
||
| 596 | |||
| 597 | public function setContent($content) { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @return array |
||
| 603 | */ |
||
| 604 | public function getAttachments() { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @param File $file |
||
| 610 | */ |
||
| 611 | public function addAttachmentFromFiles(File $file) { |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @return IMessage |
||
| 617 | */ |
||
| 618 | public function getRepliedMessage() { |
||
| 621 | |||
| 622 | /** |
||
| 623 | * @param IMessage $message |
||
| 624 | */ |
||
| 625 | public function setRepliedMessage(IMessage $message) { |
||
| 628 | |||
| 629 | } |
||
| 630 |
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: