Complex classes like Swift_Mime_Headers_AbstractHeader 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_Headers_AbstractHeader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | abstract class Swift_Mime_Headers_AbstractHeader implements Swift_Mime_Header |
||
|
|
|||
| 17 | { |
||
| 18 | const PHRASE_PATTERN = '(?:(?:(?:(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?[a-zA-Z0-9!#\$%&\'\*\+\-\/=\?\^_`\{\}\|~]+(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(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(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?))+?)'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The name of this Header. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $_name; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The Encoder used to encode this Header. |
||
| 29 | * |
||
| 30 | * @var Swift_Encoder|Swift_Mime_ContentEncoder |
||
| 31 | */ |
||
| 32 | private $_encoder; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The maximum length of a line in the header. |
||
| 36 | * |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | private $_lineLength = 78; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The language used in this Header. |
||
| 43 | * |
||
| 44 | * @var string|null |
||
| 45 | */ |
||
| 46 | private $_lang; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The character set of the text in this Header. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $_charset = 'utf-8'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The value of this Header, cached. |
||
| 57 | * |
||
| 58 | * @var string|null |
||
| 59 | */ |
||
| 60 | private $_cachedValue; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Creates a new Header. |
||
| 64 | */ |
||
| 65 | 256 | public function __construct() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Set the character set used in this Header. |
||
| 71 | * |
||
| 72 | * @param string $charset |
||
| 73 | */ |
||
| 74 | 185 | public function setCharset($charset) |
|
| 86 | |||
| 87 | /** |
||
| 88 | 83 | * Get the character set used in this Header. |
|
| 89 | * |
||
| 90 | 83 | * @return string |
|
| 91 | */ |
||
| 92 | public function getCharset() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Set the language used in this Header. |
||
| 99 | * |
||
| 100 | * For example, for US English, 'en-us'. |
||
| 101 | 4 | * This can be unspecified. |
|
| 102 | * |
||
| 103 | 4 | * @param string $lang |
|
| 104 | 4 | */ |
|
| 105 | 4 | public function setLanguage($lang) |
|
| 112 | 7 | ||
| 113 | /** |
||
| 114 | 7 | * Get the language used in this Header. |
|
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getLanguage() |
||
| 122 | 216 | ||
| 123 | /** |
||
| 124 | 216 | * Set the encoder used for encoding the header. |
|
| 125 | 216 | * |
|
| 126 | * @param Swift_Mime_HeaderEncoder $encoder |
||
| 127 | 216 | */ |
|
| 128 | 216 | public function setEncoder(Swift_Mime_HeaderEncoder $encoder) |
|
| 135 | 77 | ||
| 136 | /** |
||
| 137 | 77 | * Get the encoder used for encoding this Header. |
|
| 138 | * |
||
| 139 | * @return Swift_Mime_HeaderEncoder |
||
| 140 | */ |
||
| 141 | public function getEncoder() |
||
| 145 | 153 | ||
| 146 | /** |
||
| 147 | 153 | * Get the name of this header (e.g. charset). |
|
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getFieldName() |
||
| 155 | 20 | ||
| 156 | /** |
||
| 157 | 20 | * Set the maximum length of lines in the header (excluding EOL). |
|
| 158 | 20 | * |
|
| 159 | 20 | * @param int $lineLength |
|
| 160 | * |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public function setMaxLineLength($lineLength) |
||
| 175 | |||
| 176 | 157 | /** |
|
| 177 | * Get the maximum permitted length of lines in this Header. |
||
| 178 | 157 | * |
|
| 179 | * @return int |
||
| 180 | */ |
||
| 181 | public function getMaxLineLength() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get this Header rendered as a RFC 2822 compliant string. |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function toString() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns a string representation of this object. |
||
| 198 | 256 | * |
|
| 199 | * @return string |
||
| 200 | 256 | * |
|
| 201 | 256 | * @see toString() |
|
| 202 | */ |
||
| 203 | public function __toString() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Set the name of this Header field. |
||
| 210 | * |
||
| 211 | 77 | * @param string $name |
|
| 212 | */ |
||
| 213 | protected function setFieldName($name) |
||
| 217 | 77 | ||
| 218 | /** |
||
| 219 | 6 | * Produces a compliant, formatted RFC 2822 'phrase' based on the string given. |
|
| 220 | * |
||
| 221 | 3 | * @param Swift_Mime_Header $header |
|
| 222 | * @param string $string as displayed |
||
| 223 | * |
||
| 224 | 4 | * @return string |
|
| 225 | */ |
||
| 226 | protected function createPhrase(Swift_Mime_Header $header, $string) |
||
| 248 | 3 | ||
| 249 | /** |
||
| 250 | * Escape special characters in a string (convert to quoted-pairs). |
||
| 251 | * |
||
| 252 | * @param string $token |
||
| 253 | * @param string[] $include additional chars to escape |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | protected function escapeSpecials($token, array $include = array()) |
||
| 265 | 161 | ||
| 266 | /** |
||
| 267 | 161 | * Encode needed word tokens within a string of input. |
|
| 268 | * |
||
| 269 | 16 | * @param Swift_Mime_Header|Swift_Mime_Headers_AbstractHeader $header |
|
| 270 | * @param string $input |
||
| 271 | 16 | * |
|
| 272 | 12 | * @return string |
|
| 273 | 5 | */ |
|
| 274 | 5 | protected function encodeWords(Swift_Mime_Header $header, $input) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Test if a token needs to be encoded or not. |
||
| 306 | * |
||
| 307 | * @param string $token |
||
| 308 | * |
||
| 309 | * @return integer |
||
| 310 | */ |
||
| 311 | protected function tokenNeedsEncoding($token) |
||
| 319 | 161 | ||
| 320 | 16 | /** |
|
| 321 | * Splits a string into tokens in blocks of words which can be encoded quickly. |
||
| 322 | 150 | * |
|
| 323 | 3 | * @param string $string |
|
| 324 | 3 | * |
|
| 325 | * @return string[] |
||
| 326 | 161 | */ |
|
| 327 | protected function getEncodableWordTokens($string) |
||
| 351 | 17 | ||
| 352 | 2 | /** |
|
| 353 | * Get a token as an encoded word for safe insertion into headers. |
||
| 354 | * |
||
| 355 | 17 | * @param string $token token to encode |
|
| 356 | * @param int $firstLineOffset optional |
||
| 357 | 17 | * |
|
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | protected function getTokenAsEncodedWord($token, $firstLineOffset = 0) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Generates tokens from the given string which include CRLF as individual tokens. |
||
| 395 | 245 | * |
|
| 396 | * @param string $token |
||
| 397 | 245 | * |
|
| 398 | 245 | * @return string[] |
|
| 399 | */ |
||
| 400 | protected function generateTokenLines($token) |
||
| 404 | |||
| 405 | 176 | /** |
|
| 406 | * Set a value into the cache. |
||
| 407 | 176 | * |
|
| 408 | * @param string $value |
||
| 409 | */ |
||
| 410 | protected function setCachedValue($value) |
||
| 414 | |||
| 415 | 231 | /** |
|
| 416 | * Get the value in the cache. |
||
| 417 | 231 | * |
|
| 418 | 206 | * @return string |
|
| 419 | */ |
||
| 420 | 231 | protected function getCachedValue() |
|
| 424 | |||
| 425 | private function clearCachedValue() |
||
| 429 | 157 | ||
| 430 | /** |
||
| 431 | 157 | * Clear the cached value if $condition is met. |
|
| 432 | 152 | * |
|
| 433 | * @param bool $condition |
||
| 434 | */ |
||
| 435 | 157 | protected function clearCachedValueIf($condition) |
|
| 441 | 157 | ||
| 442 | /** |
||
| 443 | * Generate a list of all tokens in the final header. |
||
| 444 | * |
||
| 445 | 157 | * @param string $string The string to tokenize |
|
| 446 | * |
||
| 447 | * @return array An array of tokens as strings |
||
| 448 | */ |
||
| 449 | protected function toTokens($string = null) |
||
| 467 | 157 | ||
| 468 | /** |
||
| 469 | * Takes an array of tokens which appear in the header and turns them into |
||
| 470 | 157 | * an RFC 2822 compliant string, adding FWSP where needed. |
|
| 471 | * |
||
| 472 | * @param string[] $tokens |
||
| 473 | 157 | * |
|
| 474 | * @return string |
||
| 475 | 157 | */ |
|
| 476 | private function _tokensToString(array $tokens) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Make a deep copy of object |
||
| 515 | */ |
||
| 516 | public function __clone() |
||
| 522 | } |
||
| 523 |
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.