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 | View Code Duplication | 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 | View Code Duplication | 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 | View Code Duplication | 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 | View Code Duplication | public function setFrom($address, $title = '') |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Setter method for setting the single 'reply-to' field |
||
| 154 | * |
||
| 155 | * @param string $address email address for the reply-to |
||
| 156 | * @param string $title name of the reply-to (optional) |
||
| 157 | * |
||
| 158 | * @return object instantiated $this |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function setReplyTo($address, $title = '') |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Setter method for setting a subject |
||
| 172 | * |
||
| 173 | * @param string $subject subject for the email |
||
| 174 | * |
||
| 175 | * @return object instantiated $this |
||
| 176 | */ |
||
| 177 | public function setSubject($subject) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Setter method for the plain text message |
||
| 186 | * |
||
| 187 | * @param string $message the plain-text message |
||
| 188 | * |
||
| 189 | * @return object instantiated $this |
||
| 190 | */ |
||
| 191 | public function setPlainMessage($message) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Setter method for the html message |
||
| 200 | * |
||
| 201 | * @param string $message the html message |
||
| 202 | * |
||
| 203 | * @return object instantiated $this |
||
| 204 | */ |
||
| 205 | public function setHTMLMessage($message) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Setter method for adding attachments |
||
| 214 | * |
||
| 215 | * @param string $path the full path of the attachment |
||
| 216 | * @param string $type mime type of the file |
||
| 217 | * @param string $title the title of the attachment (optional) |
||
| 218 | * |
||
| 219 | * @return object instantiated $this |
||
| 220 | */ |
||
| 221 | public function addAttachment($path, $type, $title = '') |
||
| 231 | |||
| 232 | /** |
||
| 233 | * The executing step, the actual sending of the email |
||
| 234 | * First checks to make sure the minimum fields are set (returns false if they are not) |
||
| 235 | * Second it attempts to send the mail with php's mail() (returns false if it fails) |
||
| 236 | * |
||
| 237 | * return boolean whether or not the email was valid & sent |
||
| 238 | */ |
||
| 239 | public function send() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Call to check the minimum required fields |
||
| 255 | * |
||
| 256 | * @return boolean whether or not the email meets the minimum required fields |
||
| 257 | */ |
||
| 258 | protected function checkRequiredFields() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Build the recipients from 'to' |
||
| 276 | * |
||
| 277 | * @return string comma-separated lit of recipients |
||
| 278 | */ |
||
| 279 | protected function buildTo() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Long, nasty creater of the actual message, with all the multipart logic you'd never want to see |
||
| 286 | * |
||
| 287 | * @return string email message |
||
| 288 | */ |
||
| 289 | protected function buildMessage() |
||
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * Builder for the additional headers needed for multipart emails |
||
| 350 | * |
||
| 351 | * @return string headers needed for multipart |
||
| 352 | */ |
||
| 353 | protected function buildHeaders() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * File reader for attachments |
||
| 380 | * |
||
| 381 | * @return string binary representation of file, base64'd |
||
| 382 | */ |
||
| 383 | protected function buildAttachmentContent($attachment) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Holder for the boundry logic |
||
| 400 | * Not called/created unless it's needed |
||
| 401 | * |
||
| 402 | * @return string boundary |
||
| 403 | */ |
||
| 404 | protected function getBoundary() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Holder to create the alternative boundry logic |
||
| 414 | * Not called/created unless it's needed |
||
| 415 | * |
||
| 416 | * @return string alternative boundary |
||
| 417 | */ |
||
| 418 | protected function getAlternativeBoundary() |
||
| 425 | } |
||
| 426 |
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.