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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 380 | |||
| 381 | /** |
||
| 382 | * Get the Cc address of this message. |
||
| 383 | * |
||
| 384 | * @return array |
||
| 385 | */ |
||
| 386 | 10 | public function getCc() |
|
| 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) |
| 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) |
|
| 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) |
|
| 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) |
|
| 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() |
| 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) |
| 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() |
|
| 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.