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() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Encode a header value (not including its label) optimally. |
||
| 106 | * Picks shortest of Q, B, or none. Result includes folding if needed. |
||
| 107 | * See RFC822 definitions for phrase, comment and text positions. |
||
| 108 | * |
||
| 109 | * @param string $str The header value to encode |
||
| 110 | * @param string $position What context the string will be used in |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function EncodeHeader($str, $position = 'text') |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Create a message and send it. |
||
| 127 | * Uses the sending method specified by $Mailer. |
||
| 128 | * |
||
| 129 | * @throws PHPMailerException |
||
| 130 | * |
||
| 131 | * @return bool false on error - See the ErrorInfo property for details of the error |
||
| 132 | */ |
||
| 133 | public function Send() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param string $header The message headers |
||
| 143 | * @param string $body The message body |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function MailSend($header, $body) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $header The message headers |
||
| 194 | * @param string $body The message body |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | public function mbMailSend($header, $body) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Add an error message to the error container. |
||
| 251 | * |
||
| 252 | * @param string $msg |
||
| 253 | */ |
||
| 254 | public function SetError($msg) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param $address |
||
| 266 | * |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | public function address_split($address) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getMIMEHeader() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public function getMIMEBody() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $header |
||
| 299 | * |
||
| 300 | * @return $this |
||
| 301 | */ |
||
| 302 | public function setMIMEHeader($header = '') |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param string $body |
||
| 311 | * |
||
| 312 | * @return $this |
||
| 313 | */ |
||
| 314 | public function setMIMEBody($body = '') |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param string $header |
||
| 323 | * |
||
| 324 | * @return $this |
||
| 325 | */ |
||
| 326 | public function setMailHeader($header = '') |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | public function getMessageID() |
||
| 340 | } |
||
| 341 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.