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_Mime_SimpleMessage 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_Mime_SimpleMessage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Swift_Mime_SimpleMessage extends Swift_Mime_MimePart implements Swift_Mime_Message |
||
|
|||
17 | { |
||
18 | const PRIORITY_HIGHEST = 1; |
||
19 | const PRIORITY_HIGH = 2; |
||
20 | const PRIORITY_NORMAL = 3; |
||
21 | const PRIORITY_LOW = 4; |
||
22 | const PRIORITY_LOWEST = 5; |
||
23 | |||
24 | /** |
||
25 | * Create a new SimpleMessage with $headers, $encoder and $cache. |
||
26 | * |
||
27 | * @param Swift_Mime_HeaderSet $headers |
||
28 | * @param Swift_Mime_ContentEncoder $encoder |
||
29 | * @param Swift_KeyCache $cache |
||
30 | * @param Swift_IdGenerator $idGenerator |
||
31 | * @param string $charset |
||
32 | */ |
||
33 | 258 | public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_IdGenerator $idGenerator, $charset = null) |
|
62 | |||
63 | /** |
||
64 | * Always returns {@link LEVEL_TOP} for a message instance. |
||
65 | * |
||
66 | * @return int |
||
67 | */ |
||
68 | 20 | public function getNestingLevel() |
|
72 | |||
73 | /** |
||
74 | * Set the subject of this message. |
||
75 | * |
||
76 | * @param string $subject |
||
77 | * |
||
78 | * @return $this |
||
79 | */ |
||
80 | 148 | public function setSubject($subject) |
|
88 | |||
89 | /** |
||
90 | * Get the subject of this message. |
||
91 | * |
||
92 | * @return null|string |
||
93 | */ |
||
94 | 1 | public function getSubject() |
|
98 | |||
99 | /** |
||
100 | * Set the date at which this message was created. |
||
101 | * |
||
102 | * @param int $date |
||
103 | * |
||
104 | * @return $this |
||
105 | */ |
||
106 | 258 | public function setDate($date) |
|
114 | |||
115 | /** |
||
116 | * Get the date at which this message was created. |
||
117 | * |
||
118 | * @return null|string |
||
119 | */ |
||
120 | 113 | public function getDate() |
|
124 | |||
125 | /** |
||
126 | * Set the return-path (the bounce address) of this message. |
||
127 | * |
||
128 | * @param string $address |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | 48 | public function setReturnPath($address) |
|
140 | |||
141 | /** |
||
142 | * Get the return-path (bounce address) of this message. |
||
143 | * |
||
144 | * @return null|string |
||
145 | */ |
||
146 | 5 | public function getReturnPath() |
|
150 | |||
151 | /** |
||
152 | * Set the sender of this message. |
||
153 | * |
||
154 | * This does not override the From field, but it has a higher significance. |
||
155 | * |
||
156 | * @param string|array $address |
||
157 | * @param string $name optional |
||
158 | * |
||
159 | * @return $this |
||
160 | */ |
||
161 | 12 | View Code Duplication | public function setSender($address, $name = null) |
173 | |||
174 | /** |
||
175 | * Get the sender of this message. |
||
176 | * |
||
177 | * @return null|string |
||
178 | */ |
||
179 | 5 | public function getSender() |
|
183 | |||
184 | /** |
||
185 | * Add a From: address to this message. |
||
186 | * |
||
187 | * If $name is passed this name will be associated with the address. |
||
188 | * |
||
189 | * @param string $address |
||
190 | * @param string $name optional |
||
191 | * |
||
192 | * @return $this |
||
193 | */ |
||
194 | 1 | public function addFrom($address, $name = null) |
|
201 | |||
202 | /** |
||
203 | * Set the from address of this message. |
||
204 | * |
||
205 | * You may pass an array of addresses if this message is from multiple people. |
||
206 | * |
||
207 | * If $name is passed and the first parameter is a string, this name will be |
||
208 | * associated with the address. |
||
209 | * |
||
210 | * @param string|array $addresses |
||
211 | * @param string $name optional |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | 110 | View Code Duplication | public function setFrom($addresses, $name = null) |
227 | |||
228 | /** |
||
229 | * Get the from address of this message. |
||
230 | * |
||
231 | * @return null|string[] |
||
232 | */ |
||
233 | 6 | public function getFrom() |
|
237 | |||
238 | /** |
||
239 | * Add a Reply-To: address to this message. |
||
240 | * |
||
241 | * If $name is passed this name will be associated with the address. |
||
242 | * |
||
243 | * @param string $address |
||
244 | * @param string $name optional |
||
245 | * |
||
246 | * @return $this |
||
247 | */ |
||
248 | 1 | public function addReplyTo($address, $name = null) |
|
255 | |||
256 | /** |
||
257 | * Set the reply-to address of this message. |
||
258 | * |
||
259 | * You may pass an array of addresses if replies will go to multiple people. |
||
260 | * |
||
261 | * If $name is passed and the first parameter is a string, this name will be |
||
262 | * associated with the address. |
||
263 | * |
||
264 | * @param mixed $addresses |
||
265 | * @param string $name optional |
||
266 | * |
||
267 | * @return $this |
||
268 | */ |
||
269 | 29 | View Code Duplication | public function setReplyTo($addresses, $name = null) |
281 | |||
282 | /** |
||
283 | * Get the reply-to address of this message. |
||
284 | * |
||
285 | * @return null|string[] |
||
286 | */ |
||
287 | 2 | public function getReplyTo() |
|
291 | |||
292 | /** |
||
293 | * Add a To: address to this message. |
||
294 | * |
||
295 | * If $name is passed this name will be associated with the address. |
||
296 | * |
||
297 | * @param string $address |
||
298 | * @param string $name optional |
||
299 | * |
||
300 | * @return $this |
||
301 | */ |
||
302 | 5 | public function addTo($address, $name = null) |
|
309 | |||
310 | /** |
||
311 | * Set the to addresses of this message. |
||
312 | * |
||
313 | * If multiple recipients will receive the message an array should be used. |
||
314 | * Example: array('[email protected]', '[email protected]' => 'A name') |
||
315 | * |
||
316 | * If $name is passed and the first parameter is a string, this name will be |
||
317 | * associated with the address. |
||
318 | * |
||
319 | * @param mixed $addresses |
||
320 | * @param string $name optional |
||
321 | * |
||
322 | * @return $this |
||
323 | */ |
||
324 | 50 | View Code Duplication | public function setTo($addresses, $name = null) |
336 | |||
337 | /** |
||
338 | * Get the To addresses of this message. |
||
339 | * |
||
340 | * @return null|string[] |
||
341 | */ |
||
342 | 11 | public function getTo() |
|
346 | |||
347 | /** |
||
348 | * Add a Cc: address to this message. |
||
349 | * |
||
350 | * If $name is passed this name will be associated with the address. |
||
351 | * |
||
352 | * @param string $address |
||
353 | * @param string $name optional |
||
354 | * |
||
355 | * @return $this |
||
356 | */ |
||
357 | 1 | public function addCc($address, $name = null) |
|
364 | |||
365 | /** |
||
366 | * Set the Cc addresses of this message. |
||
367 | * |
||
368 | * If $name is passed and the first parameter is a string, this name will be |
||
369 | * associated with the address. |
||
370 | * |
||
371 | * @param string[]|string $addresses |
||
372 | * @param string $name optional |
||
373 | * |
||
374 | * @return $this |
||
375 | */ |
||
376 | 25 | View Code Duplication | public function setCc($addresses, $name = null) |
388 | |||
389 | /** |
||
390 | * Get the Cc address of this message. |
||
391 | * |
||
392 | * @return null|string[] |
||
393 | */ |
||
394 | 10 | public function getCc() |
|
398 | |||
399 | /** |
||
400 | * Add a Bcc: address to this message. |
||
401 | * |
||
402 | * If $name is passed this name will be associated with the address. |
||
403 | * |
||
404 | * @param string $address |
||
405 | * @param string $name optional |
||
406 | * |
||
407 | * @return $this |
||
408 | */ |
||
409 | 1 | public function addBcc($address, $name = null) |
|
416 | |||
417 | /** |
||
418 | * Set the Bcc addresses of this message. |
||
419 | * |
||
420 | * If $name is passed and the first parameter is a string, this name will be |
||
421 | * associated with the address. |
||
422 | * |
||
423 | * @param mixed $addresses |
||
424 | * @param string $name optional |
||
425 | * |
||
426 | * @return $this |
||
427 | */ |
||
428 | 19 | View Code Duplication | public function setBcc($addresses, $name = null) |
440 | |||
441 | /** |
||
442 | * Get the Bcc addresses of this message. |
||
443 | * |
||
444 | * @return null|string[] |
||
445 | */ |
||
446 | 10 | public function getBcc() |
|
450 | |||
451 | /** |
||
452 | * Set the priority of this message. |
||
453 | * |
||
454 | * The value is an integer where 1 is the highest priority and 5 is the lowest. |
||
455 | * |
||
456 | * @param int $priority |
||
457 | * |
||
458 | * @return $this |
||
459 | */ |
||
460 | 3 | public function setPriority($priority) |
|
484 | |||
485 | /** |
||
486 | * Get the priority of this message. |
||
487 | * |
||
488 | * The returned value is an integer where 1 is the highest priority and 5 |
||
489 | * is the lowest. |
||
490 | * |
||
491 | * @return int |
||
492 | */ |
||
493 | 1 | public function getPriority() |
|
500 | |||
501 | /** |
||
502 | * Ask for a delivery receipt from the recipient to be sent to $addresses. |
||
503 | * |
||
504 | * @param array $addresses |
||
505 | * |
||
506 | * @return $this |
||
507 | */ |
||
508 | 3 | public function setReadReceiptTo($addresses) |
|
516 | |||
517 | /** |
||
518 | * Get the addresses to which a read-receipt will be sent. |
||
519 | * |
||
520 | * @return null|string[] |
||
521 | */ |
||
522 | 1 | public function getReadReceiptTo() |
|
526 | |||
527 | /** |
||
528 | * Attach a {@link Swift_Mime_MimeEntity} such as an Attachment or MimePart. |
||
529 | * |
||
530 | * @param Swift_Mime_MimeEntity $entity |
||
531 | * |
||
532 | * @return $this |
||
533 | */ |
||
534 | 51 | public function attach(Swift_Mime_MimeEntity $entity) |
|
540 | |||
541 | /** |
||
542 | * Remove an already attached entity. |
||
543 | * |
||
544 | * @param Swift_Mime_MimeEntity $entity |
||
545 | * |
||
546 | * @return $this |
||
547 | */ |
||
548 | 8 | public function detach(Swift_Mime_MimeEntity $entity) |
|
560 | |||
561 | /** |
||
562 | * Attach a {@link Swift_Mime_MimeEntity} and return it's CID source. |
||
563 | * This method should be used when embedding images or other data in a message. |
||
564 | * |
||
565 | * @param Swift_Mime_MimeEntity $entity |
||
566 | * |
||
567 | * @return string |
||
568 | */ |
||
569 | 9 | public function embed(Swift_Mime_MimeEntity $entity) |
|
575 | |||
576 | /** |
||
577 | * Get this message as a complete string. |
||
578 | * |
||
579 | * @return string |
||
580 | */ |
||
581 | 133 | View Code Duplication | public function toString() |
599 | |||
600 | /** |
||
601 | * Returns a string representation of this object. |
||
602 | * |
||
603 | * @see toString() |
||
604 | * |
||
605 | * @return string |
||
606 | */ |
||
607 | public function __toString() |
||
611 | |||
612 | /** |
||
613 | * Write this message to a {@link Swift_InputByteStream}. |
||
614 | * |
||
615 | * @param Swift_InputByteStream $is |
||
616 | */ |
||
617 | 21 | View Code Duplication | public function toByteStream(Swift_InputByteStream $is) |
633 | |||
634 | /** @see Swift_Mime_SimpleMimeEntity::_getIdField() */ |
||
635 | 258 | protected function _getIdField() |
|
639 | |||
640 | /** Turn the body of this message into a child of itself if needed */ |
||
641 | 19 | protected function _becomeMimePart() |
|
659 | |||
660 | /** Get the highest nesting level nested inside this message */ |
||
661 | 19 | private function _getTopNestingLevel() |
|
673 | } |
||
674 |
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.