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 $headers */ |
||
| 26 | protected $headers = array(); |
||
| 27 | |||
| 28 | /** @var string $plainMessage */ |
||
| 29 | protected $plainMessage; |
||
| 30 | |||
| 31 | /** @var string $htmlMessage */ |
||
| 32 | protected $htmlMessage; |
||
| 33 | |||
| 34 | /** @var array $attachments */ |
||
| 35 | protected $attachments = array(); |
||
| 36 | |||
| 37 | /** @var string $boundaryMixed */ |
||
| 38 | protected $boundaryMixed; |
||
| 39 | |||
| 40 | /** @var string $boundaryAlternative */ |
||
| 41 | protected $boundaryAlternative; |
||
| 42 | |||
| 43 | /** @var LoggerInterface */ |
||
| 44 | protected $logger; |
||
| 45 | |||
| 46 | /** @var string LINE_BREAK */ |
||
| 47 | const LINE_BREAK = "\r\n"; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $mailer |
||
| 51 | */ |
||
| 52 | public function __construct($mailer = null) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param LoggerInterface $logger |
||
| 66 | * |
||
| 67 | * @return $this; |
||
|
|
|||
| 68 | */ |
||
| 69 | public function setLogger(LoggerInterface $logger) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Setter method for adding recipients |
||
| 78 | * |
||
| 79 | * @param string $address email address for the recipient |
||
| 80 | * @param string $title name of the recipient (optional) |
||
| 81 | |||
| 82 | * @return object instantiated $this |
||
| 83 | */ |
||
| 84 | public function addTo($address, $title = '') |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Setter method for adding cc recipients |
||
| 96 | * |
||
| 97 | * @param string $address email address for the cc recipient |
||
| 98 | * @param string $title name of the cc recipient (optional) |
||
| 99 | * |
||
| 100 | * @return object instantiated $this |
||
| 101 | */ |
||
| 102 | 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 = '') |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Setter method for setting the single 'from' field |
||
| 140 | * |
||
| 141 | * @param string $address email address for the sender |
||
| 142 | * @param string $title name of the sender (optional) |
||
| 143 | * |
||
| 144 | * @return object instantiated $this |
||
| 145 | */ |
||
| 146 | public function setFrom($address, $title = '') |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Setter method for setting the single 'reply-to' field |
||
| 155 | * |
||
| 156 | * @param string $address email address for the reply-to |
||
| 157 | * @param string $title name of the reply-to (optional) |
||
| 158 | * |
||
| 159 | * @return object instantiated $this |
||
| 160 | */ |
||
| 161 | public function setReplyTo($address, $title = '') |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $address |
||
| 170 | * @param string $title |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | protected function formatEmailAddress($address, $title) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Setter method for setting a subject |
||
| 184 | * |
||
| 185 | * @param string $subject subject for the email |
||
| 186 | * |
||
| 187 | * @return object instantiated $this |
||
| 188 | */ |
||
| 189 | public function setSubject($subject) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Setter method for the plain text message |
||
| 198 | * |
||
| 199 | * @param string $message the plain-text message |
||
| 200 | * |
||
| 201 | * @return object instantiated $this |
||
| 202 | */ |
||
| 203 | public function setPlainMessage($message) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Setter method for the html message |
||
| 212 | * |
||
| 213 | * @param string $message the html message |
||
| 214 | * |
||
| 215 | * @return object instantiated $this |
||
| 216 | */ |
||
| 217 | public function setHTMLMessage($message) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Setter method for adding attachments |
||
| 226 | * |
||
| 227 | * @param string $path the full path of the attachment |
||
| 228 | * @param string $type mime type of the file |
||
| 229 | * @param string $title the title of the attachment (optional) |
||
| 230 | * |
||
| 231 | * @return object instantiated $this |
||
| 232 | */ |
||
| 233 | public function addAttachment($path, $type, $title = '') |
||
| 243 | |||
| 244 | /** |
||
| 245 | * The executing step, the actual sending of the email |
||
| 246 | * First checks to make sure the minimum fields are set (returns false if they are not) |
||
| 247 | * Second it attempts to send the mail with php's mail() (returns false if it fails) |
||
| 248 | * |
||
| 249 | * return boolean whether or not the email was valid & sent |
||
| 250 | */ |
||
| 251 | public function send() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Call to check the minimum required fields |
||
| 267 | * |
||
| 268 | * @return boolean whether or not the email meets the minimum required fields |
||
| 269 | */ |
||
| 270 | protected function checkRequiredFields() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Build the recipients from 'to' |
||
| 292 | * |
||
| 293 | * @return string comma-separated lit of recipients |
||
| 294 | */ |
||
| 295 | protected function buildTo() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Returns a simple email message without attachments |
||
| 302 | * |
||
| 303 | * @return string email message |
||
| 304 | */ |
||
| 305 | protected function buildMessage() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Build multi-part message with attachments |
||
| 331 | * |
||
| 332 | * @return string email message |
||
| 333 | */ |
||
| 334 | protected function buildMessageWithAttachments() |
||
| 372 | |||
| 373 | |||
| 374 | /** |
||
| 375 | * Shared holder for the plain message header |
||
| 376 | * |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | protected function getPlainMessageHeader() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Shared holder for the html message header |
||
| 390 | * |
||
| 391 | * @return array |
||
| 392 | */ |
||
| 393 | protected function getHtmlMessageHeader() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Builder for the additional headers needed for multipart emails |
||
| 404 | * |
||
| 405 | * @return string headers needed for multipart |
||
| 406 | */ |
||
| 407 | protected function buildHeaders() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * File reader for attachments |
||
| 439 | * |
||
| 440 | * @param string $path filepath of the attachment |
||
| 441 | * |
||
| 442 | * @return string binary representation of file, base64'd |
||
| 443 | */ |
||
| 444 | protected function buildAttachmentContent($path) |
||
| 459 | } |
||
| 460 |
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.