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_EsmtpTransport 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_EsmtpTransport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTransport implements Swift_Transport_SmtpAgent |
||
|
|||
17 | { |
||
18 | /** |
||
19 | * ESMTP extension handlers. |
||
20 | * |
||
21 | * @var Swift_Transport_EsmtpHandler[] |
||
22 | */ |
||
23 | private $_handlers = array(); |
||
24 | |||
25 | /** |
||
26 | * ESMTP capabilities. |
||
27 | * |
||
28 | * @var string[][] |
||
29 | */ |
||
30 | private $_capabilities = array(); |
||
31 | |||
32 | /** |
||
33 | * Connection buffer parameters. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | private $_params = array( |
||
38 | 'protocol' => 'tcp', |
||
39 | 'host' => 'localhost', |
||
40 | 'port' => 25, |
||
41 | 'timeout' => 30, |
||
42 | 'blocking' => 1, |
||
43 | 'tls' => false, |
||
44 | 'type' => Swift_Transport_IoBuffer::TYPE_SOCKET, |
||
45 | 'stream_context_options' => array(), |
||
46 | 'verifySsl' => array(), |
||
47 | ); |
||
48 | |||
49 | /** |
||
50 | * Creates a new EsmtpTransport using the given I/O buffer. |
||
51 | * |
||
52 | * @param Swift_Transport_IoBuffer $buf |
||
53 | * @param Swift_Transport_EsmtpHandler[] $extensionHandlers |
||
54 | * @param Swift_Events_EventDispatcher $dispatcher |
||
55 | * @param string $localDomain |
||
56 | */ |
||
57 | 123 | public function __construct(Swift_Transport_IoBuffer $buf, array $extensionHandlers, Swift_Events_EventDispatcher $dispatcher, $localDomain = '127.0.0.1') |
|
62 | |||
63 | /** |
||
64 | * Set the host to connect to. |
||
65 | * |
||
66 | * @param string $host |
||
67 | * |
||
68 | * @return $this |
||
69 | */ |
||
70 | 8 | public function setHost($host) |
|
76 | |||
77 | /** |
||
78 | * Get the host to connect to. |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | 3 | public function getHost() |
|
86 | |||
87 | /** |
||
88 | * Set the port to connect to. |
||
89 | * |
||
90 | * @param int $port |
||
91 | * |
||
92 | * @return $this |
||
93 | */ |
||
94 | 8 | public function setPort($port) |
|
100 | |||
101 | /** |
||
102 | * Get the port to connect to. |
||
103 | * |
||
104 | * @return int |
||
105 | */ |
||
106 | 3 | public function getPort() |
|
110 | |||
111 | /** |
||
112 | * Set the connection timeout. |
||
113 | * |
||
114 | * @param int $timeout seconds |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | 4 | public function setTimeout($timeout) |
|
125 | |||
126 | /** |
||
127 | * Get the connection timeout. |
||
128 | * |
||
129 | * @return int |
||
130 | */ |
||
131 | 3 | public function getTimeout() |
|
135 | |||
136 | /** |
||
137 | * Set the encryption type (tls or ssl). |
||
138 | * |
||
139 | * @param string $encryption |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | 8 | public function setEncryption($encryption) |
|
157 | |||
158 | /** |
||
159 | * Get the encryption type. |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | 3 | public function getEncryption() |
|
167 | |||
168 | /** |
||
169 | * Disable SSL-Verify to prevent error like "SSL3_GET_SERVER_CERTIFICATE:certificate verify failed" |
||
170 | * |
||
171 | * @param bool $bool |
||
172 | * |
||
173 | * @return Swift_Transport_EsmtpTransport |
||
174 | */ |
||
175 | public function setVerifySsl($bool = false) |
||
185 | |||
186 | /** |
||
187 | * Sets the stream context options. |
||
188 | * |
||
189 | * @param array $options |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function setStreamOptions($options) |
||
199 | |||
200 | /** |
||
201 | * Returns the stream context options. |
||
202 | * |
||
203 | * @return array |
||
204 | */ |
||
205 | 1 | public function getStreamOptions() |
|
209 | |||
210 | /** |
||
211 | * Sets the source IP. |
||
212 | * |
||
213 | * @param string $source |
||
214 | * |
||
215 | * @return $this |
||
216 | */ |
||
217 | public function setSourceIp($source) |
||
223 | |||
224 | /** |
||
225 | * Returns the IP used to connect to the destination. |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | 1 | public function getSourceIp() |
|
233 | |||
234 | /** |
||
235 | * Set ESMTP extension handlers. |
||
236 | * |
||
237 | * @param Swift_Transport_EsmtpHandler[] $handlers |
||
238 | * |
||
239 | * @return $this |
||
240 | */ |
||
241 | 123 | public function setExtensionHandlers(array $handlers) |
|
256 | |||
257 | /** |
||
258 | * Get ESMTP extension handlers. |
||
259 | * |
||
260 | * @return Swift_Transport_EsmtpHandler[] |
||
261 | */ |
||
262 | 2 | public function getExtensionHandlers() |
|
266 | |||
267 | /** |
||
268 | * Run a command against the buffer, expecting the given response codes. |
||
269 | * |
||
270 | * If no response codes are given, the response will not be validated. |
||
271 | * If codes are given, an exception will be thrown on an invalid response. |
||
272 | * |
||
273 | * @param string $command |
||
274 | * @param int[] $codes |
||
275 | * @param string[] $failures An array of failures by-reference |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | 96 | public function executeCommand($command, $codes = array(), &$failures = null) |
|
297 | |||
298 | /** |
||
299 | * Mixin handling method for ESMTP handlers |
||
300 | * |
||
301 | * @param $method |
||
302 | * @param $args |
||
303 | * |
||
304 | * @return $this|mixed |
||
305 | */ |
||
306 | 7 | public function __call($method, $args) |
|
334 | |||
335 | /** Get the params to initialize the buffer */ |
||
336 | 96 | protected function _getBufferParams() |
|
340 | |||
341 | /** |
||
342 | * Overridden to perform EHLO instead |
||
343 | * |
||
344 | * @throws Swift_TransportException |
||
345 | */ |
||
346 | 92 | protected function _doHeloCommand() |
|
386 | |||
387 | /** |
||
388 | * Overridden to add Extension support |
||
389 | * |
||
390 | * @param $address |
||
391 | */ |
||
392 | 56 | View Code Duplication | protected function _doMailFromCommand($address) |
406 | |||
407 | /** |
||
408 | * Overridden to add Extension support |
||
409 | * |
||
410 | * @param $address |
||
411 | */ |
||
412 | 54 | View Code Duplication | protected function _doRcptToCommand($address) |
426 | |||
427 | /** |
||
428 | * Determine ESMTP capabilities by function group |
||
429 | * |
||
430 | * @param string $ehloResponse |
||
431 | * |
||
432 | * @return array |
||
433 | */ |
||
434 | 86 | private function _getCapabilities($ehloResponse) |
|
451 | |||
452 | /** |
||
453 | * Set parameters which are used by each extension handler |
||
454 | */ |
||
455 | 123 | private function _setHandlerParams() |
|
463 | |||
464 | /** |
||
465 | * Get ESMTP handlers which are currently ok to use |
||
466 | * |
||
467 | * @return Swift_Transport_EsmtpHandler[] |
||
468 | */ |
||
469 | 96 | private function _getActiveHandlers() |
|
480 | |||
481 | /** |
||
482 | * Custom sort for extension handler ordering |
||
483 | * |
||
484 | * @param Swift_Transport_EsmtpHandler $a |
||
485 | * @param Swift_Transport_EsmtpHandler $b |
||
486 | * |
||
487 | * @return integer |
||
488 | */ |
||
489 | 10 | private function _sortHandlers($a, $b) |
|
493 | } |
||
494 |
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.