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_Signers_DKIMSigner 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_Signers_DKIMSigner, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Swift_Signers_DKIMSigner implements Swift_Signers_HeaderSigner |
||
|
|||
17 | { |
||
18 | /** |
||
19 | * PrivateKey. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $_privateKey; |
||
24 | |||
25 | /** |
||
26 | * DomainName. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $_domainName; |
||
31 | |||
32 | /** |
||
33 | * Selector. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $_selector; |
||
38 | |||
39 | /** |
||
40 | * Hash algorithm used. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $_hashAlgorithm = 'rsa-sha1'; |
||
45 | |||
46 | /** |
||
47 | * Body canon method. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $_bodyCanon = 'simple'; |
||
52 | |||
53 | /** |
||
54 | * Header canon method. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $_headerCanon = 'simple'; |
||
59 | |||
60 | /** |
||
61 | * Headers not being signed. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $_ignoredHeaders = array('return-path' => true); |
||
66 | |||
67 | /** |
||
68 | * Signer identity. |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $_signerIdentity; |
||
73 | |||
74 | /** |
||
75 | * BodyLength. |
||
76 | * |
||
77 | * @var int |
||
78 | */ |
||
79 | protected $_bodyLen = 0; |
||
80 | |||
81 | /** |
||
82 | * Maximum signedLen. |
||
83 | * |
||
84 | * @var int |
||
85 | */ |
||
86 | protected $_maxLen = PHP_INT_MAX; |
||
87 | |||
88 | /** |
||
89 | * Embbed bodyLen in signature. |
||
90 | * |
||
91 | * @var bool |
||
92 | */ |
||
93 | protected $_showLen = false; |
||
94 | |||
95 | /** |
||
96 | * When the signature has been applied (true means time()), false means not embedded. |
||
97 | * |
||
98 | * @var mixed |
||
99 | */ |
||
100 | protected $_signatureTimestamp = true; |
||
101 | |||
102 | /** |
||
103 | * When will the signature expires false means not embedded, if sigTimestamp is auto |
||
104 | * Expiration is relative, otherwhise it's absolute. |
||
105 | * |
||
106 | * @var int |
||
107 | */ |
||
108 | protected $_signatureExpiration = false; |
||
109 | |||
110 | /** |
||
111 | * Must we embed signed headers? |
||
112 | * |
||
113 | * @var bool |
||
114 | */ |
||
115 | protected $_debugHeaders = false; |
||
116 | |||
117 | // work variables |
||
118 | /** |
||
119 | * Headers used to generate hash. |
||
120 | * |
||
121 | * @var array |
||
122 | */ |
||
123 | protected $_signedHeaders = array(); |
||
124 | |||
125 | /** |
||
126 | * If debugHeaders is set store debugDatas here. |
||
127 | * |
||
128 | * @var string |
||
129 | */ |
||
130 | private $_debugHeadersData = ''; |
||
131 | |||
132 | /** |
||
133 | * Stores the bodyHash. |
||
134 | * |
||
135 | * @var string |
||
136 | */ |
||
137 | private $_bodyHash = ''; |
||
138 | |||
139 | /** |
||
140 | * Stores the signature header. |
||
141 | * |
||
142 | * @var Swift_Mime_Headers_ParameterizedHeader |
||
143 | */ |
||
144 | protected $_dkimHeader; |
||
145 | |||
146 | /** |
||
147 | * @var resource |
||
148 | */ |
||
149 | private $_bodyHashHandler; |
||
150 | |||
151 | /** |
||
152 | * @var null |
||
153 | * |
||
154 | * no used? |
||
155 | */ |
||
156 | private $_headerHash; |
||
157 | |||
158 | /** |
||
159 | * @var string |
||
160 | */ |
||
161 | private $_headerCanonData = ''; |
||
162 | |||
163 | /** |
||
164 | * @var int |
||
165 | */ |
||
166 | private $_bodyCanonEmptyCounter = 0; |
||
167 | |||
168 | /** |
||
169 | * @var int |
||
170 | */ |
||
171 | private $_bodyCanonIgnoreStart = 2; |
||
172 | |||
173 | /** |
||
174 | * @var bool |
||
175 | */ |
||
176 | private $_bodyCanonSpace = false; |
||
177 | |||
178 | /** |
||
179 | * @var null|string |
||
180 | */ |
||
181 | private $_bodyCanonLastChar = null; |
||
182 | |||
183 | /** |
||
184 | * @var string |
||
185 | */ |
||
186 | private $_bodyCanonLine = ''; |
||
187 | |||
188 | /** |
||
189 | * @var array |
||
190 | */ |
||
191 | private $_bound = array(); |
||
192 | |||
193 | /** |
||
194 | * Constructor. |
||
195 | * |
||
196 | * @param string $privateKey |
||
197 | * @param string $domainName |
||
198 | * @param string $selector |
||
199 | */ |
||
200 | 7 | View Code Duplication | public function __construct($privateKey, $domainName, $selector) |
207 | |||
208 | /** |
||
209 | * Instanciate DKIMSigner. |
||
210 | * |
||
211 | * @param string $privateKey |
||
212 | * @param string $domainName |
||
213 | * @param string $selector |
||
214 | * |
||
215 | * @return Swift_Signers_DKIMSigner |
||
216 | */ |
||
217 | public static function newInstance($privateKey, $domainName, $selector) |
||
221 | |||
222 | /** |
||
223 | * Reset the Signer. |
||
224 | * |
||
225 | * @see Swift_Signer::reset() |
||
226 | */ |
||
227 | 6 | public function reset() |
|
238 | |||
239 | /** |
||
240 | * Writes $bytes to the end of the stream. |
||
241 | * |
||
242 | * Writing may not happen immediately if the stream chooses to buffer. If |
||
243 | * you want to write these bytes with immediate effect, call {@link commit()} |
||
244 | * after calling write(). |
||
245 | * |
||
246 | * This method returns the sequence ID of the write (i.e. 1 for first, 2 for |
||
247 | * second, etc etc). |
||
248 | * |
||
249 | * @param string $bytes |
||
250 | * |
||
251 | * @throws Swift_IoException |
||
252 | * |
||
253 | * @return int |
||
254 | */ |
||
255 | 6 | public function write($bytes) |
|
262 | |||
263 | /** |
||
264 | * For any bytes that are currently buffered inside the stream, force them |
||
265 | * off the buffer. |
||
266 | * |
||
267 | * @throws Swift_IoException |
||
268 | */ |
||
269 | public function commit() |
||
274 | |||
275 | /** |
||
276 | * Attach $is to this stream. |
||
277 | * The stream acts as an observer, receiving all data that is written. |
||
278 | * All {@link write()} and {@link flushBuffers()} operations will be mirrored. |
||
279 | * |
||
280 | * @param Swift_InputByteStream $is |
||
281 | */ |
||
282 | public function bind(Swift_InputByteStream $is) |
||
289 | |||
290 | /** |
||
291 | * Remove an already bound stream. |
||
292 | * If $is is not bound, no errors will be raised. |
||
293 | * If the stream currently has any buffered data it will be written to $is |
||
294 | * before unbinding occurs. |
||
295 | * |
||
296 | * @param Swift_InputByteStream $is |
||
297 | */ |
||
298 | View Code Duplication | public function unbind(Swift_InputByteStream $is) |
|
311 | |||
312 | /** |
||
313 | * Flush the contents of the stream (empty it) and set the internal pointer |
||
314 | * to the beginning. |
||
315 | * |
||
316 | * @throws Swift_IoException |
||
317 | */ |
||
318 | public function flushBuffers() |
||
322 | |||
323 | /** |
||
324 | * Set hash_algorithm, must be one of rsa-sha256 | rsa-sha1 defaults to rsa-sha256. |
||
325 | * |
||
326 | * @param string $hash |
||
327 | * |
||
328 | * @return Swift_Signers_DKIMSigner |
||
329 | */ |
||
330 | 4 | public function setHashAlgorithm($hash) |
|
341 | |||
342 | /** |
||
343 | * Set the body canonicalization algorithm. |
||
344 | * |
||
345 | * @param string $canon |
||
346 | * |
||
347 | * @return Swift_Signers_DKIMSigner |
||
348 | */ |
||
349 | 2 | public function setBodyCanon($canon) |
|
359 | |||
360 | /** |
||
361 | * Set the header canonicalization algorithm. |
||
362 | * |
||
363 | * @param string $canon |
||
364 | * |
||
365 | * @return Swift_Signers_DKIMSigner |
||
366 | */ |
||
367 | 2 | public function setHeaderCanon($canon) |
|
377 | |||
378 | /** |
||
379 | * Set the signer identity. |
||
380 | * |
||
381 | * @param string $identity |
||
382 | * |
||
383 | * @return Swift_Signers_DKIMSigner |
||
384 | */ |
||
385 | public function setSignerIdentity($identity) |
||
391 | |||
392 | /** |
||
393 | * Set the length of the body to sign. |
||
394 | * |
||
395 | * @param mixed $len (bool or int) |
||
396 | * |
||
397 | * @return Swift_Signers_DKIMSigner |
||
398 | */ |
||
399 | public function setBodySignedLen($len) |
||
414 | |||
415 | /** |
||
416 | * Set the signature timestamp. |
||
417 | * |
||
418 | * @param int $time A timestamp |
||
419 | * |
||
420 | * @return Swift_Signers_DKIMSigner |
||
421 | */ |
||
422 | 5 | public function setSignatureTimestamp($time) |
|
428 | |||
429 | /** |
||
430 | * Set the signature expiration timestamp. |
||
431 | * |
||
432 | * @param int $time A timestamp |
||
433 | * |
||
434 | * @return Swift_Signers_DKIMSigner |
||
435 | */ |
||
436 | public function setSignatureExpiration($time) |
||
442 | |||
443 | /** |
||
444 | * Enable / disable the DebugHeaders. |
||
445 | * |
||
446 | * @param bool $debug |
||
447 | * |
||
448 | * @return Swift_Signers_DKIMSigner |
||
449 | */ |
||
450 | public function setDebugHeaders($debug) |
||
456 | |||
457 | /** |
||
458 | * Start Body. |
||
459 | */ |
||
460 | 6 | public function startBody() |
|
473 | |||
474 | /** |
||
475 | * End Body. |
||
476 | */ |
||
477 | 6 | public function endBody() |
|
481 | |||
482 | /** |
||
483 | * Returns the list of Headers Tampered by this plugin. |
||
484 | * |
||
485 | * @return string[] |
||
486 | */ |
||
487 | 6 | public function getAlteredHeaders() |
|
495 | |||
496 | /** |
||
497 | * Adds an ignored Header. |
||
498 | * |
||
499 | * @param string $header_name |
||
500 | * |
||
501 | * @return Swift_Signers_DKIMSigner |
||
502 | */ |
||
503 | public function ignoreHeader($header_name) |
||
509 | |||
510 | /** |
||
511 | * Set the headers to sign. |
||
512 | * |
||
513 | * @param Swift_Mime_HeaderSet $headers |
||
514 | * |
||
515 | * @return Swift_Signers_DKIMSigner |
||
516 | */ |
||
517 | 6 | View Code Duplication | public function setHeaders(Swift_Mime_HeaderSet $headers) |
539 | |||
540 | /** |
||
541 | * Add the signature to the given Headers. |
||
542 | * |
||
543 | * @param Swift_Mime_HeaderSet $headers |
||
544 | * |
||
545 | * @return Swift_Signers_DKIMSigner |
||
546 | */ |
||
547 | 6 | public function addSignature(Swift_Mime_HeaderSet $headers) |
|
593 | |||
594 | /* Private helpers */ |
||
595 | |||
596 | 6 | View Code Duplication | protected function _addHeader($header, $is_sig = false) |
611 | |||
612 | /** |
||
613 | * @deprecated This method is currently useless in this class but it must be |
||
614 | * kept for BC reasons due to its "protected" scope. This method |
||
615 | * might be overriden by custom client code. |
||
616 | */ |
||
617 | protected function _endOfHeaders() |
||
620 | |||
621 | 6 | protected function _canonicalizeBody($string) |
|
673 | |||
674 | 6 | protected function _endOfBody() |
|
683 | |||
684 | /** |
||
685 | * @param string $string |
||
686 | */ |
||
687 | 6 | private function _addToBodyHash($string) |
|
697 | |||
698 | 6 | private function _addToHeaderHash($header) |
|
705 | |||
706 | /** |
||
707 | * @throws Swift_SwiftException |
||
708 | * |
||
709 | * @return string |
||
710 | */ |
||
711 | 6 | private function _getEncryptedHash() |
|
731 | } |
||
732 |
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.