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_LoadBalancedTransport implements Swift_Transport |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Transports which are deemed useless. |
||
| 20 | * |
||
| 21 | * @var Swift_Transport[] |
||
| 22 | */ |
||
| 23 | private $_deadTransports = array(); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The Transports which are used in rotation. |
||
| 27 | * |
||
| 28 | * @var Swift_Transport[] |
||
| 29 | */ |
||
| 30 | protected $_transports = array(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The Transport used in the last successful send operation. |
||
| 34 | * |
||
| 35 | * @var Swift_Transport |
||
| 36 | */ |
||
| 37 | protected $_lastUsedTransport; |
||
| 38 | |||
| 39 | // needed as __construct is called from elsewhere explicitly |
||
| 40 | 23 | public function __construct() |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Set $transports to delegate to. |
||
| 46 | * |
||
| 47 | * @param Swift_Transport[] $transports |
||
| 48 | */ |
||
| 49 | 22 | public function setTransports(array $transports) |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Get $transports to delegate to. |
||
| 57 | * |
||
| 58 | * @return Swift_Transport[] |
||
| 59 | */ |
||
| 60 | public function getTransports() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get the Transport used in the last successful send operation. |
||
| 67 | * |
||
| 68 | * @return Swift_Transport |
||
| 69 | */ |
||
| 70 | public function getLastUsedTransport() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Test if this Transport mechanism has started. |
||
| 77 | * |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | 4 | public function isStarted() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Start this Transport mechanism. |
||
| 87 | */ |
||
| 88 | 20 | public function start() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Stop this Transport mechanism. |
||
| 95 | */ |
||
| 96 | 2 | public function stop() |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Send the given Message. |
||
| 105 | * |
||
| 106 | * Recipient/sender data will be retrieved from the Message API. |
||
| 107 | * The return value is the number of recipients who were accepted for delivery. |
||
| 108 | * |
||
| 109 | * @param Swift_Mime_Message $message |
||
| 110 | * @param string[] $failedRecipients An array of failures by-reference |
||
| 111 | * |
||
| 112 | * @return int |
||
| 113 | * |
||
| 114 | * @throws Swift_TransportException |
||
| 115 | */ |
||
| 116 | 10 | View Code Duplication | public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
| 117 | { |
||
| 118 | 10 | $maxTransports = count($this->_transports); |
|
| 119 | 10 | $sent = 0; |
|
| 120 | 10 | $this->_lastUsedTransport = null; |
|
| 121 | |||
| 122 | 10 | for ($i = 0; $i < $maxTransports |
|
| 123 | 10 | && $transport = $this->_getNextTransport(); ++$i) { |
|
| 124 | try { |
||
| 125 | 10 | if (!$transport->isStarted()) { |
|
| 126 | 10 | $transport->start(); |
|
| 127 | } |
||
| 128 | |||
| 129 | 10 | $sent = $transport->send($message, $failedRecipients); |
|
| 130 | 8 | if ($sent) { |
|
| 131 | 7 | $this->_lastUsedTransport = $transport; |
|
| 132 | 8 | break; |
|
| 133 | } |
||
| 134 | |||
| 135 | 5 | } catch (Swift_TransportException $e) { |
|
| 136 | 5 | $this->_killCurrentTransport(); |
|
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | 10 | if (count($this->_transports) === 0) { |
|
| 141 | 3 | throw new Swift_TransportException( |
|
| 142 | 3 | 'All Transports in LoadBalancedTransport failed, or no Transports available' |
|
| 143 | ); |
||
| 144 | } |
||
| 145 | |||
| 146 | 8 | return $sent; |
|
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Register a plugin. |
||
| 151 | * |
||
| 152 | * @param Swift_Events_EventListener $plugin |
||
| 153 | */ |
||
| 154 | 2 | public function registerPlugin(Swift_Events_EventListener $plugin) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Rotates the transport list around and returns the first instance. |
||
| 163 | * |
||
| 164 | * @return Swift_Transport |
||
| 165 | */ |
||
| 166 | 18 | protected function _getNextTransport() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Tag the currently used (top of stack) transport as dead/useless. |
||
| 178 | */ |
||
| 179 | 10 | protected function _killCurrentTransport() |
|
| 192 | } |
||
| 193 |
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.