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_Message extends Swift_Mime_SimpleMessage |
||
|
|||
17 | { |
||
18 | /** |
||
19 | * @var Swift_Signers_HeaderSigner[] |
||
20 | */ |
||
21 | private $headerSigners = array(); |
||
22 | |||
23 | /** |
||
24 | * @var Swift_Signers_BodySigner[] |
||
25 | */ |
||
26 | private $bodySigners = array(); |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $savedMessage = array(); |
||
32 | |||
33 | /** |
||
34 | * Create a new Message. |
||
35 | * |
||
36 | * Details may be optionally passed into the constructor. |
||
37 | * |
||
38 | * @param string $subject |
||
39 | * @param string $body |
||
40 | * @param string $contentType |
||
41 | * @param string $charset |
||
42 | */ |
||
43 | 145 | public function __construct($subject = null, $body = null, $contentType = null, $charset = null) |
|
44 | { |
||
45 | 145 | call_user_func_array( |
|
46 | 145 | array($this, 'Swift_Mime_SimpleMessage::__construct'), |
|
47 | 145 | Swift_DependencyContainer::getInstance()->createDependenciesFor('mime.message') |
|
48 | 145 | ); |
|
49 | |||
50 | 145 | if (!isset($charset)) { |
|
51 | 145 | $charset = Swift_DependencyContainer::getInstance()->lookup('properties.charset'); |
|
52 | 145 | } |
|
53 | |||
54 | 145 | $this->setSubject($subject); |
|
55 | 145 | $this->setBody($body); |
|
56 | 145 | $this->setCharset($charset); |
|
57 | 145 | if ($contentType) { |
|
58 | 10 | $this->setContentType($contentType); |
|
59 | 10 | } |
|
60 | 145 | } |
|
61 | |||
62 | /** |
||
63 | * Create a new Message. |
||
64 | * |
||
65 | * @param string $subject |
||
66 | * @param string $body |
||
67 | * @param string $contentType |
||
68 | * @param string $charset |
||
69 | * |
||
70 | * @return $this |
||
71 | */ |
||
72 | 4 | public static function newInstance($subject = null, $body = null, $contentType = null, $charset = null) |
|
73 | { |
||
74 | 4 | return new self($subject, $body, $contentType, $charset); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Add a MimePart to this Message. |
||
79 | * |
||
80 | * @param string|Swift_OutputByteStream $body |
||
81 | * @param string $contentType |
||
82 | * @param string $charset |
||
83 | * |
||
84 | * @return $this |
||
85 | */ |
||
86 | 3 | public function addPart($body, $contentType = null, $charset = null) |
|
87 | { |
||
88 | 3 | return $this->attach( |
|
89 | 3 | Swift_MimePart::newInstance( |
|
90 | 3 | $body, |
|
91 | 3 | $contentType, |
|
92 | $charset |
||
93 | 3 | )->setEncoder($this->getEncoder()) |
|
94 | 3 | ); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Detach a signature handler from a message. |
||
99 | * |
||
100 | * @param Swift_Signer $signer |
||
101 | * |
||
102 | * @return $this |
||
103 | */ |
||
104 | 8 | public function attachSigner(Swift_Signer $signer) |
|
105 | { |
||
106 | 8 | if ($signer instanceof Swift_Signers_HeaderSigner) { |
|
107 | $this->headerSigners[] = $signer; |
||
108 | 8 | } elseif ($signer instanceof Swift_Signers_BodySigner) { |
|
109 | 8 | $this->bodySigners[] = $signer; |
|
110 | 8 | } |
|
111 | |||
112 | 8 | return $this; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * Attach a new signature handler to the message. |
||
117 | * |
||
118 | * @param Swift_Signer $signer |
||
119 | * |
||
120 | * @return $this |
||
121 | */ |
||
122 | public function detachSigner(Swift_Signer $signer) |
||
144 | |||
145 | /** |
||
146 | * Get this message as a complete string. |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | 118 | View Code Duplication | public function toString() |
166 | |||
167 | /** |
||
168 | * Write this message to a {@link Swift_InputByteStream}. |
||
169 | * |
||
170 | * @param Swift_InputByteStream $is |
||
171 | */ |
||
172 | 17 | View Code Duplication | public function toByteStream(Swift_InputByteStream $is) |
188 | |||
189 | public function __wakeup() |
||
193 | |||
194 | /** |
||
195 | * loops through signers and apply the signatures. |
||
196 | */ |
||
197 | 8 | protected function doSign() |
|
198 | { |
||
199 | 8 | foreach ($this->bodySigners as $signer) { |
|
200 | 8 | $altered = $signer->getAlteredHeaders(); |
|
201 | 8 | $this->saveHeaders($altered); |
|
202 | 8 | $signer->signMessage($this); |
|
203 | 8 | } |
|
204 | |||
205 | 8 | foreach ($this->headerSigners as $signer) { |
|
206 | $altered = $signer->getAlteredHeaders(); |
||
207 | $this->saveHeaders($altered); |
||
208 | $signer->reset(); |
||
209 | |||
210 | $signer->setHeaders($this->getHeaders()); |
||
211 | |||
212 | $signer->startBody(); |
||
213 | $this->_bodyToByteStream($signer); |
||
214 | $signer->endBody(); |
||
215 | |||
216 | $signer->addSignature($this->getHeaders()); |
||
217 | 8 | } |
|
218 | 8 | } |
|
219 | |||
220 | /** |
||
221 | * save the message before any signature is applied. |
||
222 | */ |
||
223 | 12 | protected function saveMessage() |
|
224 | 4 | { |
|
225 | 12 | $this->savedMessage = array('headers' => array()); |
|
226 | 8 | $this->savedMessage['body'] = $this->getBody(); |
|
227 | 8 | $this->savedMessage['children'] = $this->getChildren(); |
|
228 | |||
229 | if ( |
||
230 | 8 | $this->getBody() != '' |
|
231 | 8 | && |
|
232 | 8 | count($this->savedMessage['children']) > 0 |
|
233 | 8 | ) { |
|
234 | 1 | $this->setChildren(array_merge(array($this->_becomeMimePart()), $this->savedMessage['children'])); |
|
235 | 1 | $this->setBody(''); |
|
236 | 1 | } |
|
237 | 8 | } |
|
238 | |||
239 | /** |
||
240 | * save the original headers. |
||
241 | * |
||
242 | * @param array $altered |
||
243 | */ |
||
244 | 8 | protected function saveHeaders(array $altered) |
|
254 | |||
255 | /** |
||
256 | * Remove or restore altered headers. |
||
257 | */ |
||
258 | 8 | protected function restoreHeaders() |
|
270 | |||
271 | /** |
||
272 | * Restore message body. |
||
273 | */ |
||
274 | 8 | protected function restoreMessage() |
|
282 | |||
283 | /** |
||
284 | * Clone Message Signers. |
||
285 | * |
||
286 | * @see Swift_Mime_SimpleMimeEntity::__clone() |
||
287 | */ |
||
288 | 4 | public function __clone() |
|
300 | } |
||
301 |
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.