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 | */ |
||
| 64 | 173 | public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher) |
|
| 65 | { |
||
| 66 | 173 | $this->_eventDispatcher = $dispatcher; |
|
| 67 | 173 | $this->_buffer = $buf; |
|
| 68 | 173 | $this->_lookupHostname(); |
|
| 69 | 173 | } |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Set the name of the local domain which Swift will identify itself as. |
||
| 73 | * |
||
| 74 | * This should be a fully-qualified domain name and should be truly the domain |
||
| 75 | * you're using. |
||
| 76 | * |
||
| 77 | * If your server doesn't have a domain name, use the IP in square |
||
| 78 | * brackets (i.e. [127.0.0.1]). |
||
| 79 | * |
||
| 80 | * @param string $domain |
||
| 81 | * |
||
| 82 | * @return Swift_Transport_AbstractSmtpTransport |
||
| 83 | */ |
||
| 84 | 5 | public function setLocalDomain($domain) |
|
| 85 | { |
||
| 86 | 5 | $this->_domain = $domain; |
|
| 87 | |||
| 88 | 5 | return $this; |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get the name of the domain Swift will identify as. |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function getLocalDomain() |
||
| 97 | { |
||
| 98 | return $this->_domain; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Sets the source IP. |
||
| 103 | * |
||
| 104 | * @param string $source |
||
| 105 | */ |
||
| 106 | public function setSourceIp($source) |
||
| 107 | { |
||
| 108 | $this->_sourceIp = $source; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the IP used to connect to the destination. |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public function getSourceIp() |
||
| 117 | { |
||
| 118 | return $this->_sourceIp; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Start the SMTP connection. |
||
| 123 | */ |
||
| 124 | 140 | public function start() |
|
| 125 | { |
||
| 126 | 140 | if (!$this->_started) { |
|
| 127 | |||
| 128 | 140 | $evt = $this->_eventDispatcher->createTransportChangeEvent($this); |
|
| 129 | 140 | if ($evt) { |
|
| 130 | |||
| 131 | 22 | $this->_eventDispatcher->dispatchEvent($evt, 'beforeTransportStarted'); |
|
| 132 | 22 | if ($evt->bubbleCancelled()) { |
|
| 133 | 3 | return; |
|
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | try { |
||
| 138 | 137 | $this->_buffer->initialize($this->_getBufferParams()); |
|
| 139 | } catch (Swift_TransportException $e) { |
||
| 140 | $this->_throwException($e); |
||
| 141 | } |
||
| 142 | 137 | $this->_readGreeting(); |
|
| 143 | 131 | $this->_doHeloCommand(); |
|
| 144 | |||
| 145 | 128 | if ($evt) { |
|
| 146 | 19 | $this->_eventDispatcher->dispatchEvent($evt, 'transportStarted'); |
|
| 147 | } |
||
| 148 | |||
| 149 | 128 | $this->_started = true; |
|
| 150 | } |
||
| 151 | 128 | } |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Test if an SMTP connection has been established. |
||
| 155 | * |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | 22 | public function isStarted() |
|
| 159 | { |
||
| 160 | 22 | return $this->_started; |
|
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Send the given Message. |
||
| 165 | * |
||
| 166 | * Recipient/sender data will be retrieved from the Message API. |
||
| 167 | * The return value is the number of recipients who were accepted for delivery. |
||
| 168 | * |
||
| 169 | * @param Swift_Mime_Message $message |
||
| 170 | * @param string[]|null $failedRecipients An array of failures by-reference |
||
| 171 | * |
||
| 172 | * @return int |
||
| 173 | * |
||
| 174 | * @throws Exception |
||
| 175 | * @throws Swift_TransportException |
||
| 176 | */ |
||
| 177 | 84 | public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Stop the SMTP connection. |
||
| 237 | */ |
||
| 238 | 39 | public function stop() |
|
| 239 | { |
||
| 240 | 39 | if ($this->_started) { |
|
| 241 | |||
| 242 | 16 | $evt = $this->_eventDispatcher->createTransportChangeEvent($this); |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Check if this Transport mechanism is alive. |
||
| 271 | * |
||
| 272 | * If a Transport mechanism session is no longer functional, the method |
||
| 273 | * returns FALSE. It is the responsibility of the developer to handle this |
||
| 274 | * case and restart the Transport mechanism manually. |
||
| 275 | * |
||
| 276 | * @example |
||
| 277 | * |
||
| 278 | * if (!$transport->ping()) { |
||
| 279 | * $transport->stop(); |
||
| 280 | * $transport->start(); |
||
| 281 | * } |
||
| 282 | * |
||
| 283 | * The Transport mechanism will be started, if it is not already. |
||
| 284 | * |
||
| 285 | * It is undefined if the Transport mechanism attempts to restart as long as |
||
| 286 | * the return value reflects whether the mechanism is now functional. |
||
| 287 | * |
||
| 288 | * @return bool TRUE if the transport is alive |
||
| 289 | */ |
||
| 290 | public function ping() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Register a plugin. |
||
| 312 | * |
||
| 313 | * @param Swift_Events_EventListener $plugin |
||
| 314 | */ |
||
| 315 | 3 | public function registerPlugin(Swift_Events_EventListener $plugin) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Reset the current mail transaction. |
||
| 322 | */ |
||
| 323 | 12 | public function reset() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Get the IoBuffer where read/writes are occurring. |
||
| 330 | * |
||
| 331 | * @return Swift_Transport_IoBuffer |
||
| 332 | */ |
||
| 333 | 33 | public function getBuffer() |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Run a command against the buffer, expecting the given response codes. |
||
| 340 | * |
||
| 341 | * If no response codes are given, the response will not be validated. |
||
| 342 | * If codes are given, an exception will be thrown on an invalid response. |
||
| 343 | * |
||
| 344 | * @param string $command |
||
| 345 | * @param int[] $codes |
||
| 346 | * @param string[] $failures An array of failures by-reference |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | 137 | public function executeCommand($command, $codes = array(), &$failures = null) |
|
| 365 | |||
| 366 | /** Read the opening SMTP greeting */ |
||
| 367 | 137 | protected function _readGreeting() |
|
| 371 | |||
| 372 | /** Send the HELO welcome */ |
||
| 373 | 45 | protected function _doHeloCommand() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Send the MAIL FROM command |
||
| 382 | * |
||
| 383 | * @param string $address |
||
| 384 | */ |
||
| 385 | 25 | protected function _doMailFromCommand($address) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Send the RCPT TO command |
||
| 394 | * |
||
| 395 | * @param string $address |
||
| 396 | */ |
||
| 397 | 24 | protected function _doRcptToCommand($address) |
|
| 403 | |||
| 404 | /** Send the DATA command */ |
||
| 405 | 69 | protected function _doDataCommand() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Stream the contents of the message over the buffer |
||
| 412 | * |
||
| 413 | * @param Swift_Mime_Message $message |
||
| 414 | * |
||
| 415 | * @throws Swift_TransportException |
||
| 416 | */ |
||
| 417 | 63 | protected function _streamMessage(Swift_Mime_Message $message) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Determine the best-use reverse path for this message |
||
| 434 | * |
||
| 435 | * @param Swift_Mime_Message $message |
||
| 436 | * |
||
| 437 | * @return mixed|null|string |
||
| 438 | */ |
||
| 439 | 85 | View Code Duplication | protected function _getReversePath(Swift_Mime_Message $message) |
| 459 | |||
| 460 | /** |
||
| 461 | * Throw a TransportException, first sending it to any listeners. |
||
| 462 | * |
||
| 463 | * @param Swift_TransportException $e |
||
| 464 | * |
||
| 465 | * @throws Swift_TransportException |
||
| 466 | */ |
||
| 467 | 55 | View Code Duplication | protected function _throwException(Swift_TransportException $e) |
| 481 | |||
| 482 | /** |
||
| 483 | * Throws an Exception if a response code is incorrect |
||
| 484 | * |
||
| 485 | * @param string $response |
||
| 486 | * @param integer[] $wanted |
||
| 487 | * |
||
| 488 | * @throws Swift_TransportException |
||
| 489 | */ |
||
| 490 | 143 | protected function _assertResponseCode($response, $wanted) |
|
| 513 | |||
| 514 | /** |
||
| 515 | * Get an entire multi-line response using its sequence number |
||
| 516 | * |
||
| 517 | * @param int $seq |
||
| 518 | * |
||
| 519 | * @return string |
||
| 520 | * @throws Swift_TransportException |
||
| 521 | */ |
||
| 522 | 143 | protected function _getFullResponse($seq) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Send an email to the given recipients from the given reverse path |
||
| 545 | * |
||
| 546 | * @param Swift_Mime_Message $message |
||
| 547 | * @param string $reversePath |
||
| 548 | * @param array $recipients |
||
| 549 | * @param array $failedRecipients |
||
| 550 | * |
||
| 551 | * @return int |
||
| 552 | */ |
||
| 553 | 81 | private function _doMailTransaction($message, $reversePath, array $recipients, array &$failedRecipients) |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Send a message to the given To: recipients |
||
| 583 | * |
||
| 584 | * @param Swift_Mime_Message $message |
||
| 585 | * @param $reversePath |
||
| 586 | * @param array $to |
||
| 587 | * @param array $failedRecipients |
||
| 588 | * |
||
| 589 | * @return int |
||
| 590 | */ |
||
| 591 | 81 | private function _sendTo(Swift_Mime_Message $message, $reversePath, array $to, array &$failedRecipients) |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Send a message to all Bcc: recipients |
||
| 602 | * |
||
| 603 | * @param Swift_Mime_Message $message |
||
| 604 | * @param $reversePath |
||
| 605 | * @param array $bcc |
||
| 606 | * @param array $failedRecipients |
||
| 607 | * |
||
| 608 | * @return int |
||
| 609 | */ |
||
| 610 | 69 | private function _sendBcc(Swift_Mime_Message $message, $reversePath, array $bcc, array &$failedRecipients) |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Try to determine the hostname of the server this is run on |
||
| 628 | * |
||
| 629 | * @return bool |
||
| 630 | */ |
||
| 631 | 173 | private function _lookupHostname() |
|
| 659 | |||
| 660 | /** |
||
| 661 | * Determine is the $hostname is a fully-qualified name |
||
| 662 | * |
||
| 663 | * @param $hostname |
||
| 664 | * |
||
| 665 | * @return bool |
||
| 666 | */ |
||
| 667 | private function _isFqdn($hostname) |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Destructor. |
||
| 681 | */ |
||
| 682 | 27 | public function __destruct() |
|
| 686 | } |
||
| 687 |
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.