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 |
||
| 16 | class Swift_Transport_FailoverTransport extends Swift_Transport_LoadBalancedTransport |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Registered transport currently used. |
||
| 20 | * |
||
| 21 | * @var Swift_Transport |
||
| 22 | */ |
||
| 23 | private $_currentTransport; |
||
| 24 | |||
| 25 | // needed as __construct is called from elsewhere explicitly |
||
| 26 | 11 | public function __construct() |
|
| 30 | |||
| 31 | /** |
||
| 32 | * Send the given Message. |
||
| 33 | * |
||
| 34 | * Recipient/sender data will be retrieved from the Message API. |
||
| 35 | * The return value is the number of recipients who were accepted for delivery. |
||
| 36 | * |
||
| 37 | * @param Swift_Mime_Message $message |
||
| 38 | * @param string[] $failedRecipients An array of failures by-reference |
||
| 39 | * |
||
| 40 | * @return int |
||
| 41 | * @throws Swift_TransportException |
||
| 42 | */ |
||
| 43 | 8 | View Code Duplication | public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
| 44 | { |
||
| 45 | 8 | $maxTransports = count($this->_transports); |
|
| 46 | 8 | $sent = 0; |
|
| 47 | 8 | $this->_lastUsedTransport = null; |
|
| 48 | |||
| 49 | 8 | for ($i = 0; $i < $maxTransports |
|
| 50 | 8 | && $transport = $this->_getNextTransport(); ++$i) { |
|
| 51 | try { |
||
| 52 | 8 | if (!$transport->isStarted()) { |
|
| 53 | 8 | $transport->start(); |
|
| 54 | } |
||
| 55 | |||
| 56 | 8 | $sent = $transport->send($message, $failedRecipients); |
|
| 57 | 6 | if ($sent) { |
|
| 58 | 4 | $this->_lastUsedTransport = $transport; |
|
| 59 | |||
| 60 | 6 | return $sent; |
|
| 61 | } |
||
| 62 | |||
| 63 | 5 | } catch (Swift_TransportException $e) { |
|
| 64 | 5 | $this->_killCurrentTransport(); |
|
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | 5 | if (count($this->_transports) === 0) { |
|
| 69 | 3 | throw new Swift_TransportException( |
|
| 70 | 3 | 'All Transports in FailoverTransport failed, or no Transports available' |
|
| 71 | ); |
||
| 72 | } |
||
| 73 | |||
| 74 | 2 | return $sent; |
|
| 75 | } |
||
| 76 | |||
| 77 | 8 | protected function _getNextTransport() |
|
| 78 | { |
||
| 79 | 8 | if (!isset($this->_currentTransport)) { |
|
| 80 | 8 | $this->_currentTransport = parent::_getNextTransport(); |
|
| 81 | } |
||
| 82 | |||
| 83 | 8 | return $this->_currentTransport; |
|
| 84 | } |
||
| 85 | |||
| 86 | 5 | protected function _killCurrentTransport() |
|
| 91 | } |
||
| 92 |
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.