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 Mail 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 Mail, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace EvolutionCMS; |
||
| 6 | class Mail extends PHPMailer |
||
| 7 | { |
||
| 8 | /** |
||
| 9 | * @var string |
||
| 10 | */ |
||
| 11 | protected $mb_language = 'UNI'; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | protected $encode_header_method = ''; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var |
||
| 20 | */ |
||
| 21 | public $PluginDir; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Core $modx |
||
| 25 | */ |
||
| 26 | protected $modx; |
||
| 27 | |||
| 28 | public function init($modx = null) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Encode a header value (not including its label) optimally. |
||
| 110 | * Picks shortest of Q, B, or none. Result includes folding if needed. |
||
| 111 | * See RFC822 definitions for phrase, comment and text positions. |
||
| 112 | * |
||
| 113 | * @param string $str The header value to encode |
||
| 114 | * @param string $position What context the string will be used in |
||
| 115 | * |
||
| 116 | * @return string |
||
|
|
|||
| 117 | */ |
||
| 118 | public function EncodeHeader($str, $position = 'text') |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Create a message and send it. |
||
| 131 | * Uses the sending method specified by $Mailer. |
||
| 132 | * |
||
| 133 | * @throws PHPMailerException |
||
| 134 | * |
||
| 135 | * @return bool false on error - See the ErrorInfo property for details of the error |
||
| 136 | */ |
||
| 137 | public function Send() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $header The message headers |
||
| 147 | * @param string $body The message body |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | public function MailSend($header, $body) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $header The message headers |
||
| 198 | * @param string $body The message body |
||
| 199 | * |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | public function mbMailSend($header, $body) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Add an error message to the error container. |
||
| 255 | * |
||
| 256 | * @param string $msg |
||
| 257 | */ |
||
| 258 | public function SetError($msg) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param $address |
||
| 270 | * |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | public function address_split($address) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function getMIMEHeader() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public function getMIMEBody() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param string $header |
||
| 303 | * |
||
| 304 | * @return $this |
||
| 305 | */ |
||
| 306 | public function setMIMEHeader($header = '') |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $body |
||
| 315 | * |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function setMIMEBody($body = '') |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param string $header |
||
| 327 | * |
||
| 328 | * @return $this |
||
| 329 | */ |
||
| 330 | public function setMailHeader($header = '') |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public function getMessageID() |
||
| 344 | } |
||
| 345 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.