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 Archangel 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 Archangel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Archangel implements LoggerAwareInterface |
||
| 17 | { |
||
| 18 | |||
| 19 | /** @var string $subject */ |
||
| 20 | protected $subject; |
||
| 21 | |||
| 22 | /** @var array $toAddresses */ |
||
| 23 | protected $toAddresses = array(); |
||
| 24 | |||
| 25 | /** @var array $ccAddresses */ |
||
| 26 | protected $ccAddresses = array(); |
||
| 27 | |||
| 28 | /** @var array $bccAddresses */ |
||
| 29 | protected $bccAddresses = array(); |
||
| 30 | |||
| 31 | /** @var array $headers */ |
||
| 32 | protected $headers = array(); |
||
| 33 | |||
| 34 | /** @var string $plainMessage */ |
||
| 35 | protected $plainMessage; |
||
| 36 | |||
| 37 | /** @var string $htmlMessage */ |
||
| 38 | protected $htmlMessage; |
||
| 39 | |||
| 40 | /** @var array $attachments */ |
||
| 41 | protected $attachments = array(); |
||
| 42 | |||
| 43 | /** @var string $boundary */ |
||
| 44 | protected $boundary; |
||
| 45 | |||
| 46 | /** @var string $alternativeBoundary */ |
||
| 47 | protected $alternativeBoundary; |
||
| 48 | |||
| 49 | /** @var LoggerInterface */ |
||
| 50 | protected $logger; |
||
| 51 | |||
| 52 | /** @var string LINE_BREAK */ |
||
| 53 | const LINE_BREAK = "\r\n"; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param string $mailer |
||
| 57 | */ |
||
| 58 | public function __construct($mailer = null) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param LoggerInterface $logger |
||
| 70 | * |
||
| 71 | * @return $this; |
||
|
|
|||
| 72 | */ |
||
| 73 | public function setLogger(LoggerInterface $logger) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Setter method for adding recipients |
||
| 82 | * |
||
| 83 | * @param string $address email address for the recipient |
||
| 84 | * @param string $title name of the recipient (optional) |
||
| 85 | |||
| 86 | * @return object instantiated $this |
||
| 87 | */ |
||
| 88 | public function addTo($address, $title = '') |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Setter method for adding cc recipients |
||
| 100 | * |
||
| 101 | * @param string $address email address for the cc recipient |
||
| 102 | * @param string $title name of the cc recipient (optional) |
||
| 103 | * |
||
| 104 | * @return object instantiated $this |
||
| 105 | */ |
||
| 106 | public function addCC($address, $title = '') |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Setter method for adding bcc recipients |
||
| 118 | * |
||
| 119 | * @param string $address email address for the bcc recipient |
||
| 120 | * @param string $title name of the bcc recipient (optional) |
||
| 121 | * |
||
| 122 | * @return object instantiated $this |
||
| 123 | */ |
||
| 124 | public function addBCC($address, $title = '') |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Setter method for setting the single 'from' field |
||
| 136 | * |
||
| 137 | * @param string $address email address for the sender |
||
| 138 | * @param string $title name of the sender (optional) |
||
| 139 | * |
||
| 140 | * @return object instantiated $this |
||
| 141 | */ |
||
| 142 | public function setFrom($address, $title = '') |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Setter method for setting the single 'reply-to' field |
||
| 151 | * |
||
| 152 | * @param string $address email address for the reply-to |
||
| 153 | * @param string $title name of the reply-to (optional) |
||
| 154 | * |
||
| 155 | * @return object instantiated $this |
||
| 156 | */ |
||
| 157 | public function setReplyTo($address, $title = '') |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $address |
||
| 166 | * @param string $title |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | protected function formatEmailAddress($address, $title) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Setter method for setting a subject |
||
| 180 | * |
||
| 181 | * @param string $subject subject for the email |
||
| 182 | * |
||
| 183 | * @return object instantiated $this |
||
| 184 | */ |
||
| 185 | public function setSubject($subject) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Setter method for the plain text message |
||
| 194 | * |
||
| 195 | * @param string $message the plain-text message |
||
| 196 | * |
||
| 197 | * @return object instantiated $this |
||
| 198 | */ |
||
| 199 | public function setPlainMessage($message) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Setter method for the html message |
||
| 208 | * |
||
| 209 | * @param string $message the html message |
||
| 210 | * |
||
| 211 | * @return object instantiated $this |
||
| 212 | */ |
||
| 213 | public function setHTMLMessage($message) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Setter method for adding attachments |
||
| 222 | * |
||
| 223 | * @param string $path the full path of the attachment |
||
| 224 | * @param string $type mime type of the file |
||
| 225 | * @param string $title the title of the attachment (optional) |
||
| 226 | * |
||
| 227 | * @return object instantiated $this |
||
| 228 | */ |
||
| 229 | public function addAttachment($path, $type, $title = '') |
||
| 239 | |||
| 240 | /** |
||
| 241 | * The executing step, the actual sending of the email |
||
| 242 | * First checks to make sure the minimum fields are set (returns false if they are not) |
||
| 243 | * Second it attempts to send the mail with php's mail() (returns false if it fails) |
||
| 244 | * |
||
| 245 | * return boolean whether or not the email was valid & sent |
||
| 246 | */ |
||
| 247 | public function send() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Call to check the minimum required fields |
||
| 263 | * |
||
| 264 | * @return boolean whether or not the email meets the minimum required fields |
||
| 265 | */ |
||
| 266 | protected function checkRequiredFields() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Build the recipients from 'to' |
||
| 284 | * |
||
| 285 | * @return string comma-separated lit of recipients |
||
| 286 | */ |
||
| 287 | protected function buildTo() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Long, nasty creater of the actual message, with all the multipart logic you'd never want to see |
||
| 294 | * |
||
| 295 | * @return string email message |
||
| 296 | */ |
||
| 297 | protected function buildMessage() |
||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * Builder for the additional headers needed for multipart emails |
||
| 358 | * |
||
| 359 | * @return string headers needed for multipart |
||
| 360 | */ |
||
| 361 | protected function buildHeaders() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * File reader for attachments |
||
| 388 | * |
||
| 389 | * @return string binary representation of file, base64'd |
||
| 390 | */ |
||
| 391 | protected function buildAttachmentContent($attachment) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Holder for the boundry logic |
||
| 408 | * Not called/created unless it's needed |
||
| 409 | * |
||
| 410 | * @return string boundary |
||
| 411 | */ |
||
| 412 | protected function getBoundary() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Holder to create the alternative boundry logic |
||
| 422 | * Not called/created unless it's needed |
||
| 423 | * |
||
| 424 | * @return string alternative boundary |
||
| 425 | */ |
||
| 426 | protected function getAlternativeBoundary() |
||
| 433 | } |
||
| 434 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.