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 | * @var string |
||
41 | */ |
||
42 | private $_passphrase = ''; |
||
43 | |||
44 | /** |
||
45 | * Hash algorithm used. |
||
46 | * |
||
47 | * @see RFC6376 3.3: Signers MUST implement and SHOULD sign using rsa-sha256. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $_hashAlgorithm = 'rsa-sha256'; |
||
52 | |||
53 | /** |
||
54 | * Body canon method. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $_bodyCanon = 'simple'; |
||
59 | |||
60 | /** |
||
61 | * Header canon method. |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $_headerCanon = 'simple'; |
||
66 | |||
67 | /** |
||
68 | * Headers not being signed. |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $_ignoredHeaders = array('return-path' => true); |
||
73 | |||
74 | /** |
||
75 | * Signer identity. |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $_signerIdentity; |
||
80 | |||
81 | /** |
||
82 | * BodyLength. |
||
83 | * |
||
84 | * @var int |
||
85 | */ |
||
86 | protected $_bodyLen = 0; |
||
87 | |||
88 | /** |
||
89 | * Maximum signedLen. |
||
90 | * |
||
91 | * @var int |
||
92 | */ |
||
93 | protected $_maxLen = PHP_INT_MAX; |
||
94 | |||
95 | /** |
||
96 | * Embbed bodyLen in signature. |
||
97 | * |
||
98 | * @var bool |
||
99 | */ |
||
100 | protected $_showLen = false; |
||
101 | |||
102 | /** |
||
103 | * When the signature has been applied (true means time()), false means not embedded. |
||
104 | * |
||
105 | * @var mixed |
||
106 | */ |
||
107 | protected $_signatureTimestamp = true; |
||
108 | |||
109 | /** |
||
110 | * When will the signature expires false means not embedded, if sigTimestamp is auto |
||
111 | * Expiration is relative, otherwise it's absolute. |
||
112 | * |
||
113 | * @var int |
||
114 | */ |
||
115 | protected $_signatureExpiration = false; |
||
116 | |||
117 | /** |
||
118 | * Must we embed signed headers? |
||
119 | * |
||
120 | * @var bool |
||
121 | */ |
||
122 | protected $_debugHeaders = false; |
||
123 | |||
124 | // work variables |
||
125 | /** |
||
126 | * Headers used to generate hash. |
||
127 | * |
||
128 | * @var array |
||
129 | */ |
||
130 | protected $_signedHeaders = array(); |
||
131 | |||
132 | /** |
||
133 | * If debugHeaders is set store debugData here. |
||
134 | * |
||
135 | * @var string |
||
136 | */ |
||
137 | private $_debugHeadersData = ''; |
||
138 | |||
139 | /** |
||
140 | * Stores the bodyHash. |
||
141 | * |
||
142 | * @var string |
||
143 | */ |
||
144 | private $_bodyHash = ''; |
||
145 | |||
146 | /** |
||
147 | * Stores the signature header. |
||
148 | * |
||
149 | * @var Swift_Mime_Headers_ParameterizedHeader |
||
150 | */ |
||
151 | protected $_dkimHeader; |
||
152 | |||
153 | /** |
||
154 | * @var resource |
||
155 | */ |
||
156 | private $_bodyHashHandler; |
||
157 | |||
158 | /** |
||
159 | * @var null |
||
160 | * |
||
161 | * no used? |
||
162 | */ |
||
163 | private $_headerHash; |
||
164 | |||
165 | /** |
||
166 | * @var string |
||
167 | */ |
||
168 | private $_headerCanonData = ''; |
||
169 | |||
170 | /** |
||
171 | * @var int |
||
172 | */ |
||
173 | private $_bodyCanonEmptyCounter = 0; |
||
174 | |||
175 | /** |
||
176 | * @var int |
||
177 | */ |
||
178 | private $_bodyCanonIgnoreStart = 2; |
||
179 | |||
180 | /** |
||
181 | * @var bool |
||
182 | */ |
||
183 | private $_bodyCanonSpace = false; |
||
184 | |||
185 | /** |
||
186 | * @var null|string |
||
187 | */ |
||
188 | private $_bodyCanonLastChar = null; |
||
189 | |||
190 | /** |
||
191 | * @var string |
||
192 | */ |
||
193 | private $_bodyCanonLine = ''; |
||
194 | |||
195 | /** |
||
196 | * @var array |
||
197 | */ |
||
198 | private $_bound = array(); |
||
199 | |||
200 | /** |
||
201 | * Constructor. |
||
202 | * |
||
203 | * @param string $privateKey |
||
204 | * @param string $domainName |
||
205 | * @param string $selector |
||
206 | * @param string $passphrase |
||
207 | */ |
||
208 | 1 | public function __construct($privateKey, $domainName, $selector, $passphrase = '') |
|
209 | { |
||
210 | 1 | $this->_privateKey = $privateKey; |
|
211 | 1 | $this->_domainName = $domainName; |
|
212 | 1 | $this->_signerIdentity = '@' . $domainName; |
|
213 | 1 | $this->_selector = $selector; |
|
214 | 1 | $this->_passphrase = $passphrase; |
|
215 | |||
216 | // keep fallback hash algorithm sha1 if php version is lower than 5.4.8 |
||
217 | 1 | if (PHP_VERSION_ID < 50408) { |
|
218 | 1 | $this->_hashAlgorithm = 'rsa-sha1'; |
|
219 | 1 | } |
|
220 | 1 | } |
|
221 | |||
222 | /** |
||
223 | * Instanciate DKIMSigner. |
||
224 | * |
||
225 | * @param string $privateKey |
||
226 | * @param string $domainName |
||
227 | * @param string $selector |
||
228 | * |
||
229 | * @return self |
||
230 | */ |
||
231 | public static function newInstance($privateKey, $domainName, $selector) |
||
235 | |||
236 | /** |
||
237 | * Reset the Signer. |
||
238 | * |
||
239 | * @see Swift_Signer::reset() |
||
240 | */ |
||
241 | public function reset() |
||
252 | |||
253 | /** |
||
254 | * Writes $bytes to the end of the stream. |
||
255 | * |
||
256 | * Writing may not happen immediately if the stream chooses to buffer. If |
||
257 | * you want to write these bytes with immediate effect, call {@link commit()} |
||
258 | * after calling write(). |
||
259 | * |
||
260 | * This method returns the sequence ID of the write (i.e. 1 for first, 2 for |
||
261 | * second, etc etc). |
||
262 | * |
||
263 | * @param string $bytes |
||
264 | * |
||
265 | * @throws Swift_IoException |
||
266 | * |
||
267 | * @return int |
||
268 | */ |
||
269 | // TODO: fix return |
||
270 | public function write($bytes) |
||
277 | |||
278 | /** |
||
279 | * For any bytes that are currently buffered inside the stream, force them |
||
280 | * off the buffer. |
||
281 | */ |
||
282 | public function commit() |
||
287 | |||
288 | /** |
||
289 | * Attach $is to this stream. |
||
290 | * The stream acts as an observer, receiving all data that is written. |
||
291 | * All {@link write()} and {@link flushBuffers()} operations will be mirrored. |
||
292 | * |
||
293 | * @param Swift_InputByteStream $is |
||
294 | */ |
||
295 | public function bind(Swift_InputByteStream $is) |
||
302 | |||
303 | /** |
||
304 | * Remove an already bound stream. |
||
305 | * If $is is not bound, no errors will be raised. |
||
306 | * If the stream currently has any buffered data it will be written to $is |
||
307 | * before unbinding occurs. |
||
308 | * |
||
309 | * @param Swift_InputByteStream $is |
||
310 | */ |
||
311 | View Code Duplication | public function unbind(Swift_InputByteStream $is) |
|
322 | |||
323 | /** |
||
324 | * Flush the contents of the stream (empty it) and set the internal pointer |
||
325 | * to the beginning. |
||
326 | * |
||
327 | * @throws Swift_IoException |
||
328 | */ |
||
329 | public function flushBuffers() |
||
333 | |||
334 | /** |
||
335 | * Set hash_algorithm, must be one of rsa-sha256 | rsa-sha1. |
||
336 | * |
||
337 | * @param string $hash 'rsa-sha1' or 'rsa-sha256' |
||
338 | * |
||
339 | * @throws Swift_SwiftException |
||
340 | * |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function setHashAlgorithm($hash) |
||
361 | |||
362 | /** |
||
363 | * Set the body canonicalization algorithm. |
||
364 | * |
||
365 | * @param string $canon |
||
366 | * |
||
367 | * @return $this |
||
368 | */ |
||
369 | public function setBodyCanon($canon) |
||
379 | |||
380 | /** |
||
381 | * Set the header canonicalization algorithm. |
||
382 | * |
||
383 | * @param string $canon |
||
384 | * |
||
385 | * @return $this |
||
386 | */ |
||
387 | public function setHeaderCanon($canon) |
||
397 | |||
398 | /** |
||
399 | * Set the signer identity. |
||
400 | * |
||
401 | * @param string $identity |
||
402 | * |
||
403 | * @return $this |
||
404 | */ |
||
405 | public function setSignerIdentity($identity) |
||
411 | |||
412 | /** |
||
413 | * Set the length of the body to sign. |
||
414 | * |
||
415 | * @param mixed $len (bool or int) |
||
416 | * |
||
417 | * @return $this |
||
418 | */ |
||
419 | public function setBodySignedLen($len) |
||
434 | |||
435 | /** |
||
436 | * Set the signature timestamp. |
||
437 | * |
||
438 | * @param int $time A timestamp |
||
439 | * |
||
440 | * @return $this |
||
441 | */ |
||
442 | public function setSignatureTimestamp($time) |
||
448 | |||
449 | /** |
||
450 | * Set the signature expiration timestamp. |
||
451 | * |
||
452 | * @param int $time A timestamp |
||
453 | * |
||
454 | * @return $this |
||
455 | */ |
||
456 | public function setSignatureExpiration($time) |
||
462 | |||
463 | /** |
||
464 | * Enable / disable the DebugHeaders. |
||
465 | * |
||
466 | * @param bool $debug |
||
467 | * |
||
468 | * @return Swift_Signers_DKIMSigner |
||
469 | */ |
||
470 | public function setDebugHeaders($debug) |
||
476 | |||
477 | /** |
||
478 | * Start Body. |
||
479 | */ |
||
480 | public function startBody() |
||
493 | |||
494 | /** |
||
495 | * End Body. |
||
496 | */ |
||
497 | public function endBody() |
||
501 | |||
502 | /** |
||
503 | * Returns the list of Headers Tampered by this plugin. |
||
504 | * |
||
505 | * @return string[] |
||
506 | */ |
||
507 | public function getAlteredHeaders() |
||
515 | |||
516 | /** |
||
517 | * Adds an ignored Header. |
||
518 | * |
||
519 | * @param string $header_name |
||
520 | * |
||
521 | * @return Swift_Signers_DKIMSigner |
||
522 | */ |
||
523 | public function ignoreHeader($header_name) |
||
529 | |||
530 | /** |
||
531 | * Set the headers to sign. |
||
532 | * |
||
533 | * @param Swift_Mime_HeaderSet $headers |
||
534 | * |
||
535 | * @return Swift_Signers_DKIMSigner |
||
536 | */ |
||
537 | public function setHeaders(Swift_Mime_HeaderSet $headers) |
||
561 | |||
562 | /** |
||
563 | * Add the signature to the given Headers. |
||
564 | * |
||
565 | * @param Swift_Mime_HeaderSet $headers |
||
566 | * |
||
567 | * @return Swift_Signers_DKIMSigner |
||
568 | */ |
||
569 | public function addSignature(Swift_Mime_HeaderSet $headers) |
||
637 | |||
638 | /** |
||
639 | * @param string $header |
||
640 | * @param bool $is_sig |
||
641 | */ |
||
642 | View Code Duplication | protected function _addHeader($header, $is_sig = false) |
|
657 | |||
658 | /** |
||
659 | * @deprecated This method is currently useless in this class but it must be |
||
660 | * kept for BC reasons due to its "protected" scope. This method |
||
661 | * might be overridden by custom client code. |
||
662 | */ |
||
663 | protected function _endOfHeaders() |
||
666 | |||
667 | /** |
||
668 | * @param string $string |
||
669 | */ |
||
670 | protected function _canonicalizeBody($string) |
||
724 | |||
725 | protected function _endOfBody() |
||
734 | |||
735 | /** |
||
736 | * @param string $string |
||
737 | */ |
||
738 | private function _addToBodyHash($string) |
||
749 | |||
750 | /** |
||
751 | * @param string $header |
||
752 | */ |
||
753 | private function _addToHeaderHash($header) |
||
760 | |||
761 | /** |
||
762 | * @throws Swift_SwiftException |
||
763 | * |
||
764 | * @return string |
||
765 | */ |
||
766 | private function _getEncryptedHash() |
||
796 | } |
||
797 |
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.