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:
| 1 | <?php |
||
| 50 | class Mailer implements IMailer { |
||
| 51 | /** @var \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport Cached transport */ |
||
| 52 | private $instance = null; |
||
| 53 | /** @var IConfig */ |
||
| 54 | private $config; |
||
| 55 | /** @var ILogger */ |
||
| 56 | private $logger; |
||
| 57 | /** @var Defaults */ |
||
| 58 | private $defaults; |
||
| 59 | /** @var IURLGenerator */ |
||
| 60 | private $urlGenerator; |
||
| 61 | /** @var IL10N */ |
||
| 62 | private $l10n; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param IConfig $config |
||
| 66 | * @param ILogger $logger |
||
| 67 | * @param Defaults $defaults |
||
| 68 | * @param IURLGenerator $urlGenerator |
||
| 69 | * @param IL10N $l10n |
||
| 70 | */ |
||
| 71 | View Code Duplication | public function __construct(IConfig $config, |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Creates a new message object that can be passed to send() |
||
| 85 | * |
||
| 86 | * @return Message |
||
| 87 | */ |
||
| 88 | public function createMessage() { |
||
| 91 | |||
| 92 | public function createEMailTemplate() { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Send the specified message. Also sets the from address to the value defined in config.php |
||
| 102 | * if no-one has been passed. |
||
| 103 | * |
||
| 104 | * @param Message $message Message to send |
||
| 105 | * @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and |
||
| 106 | * therefore should be considered |
||
| 107 | * @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address |
||
| 108 | * has been supplied.) |
||
| 109 | */ |
||
| 110 | public function send(Message $message) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Checks if an e-mail address is valid |
||
| 141 | * |
||
| 142 | * @param string $email Email address to be validated |
||
| 143 | * @return bool True if the mail address is valid, false otherwise |
||
| 144 | */ |
||
| 145 | public function validateMailAddress($email) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains |
||
| 151 | * |
||
| 152 | * FIXME: Remove this once SwiftMailer supports IDN |
||
| 153 | * |
||
| 154 | * @param string $email |
||
| 155 | * @return string Converted mail address if `idn_to_ascii` exists |
||
| 156 | */ |
||
| 157 | protected function convertEmail($email) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns whatever transport is configured within the config |
||
| 169 | * |
||
| 170 | * @return \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport |
||
| 171 | */ |
||
| 172 | protected function getInstance() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Returns the SMTP transport |
||
| 197 | * |
||
| 198 | * @return \Swift_SmtpTransport |
||
| 199 | */ |
||
| 200 | protected function getSmtpInstance() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Returns the sendmail transport |
||
| 220 | * |
||
| 221 | * @return \Swift_SendmailTransport |
||
| 222 | */ |
||
| 223 | protected function getSendMailInstance() { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Returns the mail transport |
||
| 238 | * |
||
| 239 | * @return \Swift_MailTransport |
||
| 240 | */ |
||
| 241 | protected function getMailInstance() { |
||
| 244 | |||
| 245 | } |
||
| 246 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.