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 |
||
24 | class Swift_Transport_MailTransport implements Swift_Transport |
||
|
|||
25 | { |
||
26 | /** Additional parameters to pass to mail() */ |
||
27 | private $_extraParams = '-f%s'; |
||
28 | |||
29 | /** The event dispatcher from the plugin API */ |
||
30 | private $_eventDispatcher; |
||
31 | |||
32 | /** |
||
33 | * Create a new MailTransport with the $log. |
||
34 | * |
||
35 | * @param Swift_Events_EventDispatcher $eventDispatcher |
||
36 | */ |
||
37 | 16 | public function __construct(Swift_Events_EventDispatcher $eventDispatcher) |
|
41 | |||
42 | /** |
||
43 | * Not used. |
||
44 | */ |
||
45 | public function isStarted() |
||
49 | |||
50 | /** |
||
51 | * Not used. |
||
52 | */ |
||
53 | public function start() |
||
56 | |||
57 | /** |
||
58 | * Not used. |
||
59 | */ |
||
60 | public function stop() |
||
63 | |||
64 | /** |
||
65 | * Check if this Transport mechanism is alive. |
||
66 | * |
||
67 | * If a Transport mechanism session is no longer functional, the method |
||
68 | * returns FALSE. It is the responsibility of the developer to handle this |
||
69 | * case and restart the Transport mechanism manually. |
||
70 | * |
||
71 | * @example |
||
72 | * |
||
73 | * if (!$transport->ping()) { |
||
74 | * $transport->stop(); |
||
75 | * $transport->start(); |
||
76 | * } |
||
77 | * |
||
78 | * The Transport mechanism will be started, if it is not already. |
||
79 | * |
||
80 | * It is undefined if the Transport mechanism attempts to restart as long as |
||
81 | * the return value reflects whether the mechanism is now functional. |
||
82 | * |
||
83 | * @return bool TRUE if the transport is alive |
||
84 | */ |
||
85 | public function ping() |
||
89 | |||
90 | /** |
||
91 | * Set the additional parameters used on the mail() function. |
||
92 | * |
||
93 | * This string is formatted for sprintf() where %s is the sender address. |
||
94 | * |
||
95 | * @param string $params |
||
96 | * |
||
97 | * @return Swift_Transport_MailTransport |
||
98 | */ |
||
99 | 2 | public function setExtraParams($params) |
|
105 | |||
106 | /** |
||
107 | * Get the additional parameters used on the mail() function. |
||
108 | * |
||
109 | * This string is formatted for sprintf() where %s is the sender address. |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getExtraParams() |
||
117 | |||
118 | /** |
||
119 | * Send the given Message. |
||
120 | * |
||
121 | * Recipient/sender data will be retrieved from the Message API. |
||
122 | * The return value is the number of recipients who were accepted for delivery. |
||
123 | * |
||
124 | * @param Swift_Mime_Message $message |
||
125 | * @param string[] $failedRecipients An array of failures by-reference |
||
126 | * |
||
127 | * @return int |
||
128 | * |
||
129 | * @throws Swift_TransportException |
||
130 | */ |
||
131 | 15 | public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
|
231 | |||
232 | /** |
||
233 | * Register a plugin. |
||
234 | * |
||
235 | * @param Swift_Events_EventListener $plugin |
||
236 | */ |
||
237 | public function registerPlugin(Swift_Events_EventListener $plugin) |
||
241 | |||
242 | /** |
||
243 | * Throw a TransportException, first sending it to any listeners |
||
244 | * |
||
245 | * @param Swift_TransportException $e |
||
246 | * |
||
247 | * @throws Swift_TransportException |
||
248 | */ |
||
249 | View Code Duplication | protected function _throwException(Swift_TransportException $e) |
|
263 | |||
264 | /** |
||
265 | * Send mail via the mail() function. |
||
266 | * |
||
267 | * This method takes the same arguments as PHP mail(). |
||
268 | * |
||
269 | * @param string $to |
||
270 | * @param string $subject |
||
271 | * @param string $body |
||
272 | * @param string $headers |
||
273 | * @param string $extraParams |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | public function mail($to, $subject, $body, $headers = null, $extraParams = null) |
||
285 | |||
286 | /** |
||
287 | * Determine the best-use reverse path for this message |
||
288 | * |
||
289 | * @param Swift_Mime_Message $message |
||
290 | * |
||
291 | * @return mixed|null|string |
||
292 | */ |
||
293 | 15 | View Code Duplication | private function _getReversePath(Swift_Mime_Message $message) |
311 | |||
312 | /** |
||
313 | * Return php mail extra params to use for invoker->mail. |
||
314 | * |
||
315 | * @param $extraParams |
||
316 | * @param $reversePath |
||
317 | * |
||
318 | * @return mixed string|null |
||
319 | */ |
||
320 | 15 | private function _formatExtraParams($extraParams, $reversePath) |
|
330 | } |
||
331 |
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.