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 |
||
| 63 | class IMAPMessage implements IMessage { |
||
| 64 | |||
| 65 | use ConvertAddresses; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string[] |
||
| 69 | */ |
||
| 70 | private $attachmentsToIgnore = ['signature.asc', 'smime.p7s']; |
||
| 71 | |||
| 72 | /** @var string */ |
||
| 73 | private $uid; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param \Horde_Imap_Client_Socket|null $conn |
||
| 77 | * @param \Horde_Imap_Client_Mailbox $mailBox |
||
| 78 | * @param integer $messageId |
||
| 79 | * @param \Horde_Imap_Client_Data_Fetch|null $fetch |
||
| 80 | * @param boolean $loadHtmlMessage |
||
| 81 | * @param Html|null $htmlService |
||
| 82 | */ |
||
| 83 | 9 | public function __construct($conn, $mailBox, $messageId, $fetch=null, |
|
| 103 | |||
| 104 | // output all the following: |
||
| 105 | public $header = null; |
||
| 106 | public $htmlMessage = ''; |
||
| 107 | public $plainMessage = ''; |
||
| 108 | 1 | public $attachments = []; |
|
| 109 | private $loadHtmlMessage = false; |
||
| 110 | private $hasHtmlMessage = false; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var \Horde_Imap_Client_Socket |
||
| 114 | */ |
||
| 115 | private $conn; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var \Horde_Imap_Client_Mailbox |
||
| 119 | */ |
||
| 120 | private $mailBox; |
||
| 121 | private $messageId; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var \Horde_Imap_Client_Data_Fetch |
||
| 125 | */ |
||
| 126 | private $fetch; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return int |
||
| 130 | */ |
||
| 131 | 7 | public function getUid() { |
|
| 137 | |||
| 138 | 7 | public function setUid($uid) { |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | 7 | public function getFlags() { |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @param array $flags |
||
| 164 | */ |
||
| 165 | public function setFlags(array $flags) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return \Horde_Imap_Client_Data_Envelope |
||
| 172 | */ |
||
| 173 | 8 | public function getEnvelope() { |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | 7 | public function getFromEmail() { |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | 7 | public function getFrom() { |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $from |
||
| 197 | * @throws Exception |
||
| 198 | */ |
||
| 199 | public function setFrom($from) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | 6 | public function getFromList() { |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | 6 | public function getToEmail() { |
|
| 219 | |||
| 220 | 6 | public function getTo() { |
|
| 221 | 6 | $e = $this->getEnvelope(); |
|
| 222 | 6 | $to = $e->to[0]; |
|
| 223 | 6 | return $to ? $to->label : null; |
|
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param Horde_Mail_Rfc822_List $to |
||
| 228 | * @throws Exception |
||
| 229 | */ |
||
| 230 | public function setTo(Horde_Mail_Rfc822_List $to) { |
||
| 231 | throw new Exception('IMAP message is immutable'); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | 6 | View Code Duplication | public function getToList($assoc = false) { |
| 238 | 6 | $e = $this->getEnvelope(); |
|
| 239 | 6 | if ($assoc) { |
|
| 240 | 6 | return $this->convertAddressList($e->to); |
|
| 241 | } else { |
||
| 242 | return $this->hordeListToStringArray($e->to); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | 6 | View Code Duplication | public function getCCList($assoc = false) { |
| 247 | 6 | $e = $this->getEnvelope(); |
|
| 248 | 6 | if ($assoc) { |
|
| 249 | 6 | return $this->convertAddressList($e->cc); |
|
| 250 | } else { |
||
| 251 | return $this->hordeListToStringArray($e->cc); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | public function setCC(Horde_Mail_Rfc822_List $cc) { |
||
| 256 | throw new Exception('IMAP message is immutable'); |
||
| 257 | } |
||
| 258 | |||
| 259 | View Code Duplication | public function getBCCList($assoc = false) { |
|
| 260 | $e = $this->getEnvelope(); |
||
| 261 | if ($assoc) { |
||
| 262 | return $this->convertAddressList($e->bcc); |
||
| 263 | } else { |
||
| 264 | return $this->hordeListToStringArray($e->bcc); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | public function setBcc(Horde_Mail_Rfc822_List $bcc) { |
||
| 269 | throw new Exception('IMAP message is immutable'); |
||
| 270 | } |
||
| 271 | |||
| 272 | public function getReplyToList() { |
||
| 276 | |||
| 277 | public function setReplyTo(array $replyTo) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * on reply, fill cc with everyone from to and cc except yourself |
||
| 283 | * |
||
| 284 | * @param string $ownMail |
||
| 285 | */ |
||
| 286 | 1 | public function getReplyCcList($ownMail) { |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Get the ID if available |
||
| 298 | * |
||
| 299 | * @return int|null |
||
| 300 | */ |
||
| 301 | public function getMessageId() { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | 6 | public function getSubject() { |
|
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $subject |
||
| 316 | * @throws Exception |
||
| 317 | */ |
||
| 318 | public function setSubject($subject) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return \Horde_Imap_Client_DateTime |
||
| 324 | */ |
||
| 325 | 6 | public function getSentDate() { |
|
| 328 | |||
| 329 | 6 | public function getSize() { |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param \Horde_Mime_Part $part |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | 6 | private function hasAttachments($part) { |
|
| 362 | |||
| 363 | 1 | private function loadMessageBodies() { |
|
| 415 | |||
| 416 | /** |
||
| 417 | * @param $p \Horde_Mime_Part |
||
| 418 | * @param $partNo |
||
| 419 | */ |
||
| 420 | 7 | private function getPart($p, $partNo) { |
|
| 471 | |||
| 472 | /** |
||
| 473 | * @param string $ownMail |
||
| 474 | * @param string $specialRole |
||
| 475 | */ |
||
| 476 | 1 | public function getFullMessage($ownMail, $specialRole=null) { |
|
| 477 | $mailBody = $this->plainMessage; |
||
| 478 | |||
| 500 | |||
| 501 | 6 | public function getListArray() { |
|
| 519 | |||
| 520 | /** |
||
| 521 | * @param int $accountId |
||
| 522 | * @param string $folderId |
||
| 523 | * @param int $messageId |
||
| 524 | * @param Closure $attachments |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | 1 | public function getHtmlBody($accountId, $folderId, $messageId, Closure $attachments) { |
|
| 534 | |||
| 535 | /** |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | 1 | public function getPlainBody() { |
|
| 541 | |||
| 542 | /** |
||
| 543 | * @param \Horde_Mime_Part $part |
||
| 544 | * @param int $partNo |
||
| 545 | */ |
||
| 546 | private function handleMultiPartMessage($part, $partNo) { |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param \Horde_Mime_Part $p |
||
| 556 | * @param int $partNo |
||
| 557 | */ |
||
| 558 | 1 | private function handleTextMessage($p, $partNo) { |
|
| 563 | |||
| 564 | /** |
||
| 565 | * @param \Horde_Mime_Part $p |
||
| 566 | * @param int $partNo |
||
| 567 | */ |
||
| 568 | 1 | private function handleHtmlMessage($p, $partNo) { |
|
| 575 | |||
| 576 | /** |
||
| 577 | * @param \Horde_Mime_Part $p |
||
| 578 | * @param int $partNo |
||
| 579 | * @return string |
||
| 580 | * @throws DoesNotExistException |
||
| 581 | * @throws \Exception |
||
| 582 | */ |
||
| 583 | 1 | private function loadBodyData($p, $partNo) { |
|
| 616 | |||
| 617 | public function getContent() { |
||
| 620 | |||
| 621 | public function setContent($content) { |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @return array |
||
| 627 | */ |
||
| 628 | public function getAttachments() { |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @param File $file |
||
| 634 | */ |
||
| 635 | public function addAttachmentFromFiles(File $file) { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @return IMessage |
||
| 641 | */ |
||
| 642 | public function getRepliedMessage() { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @param IMessage $message |
||
| 648 | */ |
||
| 649 | public function setRepliedMessage(IMessage $message) { |
||
| 652 | |||
| 653 | } |
||
| 654 |
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: