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 |
||
| 53 | class Mailer implements IMailer { |
||
| 54 | /** @var \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport Cached transport */ |
||
| 55 | private $instance = null; |
||
| 56 | /** @var IConfig */ |
||
| 57 | private $config; |
||
| 58 | /** @var ILogger */ |
||
| 59 | private $logger; |
||
| 60 | /** @var Defaults */ |
||
| 61 | private $defaults; |
||
| 62 | /** @var IURLGenerator */ |
||
| 63 | private $urlGenerator; |
||
| 64 | /** @var IL10N */ |
||
| 65 | private $l10n; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param IConfig $config |
||
| 69 | * @param ILogger $logger |
||
| 70 | * @param Defaults $defaults |
||
| 71 | * @param IURLGenerator $urlGenerator |
||
| 72 | * @param IL10N $l10n |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function __construct(IConfig $config, |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Creates a new message object that can be passed to send() |
||
| 88 | * |
||
| 89 | * @return IMessage |
||
| 90 | */ |
||
| 91 | public function createMessage() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string|null $data |
||
| 97 | * @param string|null $filename |
||
| 98 | * @param string|null $contentType |
||
| 99 | * @return IAttachment |
||
| 100 | * @since 13.0.0 |
||
| 101 | */ |
||
| 102 | public function createAttachment($data = null, $filename = null, $contentType = null) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $path |
||
| 108 | * @param string|null $contentType |
||
| 109 | * @return IAttachment |
||
| 110 | * @since 13.0.0 |
||
| 111 | */ |
||
| 112 | public function createAttachmentFromPath($path, $contentType = null) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Creates a new email template object |
||
| 118 | * |
||
| 119 | * @param string $emailId |
||
| 120 | * @param array $data |
||
| 121 | * @return IEMailTemplate |
||
| 122 | * @since 12.0.0 |
||
| 123 | */ |
||
| 124 | public function createEMailTemplate($emailId, array $data = []) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Send the specified message. Also sets the from address to the value defined in config.php |
||
| 148 | * if no-one has been passed. |
||
| 149 | * |
||
| 150 | * @param IMessage|Message $message Message to send |
||
| 151 | * @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and |
||
| 152 | * therefore should be considered |
||
| 153 | * @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address |
||
| 154 | * has been supplied.) |
||
| 155 | */ |
||
| 156 | public function send(IMessage $message) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Checks if an e-mail address is valid |
||
| 187 | * |
||
| 188 | * @param string $email Email address to be validated |
||
| 189 | * @return bool True if the mail address is valid, false otherwise |
||
| 190 | */ |
||
| 191 | public function validateMailAddress($email) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains |
||
| 197 | * |
||
| 198 | * FIXME: Remove this once SwiftMailer supports IDN |
||
| 199 | * |
||
| 200 | * @param string $email |
||
| 201 | * @return string Converted mail address if `idn_to_ascii` exists |
||
| 202 | */ |
||
| 203 | protected function convertEmail($email) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Returns whatever transport is configured within the config |
||
| 215 | * |
||
| 216 | * @return \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport |
||
| 217 | */ |
||
| 218 | protected function getInstance() { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns the SMTP transport |
||
| 243 | * |
||
| 244 | * @return \Swift_SmtpTransport |
||
| 245 | */ |
||
| 246 | protected function getSmtpInstance() { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Returns the sendmail transport |
||
| 266 | * |
||
| 267 | * @return \Swift_SendmailTransport |
||
| 268 | */ |
||
| 269 | protected function getSendMailInstance() { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Returns the mail transport |
||
| 284 | * |
||
| 285 | * @return \Swift_MailTransport |
||
| 286 | */ |
||
| 287 | protected function getMailInstance() { |
||
| 290 | |||
| 291 | } |
||
| 292 |