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])?\)))*(?:(?:(?:(?:[ \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 |
||
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 |
||
59 | */ |
||
60 | private $_cachedValue = null; |
||
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) |
|
82 | |||
83 | /** |
||
84 | * Get the character set used in this Header. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 83 | public function getCharset() |
|
92 | |||
93 | /** |
||
94 | * Set the language used in this Header. |
||
95 | * |
||
96 | * For example, for US English, 'en-us'. |
||
97 | * This can be unspecified. |
||
98 | * |
||
99 | * @param string $lang |
||
100 | */ |
||
101 | 4 | public function setLanguage($lang) |
|
106 | |||
107 | /** |
||
108 | * Get the language used in this Header. |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | 7 | public function getLanguage() |
|
116 | |||
117 | /** |
||
118 | * Set the encoder used for encoding the header. |
||
119 | * |
||
120 | * @param Swift_Mime_HeaderEncoder $encoder |
||
121 | */ |
||
122 | 216 | public function setEncoder(Swift_Mime_HeaderEncoder $encoder) |
|
129 | |||
130 | /** |
||
131 | * Get the encoder used for encoding this Header. |
||
132 | * |
||
133 | * @return Swift_Mime_HeaderEncoder |
||
134 | */ |
||
135 | 77 | public function getEncoder() |
|
139 | |||
140 | /** |
||
141 | * Get the name of this header (e.g. charset). |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | 153 | public function getFieldName() |
|
149 | |||
150 | /** |
||
151 | * Set the maximum length of lines in the header (excluding EOL). |
||
152 | * |
||
153 | * @param int $lineLength |
||
154 | */ |
||
155 | 20 | public function setMaxLineLength($lineLength) |
|
160 | |||
161 | /** |
||
162 | * Get the maximum permitted length of lines in this Header. |
||
163 | * |
||
164 | * @return int |
||
165 | */ |
||
166 | 82 | public function getMaxLineLength() |
|
170 | |||
171 | /** |
||
172 | * Get this Header rendered as a RFC 2822 compliant string. |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | 157 | public function toString() |
|
180 | |||
181 | /** |
||
182 | * Returns a string representation of this object. |
||
183 | * |
||
184 | * @return string |
||
185 | * |
||
186 | * @see toString() |
||
187 | */ |
||
188 | public function __toString() |
||
192 | |||
193 | // -- Points of extension |
||
194 | |||
195 | /** |
||
196 | * Set the name of this Header field. |
||
197 | * |
||
198 | * @param string $name |
||
199 | */ |
||
200 | 256 | protected function setFieldName($name) |
|
204 | |||
205 | /** |
||
206 | * Produces a compliant, formatted RFC 2822 'phrase' based on the string given. |
||
207 | * |
||
208 | * @param Swift_Mime_Header $header |
||
209 | * @param string $string as displayed |
||
210 | * @param string $charset of the text |
||
211 | * @param Swift_Mime_HeaderEncoder $encoder |
||
212 | * @param bool $shorten the first line to make remove for header name |
||
213 | * |
||
214 | * @return string |
||
215 | */ |
||
216 | 77 | protected function createPhrase(Swift_Mime_Header $header, $string, $charset, Swift_Mime_HeaderEncoder $encoder = null, $shorten = false) |
|
241 | |||
242 | /** |
||
243 | * Escape special characters in a string (convert to quoted-pairs). |
||
244 | * |
||
245 | * @param string $token |
||
246 | * @param string[] $include additional chars to escape |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | 3 | protected function escapeSpecials($token, array $include = array()) |
|
258 | |||
259 | /** |
||
260 | * Encode needed word tokens within a string of input. |
||
261 | * |
||
262 | * @param Swift_Mime_Header|Swift_Mime_Headers_AbstractHeader $header |
||
263 | * @param string $input |
||
264 | * @param integer $usedLength optional |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | 161 | protected function encodeWords(Swift_Mime_Header $header, $input, $usedLength = -1) |
|
299 | |||
300 | /** |
||
301 | * Test if a token needs to be encoded or not. |
||
302 | * |
||
303 | * @param string $token |
||
304 | * |
||
305 | * @return integer |
||
306 | */ |
||
307 | 161 | protected function tokenNeedsEncoding($token) |
|
315 | |||
316 | /** |
||
317 | * Splits a string into tokens in blocks of words which can be encoded quickly. |
||
318 | * |
||
319 | * @param string $string |
||
320 | * |
||
321 | * @return string[] |
||
322 | */ |
||
323 | 161 | protected function getEncodableWordTokens($string) |
|
347 | |||
348 | /** |
||
349 | * Get a token as an encoded word for safe insertion into headers. |
||
350 | * |
||
351 | * @param string $token token to encode |
||
352 | * @param int $firstLineOffset optional |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | 17 | protected function getTokenAsEncodedWord($token, $firstLineOffset = 0) |
|
388 | |||
389 | /** |
||
390 | * Generates tokens from the given string which include CRLF as individual tokens. |
||
391 | * |
||
392 | * @param string $token |
||
393 | * |
||
394 | * @return string[] |
||
395 | */ |
||
396 | 157 | protected function generateTokenLines($token) |
|
400 | |||
401 | /** |
||
402 | * Set a value into the cache. |
||
403 | * |
||
404 | * @param string $value |
||
405 | */ |
||
406 | 245 | protected function setCachedValue($value) |
|
410 | |||
411 | /** |
||
412 | * Get the value in the cache. |
||
413 | * |
||
414 | * @return string |
||
415 | */ |
||
416 | 176 | protected function getCachedValue() |
|
420 | |||
421 | /** |
||
422 | * Clear the cached value if $condition is met. |
||
423 | * |
||
424 | * @param bool $condition |
||
425 | */ |
||
426 | 231 | protected function clearCachedValueIf($condition) |
|
432 | |||
433 | /** |
||
434 | * Generate a list of all tokens in the final header. |
||
435 | * |
||
436 | * @param string $string The string to tokenize |
||
437 | * |
||
438 | * @return array An array of tokens as strings |
||
439 | */ |
||
440 | 157 | protected function toTokens($string = null) |
|
458 | |||
459 | /** |
||
460 | * Takes an array of tokens which appear in the header and turns them into |
||
461 | * an RFC 2822 compliant string, adding FWSP where needed. |
||
462 | * |
||
463 | * @param string[] $tokens |
||
464 | * |
||
465 | * @return string |
||
466 | */ |
||
467 | 157 | private function _tokensToString(array $tokens) |
|
497 | |||
498 | /** |
||
499 | * Make a deep copy of object |
||
500 | */ |
||
501 | 5 | public function __clone() |
|
507 | } |
||
508 |
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.