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_Headers 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_Headers, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Swift_Message_Headers |
||
20 | { |
||
21 | /** |
||
22 | * Headers which may contain email addresses, and therefore should take notice when encoding |
||
23 | * @var array headers |
||
24 | */ |
||
25 | protected $emailContainingHeaders = array( |
||
|
|||
26 | "To", "From", "Reply-To", "Cc", "Bcc", "Return-Path", "Sender"); |
||
27 | /** |
||
28 | * The encoding format used for the body of the document |
||
29 | * @var string format |
||
30 | */ |
||
31 | protected $encoding = "B"; |
||
32 | /** |
||
33 | * The charset used in the headers |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $charset = false; |
||
37 | /** |
||
38 | * A collection of headers |
||
39 | * @var array headers |
||
40 | */ |
||
41 | protected $headers = array(); |
||
42 | /** |
||
43 | * A container of references to the headers |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $lowerHeaders = array(); |
||
47 | /** |
||
48 | * Attributes appended to headers |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $attributes = array(); |
||
52 | /** |
||
53 | * If QP or Base64 encoding should be forced |
||
54 | * @var boolean |
||
55 | */ |
||
56 | protected $forceEncoding = false; |
||
57 | /** |
||
58 | * The language used in the headers (doesn't really matter much) |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $language = "en-us"; |
||
62 | /** |
||
63 | * Cached, pre-built headers |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $cached = array(); |
||
67 | /** |
||
68 | * The line ending used in the headers |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $LE = "\r\n"; |
||
72 | |||
73 | /** |
||
74 | * Set the line ending character to use |
||
75 | * @param string The line ending sequence |
||
76 | * @return boolean |
||
77 | */ |
||
78 | public function setLE($le) |
||
88 | /** |
||
89 | * Get the line ending sequence |
||
90 | * @return string |
||
91 | */ |
||
92 | public function getLE() |
||
96 | /** |
||
97 | * Reset the cache state in these headers |
||
98 | */ |
||
99 | public function uncacheAll() |
||
106 | /** |
||
107 | * Add a header or change an existing header value |
||
108 | * @param string The header name, for example "From" or "Subject" |
||
109 | * @param string The value to be inserted into the header. This is safe from header injection. |
||
110 | */ |
||
111 | public function set($name, $value) |
||
142 | /** |
||
143 | * Get the value at a given header |
||
144 | * @param string The name of the header, for example "From" or "Subject" |
||
145 | * @return string |
||
146 | * @throws Swift_Message_MimeException If no such header exists |
||
147 | * @see hasHeader |
||
148 | */ |
||
149 | public function get($name) |
||
157 | /** |
||
158 | * Remove a header from the list |
||
159 | * @param string The name of the header |
||
160 | */ |
||
161 | public function remove($name) |
||
172 | /** |
||
173 | * Just fetch the array containing the headers |
||
174 | * @return array |
||
175 | */ |
||
176 | public function getList() |
||
180 | /** |
||
181 | * Check if a header has been set or not |
||
182 | * @param string The name of the header, for example "From" or "Subject" |
||
183 | * @return boolean |
||
184 | */ |
||
185 | public function has($name) |
||
190 | /** |
||
191 | * Set the language used in the headers to $lang (e.g. en-us, en-gb, sv etc) |
||
192 | * @param string The language to use |
||
193 | */ |
||
194 | public function setLanguage($lang) |
||
198 | /** |
||
199 | * Get the language used in the headers to $lang (e.g. en-us, en-gb, sv etc) |
||
200 | * @return string |
||
201 | */ |
||
202 | public function getLanguage() |
||
206 | /** |
||
207 | * Set the charset used in the headers |
||
208 | * @param string The charset name |
||
209 | * @param string $charset |
||
210 | */ |
||
211 | public function setCharset($charset) |
||
215 | /** |
||
216 | * Get the current charset used |
||
217 | * @return string |
||
218 | */ |
||
219 | public function getCharset() |
||
223 | /** |
||
224 | * Specify the encoding to use for the headers if characters outside the 7-bit-printable ascii range are found |
||
225 | * This encoding will never be used if only 7-bit-printable characters are found in the headers. |
||
226 | * Possible values are: |
||
227 | * - QP |
||
228 | * - Q |
||
229 | * - Quoted-Printable |
||
230 | * - B |
||
231 | * - Base64 |
||
232 | * NOTE: Q, QP, Quoted-Printable are all the same; as are B and Base64 |
||
233 | * @param string The encoding format to use |
||
234 | * @param string $encoding |
||
235 | * @return boolean |
||
236 | */ |
||
237 | public function setEncoding($encoding) |
||
250 | /** |
||
251 | * Get the encoding format used in this document |
||
252 | * @return string |
||
253 | */ |
||
254 | public function getEncoding() |
||
258 | /** |
||
259 | * Turn on or off forced header encoding |
||
260 | * @param boolean On/Off |
||
261 | */ |
||
262 | public function forceEncoding($force=true) |
||
266 | /** |
||
267 | * Set an attribute in a major header |
||
268 | * For example $headers->setAttribute("Content-Type", "format", "flowed") |
||
269 | * @param string The main header these values exist in |
||
270 | * @param string The name for this value |
||
271 | * @param string The value to set |
||
272 | * @throws Swift_Message_MimeException If no such header exists |
||
273 | */ |
||
274 | public function setAttribute($header, $name, $value) |
||
294 | /** |
||
295 | * Check if a header has a given attribute applied to it |
||
296 | * @param string The name of the main header |
||
297 | * @param string The name of the attribute |
||
298 | * @param string $name |
||
299 | * @return boolean |
||
300 | */ |
||
301 | public function hasAttribute($header, $name) |
||
314 | /** |
||
315 | * Get the value for a given attribute on a given header |
||
316 | * @param string The name of the main header |
||
317 | * @param string The name of the attribute |
||
318 | * @param string $header |
||
319 | * @param string $name |
||
320 | * @return string |
||
321 | * @throws Swift_Message_MimeException If no header is set |
||
322 | */ |
||
323 | public function getAttribute($header, $name) |
||
340 | /** |
||
341 | * Remove an attribute from a header |
||
342 | * @param string The name of the header to remove the attribute from |
||
343 | * @param string The name of the attribute to remove |
||
344 | */ |
||
345 | public function removeAttribute($header, $name) |
||
354 | /** |
||
355 | * Get a list of all the attributes in the given header. |
||
356 | * @param string The name of the header |
||
357 | * @return array |
||
358 | */ |
||
359 | public function listAttributes($header) |
||
368 | /** |
||
369 | * Get the header in it's compliant, encoded form |
||
370 | * @param string The name of the header |
||
371 | * @return string |
||
372 | * @throws Swift_Message_MimeException If the header doesn't exist |
||
373 | */ |
||
374 | public function getEncoded($name) |
||
484 | /** |
||
485 | * Build the list of attributes for appending to the given header |
||
486 | * This is RFC 2231 & 2047 compliant. |
||
487 | * A HUGE thanks to Joaquim Homrighausen for heaps of help, advice |
||
488 | * and testing to get this working rock solid. |
||
489 | * @param string The header built without attributes |
||
490 | * @param string The lowercase name of the header |
||
491 | * @param string $header_line |
||
492 | * @param string $header_name |
||
493 | * @return string |
||
494 | * @throws Swift_Message_MimeException If no such header exists or there are no attributes |
||
495 | */ |
||
496 | protected function buildAttributes($header_line, $header_name) |
||
563 | /** |
||
564 | * Compile the list of headers which have been set and return an ascii string |
||
565 | * The return value should always be 7-bit ascii and will have been cleaned for header injection |
||
566 | * If this looks complicated it's probably because it is!! Keeping everything compliant is not easy. |
||
567 | * This is RFC 2822 compliant |
||
568 | * @return string |
||
569 | */ |
||
570 | public function build() |
||
580 | } |
||
581 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.