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 |
||
| 24 | class Swift_Transport_MailTransport implements Swift_Transport |
||
|
|
|||
| 25 | { |
||
| 26 | /** Additional parameters to pass to mail() */ |
||
| 27 | private $_extraParams = '-f%s'; |
||
| 28 | |||
| 29 | /** The event dispatcher from the plugin API */ |
||
| 30 | private $_eventDispatcher; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Create a new MailTransport with the $log. |
||
| 34 | * |
||
| 35 | * @param Swift_Events_EventDispatcher $eventDispatcher |
||
| 36 | */ |
||
| 37 | 16 | public function __construct(Swift_Events_EventDispatcher $eventDispatcher) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Not used. |
||
| 44 | */ |
||
| 45 | public function isStarted() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Not used. |
||
| 52 | */ |
||
| 53 | public function start() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Not used. |
||
| 59 | */ |
||
| 60 | public function stop() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Set the additional parameters used on the mail() function. |
||
| 66 | * |
||
| 67 | * This string is formatted for sprintf() where %s is the sender address. |
||
| 68 | * |
||
| 69 | * @param string $params |
||
| 70 | * |
||
| 71 | * @return Swift_Transport_MailTransport |
||
| 72 | */ |
||
| 73 | 2 | public function setExtraParams($params) |
|
| 74 | { |
||
| 75 | 2 | $this->_extraParams = $params; |
|
| 76 | |||
| 77 | 2 | return $this; |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get the additional parameters used on the mail() function. |
||
| 82 | * |
||
| 83 | * This string is formatted for sprintf() where %s is the sender address. |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function getExtraParams() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Send the given Message. |
||
| 94 | * |
||
| 95 | * Recipient/sender data will be retrieved from the Message API. |
||
| 96 | * The return value is the number of recipients who were accepted for delivery. |
||
| 97 | * |
||
| 98 | * @param Swift_Mime_Message $message |
||
| 99 | * @param string[] $failedRecipients An array of failures by-reference |
||
| 100 | * |
||
| 101 | * @return int |
||
| 102 | * |
||
| 103 | * @throws Swift_TransportException |
||
| 104 | */ |
||
| 105 | 15 | public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
|
| 106 | { |
||
| 107 | 15 | $failedRecipients = (array)$failedRecipients; |
|
| 108 | |||
| 109 | 15 | $evt = $this->_eventDispatcher->createSendEvent($this, $message); |
|
| 110 | 15 | if ($evt) { |
|
| 111 | |||
| 112 | $this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed'); |
||
| 113 | if ($evt->bubbleCancelled()) { |
||
| 114 | return 0; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | $count = ( |
||
| 119 | 15 | count((array)$message->getTo()) |
|
| 120 | 15 | + count((array)$message->getCc()) |
|
| 121 | 15 | + count((array)$message->getBcc()) |
|
| 122 | ); |
||
| 123 | |||
| 124 | /* |
||
| 125 | // TODO: check if we need this check, breaks "Mockery"-Tests |
||
| 126 | if ($count == 0) { |
||
| 127 | $this->_throwException(new Swift_TransportException('Cannot send message without a recipient')); |
||
| 128 | } |
||
| 129 | */ |
||
| 130 | |||
| 131 | 15 | $toHeader = $message->getHeaders()->get('To'); |
|
| 132 | 15 | $subjectHeader = $message->getHeaders()->get('Subject'); |
|
| 133 | |||
| 134 | 15 | if (!$toHeader) { |
|
| 135 | $this->_throwException(new Swift_TransportException('Cannot send message without a recipient')); |
||
| 136 | } |
||
| 137 | |||
| 138 | 15 | $to = $toHeader->getFieldBody(); |
|
| 139 | 15 | $subject = $subjectHeader ? $subjectHeader->getFieldBody() : ''; |
|
| 140 | |||
| 141 | 15 | $reversePath = $this->_getReversePath($message); |
|
| 142 | |||
| 143 | // Remove headers that would otherwise be duplicated |
||
| 144 | 15 | $message->getHeaders()->remove('To'); |
|
| 145 | 15 | $message->getHeaders()->remove('Subject'); |
|
| 146 | |||
| 147 | 15 | $messageStr = $message->toString(); |
|
| 148 | |||
| 149 | 15 | $message->getHeaders()->set($toHeader); |
|
| 150 | 15 | $message->getHeaders()->set($subjectHeader); |
|
| 151 | |||
| 152 | // Separate headers from body |
||
| 153 | 15 | if (false !== $endHeaders = strpos($messageStr, "\r\n\r\n")) { |
|
| 154 | 3 | $headers = substr($messageStr, 0, $endHeaders) . "\r\n"; // Keep last EOL |
|
| 155 | 3 | $body = substr($messageStr, $endHeaders + 4); |
|
| 156 | } else { |
||
| 157 | 12 | $headers = $messageStr . "\r\n"; |
|
| 158 | 12 | $body = ''; |
|
| 159 | } |
||
| 160 | |||
| 161 | 15 | unset($messageStr); |
|
| 162 | |||
| 163 | 15 | if ("\r\n" != PHP_EOL) { |
|
| 164 | // Non-windows (not using SMTP) |
||
| 165 | 15 | $headers = str_replace("\r\n", PHP_EOL, $headers); |
|
| 166 | 15 | $subject = str_replace("\r\n", PHP_EOL, $subject); |
|
| 167 | 15 | $body = str_replace("\r\n", PHP_EOL, $body); |
|
| 168 | } else { |
||
| 169 | // Windows, using SMTP |
||
| 170 | $headers = str_replace("\r\n.", "\r\n..", $headers); |
||
| 171 | $subject = str_replace("\r\n.", "\r\n..", $subject); |
||
| 172 | $body = str_replace("\r\n.", "\r\n..", $body); |
||
| 173 | } |
||
| 174 | |||
| 175 | 15 | if ($this->mail($to, $subject, $body, $headers, $this->_formatExtraParams($this->_extraParams, $reversePath))) { |
|
| 176 | 1 | if ($evt) { |
|
| 177 | $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS); |
||
| 178 | $evt->setFailedRecipients($failedRecipients); |
||
| 179 | 1 | $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed'); |
|
| 180 | } |
||
| 181 | } else { |
||
| 182 | 14 | $failedRecipients = array_merge( |
|
| 183 | $failedRecipients, |
||
| 184 | 14 | array_keys((array)$message->getTo()), |
|
| 185 | 14 | array_keys((array)$message->getCc()), |
|
| 186 | 14 | array_keys((array)$message->getBcc()) |
|
| 187 | ); |
||
| 188 | |||
| 189 | 14 | if ($evt) { |
|
| 190 | $evt->setResult(Swift_Events_SendEvent::RESULT_FAILED); |
||
| 191 | $evt->setFailedRecipients($failedRecipients); |
||
| 192 | $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed'); |
||
| 193 | } |
||
| 194 | |||
| 195 | // TODO: Why do we do this only on error? |
||
| 196 | // -> take a look at "AbstractSmtpTransport" |
||
| 197 | // |
||
| 198 | 14 | $message->generateId(); // Make sure a new Message ID is used |
|
| 199 | |||
| 200 | 14 | $count = 0; |
|
| 201 | } |
||
| 202 | |||
| 203 | 15 | return $count; |
|
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Register a plugin. |
||
| 208 | * |
||
| 209 | * @param Swift_Events_EventListener $plugin |
||
| 210 | */ |
||
| 211 | public function registerPlugin(Swift_Events_EventListener $plugin) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Throw a TransportException, first sending it to any listeners |
||
| 218 | * |
||
| 219 | * @param Swift_TransportException $e |
||
| 220 | * |
||
| 221 | * @throws Swift_TransportException |
||
| 222 | */ |
||
| 223 | View Code Duplication | protected function _throwException(Swift_TransportException $e) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Send mail via the mail() function. |
||
| 240 | * |
||
| 241 | * This method takes the same arguments as PHP mail(). |
||
| 242 | * |
||
| 243 | * @param string $to |
||
| 244 | * @param string $subject |
||
| 245 | * @param string $body |
||
| 246 | * @param string $headers |
||
| 247 | * @param string $extraParams |
||
| 248 | * |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public function mail($to, $subject, $body, $headers = null, $extraParams = null) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Determine the best-use reverse path for this message |
||
| 262 | * |
||
| 263 | * @param Swift_Mime_Message $message |
||
| 264 | * |
||
| 265 | * @return mixed|null|string |
||
| 266 | */ |
||
| 267 | 15 | View Code Duplication | private function _getReversePath(Swift_Mime_Message $message) |
| 285 | |||
| 286 | /** |
||
| 287 | * Return php mail extra params to use for invoker->mail. |
||
| 288 | * |
||
| 289 | * @param $extraParams |
||
| 290 | * @param $reversePath |
||
| 291 | * |
||
| 292 | * @return mixed <string|null> |
||
| 293 | */ |
||
| 294 | 15 | private function _formatExtraParams($extraParams, $reversePath) |
|
| 304 | } |
||
| 305 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.