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 | /** |
||
| 128 | * @var string |
||
| 129 | */ |
||
| 130 | protected $_userContentType; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Create a new SimpleMimeEntity with $headers, $encoder and $cache. |
||
| 134 | * |
||
| 135 | * @param Swift_Mime_HeaderSet $headers |
||
| 136 | * @param Swift_Mime_ContentEncoder $encoder |
||
| 137 | * @param Swift_KeyCache $cache |
||
| 138 | * @param Swift_EmailValidatorBridge $emailValidator |
||
| 139 | */ |
||
| 140 | 497 | public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_EmailValidatorBridge $emailValidator) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Generate a new Content-ID or Message-ID for this MIME entity. |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | 16 | public function generateId() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Get the {@link Swift_Mime_HeaderSet} for this entity. |
||
| 188 | * |
||
| 189 | * @return Swift_Mime_HeaderSet |
||
| 190 | */ |
||
| 191 | 360 | public function getHeaders() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Get the nesting level of this entity. |
||
| 198 | * |
||
| 199 | * @see LEVEL_TOP, LEVEL_MIXED, LEVEL_RELATED, LEVEL_ALTERNATIVE |
||
| 200 | * |
||
| 201 | * @return int |
||
| 202 | */ |
||
| 203 | 20 | public function getNestingLevel() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Get the Content-type of this entity. |
||
| 210 | * |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | 53 | public function getContentType() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Set the Content-type of this entity. |
||
| 220 | * |
||
| 221 | * @param string $type |
||
| 222 | * |
||
| 223 | * @return Swift_Mime_SimpleMimeEntity |
||
| 224 | */ |
||
| 225 | 452 | public function setContentType($type) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Get the CID of this entity. |
||
| 237 | * |
||
| 238 | * The CID will only be present in headers if a Content-ID header is present. |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | 307 | public function getId() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Set the CID of this entity. |
||
| 251 | * |
||
| 252 | * @param string $id |
||
| 253 | * |
||
| 254 | * @return Swift_Mime_SimpleMimeEntity |
||
| 255 | */ |
||
| 256 | 310 | public function setId($id) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Get the description of this entity. |
||
| 268 | * |
||
| 269 | * This value comes from the Content-Description header if set. |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | 5 | public function getDescription() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Set the description of this entity. |
||
| 280 | * |
||
| 281 | * This method sets a value in the Content-ID header. |
||
| 282 | * |
||
| 283 | * @param string $description |
||
| 284 | * |
||
| 285 | * @return Swift_Mime_SimpleMimeEntity |
||
| 286 | */ |
||
| 287 | 15 | public function setDescription($description) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Get the maximum line length of the body of this entity. |
||
| 298 | * |
||
| 299 | * @return int |
||
| 300 | */ |
||
| 301 | 114 | public function getMaxLineLength() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Set the maximum line length of lines in this body. |
||
| 308 | * |
||
| 309 | * Though not enforced by the library, lines should not exceed 1000 chars. |
||
| 310 | * |
||
| 311 | * @param int $length |
||
| 312 | * |
||
| 313 | * @return Swift_Mime_SimpleMimeEntity |
||
| 314 | */ |
||
| 315 | 32 | public function setMaxLineLength($length) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Get all children added to this entity. |
||
| 324 | * |
||
| 325 | * @return Swift_Mime_MimeEntity[] |
||
| 326 | */ |
||
| 327 | 147 | public function getChildren() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Set all children of this entity. |
||
| 334 | * |
||
| 335 | * @param Swift_Mime_MimeEntity[] $children |
||
| 336 | * @param int $compoundLevel For internal use only |
||
| 337 | * |
||
| 338 | * @return Swift_Mime_SimpleMimeEntity |
||
| 339 | */ |
||
| 340 | 117 | public function setChildren(array $children, $compoundLevel = null) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Get the body of this entity as a string. |
||
| 402 | * |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | 125 | public function getBody() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Set the body of this entity, either as a string, or as an instance of |
||
| 412 | * {@link Swift_OutputByteStream}. |
||
| 413 | * |
||
| 414 | * @param mixed $body |
||
| 415 | * @param string $contentType optional |
||
| 416 | * |
||
| 417 | * @return Swift_Mime_SimpleMimeEntity |
||
| 418 | */ |
||
| 419 | 218 | public function setBody($body, $contentType = null) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Get the encoder used for the body of this entity. |
||
| 435 | * |
||
| 436 | * @return Swift_Mime_ContentEncoder |
||
| 437 | */ |
||
| 438 | 27 | public function getEncoder() |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Set the encoder used for the body of this entity. |
||
| 445 | * |
||
| 446 | * @param Swift_Mime_ContentEncoder $encoder |
||
| 447 | * |
||
| 448 | * @return Swift_Mime_SimpleMimeEntity |
||
| 449 | */ |
||
| 450 | 497 | public function setEncoder(Swift_Mime_ContentEncoder $encoder) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Get the boundary used to separate children in this entity. |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | 118 | public function getBoundary() |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Set the boundary used to separate children in this entity. |
||
| 479 | * |
||
| 480 | * @param string $boundary |
||
| 481 | * |
||
| 482 | * @throws Swift_RfcComplianceException |
||
| 483 | * |
||
| 484 | * @return Swift_Mime_SimpleMimeEntity |
||
| 485 | */ |
||
| 486 | 29 | public function setBoundary($boundary) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Receive notification that the charset of this entity, or a parent entity |
||
| 496 | * has changed. |
||
| 497 | * |
||
| 498 | * @param string $charset |
||
| 499 | */ |
||
| 500 | 140 | public function charsetChanged($charset) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Receive notification that the encoder of this entity or a parent entity |
||
| 507 | * has changed. |
||
| 508 | * |
||
| 509 | * @param Swift_Mime_ContentEncoder $encoder |
||
| 510 | */ |
||
| 511 | 6 | public function encoderChanged(Swift_Mime_ContentEncoder $encoder) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Get this entire entity as a string. |
||
| 518 | * |
||
| 519 | * @return string |
||
| 520 | */ |
||
| 521 | 183 | public function toString() |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Get this entire entity as a string. |
||
| 531 | * |
||
| 532 | * @return string |
||
| 533 | */ |
||
| 534 | 183 | protected function _bodyToString() |
|
| 562 | |||
| 563 | /** |
||
| 564 | * Returns a string representation of this object. |
||
| 565 | * |
||
| 566 | * @see toString() |
||
| 567 | * |
||
| 568 | * @return string |
||
| 569 | */ |
||
| 570 | public function __toString() |
||
| 574 | |||
| 575 | /** |
||
| 576 | 37 | * Write this entire entity to a {@see Swift_InputByteStream}. |
|
| 577 | * |
||
| 578 | 37 | * @param Swift_InputByteStream |
|
| 579 | 37 | */ |
|
| 580 | public function toByteStream(Swift_InputByteStream $is) |
||
| 587 | |||
| 588 | /** |
||
| 589 | 37 | * Write this entire entity to a {@link Swift_InputByteStream}. |
|
| 590 | * |
||
| 591 | 37 | * @param Swift_InputByteStream |
|
| 592 | 37 | */ |
|
| 593 | 27 | protected function _bodyToByteStream(Swift_InputByteStream $is) |
|
| 630 | 106 | ||
| 631 | /** |
||
| 632 | 106 | * Get the name of the header that provides the ID of this entity. |
|
| 633 | */ |
||
| 634 | protected function _getIdField() |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Get the model data (usually an array or a string) for $field. |
||
| 641 | * |
||
| 642 | 317 | * @param string $field |
|
| 643 | * |
||
| 644 | 317 | * @return string |
|
| 645 | 152 | */ |
|
| 646 | protected function _getHeaderFieldModel($field) |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Set the model data for $field. |
||
| 657 | * |
||
| 658 | * @param string $field |
||
| 659 | 497 | * @param $model |
|
| 660 | * |
||
| 661 | 497 | * @return bool |
|
| 662 | 247 | */ |
|
| 663 | protected function _setHeaderFieldModel($field, $model) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Get the parameter value of $parameter on $field header. |
||
| 676 | * @param string $field |
||
| 677 | 163 | * @param string $parameter |
|
| 678 | * |
||
| 679 | 163 | * @return string |
|
| 680 | 131 | */ |
|
| 681 | protected function _getHeaderParameter($field, $parameter) |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Set the parameter value of $parameter on $field header. |
||
| 690 | * @param string $field |
||
| 691 | * @param string $parameter |
||
| 692 | 238 | * @param $value |
|
| 693 | * |
||
| 694 | 238 | * @return bool |
|
| 695 | 189 | */ |
|
| 696 | protected function _setHeaderParameter($field, $parameter, $value) |
||
| 706 | 117 | ||
| 707 | /** |
||
| 708 | 117 | * Re-evaluate what content type and encoding should be used on this entity. |
|
| 709 | 103 | */ |
|
| 710 | 103 | protected function _fixHeaders() |
|
| 720 | |||
| 721 | /** |
||
| 722 | 17 | * Get the KeyCache used in this entity. |
|
| 723 | * |
||
| 724 | 17 | * @return Swift_KeyCache |
|
| 725 | */ |
||
| 726 | protected function _getCache() |
||
| 730 | |||
| 731 | /** |
||
| 732 | 17 | * Get the email-validator. |
|
| 733 | * |
||
| 734 | 17 | * @return Swift_EmailValidatorBridge |
|
| 735 | */ |
||
| 736 | protected function _getEmailValidator() |
||
| 740 | 497 | ||
| 741 | /** |
||
| 742 | 497 | * Empty the KeyCache for this entity. |
|
| 743 | 497 | */ |
|
| 744 | protected function _clearCache() |
||
| 748 | |||
| 749 | /** |
||
| 750 | 497 | * Returns a random Content-ID or Message-ID. |
|
| 751 | * |
||
| 752 | 497 | * @return string |
|
| 753 | 497 | */ |
|
| 754 | 497 | protected function getRandomId() |
|
| 768 | |||
| 769 | /** |
||
| 770 | 497 | * generate a new cache-key |
|
| 771 | * |
||
| 772 | 497 | * @return string |
|
| 773 | */ |
||
| 774 | private function _generateNewCacheKey() |
||
| 778 | 14 | ||
| 779 | 14 | private function _readStream(Swift_OutputByteStream $os) |
|
| 790 | 497 | ||
| 791 | /** |
||
| 792 | 497 | * @param string $encoding |
|
| 793 | 492 | */ |
|
| 794 | 492 | private function _setEncoding($encoding) |
|
| 800 | |||
| 801 | /** |
||
| 802 | 29 | * @param string $boundary |
|
| 803 | * |
||
| 804 | 29 | * @throws Swift_RfcComplianceException |
|
| 805 | */ |
||
| 806 | private function _assertValidBoundary($boundary) |
||
| 812 | 402 | ||
| 813 | 402 | private function _setContentTypeInHeaders($type) |
|
| 819 | 20 | ||
| 820 | private function _setNestingLevel($level) |
||
| 824 | |||
| 825 | /** |
||
| 826 | 117 | * @param Swift_Mime_MimeEntity[] $children |
|
| 827 | * |
||
| 828 | 117 | * @return int |
|
| 829 | 117 | */ |
|
| 830 | 103 | private function _getCompoundLevel($children) |
|
| 839 | |||
| 840 | /** |
||
| 841 | * @param Swift_Mime_MimeEntity $child |
||
| 842 | 103 | * @param integer $compoundLevel |
|
| 843 | * |
||
| 844 | 103 | * @return int |
|
| 845 | 103 | */ |
|
| 846 | 103 | private function _getNeededChildLevel($child, $compoundLevel) |
|
| 864 | 20 | ||
| 865 | /** |
||
| 866 | 20 | * @return Swift_Mime_SimpleMimeEntity |
|
| 867 | */ |
||
| 868 | private function _createChild() |
||
| 872 | 497 | ||
| 873 | /** |
||
| 874 | 497 | * @param Swift_Mime_ContentEncoder $encoder |
|
| 875 | 11 | */ |
|
| 876 | 497 | private function _notifyEncoderChanged(Swift_Mime_ContentEncoder $encoder) |
|
| 882 | 140 | ||
| 883 | /** |
||
| 884 | 140 | * @param string $charset |
|
| 885 | 140 | */ |
|
| 886 | 140 | private function _notifyCharsetChanged($charset) |
|
| 894 | 117 | ||
| 895 | private function _sortChildren() |
||
| 911 | |||
| 912 | /** |
||
| 913 | * @param Swift_Mime_MimeEntity $a |
||
| 914 | 34 | * @param Swift_Mime_MimeEntity $b |
|
| 915 | * |
||
| 916 | 34 | * @return int |
|
| 917 | */ |
||
| 918 | private function _childSortAlgorithm($a, $b) |
||
| 937 | 495 | ||
| 938 | /** |
||
| 939 | 495 | * Empties it's own contents from the cache. |
|
| 940 | 495 | */ |
|
| 941 | public function __destruct() |
||
| 945 | |||
| 946 | /** |
||
| 947 | * Throws an Exception if the id passed does not comply with RFC 2822. |
||
| 948 | * |
||
| 949 | 497 | * @param string $id |
|
| 950 | * |
||
| 951 | 497 | * @throws Swift_RfcComplianceException |
|
| 952 | */ |
||
| 953 | private function _assertValidId($id) |
||
| 959 | 5 | ||
| 960 | /** |
||
| 961 | 5 | * Make a deep copy of object. |
|
| 962 | 5 | */ |
|
| 963 | public function __clone() |
||
| 982 | } |
||
| 983 |
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.