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 | /** |
||
19 | * Create a new SimpleMessage with $headers, $encoder and $cache. |
||
20 | * |
||
21 | * @param Swift_Mime_HeaderSet $headers |
||
22 | * @param Swift_Mime_ContentEncoder $encoder |
||
23 | * @param Swift_KeyCache $cache |
||
24 | * @param Swift_EmailValidatorBridge $emailValidator |
||
25 | * @param string $charset |
||
26 | */ |
||
27 | 221 | public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_EmailValidatorBridge $emailValidator, $charset = null) |
|
28 | { |
||
29 | 221 | parent::__construct($headers, $encoder, $cache, $emailValidator, $charset); |
|
30 | 221 | $this->getHeaders()->defineOrdering(array( |
|
31 | 221 | 'Return-Path', |
|
32 | 221 | 'Received', |
|
33 | 221 | 'DKIM-Signature', |
|
34 | 221 | 'DomainKey-Signature', |
|
35 | 221 | 'Sender', |
|
36 | 221 | 'Message-ID', |
|
37 | 221 | 'Date', |
|
38 | 221 | 'Subject', |
|
39 | 221 | 'From', |
|
40 | 221 | 'Reply-To', |
|
41 | 221 | 'To', |
|
42 | 221 | 'Cc', |
|
43 | 221 | 'Bcc', |
|
44 | 221 | 'MIME-Version', |
|
45 | 221 | 'Content-Type', |
|
46 | 221 | 'Content-Transfer-Encoding', |
|
47 | 221 | )); |
|
48 | 221 | $this->getHeaders()->setAlwaysDisplayed(array('Date', 'Message-ID', 'From')); |
|
49 | 221 | $this->getHeaders()->addTextHeader('MIME-Version', '1.0'); |
|
50 | 221 | $this->setDate(time()); |
|
51 | 221 | $this->setId($this->getId()); |
|
52 | 221 | $this->getHeaders()->addMailboxHeader('From'); |
|
53 | 221 | } |
|
54 | |||
55 | /** |
||
56 | * Always returns {@link LEVEL_TOP} for a message instance. |
||
57 | * |
||
58 | * @return int |
||
59 | */ |
||
60 | 18 | public function getNestingLevel() |
|
64 | |||
65 | /** |
||
66 | * Set the subject of this message. |
||
67 | * |
||
68 | * @param string $subject |
||
69 | * |
||
70 | * @return Swift_Mime_SimpleMessage |
||
71 | */ |
||
72 | 112 | public function setSubject($subject) |
|
73 | { |
||
74 | 112 | if (!$this->_setHeaderFieldModel('Subject', $subject)) { |
|
75 | 111 | $this->getHeaders()->addTextHeader('Subject', $subject); |
|
76 | 111 | } |
|
77 | |||
78 | 112 | return $this; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Get the subject of this message. |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | 1 | public function getSubject() |
|
90 | |||
91 | /** |
||
92 | * Set the date at which this message was created. |
||
93 | * |
||
94 | * @param int $date |
||
95 | * |
||
96 | * @return Swift_Mime_SimpleMessage |
||
97 | */ |
||
98 | 221 | public function setDate($date) |
|
99 | { |
||
100 | 221 | if (!$this->_setHeaderFieldModel('Date', $date)) { |
|
101 | 219 | $this->getHeaders()->addDateHeader('Date', $date); |
|
102 | 219 | } |
|
103 | |||
104 | 221 | return $this; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Get the date at which this message was created. |
||
109 | * |
||
110 | * @return int |
||
111 | */ |
||
112 | 78 | public function getDate() |
|
116 | |||
117 | /** |
||
118 | * Set the return-path (the bounce address) of this message. |
||
119 | * |
||
120 | * @param string $address |
||
121 | * |
||
122 | * @return Swift_Mime_SimpleMessage |
||
123 | */ |
||
124 | 33 | public function setReturnPath($address) |
|
125 | { |
||
126 | 33 | if (!$this->_setHeaderFieldModel('Return-Path', $address)) { |
|
127 | 32 | $this->getHeaders()->addPathHeader('Return-Path', $address); |
|
128 | 32 | } |
|
129 | |||
130 | 33 | return $this; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Get the return-path (bounce address) of this message. |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | 5 | public function getReturnPath() |
|
142 | |||
143 | /** |
||
144 | * Set the sender of this message. |
||
145 | * |
||
146 | * This does not override the From field, but it has a higher significance. |
||
147 | * |
||
148 | * @param string|array $address |
||
149 | * @param string $name optional |
||
150 | * |
||
151 | * @return Swift_Mime_SimpleMessage |
||
152 | */ |
||
153 | 10 | View Code Duplication | public function setSender($address, $name = null) |
154 | { |
||
155 | 10 | if (!is_array($address) && isset($name)) { |
|
156 | 1 | $address = array($address => $name); |
|
157 | 1 | } |
|
158 | |||
159 | 10 | if (!$this->_setHeaderFieldModel('Sender', (array) $address)) { |
|
160 | 9 | $this->getHeaders()->addMailboxHeader('Sender', (array) $address); |
|
161 | 9 | } |
|
162 | |||
163 | 10 | return $this; |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * Get the sender of this message. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | 5 | public function getSender() |
|
175 | |||
176 | /** |
||
177 | * Add a From: address to this message. |
||
178 | * |
||
179 | * If $name is passed this name will be associated with the address. |
||
180 | * |
||
181 | * @param string $address |
||
182 | * @param string $name optional |
||
183 | * |
||
184 | * @return Swift_Mime_SimpleMessage |
||
185 | */ |
||
186 | 1 | public function addFrom($address, $name = null) |
|
193 | |||
194 | /** |
||
195 | * Set the from address of this message. |
||
196 | * |
||
197 | * You may pass an array of addresses if this message is from multiple people. |
||
198 | * |
||
199 | * If $name is passed and the first parameter is a string, this name will be |
||
200 | * associated with the address. |
||
201 | * |
||
202 | * @param string|array $addresses |
||
203 | * @param string $name optional |
||
204 | * |
||
205 | * @return Swift_Mime_SimpleMessage |
||
206 | */ |
||
207 | 84 | View Code Duplication | public function setFrom($addresses, $name = null) |
208 | { |
||
209 | 84 | if (!is_array($addresses) && isset($name)) { |
|
210 | 1 | $addresses = array($addresses => $name); |
|
211 | 1 | } |
|
212 | |||
213 | 84 | if (!$this->_setHeaderFieldModel('From', (array) $addresses)) { |
|
214 | 3 | $this->getHeaders()->addMailboxHeader('From', (array) $addresses); |
|
215 | 3 | } |
|
216 | |||
217 | 84 | return $this; |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * Get the from address of this message. |
||
222 | * |
||
223 | * @return mixed |
||
224 | */ |
||
225 | 6 | public function getFrom() |
|
229 | |||
230 | /** |
||
231 | * Add a Reply-To: address to this message. |
||
232 | * |
||
233 | * If $name is passed this name will be associated with the address. |
||
234 | * |
||
235 | * @param string $address |
||
236 | * @param string $name optional |
||
237 | * |
||
238 | * @return Swift_Mime_SimpleMessage |
||
239 | */ |
||
240 | 1 | public function addReplyTo($address, $name = null) |
|
247 | |||
248 | /** |
||
249 | * Set the reply-to address of this message. |
||
250 | * |
||
251 | * You may pass an array of addresses if replies will go to multiple people. |
||
252 | * |
||
253 | * If $name is passed and the first parameter is a string, this name will be |
||
254 | * associated with the address. |
||
255 | * |
||
256 | * @param mixed $addresses |
||
257 | * @param string $name optional |
||
258 | * |
||
259 | * @return Swift_Mime_SimpleMessage |
||
260 | */ |
||
261 | 21 | View Code Duplication | public function setReplyTo($addresses, $name = null) |
262 | { |
||
263 | 21 | if (!is_array($addresses) && isset($name)) { |
|
264 | 1 | $addresses = array($addresses => $name); |
|
265 | 1 | } |
|
266 | |||
267 | 21 | if (!$this->_setHeaderFieldModel('Reply-To', (array) $addresses)) { |
|
268 | 19 | $this->getHeaders()->addMailboxHeader('Reply-To', (array) $addresses); |
|
269 | 19 | } |
|
270 | |||
271 | 21 | return $this; |
|
272 | } |
||
273 | |||
274 | /** |
||
275 | * Get the reply-to address of this message. |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | 2 | public function getReplyTo() |
|
283 | |||
284 | /** |
||
285 | * Add a To: address to this message. |
||
286 | * |
||
287 | * If $name is passed this name will be associated with the address. |
||
288 | * |
||
289 | * @param string $address |
||
290 | * @param string $name optional |
||
291 | * |
||
292 | * @return Swift_Mime_SimpleMessage |
||
293 | */ |
||
294 | 5 | public function addTo($address, $name = null) |
|
301 | |||
302 | /** |
||
303 | * Set the to addresses of this message. |
||
304 | * |
||
305 | * If multiple recipients will receive the message an array should be used. |
||
306 | * Example: array('[email protected]', '[email protected]' => 'A name') |
||
307 | * |
||
308 | * If $name is passed and the first parameter is a string, this name will be |
||
309 | * associated with the address. |
||
310 | * |
||
311 | * @param mixed $addresses |
||
312 | * @param string $name optional |
||
313 | * |
||
314 | * @return Swift_Mime_SimpleMessage |
||
315 | */ |
||
316 | 44 | View Code Duplication | public function setTo($addresses, $name = null) |
317 | { |
||
318 | 44 | if (!is_array($addresses) && isset($name)) { |
|
319 | 1 | $addresses = array($addresses => $name); |
|
320 | 1 | } |
|
321 | |||
322 | 44 | if (!$this->_setHeaderFieldModel('To', (array) $addresses)) { |
|
323 | 42 | $this->getHeaders()->addMailboxHeader('To', (array) $addresses); |
|
324 | 42 | } |
|
325 | |||
326 | 44 | return $this; |
|
327 | } |
||
328 | |||
329 | /** |
||
330 | * Get the To addresses of this message. |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | 11 | public function getTo() |
|
338 | |||
339 | /** |
||
340 | * Add a Cc: address to this message. |
||
341 | * |
||
342 | * If $name is passed this name will be associated with the address. |
||
343 | * |
||
344 | * @param string $address |
||
345 | * @param string $name optional |
||
346 | * |
||
347 | * @return Swift_Mime_SimpleMessage |
||
348 | */ |
||
349 | 1 | public function addCc($address, $name = null) |
|
356 | |||
357 | /** |
||
358 | * Set the Cc addresses of this message. |
||
359 | * |
||
360 | * If $name is passed and the first parameter is a string, this name will be |
||
361 | * associated with the address. |
||
362 | * |
||
363 | * @param mixed $addresses |
||
364 | * @param string $name optional |
||
365 | * |
||
366 | * @return Swift_Mime_SimpleMessage |
||
367 | */ |
||
368 | 21 | View Code Duplication | public function setCc($addresses, $name = null) |
369 | { |
||
370 | 21 | if (!is_array($addresses) && isset($name)) { |
|
371 | 1 | $addresses = array($addresses => $name); |
|
372 | 1 | } |
|
373 | |||
374 | 21 | if (!$this->_setHeaderFieldModel('Cc', (array) $addresses)) { |
|
375 | 19 | $this->getHeaders()->addMailboxHeader('Cc', (array) $addresses); |
|
376 | 19 | } |
|
377 | |||
378 | 21 | return $this; |
|
379 | } |
||
380 | |||
381 | /** |
||
382 | * Get the Cc address of this message. |
||
383 | * |
||
384 | * @return array |
||
385 | */ |
||
386 | 18 | public function getCc() |
|
387 | { |
||
388 | 18 | return $this->_getHeaderFieldModel('Cc'); |
|
389 | 16 | } |
|
390 | |||
391 | /** |
||
392 | * Add a Bcc: address to this message. |
||
393 | * |
||
394 | * If $name is passed this name will be associated with the address. |
||
395 | * |
||
396 | * @param string $address |
||
397 | * @param string $name optional |
||
398 | * |
||
399 | * @return Swift_Mime_SimpleMessage |
||
400 | */ |
||
401 | 1 | public function addBcc($address, $name = null) |
|
408 | |||
409 | /** |
||
410 | * Set the Bcc addresses of this message. |
||
411 | * |
||
412 | * If $name is passed and the first parameter is a string, this name will be |
||
413 | * associated with the address. |
||
414 | * |
||
415 | * @param mixed $addresses |
||
416 | * @param string $name optional |
||
417 | * |
||
418 | * @return Swift_Mime_SimpleMessage |
||
419 | */ |
||
420 | 17 | View Code Duplication | public function setBcc($addresses, $name = null) |
421 | { |
||
422 | 17 | if (!is_array($addresses) && isset($name)) { |
|
423 | 1 | $addresses = array($addresses => $name); |
|
424 | 1 | } |
|
425 | |||
426 | 17 | if (!$this->_setHeaderFieldModel('Bcc', (array) $addresses)) { |
|
427 | 15 | $this->getHeaders()->addMailboxHeader('Bcc', (array) $addresses); |
|
428 | 15 | } |
|
429 | |||
430 | 17 | return $this; |
|
431 | } |
||
432 | |||
433 | /** |
||
434 | * Get the Bcc addresses of this message. |
||
435 | * |
||
436 | * @return array |
||
437 | */ |
||
438 | 10 | public function getBcc() |
|
442 | |||
443 | /** |
||
444 | * Set the priority of this message. |
||
445 | * |
||
446 | * The value is an integer where 1 is the highest priority and 5 is the lowest. |
||
447 | * |
||
448 | * @param int $priority |
||
449 | * |
||
450 | * @return Swift_Mime_SimpleMessage |
||
451 | */ |
||
452 | 3 | public function setPriority($priority) |
|
453 | { |
||
454 | $priorityMap = array( |
||
455 | 3 | 1 => 'Highest', |
|
456 | 3 | 2 => 'High', |
|
457 | 3 | 3 => 'Normal', |
|
458 | 3 | 4 => 'Low', |
|
459 | 3 | 5 => 'Lowest', |
|
460 | 3 | ); |
|
461 | 3 | $pMapKeys = array_keys($priorityMap); |
|
462 | 3 | if ($priority > max($pMapKeys)) { |
|
463 | $priority = max($pMapKeys); |
||
464 | 3 | } elseif ($priority < min($pMapKeys)) { |
|
465 | $priority = min($pMapKeys); |
||
466 | } |
||
467 | 3 | if (!$this->_setHeaderFieldModel('X-Priority', |
|
468 | 3 | sprintf('%d (%s)', $priority, $priorityMap[$priority]))) { |
|
469 | 2 | $this->getHeaders()->addTextHeader('X-Priority', |
|
470 | 2 | sprintf('%d (%s)', $priority, $priorityMap[$priority])); |
|
471 | 2 | } |
|
472 | |||
473 | 3 | return $this; |
|
474 | } |
||
475 | |||
476 | /** |
||
477 | * Get the priority of this message. |
||
478 | * |
||
479 | * The returned value is an integer where 1 is the highest priority and 5 |
||
480 | * is the lowest. |
||
481 | * |
||
482 | * @return int |
||
483 | */ |
||
484 | 1 | public function getPriority() |
|
490 | |||
491 | /** |
||
492 | * Ask for a delivery receipt from the recipient to be sent to $addresses. |
||
493 | * |
||
494 | * @param array $addresses |
||
495 | * |
||
496 | * @return Swift_Mime_SimpleMessage |
||
497 | */ |
||
498 | 3 | public function setReadReceiptTo($addresses) |
|
499 | { |
||
500 | 3 | if (!$this->_setHeaderFieldModel('Disposition-Notification-To', $addresses)) { |
|
501 | 2 | $this->getHeaders()->addMailboxHeader('Disposition-Notification-To', $addresses); |
|
502 | 2 | } |
|
503 | |||
504 | 3 | return $this; |
|
505 | } |
||
506 | |||
507 | /** |
||
508 | * Get the addresses to which a read-receipt will be sent. |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | 1 | public function getReadReceiptTo() |
|
516 | |||
517 | /** |
||
518 | * Attach a {@link Swift_Mime_MimeEntity} such as an Attachment or MimePart. |
||
519 | * |
||
520 | * @param Swift_Mime_MimeEntity $entity |
||
521 | * |
||
522 | * @return Swift_Mime_SimpleMessage |
||
523 | */ |
||
524 | 41 | public function attach(Swift_Mime_MimeEntity $entity) |
|
530 | |||
531 | /** |
||
532 | * Remove an already attached entity. |
||
533 | * |
||
534 | * @param Swift_Mime_MimeEntity $entity |
||
535 | * |
||
536 | * @return Swift_Mime_SimpleMessage |
||
537 | */ |
||
538 | 6 | public function detach(Swift_Mime_MimeEntity $entity) |
|
539 | { |
||
540 | 6 | $newChildren = array(); |
|
541 | 6 | foreach ($this->getChildren() as $child) { |
|
542 | 6 | if ($entity !== $child) { |
|
543 | 5 | $newChildren[] = $child; |
|
544 | 5 | } |
|
545 | 6 | } |
|
546 | 6 | $this->setChildren($newChildren); |
|
547 | |||
548 | 6 | return $this; |
|
549 | } |
||
550 | |||
551 | /** |
||
552 | * Attach a {@link Swift_Mime_MimeEntity} and return it's CID source. |
||
553 | * This method should be used when embedding images or other data in a message. |
||
554 | * |
||
555 | * @param Swift_Mime_MimeEntity $entity |
||
556 | * |
||
557 | * @return string |
||
558 | */ |
||
559 | 8 | public function embed(Swift_Mime_MimeEntity $entity) |
|
565 | |||
566 | /** |
||
567 | * Get this message as a complete string. |
||
568 | * |
||
569 | * @return string |
||
570 | */ |
||
571 | 96 | View Code Duplication | public function toString() |
572 | { |
||
573 | 96 | if (count($children = $this->getChildren()) > 0 && $this->getBody() != '') { |
|
574 | 8 | $this->setChildren(array_merge(array($this->_becomeMimePart()), $children)); |
|
575 | 8 | $string = parent::toString(); |
|
576 | 8 | $this->setChildren($children); |
|
577 | 8 | } else { |
|
578 | 88 | $string = parent::toString(); |
|
579 | } |
||
580 | |||
581 | 96 | return $string; |
|
582 | } |
||
583 | |||
584 | /** |
||
585 | * Returns a string representation of this object. |
||
586 | * |
||
587 | * @see toString() |
||
588 | * |
||
589 | * @return string |
||
590 | */ |
||
591 | public function __toString() |
||
595 | |||
596 | /** |
||
597 | * Write this message to a {@link Swift_InputByteStream}. |
||
598 | * |
||
599 | * @param Swift_InputByteStream $is |
||
600 | */ |
||
601 | 21 | View Code Duplication | public function toByteStream(Swift_InputByteStream $is) |
602 | { |
||
603 | 21 | if (count($children = $this->getChildren()) > 0 && $this->getBody() != '') { |
|
604 | 8 | $this->setChildren(array_merge(array($this->_becomeMimePart()), $children)); |
|
605 | 8 | parent::toByteStream($is); |
|
606 | 8 | $this->setChildren($children); |
|
607 | 8 | } else { |
|
608 | 13 | parent::toByteStream($is); |
|
609 | } |
||
610 | 21 | } |
|
611 | |||
612 | /** @see Swift_Mime_SimpleMimeEntity::_getIdField() */ |
||
613 | 221 | protected function _getIdField() |
|
617 | |||
618 | /** Turn the body of this message into a child of itself if needed */ |
||
619 | 17 | protected function _becomeMimePart() |
|
620 | { |
||
621 | 17 | $part = new parent( |
|
622 | 17 | $this->getHeaders()->newInstance(), |
|
623 | 17 | $this->getEncoder(), |
|
624 | 17 | $this->_getCache(), |
|
625 | 17 | $this->_getEmailValidator(), |
|
626 | 17 | $this->_userCharset |
|
627 | 17 | ); |
|
628 | 17 | $part->setContentType($this->_userContentType); |
|
629 | 17 | $part->setBody($this->getBody()); |
|
630 | 17 | $part->setFormat($this->_userFormat); |
|
631 | 17 | $part->setDelSp($this->_userDelSp); |
|
632 | 17 | $part->setMaxLineLength($this->getMaxLineLength()); |
|
633 | 17 | $part->_setNestingLevel($this->_getTopNestingLevel()); |
|
637 | |||
638 | /** Get the highest nesting level nested inside this message */ |
||
639 | 17 | private function _getTopNestingLevel() |
|
651 | } |
||
652 |
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.