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 Email 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 Email, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Email extends \SerializableObject |
||
| 20 | { |
||
| 21 | /** The email's sender */ |
||
| 22 | protected $sender; |
||
| 23 | /** An array of receipients */ |
||
| 24 | protected $to; |
||
| 25 | /** An array of CC receipients */ |
||
| 26 | protected $cc; |
||
| 27 | /** An array of BCC receipients */ |
||
| 28 | protected $bcc; |
||
| 29 | /** The email's reply to address */ |
||
| 30 | protected $replyTo; |
||
| 31 | /** The subject of the email */ |
||
| 32 | protected $subject; |
||
| 33 | /** The email's HTML body */ |
||
| 34 | protected $htmlBody; |
||
| 35 | /** The email's plain text body */ |
||
| 36 | protected $textBody; |
||
| 37 | /** An array of attachements for the email */ |
||
| 38 | protected $attachments; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Initialize a new email |
||
| 42 | */ |
||
| 43 | public function __construct() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Who is this email going to be sent from |
||
| 58 | * |
||
| 59 | * This will return a string either in the format of 'email@address' or 'name <email@address>' |
||
| 60 | * |
||
| 61 | * @return string The sender of the email |
||
| 62 | */ |
||
| 63 | public function getFromAddress() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Who is this email going to |
||
| 74 | * |
||
| 75 | * @return array The recipients of the email |
||
| 76 | */ |
||
| 77 | public function getToAddresses() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Who is this email going to (CC) |
||
| 84 | * |
||
| 85 | * @return array The recipients of the email |
||
| 86 | */ |
||
| 87 | public function getCCAddresses() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Who is this email going to (BCC) |
||
| 94 | * |
||
| 95 | * @return array The recipients of the email |
||
| 96 | */ |
||
| 97 | public function getBCCAddresses() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Who should a recipient reply to? |
||
| 104 | * |
||
| 105 | * @return string The reply to address of the email |
||
| 106 | */ |
||
| 107 | public function getReplyTo() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * What is the email's subject? |
||
| 118 | * |
||
| 119 | * @return string The email's subject |
||
| 120 | */ |
||
| 121 | public function getSubject() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * What should a user with an HTML capable email client see? |
||
| 128 | * |
||
| 129 | * @return string The email in HTML form |
||
| 130 | */ |
||
| 131 | public function getHTMLBody() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * What should a user with a plain text only email client see? |
||
| 138 | * |
||
| 139 | * @return string The email in ASCII form |
||
| 140 | */ |
||
| 141 | public function getTextBody() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Create the address string given an email address and if specified a name |
||
| 148 | * |
||
| 149 | * @param string $email The email address |
||
| 150 | * @param string $name The name to associate with the address |
||
| 151 | * |
||
| 152 | * @return string The email address and name format |
||
| 153 | */ |
||
| 154 | protected function constructEmailAddressString($email, $name = false) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Set the address the email should be sent from |
||
| 165 | * |
||
| 166 | * @param string $email The email address to send from |
||
| 167 | * @param string $name The name to associate with the from address |
||
| 168 | */ |
||
| 169 | public function setFromAddress($email, $name = false) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Add a new address to the To: line |
||
| 176 | * |
||
| 177 | * @param string $email The email address to send to |
||
| 178 | * @param string $name The name to associate with the address |
||
| 179 | */ |
||
| 180 | public function addToAddress($email, $name = false) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Add a new address to the CC: line |
||
| 187 | * |
||
| 188 | * @param string $email The email address to send to |
||
| 189 | * @param string $name The name to associate with the address |
||
| 190 | */ |
||
| 191 | public function addCCAddress($email, $name = false) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Add a new address to the BCC: line |
||
| 198 | * |
||
| 199 | * @param string $email The email address to send to |
||
| 200 | * @param string $name The name to associate with the address |
||
| 201 | */ |
||
| 202 | public function addBCCAddress($email, $name = false) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Add an address to the inidicated list |
||
| 209 | * |
||
| 210 | * @param array $list The list to add the address to |
||
| 211 | * @param string $email The email address to send to |
||
| 212 | * @param string $name The name to associate with the address |
||
| 213 | */ |
||
| 214 | protected function addAddress(&$list, $email, $name = false) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Set the address a recipient should reply to |
||
| 221 | * |
||
| 222 | * @param string $email The email address to reply to |
||
| 223 | * @param string $name The name to associate with the from address |
||
| 224 | */ |
||
| 225 | public function setReplyTo($email, $name = false) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set the subject line for the email |
||
| 232 | * |
||
| 233 | * @param string $subject The email's new subject line |
||
| 234 | */ |
||
| 235 | public function setSubject($subject) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Set the HTML body for the email |
||
| 242 | * |
||
| 243 | * @param string $body The email's new HTML body |
||
| 244 | */ |
||
| 245 | public function setHTMLBody($body) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Set the plain text for the email |
||
| 252 | * |
||
| 253 | * @param string $body The email's new plain text body |
||
| 254 | */ |
||
| 255 | public function setTextBody($body) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Append a string to the HTML Body |
||
| 262 | * |
||
| 263 | * @param string $body The string to append to the HTML body |
||
| 264 | */ |
||
| 265 | public function appendToHTMLBody($body) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Append a string to the Plain Text Body |
||
| 272 | * |
||
| 273 | * @param string $body The string to append to the plain text body |
||
| 274 | */ |
||
| 275 | public function appendToTextBody($body) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Add an attachment from a memory buffer |
||
| 282 | * |
||
| 283 | * @param string $name The file name for the attachment to be sent as |
||
| 284 | * @param string $buffer The attachment as a binary string |
||
| 285 | * @param string $mimeType The MIME type to send the attachment as |
||
| 286 | */ |
||
| 287 | public function addAttachmentFromBuffer($name, $buffer, $mimeType = 'application/octet-stream') |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Add an attachment from a file on the local disk |
||
| 294 | * |
||
| 295 | * @param string $filename The file name and path on the local disk |
||
| 296 | * @param string $name The file name for the attachment to be sent as |
||
| 297 | */ |
||
| 298 | public function addAttachmentFromFile($filename, $name = false) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Does this email have an attachment? |
||
| 318 | * |
||
| 319 | * @return boolean True if the email has an attachment. False otherwise |
||
| 320 | */ |
||
| 321 | public function hasAttachments() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Serialize the message to a raw MIME encoded format suitable for sending over SMTP |
||
| 328 | * |
||
| 329 | * @return string A text version of the message suitable for sending over SMTP |
||
| 330 | */ |
||
| 331 | public function getRawMessage() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Serialize a recipient so that it can be sent over SMTP |
||
| 383 | * |
||
| 384 | * @param string $recipient The recipient in the format 'name <email@address>' |
||
| 385 | * |
||
| 386 | * @return string A text version of the recipient name and address suitable for sending over SMTP |
||
| 387 | */ |
||
| 388 | public function encodeRecipients($recipient) |
||
| 400 | } |
||
| 401 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
| 402 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: