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 POP 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 POP, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class POP { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Email regular expression. |
||
| 19 | */ |
||
| 20 | const EMAIL_REGEX = '/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Instance of the connection to the POP server. |
||
| 24 | * |
||
| 25 | * @var resource |
||
| 26 | */ |
||
| 27 | public $connection = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * List of the acceptable for a bounce emails. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | public $bouncedWhiteList = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * List of the acceptable for a bounce email domains. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | public $bouncedWhiteDomains = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Open connection to POP server. |
||
| 45 | * |
||
| 46 | * @param string $host POP server host (default: localhost). |
||
| 47 | * @return int $port POP server port (default: 110) |
||
| 48 | * @param string $username User name. |
||
| 49 | * @param string $password User password. |
||
| 50 | * |
||
| 51 | * @return resource Connection to the POP server or throw POPServerException |
||
| 52 | * @throws POPServerException |
||
| 53 | */ |
||
| 54 | public function open($host = "localhost", $port = 110, $username = "", $password = "") { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Close connection to the POP server. |
||
| 90 | */ |
||
| 91 | public function close() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Delete message from POP server. |
||
| 100 | * |
||
| 101 | * @return int $messageId Id of the message. |
||
| 102 | * |
||
| 103 | * @return string Response string. |
||
| 104 | */ |
||
| 105 | public function delete($messageId) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Count messages in POP server. |
||
| 113 | * |
||
| 114 | * @return type |
||
| 115 | */ |
||
| 116 | public function countMessages() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Return message header. |
||
| 126 | * |
||
| 127 | * @return int $messageNumber Number of the message. |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public function messageHeader($messageNumber) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Return parsed message header. |
||
| 148 | * |
||
| 149 | * @return int $messageNumber Number of the message. |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function getParsedMessageHeader($messageNumber) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Return message by number. |
||
| 161 | * |
||
| 162 | * @return int $messageNumber Number of the message |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getMessage($messageNumber) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Return list of the messages in the POP server mail box. |
||
| 186 | * |
||
| 187 | * @return array<string> |
||
| 188 | */ |
||
| 189 | public function getMessages() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Return list of bounced e-mail addresses. |
||
| 206 | * |
||
| 207 | * @return array<string> |
||
| 208 | */ |
||
| 209 | public function getBouncedEmails($delete = true, $number = null) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Return list of e-mail addresses except white lists. |
||
| 266 | * |
||
| 267 | * @return array<string> |
||
| 268 | */ |
||
| 269 | public function getEmails($delete = true, $number = null) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Returns only bounced e-mails from matches. |
||
| 297 | * |
||
| 298 | * @param array $emailData Matches array after preg_math_all funciton. |
||
| 299 | * |
||
| 300 | * @return array<string> List of bounced e-mails. |
||
| 301 | */ |
||
| 302 | private function filterBouncedEmails($emailData) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Detects if email is bounced. |
||
| 325 | * |
||
| 326 | * @param string $email Email address. |
||
| 327 | * |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | private function isBouncedEmail($email) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Parse message header. |
||
| 349 | * |
||
| 350 | * @param string $header Header of the message. |
||
| 351 | * |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | public function parseHeader($header) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get a unique-id listing for one or all messages. |
||
| 375 | * |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | public function uniqueListing() { |
||
| 400 | } |
||
| 401 | |||
| 403 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.