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 Swift_Transport_AbstractSmtpTransport 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 Swift_Transport_AbstractSmtpTransport, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Input-Output buffer for sending/receiving SMTP commands and responses |
||
| 20 | * |
||
| 21 | * @var Swift_Transport_IoBuffer|Swift_Transport_StreamBuffer |
||
| 22 | */ |
||
| 23 | protected $_buffer; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Connection status |
||
| 27 | * |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | protected $_started = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The domain name to use in HELO command |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $_domain = '[127.0.0.1]'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The event dispatching layer |
||
| 41 | * |
||
| 42 | * @var Swift_Events_EventDispatcher |
||
| 43 | */ |
||
| 44 | protected $_eventDispatcher; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Source Ip |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $_sourceIp; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Return an array of params for the Buffer |
||
| 55 | */ |
||
| 56 | abstract protected function _getBufferParams(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Creates a new EsmtpTransport using the given I/O buffer. |
||
| 60 | * |
||
| 61 | * @param Swift_Transport_IoBuffer $buf |
||
| 62 | * @param Swift_Events_EventDispatcher $dispatcher |
||
| 63 | * @param string $localDomain |
||
| 64 | */ |
||
| 65 | 176 | public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher, $localDomain = '127.0.0.1') |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Set the name of the local domain which Swift will identify itself as. |
||
| 74 | * |
||
| 75 | * This should be a fully-qualified domain name and should be truly the domain |
||
| 76 | * you're using. |
||
| 77 | * |
||
| 78 | * If your server does not have a domain name, use the IP address. This will |
||
| 79 | * automatically be wrapped in square brackets as described in RFC 5321, |
||
| 80 | * section 4.1.3. |
||
| 81 | * |
||
| 82 | * @param string $domain |
||
| 83 | * |
||
| 84 | * @return $this |
||
| 85 | */ |
||
| 86 | 176 | public function setLocalDomain($domain) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Get the name of the domain Swift will identify as. |
||
| 103 | * |
||
| 104 | * If an IP address was specified, this will be returned wrapped in square |
||
| 105 | * brackets as described in RFC 5321, section 4.1.3. |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | 4 | public function getLocalDomain() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Sets the source IP. |
||
| 116 | * |
||
| 117 | * @param string $source |
||
| 118 | */ |
||
| 119 | public function setSourceIp($source) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns the IP used to connect to the destination. |
||
| 126 | * |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | public function getSourceIp() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Start the SMTP connection. |
||
| 136 | */ |
||
| 137 | 140 | public function start() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Test if an SMTP connection has been established. |
||
| 168 | * |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | 23 | public function isStarted() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Send the given Message. |
||
| 178 | * |
||
| 179 | * Recipient/sender data will be retrieved from the Message API. |
||
| 180 | * The return value is the number of recipients who were accepted for delivery. |
||
| 181 | * |
||
| 182 | * @param Swift_Mime_Message $message |
||
| 183 | * @param string[]|null $failedRecipients An array of failures by-reference |
||
| 184 | * |
||
| 185 | * @return int |
||
| 186 | * |
||
| 187 | * @throws Exception |
||
| 188 | * @throws Swift_TransportException |
||
| 189 | */ |
||
| 190 | 84 | public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Stop the SMTP connection. |
||
| 250 | */ |
||
| 251 | 42 | public function stop() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Register a plugin. |
||
| 284 | * |
||
| 285 | * @param Swift_Events_EventListener $plugin |
||
| 286 | */ |
||
| 287 | 3 | public function registerPlugin(Swift_Events_EventListener $plugin) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Reset the current mail transaction. |
||
| 294 | */ |
||
| 295 | 12 | public function reset() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Get the IoBuffer where read/writes are occurring. |
||
| 302 | * |
||
| 303 | * @return Swift_Transport_IoBuffer |
||
| 304 | */ |
||
| 305 | 34 | public function getBuffer() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Run a command against the buffer, expecting the given response codes. |
||
| 312 | * |
||
| 313 | * If no response codes are given, the response will not be validated. |
||
| 314 | * If codes are given, an exception will be thrown on an invalid response. |
||
| 315 | * |
||
| 316 | * @param string $command |
||
| 317 | * @param int[] $codes |
||
| 318 | * @param string[] $failures An array of failures by-reference |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | 137 | public function executeCommand($command, $codes = array(), &$failures = null) |
|
| 337 | |||
| 338 | /** Read the opening SMTP greeting */ |
||
| 339 | 137 | protected function _readGreeting() |
|
| 343 | |||
| 344 | /** Send the HELO welcome */ |
||
| 345 | 45 | protected function _doHeloCommand() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Send the MAIL FROM command |
||
| 354 | * |
||
| 355 | * @param string $address |
||
| 356 | */ |
||
| 357 | 25 | protected function _doMailFromCommand($address) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Send the RCPT TO command |
||
| 366 | * |
||
| 367 | * @param string $address |
||
| 368 | */ |
||
| 369 | 24 | protected function _doRcptToCommand($address) |
|
| 375 | |||
| 376 | /** Send the DATA command */ |
||
| 377 | 69 | protected function _doDataCommand() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Stream the contents of the message over the buffer |
||
| 384 | * |
||
| 385 | * @param Swift_Mime_Message $message |
||
| 386 | * |
||
| 387 | * @throws Swift_TransportException |
||
| 388 | */ |
||
| 389 | 63 | protected function _streamMessage(Swift_Mime_Message $message) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Determine the best-use reverse path for this message |
||
| 406 | * |
||
| 407 | * @param Swift_Mime_Message $message |
||
| 408 | * |
||
| 409 | * @return mixed|null|string |
||
| 410 | */ |
||
| 411 | 85 | View Code Duplication | protected function _getReversePath(Swift_Mime_Message $message) |
| 431 | |||
| 432 | /** |
||
| 433 | * Throw a TransportException, first sending it to any listeners. |
||
| 434 | * |
||
| 435 | * @param Swift_TransportException $e |
||
| 436 | * |
||
| 437 | * @throws Swift_TransportException |
||
| 438 | */ |
||
| 439 | 55 | View Code Duplication | protected function _throwException(Swift_TransportException $e) |
| 453 | |||
| 454 | /** |
||
| 455 | * Throws an Exception if a response code is incorrect |
||
| 456 | * |
||
| 457 | * @param string $response |
||
| 458 | * @param integer[] $wanted |
||
| 459 | * |
||
| 460 | * @throws Swift_TransportException |
||
| 461 | */ |
||
| 462 | 143 | protected function _assertResponseCode($response, $wanted) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Get an entire multi-line response using its sequence number |
||
| 488 | * |
||
| 489 | * @param int $seq |
||
| 490 | * |
||
| 491 | * @return string |
||
| 492 | * @throws Swift_TransportException |
||
| 493 | */ |
||
| 494 | 143 | protected function _getFullResponse($seq) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * Send an email to the given recipients from the given reverse path |
||
| 517 | * |
||
| 518 | * @param Swift_Mime_Message $message |
||
| 519 | * @param string $reversePath |
||
| 520 | * @param array $recipients |
||
| 521 | * @param array $failedRecipients |
||
| 522 | * |
||
| 523 | * @return int |
||
| 524 | * |
||
| 525 | * @throws Swift_TransportException |
||
| 526 | */ |
||
| 527 | 81 | private function _doMailTransaction($message, $reversePath, array $recipients, array &$failedRecipients) |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Send a message to the given To: recipients |
||
| 558 | * |
||
| 559 | * @param Swift_Mime_Message $message |
||
| 560 | * @param $reversePath |
||
| 561 | * @param array $to |
||
| 562 | * @param array $failedRecipients |
||
| 563 | * |
||
| 564 | * @return int |
||
| 565 | * |
||
| 566 | * @throws Swift_TransportException |
||
| 567 | */ |
||
| 568 | 81 | private function _sendTo(Swift_Mime_Message $message, $reversePath, array $to, array &$failedRecipients) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Send a message to all Bcc: recipients |
||
| 579 | * |
||
| 580 | * @param Swift_Mime_Message $message |
||
| 581 | * @param $reversePath |
||
| 582 | * @param array $bcc |
||
| 583 | * @param array $failedRecipients |
||
| 584 | * |
||
| 585 | * @return int |
||
| 586 | * |
||
| 587 | * @throws Swift_TransportException |
||
| 588 | */ |
||
| 589 | 69 | private function _sendBcc(Swift_Mime_Message $message, $reversePath, array $bcc, array &$failedRecipients) |
|
| 604 | |||
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * Destructor. |
||
| 609 | */ |
||
| 610 | 30 | public function __destruct() |
|
| 617 | } |
||
| 618 |
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.