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_Message 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_Message, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Swift_Message extends Swift_Mime_SimpleMessage |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var Swift_Signers_HeaderSigner[] |
||
| 20 | */ |
||
| 21 | private $headerSigners = array(); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Swift_Signers_BodySigner[] |
||
| 25 | */ |
||
| 26 | private $bodySigners = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $savedMessage = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Create a new Message. |
||
| 35 | * |
||
| 36 | * Details may be optionally passed into the constructor. |
||
| 37 | * |
||
| 38 | * @param string $subject |
||
| 39 | * @param string $body |
||
| 40 | * @param string $contentType |
||
| 41 | * @param string $charset |
||
| 42 | */ |
||
| 43 | 109 | public function __construct($subject = null, $body = null, $contentType = null, $charset = null) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * use this only if you use the memory-spool and |
||
| 64 | * you need to generate a message, before you define the transport |
||
| 65 | * |
||
| 66 | * @param bool $bool |
||
| 67 | */ |
||
| 68 | 1 | public function setUseMemorySpool($bool) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Create a new Message. |
||
| 77 | * |
||
| 78 | * @param string $subject |
||
| 79 | * @param string $body |
||
| 80 | * @param string $contentType |
||
| 81 | * @param string $charset |
||
| 82 | * |
||
| 83 | * @return Swift_Message |
||
| 84 | */ |
||
| 85 | 59 | public static function newInstance($subject = null, $body = null, $contentType = null, $charset = null) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Add a MimePart to this Message. |
||
| 92 | * |
||
| 93 | * @param string|Swift_OutputByteStream $body |
||
| 94 | * @param string $contentType |
||
| 95 | * @param string $charset |
||
| 96 | * |
||
| 97 | * @return Swift_Mime_SimpleMessage |
||
| 98 | */ |
||
| 99 | 3 | public function addPart($body, $contentType = null, $charset = null) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Attach a new signature handler to the message. |
||
| 112 | * |
||
| 113 | * @param Swift_Signer $signer |
||
| 114 | * |
||
| 115 | * @return Swift_Message |
||
| 116 | */ |
||
| 117 | 9 | public function attachSigner(Swift_Signer $signer) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Attach a new signature handler to the message. |
||
| 130 | * |
||
| 131 | * @param Swift_Signer $signer |
||
| 132 | * |
||
| 133 | * @return Swift_Message |
||
| 134 | */ |
||
| 135 | public function detachSigner(Swift_Signer $signer) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get this message as a complete string. |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | 82 | View Code Duplication | public function toString() |
| 179 | |||
| 180 | /** |
||
| 181 | * Write this message to a {@link Swift_InputByteStream}. |
||
| 182 | * |
||
| 183 | * @param Swift_InputByteStream $is |
||
| 184 | */ |
||
| 185 | 17 | View Code Duplication | public function toByteStream(Swift_InputByteStream $is) |
| 201 | |||
| 202 | public function __wakeup() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * loops through signers and apply the signatures. |
||
| 209 | */ |
||
| 210 | 8 | protected function doSign() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * save the message before any signature is applied. |
||
| 235 | */ |
||
| 236 | 8 | protected function saveMessage() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * save the original headers. |
||
| 254 | * |
||
| 255 | * @param array $altered |
||
| 256 | */ |
||
| 257 | 8 | protected function saveHeaders(array $altered) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Remove or restore altered headers. |
||
| 270 | */ |
||
| 271 | 8 | protected function restoreHeaders() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Restore message body. |
||
| 286 | */ |
||
| 287 | 8 | protected function restoreMessage() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Clone Message Signers. |
||
| 298 | * |
||
| 299 | * @see Swift_Mime_SimpleMimeEntity::__clone() |
||
| 300 | */ |
||
| 301 | 5 | public function __clone() |
|
| 315 | } |
||
| 316 |
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.