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_SimpleMimeEntity 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_SimpleMimeEntity, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Swift_Mime_SimpleMimeEntity implements Swift_Mime_MimeEntity |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * A collection of Headers for this mime entity |
||
| 20 | * |
||
| 21 | * @var Swift_Mime_HeaderSet |
||
| 22 | */ |
||
| 23 | private $_headers; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The body as a string, or a stream |
||
| 27 | */ |
||
| 28 | private $_body; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The encoder that encodes the body into a streamable format |
||
| 32 | * |
||
| 33 | * @var Swift_Mime_ContentEncoder |
||
| 34 | */ |
||
| 35 | private $_encoder; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The validator for email |
||
| 39 | * |
||
| 40 | * @var Swift_EmailValidatorBridge |
||
| 41 | */ |
||
| 42 | private $_emailValidator; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * A mime boundary, if any is used |
||
| 46 | */ |
||
| 47 | private $_boundary; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Mime types to be used based on the nesting level |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $_compositeRanges = array( |
||
| 55 | 'multipart/mixed' => array(self::LEVEL_TOP, self::LEVEL_MIXED), |
||
| 56 | 'multipart/alternative' => array(self::LEVEL_MIXED, self::LEVEL_ALTERNATIVE), |
||
| 57 | 'multipart/related' => array(self::LEVEL_ALTERNATIVE, self::LEVEL_RELATED), |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * A set of filter rules to define what level an entity should be nested at |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $_compoundLevelFilters = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The nesting level of this entity |
||
| 69 | * |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | private $_nestingLevel = self::LEVEL_ALTERNATIVE; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * A KeyCache instance used during encoding and streaming |
||
| 76 | * |
||
| 77 | * @var Swift_KeyCache |
||
| 78 | */ |
||
| 79 | private $_cache; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Direct descendants of this entity |
||
| 83 | * |
||
| 84 | * @var Swift_Mime_MimeEntity[]|array |
||
| 85 | */ |
||
| 86 | private $_immediateChildren = array(); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * All descendants of this entity |
||
| 90 | * |
||
| 91 | * @var Swift_Mime_MimeEntity[] |
||
| 92 | */ |
||
| 93 | private $_children = array(); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The maximum line length of the body of this entity |
||
| 97 | * |
||
| 98 | * @var int |
||
| 99 | */ |
||
| 100 | private $_maxLineLength = 78; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The order in which alternative mime types should appear |
||
| 104 | * |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | private $_alternativePartOrder = array( |
||
| 108 | 'text/plain' => 1, |
||
| 109 | 'text/html' => 2, |
||
| 110 | 'multipart/related' => 3, |
||
| 111 | ); |
||
| 112 | |||
| 113 | /** |
||
| 114 | * The CID of this entity |
||
| 115 | * |
||
| 116 | * @var string |
||
| 117 | */ |
||
| 118 | private $_id; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * The key used for accessing the cache |
||
| 122 | * |
||
| 123 | * @var string |
||
| 124 | */ |
||
| 125 | private $_cacheKey; |
||
| 126 | |||
| 127 | protected $_userContentType; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Create a new SimpleMimeEntity with $headers, $encoder and $cache. |
||
| 131 | * |
||
| 132 | * @param Swift_Mime_HeaderSet $headers |
||
| 133 | * @param Swift_Mime_ContentEncoder $encoder |
||
| 134 | * @param Swift_KeyCache $cache |
||
| 135 | * @param Swift_EmailValidatorBridge $emailValidator |
||
| 136 | */ |
||
| 137 | 497 | public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_EmailValidatorBridge $emailValidator) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Generate a new Content-ID or Message-ID for this MIME entity. |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | 15 | public function generateId() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Get the {@link Swift_Mime_HeaderSet} for this entity. |
||
| 185 | * |
||
| 186 | * @return Swift_Mime_HeaderSet |
||
| 187 | */ |
||
| 188 | 360 | public function getHeaders() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Get the nesting level of this entity. |
||
| 195 | * |
||
| 196 | * @see LEVEL_TOP, LEVEL_MIXED, LEVEL_RELATED, LEVEL_ALTERNATIVE |
||
| 197 | * |
||
| 198 | * @return int |
||
| 199 | */ |
||
| 200 | 20 | public function getNestingLevel() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Get the Content-type of this entity. |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | 53 | public function getContentType() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Set the Content-type of this entity. |
||
| 217 | * |
||
| 218 | * @param string $type |
||
| 219 | * |
||
| 220 | * @return Swift_Mime_SimpleMimeEntity |
||
| 221 | */ |
||
| 222 | 452 | public function setContentType($type) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Get the CID of this entity. |
||
| 234 | * |
||
| 235 | * The CID will only be present in headers if a Content-ID header is present. |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | 307 | public function getId() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Set the CID of this entity. |
||
| 248 | * |
||
| 249 | * @param string $id |
||
| 250 | * |
||
| 251 | * @return Swift_Mime_SimpleMimeEntity |
||
| 252 | */ |
||
| 253 | 310 | public function setId($id) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Get the description of this entity. |
||
| 265 | * |
||
| 266 | * This value comes from the Content-Description header if set. |
||
| 267 | * |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | 5 | public function getDescription() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Set the description of this entity. |
||
| 277 | * |
||
| 278 | * This method sets a value in the Content-ID header. |
||
| 279 | * |
||
| 280 | * @param string $description |
||
| 281 | * |
||
| 282 | * @return Swift_Mime_SimpleMimeEntity |
||
| 283 | */ |
||
| 284 | 15 | public function setDescription($description) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Get the maximum line length of the body of this entity. |
||
| 295 | * |
||
| 296 | * @return int |
||
| 297 | */ |
||
| 298 | 114 | public function getMaxLineLength() |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Set the maximum line length of lines in this body. |
||
| 305 | * |
||
| 306 | * Though not enforced by the library, lines should not exceed 1000 chars. |
||
| 307 | * |
||
| 308 | * @param int $length |
||
| 309 | * |
||
| 310 | * @return Swift_Mime_SimpleMimeEntity |
||
| 311 | */ |
||
| 312 | 32 | public function setMaxLineLength($length) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Get all children added to this entity. |
||
| 321 | * |
||
| 322 | * @return Swift_Mime_MimeEntity[] |
||
| 323 | */ |
||
| 324 | 145 | public function getChildren() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Set all children of this entity. |
||
| 331 | * |
||
| 332 | * @param Swift_Mime_MimeEntity[] $children |
||
| 333 | * @param int $compoundLevel For internal use only |
||
| 334 | * |
||
| 335 | * @return Swift_Mime_SimpleMimeEntity |
||
| 336 | */ |
||
| 337 | 115 | public function setChildren(array $children, $compoundLevel = null) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Get the body of this entity as a string. |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | 125 | public function getBody() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Set the body of this entity, either as a string, or as an instance of |
||
| 409 | * {@link Swift_OutputByteStream}. |
||
| 410 | * |
||
| 411 | * @param mixed $body |
||
| 412 | * @param string $contentType optional |
||
| 413 | * |
||
| 414 | * @return Swift_Mime_SimpleMimeEntity |
||
| 415 | */ |
||
| 416 | 218 | public function setBody($body, $contentType = null) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Get the encoder used for the body of this entity. |
||
| 432 | * |
||
| 433 | * @return Swift_Mime_ContentEncoder |
||
| 434 | */ |
||
| 435 | 27 | public function getEncoder() |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Set the encoder used for the body of this entity. |
||
| 442 | * |
||
| 443 | * @param Swift_Mime_ContentEncoder $encoder |
||
| 444 | * |
||
| 445 | * @return Swift_Mime_SimpleMimeEntity |
||
| 446 | */ |
||
| 447 | 497 | public function setEncoder(Swift_Mime_ContentEncoder $encoder) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Get the boundary used to separate children in this entity. |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | 118 | public function getBoundary() |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Set the boundary used to separate children in this entity. |
||
| 476 | * |
||
| 477 | * @param string $boundary |
||
| 478 | * |
||
| 479 | * @throws Swift_RfcComplianceException |
||
| 480 | * |
||
| 481 | * @return Swift_Mime_SimpleMimeEntity |
||
| 482 | */ |
||
| 483 | 29 | public function setBoundary($boundary) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Receive notification that the charset of this entity, or a parent entity |
||
| 493 | * has changed. |
||
| 494 | * |
||
| 495 | * @param string $charset |
||
| 496 | */ |
||
| 497 | 140 | public function charsetChanged($charset) |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Receive notification that the encoder of this entity or a parent entity |
||
| 504 | * has changed. |
||
| 505 | * |
||
| 506 | * @param Swift_Mime_ContentEncoder $encoder |
||
| 507 | */ |
||
| 508 | 6 | public function encoderChanged(Swift_Mime_ContentEncoder $encoder) |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Get this entire entity as a string. |
||
| 515 | * |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | 183 | public function toString() |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Get this entire entity as a string. |
||
| 528 | * |
||
| 529 | * @return string |
||
| 530 | */ |
||
| 531 | 183 | protected function _bodyToString() |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Returns a string representation of this object. |
||
| 558 | * |
||
| 559 | * @see toString() |
||
| 560 | * |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | public function __toString() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Write this entire entity to a {@see Swift_InputByteStream}. |
||
| 570 | * |
||
| 571 | * @param Swift_InputByteStream |
||
| 572 | */ |
||
| 573 | 37 | public function toByteStream(Swift_InputByteStream $is) |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Write this entire entity to a {@link Swift_InputByteStream}. |
||
| 583 | * |
||
| 584 | * @param Swift_InputByteStream |
||
| 585 | */ |
||
| 586 | 37 | protected function _bodyToByteStream(Swift_InputByteStream $is) |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Get the name of the header that provides the ID of this entity. |
||
| 626 | */ |
||
| 627 | 106 | protected function _getIdField() |
|
| 631 | |||
| 632 | /** |
||
| 633 | * Get the model data (usually an array or a string) for $field. |
||
| 634 | * |
||
| 635 | * @param string $field |
||
| 636 | * |
||
| 637 | * @return string |
||
| 638 | */ |
||
| 639 | 317 | protected function _getHeaderFieldModel($field) |
|
| 647 | |||
| 648 | /** |
||
| 649 | * Set the model data for $field. |
||
| 650 | * |
||
| 651 | * @param string $field |
||
| 652 | * @param $model |
||
| 653 | * |
||
| 654 | * @return bool |
||
| 655 | */ |
||
| 656 | 497 | protected function _setHeaderFieldModel($field, $model) |
|
| 666 | |||
| 667 | /** |
||
| 668 | * Get the parameter value of $parameter on $field header. |
||
| 669 | * @param string $field |
||
| 670 | * @param string $parameter |
||
| 671 | */ |
||
| 672 | 163 | protected function _getHeaderParameter($field, $parameter) |
|
| 678 | |||
| 679 | /** |
||
| 680 | * Set the parameter value of $parameter on $field header. |
||
| 681 | * @param string $field |
||
| 682 | * @param string $parameter |
||
| 683 | * @param $value |
||
| 684 | * |
||
| 685 | * @return bool |
||
| 686 | */ |
||
| 687 | 238 | protected function _setHeaderParameter($field, $parameter, $value) |
|
| 697 | |||
| 698 | /** |
||
| 699 | * Re-evaluate what content type and encoding should be used on this entity. |
||
| 700 | */ |
||
| 701 | 115 | protected function _fixHeaders() |
|
| 713 | |||
| 714 | /** |
||
| 715 | * Get the KeyCache used in this entity. |
||
| 716 | * |
||
| 717 | * @return Swift_KeyCache |
||
| 718 | */ |
||
| 719 | 17 | protected function _getCache() |
|
| 723 | |||
| 724 | /** |
||
| 725 | * Get the email-validator. |
||
| 726 | * |
||
| 727 | * @return Swift_EmailValidatorBridge |
||
| 728 | */ |
||
| 729 | 17 | protected function _getEmailValidator() |
|
| 733 | |||
| 734 | /** |
||
| 735 | * Empty the KeyCache for this entity. |
||
| 736 | */ |
||
| 737 | 497 | protected function _clearCache() |
|
| 741 | |||
| 742 | /** |
||
| 743 | * Returns a random Content-ID or Message-ID. |
||
| 744 | * |
||
| 745 | * @return string |
||
| 746 | */ |
||
| 747 | 497 | protected function getRandomId() |
|
| 761 | |||
| 762 | /** |
||
| 763 | * generate a new cache-key |
||
| 764 | * |
||
| 765 | * @return string |
||
| 766 | */ |
||
| 767 | 497 | private function _generateNewCacheKey() |
|
| 771 | |||
| 772 | 14 | private function _readStream(Swift_OutputByteStream $os) |
|
| 783 | |||
| 784 | /** |
||
| 785 | * @param string $encoding |
||
| 786 | */ |
||
| 787 | 497 | private function _setEncoding($encoding) |
|
| 793 | |||
| 794 | /** |
||
| 795 | * @param string $boundary |
||
| 796 | * |
||
| 797 | * @throws Swift_RfcComplianceException |
||
| 798 | */ |
||
| 799 | 29 | private function _assertValidBoundary($boundary) |
|
| 805 | |||
| 806 | 463 | private function _setContentTypeInHeaders($type) |
|
| 812 | |||
| 813 | 20 | private function _setNestingLevel($level) |
|
| 817 | |||
| 818 | /** |
||
| 819 | * @param Swift_Mime_MimeEntity[] $children |
||
| 820 | * |
||
| 821 | * @return int |
||
| 822 | */ |
||
| 823 | 115 | private function _getCompoundLevel($children) |
|
| 832 | |||
| 833 | /** |
||
| 834 | * @param Swift_Mime_MimeEntity $child |
||
| 835 | * @param integer $compoundLevel |
||
| 836 | * |
||
| 837 | * @return mixed |
||
| 838 | */ |
||
| 839 | 103 | private function _getNeededChildLevel($child, $compoundLevel) |
|
| 857 | |||
| 858 | /** |
||
| 859 | * @return Swift_Mime_SimpleMimeEntity |
||
| 860 | */ |
||
| 861 | 20 | private function _createChild() |
|
| 865 | |||
| 866 | /** |
||
| 867 | * @param Swift_Mime_ContentEncoder $encoder |
||
| 868 | */ |
||
| 869 | 497 | private function _notifyEncoderChanged(Swift_Mime_ContentEncoder $encoder) |
|
| 875 | |||
| 876 | /** |
||
| 877 | * @param string $charset |
||
| 878 | */ |
||
| 879 | 140 | private function _notifyCharsetChanged($charset) |
|
| 887 | |||
| 888 | 115 | private function _sortChildren() |
|
| 904 | |||
| 905 | /** |
||
| 906 | * @param Swift_Mime_MimeEntity $a |
||
| 907 | * @param Swift_Mime_MimeEntity $b |
||
| 908 | * |
||
| 909 | * @return int |
||
| 910 | */ |
||
| 911 | 34 | private function _childSortAlgorithm($a, $b) |
|
| 930 | |||
| 931 | // -- Destructor |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Empties it's own contents from the cache. |
||
| 935 | */ |
||
| 936 | 493 | public function __destruct() |
|
| 940 | |||
| 941 | /** |
||
| 942 | * Throws an Exception if the id passed does not comply with RFC 2822. |
||
| 943 | * |
||
| 944 | * @param string $id |
||
| 945 | * |
||
| 946 | * @throws Swift_RfcComplianceException |
||
| 947 | */ |
||
| 948 | 497 | private function _assertValidId($id) |
|
| 954 | |||
| 955 | /** |
||
| 956 | * Make a deep copy of object. |
||
| 957 | */ |
||
| 958 | 2 | public function __clone() |
|
| 983 | } |
||
| 984 |
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.