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