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_DomainKeySigner 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_DomainKeySigner, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Swift_Signers_DomainKeySigner 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 | * Canonisation method. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $_canon = 'simple'; |
||
52 | |||
53 | /** |
||
54 | * Headers not being signed. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $_ignoredHeaders = array(); |
||
59 | |||
60 | /** |
||
61 | * Signer identity. |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $_signerIdentity; |
||
66 | |||
67 | /** |
||
68 | * Must we embed signed headers? |
||
69 | * |
||
70 | * @var bool |
||
71 | */ |
||
72 | protected $_debugHeaders = false; |
||
73 | |||
74 | // work variables |
||
75 | |||
76 | /** |
||
77 | * Headers used to generate hash. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | private $_signedHeaders = array(); |
||
82 | |||
83 | /** |
||
84 | * Stores the signature header. |
||
85 | * |
||
86 | * @var Swift_Mime_Headers_ParameterizedHeader |
||
87 | */ |
||
88 | protected $_domainKeyHeader; |
||
89 | |||
90 | /** |
||
91 | * Hash Handler. |
||
92 | * |
||
93 | * @var resource|null |
||
94 | */ |
||
95 | private $_hashHandler; |
||
96 | |||
97 | /** |
||
98 | * @var string|null |
||
99 | */ |
||
100 | private $_hash; |
||
101 | |||
102 | /** |
||
103 | * @var string |
||
104 | */ |
||
105 | private $_canonData = ''; |
||
106 | |||
107 | /** |
||
108 | * @var int |
||
109 | */ |
||
110 | private $_bodyCanonEmptyCounter = 0; |
||
111 | |||
112 | /** |
||
113 | * @var int |
||
114 | */ |
||
115 | private $_bodyCanonIgnoreStart = 2; |
||
116 | |||
117 | /** |
||
118 | * @var bool |
||
119 | */ |
||
120 | private $_bodyCanonSpace = false; |
||
121 | |||
122 | /** |
||
123 | * @var null|string |
||
124 | */ |
||
125 | private $_bodyCanonLastChar = null; |
||
126 | |||
127 | /** |
||
128 | * @var string |
||
129 | */ |
||
130 | private $_bodyCanonLine = ''; |
||
131 | |||
132 | /** |
||
133 | * @var Swift_InputByteStream[] |
||
134 | */ |
||
135 | private $_bound = array(); |
||
136 | |||
137 | /** |
||
138 | * Constructor. |
||
139 | * |
||
140 | * @param string $privateKey |
||
141 | * @param string $domainName |
||
142 | * @param string $selector |
||
143 | */ |
||
144 | public function __construct($privateKey, $domainName, $selector) |
||
145 | { |
||
146 | $this->_privateKey = $privateKey; |
||
147 | $this->_domainName = $domainName; |
||
148 | $this->_signerIdentity = '@' . $domainName; |
||
149 | $this->_selector = $selector; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Instanciate DomainKeySigner. |
||
154 | * |
||
155 | * @param string $privateKey |
||
156 | * @param string $domainName |
||
157 | * @param string $selector |
||
158 | * |
||
159 | * @return self |
||
160 | */ |
||
161 | public static function newInstance($privateKey, $domainName, $selector) |
||
165 | |||
166 | /** |
||
167 | * Resets internal states. |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function reset() |
||
182 | |||
183 | /** |
||
184 | * Writes $bytes to the end of the stream. |
||
185 | * |
||
186 | * Writing may not happen immediately if the stream chooses to buffer. If |
||
187 | * you want to write these bytes with immediate effect, call {@link commit()} |
||
188 | * after calling write(). |
||
189 | * |
||
190 | * This method returns the sequence ID of the write (i.e. 1 for first, 2 for |
||
191 | * second, etc etc). |
||
192 | * |
||
193 | * @param string $bytes |
||
194 | * |
||
195 | * @throws Swift_IoException |
||
196 | * |
||
197 | * @return $this |
||
198 | */ |
||
199 | public function write($bytes) |
||
208 | |||
209 | /** |
||
210 | * For any bytes that are currently buffered inside the stream, force them |
||
211 | * off the buffer. |
||
212 | * |
||
213 | * @throws Swift_IoException |
||
214 | * |
||
215 | * @return $this |
||
216 | */ |
||
217 | public function commit() |
||
222 | |||
223 | /** |
||
224 | * Attach $is to this stream. |
||
225 | * The stream acts as an observer, receiving all data that is written. |
||
226 | * All {@link write()} and {@link flushBuffers()} operations will be mirrored. |
||
227 | * |
||
228 | * @param Swift_InputByteStream $is |
||
229 | * |
||
230 | * @return $this |
||
231 | */ |
||
232 | public function bind(Swift_InputByteStream $is) |
||
239 | |||
240 | /** |
||
241 | * Remove an already bound stream. |
||
242 | * If $is is not bound, no errors will be raised. |
||
243 | * If the stream currently has any buffered data it will be written to $is |
||
244 | * before unbinding occurs. |
||
245 | * |
||
246 | * @param Swift_InputByteStream $is |
||
247 | * |
||
248 | * @return $this |
||
249 | */ |
||
250 | View Code Duplication | public function unbind(Swift_InputByteStream $is) |
|
263 | |||
264 | /** |
||
265 | * Flush the contents of the stream (empty it) and set the internal pointer |
||
266 | * to the beginning. |
||
267 | * |
||
268 | * @throws Swift_IoException |
||
269 | * |
||
270 | * @return $this |
||
271 | */ |
||
272 | public function flushBuffers() |
||
278 | |||
279 | /** |
||
280 | * Set hash_algorithm, must be one of rsa-sha256 | rsa-sha1 defaults to rsa-sha256. |
||
281 | * |
||
282 | * @param string $hash WARNING: $hash not used, it's set fixed to "rsa-sha1" |
||
283 | * |
||
284 | * @return $this |
||
285 | */ |
||
286 | public function setHashAlgorithm($hash) |
||
292 | |||
293 | /** |
||
294 | * Set the canonicalization algorithm. |
||
295 | * |
||
296 | * @param string $canon simple | nofws defaults to simple |
||
297 | * |
||
298 | * @return $this |
||
299 | */ |
||
300 | public function setCanon($canon) |
||
310 | |||
311 | /** |
||
312 | * Set the signer identity. |
||
313 | * |
||
314 | * @param string $identity |
||
315 | * |
||
316 | * @return $this |
||
317 | */ |
||
318 | public function setSignerIdentity($identity) |
||
324 | |||
325 | /** |
||
326 | * Enable / disable the DebugHeaders. |
||
327 | * |
||
328 | * @param bool $debug |
||
329 | * |
||
330 | * @return $this |
||
331 | */ |
||
332 | public function setDebugHeaders($debug) |
||
338 | |||
339 | /** |
||
340 | * Start Body. |
||
341 | */ |
||
342 | public function startBody() |
||
345 | |||
346 | /** |
||
347 | * End Body. |
||
348 | */ |
||
349 | public function endBody() |
||
353 | |||
354 | /** |
||
355 | * Returns the list of Headers Tampered by this plugin. |
||
356 | * |
||
357 | * @return string[] |
||
358 | */ |
||
359 | public function getAlteredHeaders() |
||
367 | |||
368 | /** |
||
369 | * Adds an ignored Header. |
||
370 | * |
||
371 | * @param string $header_name |
||
372 | * |
||
373 | * @return $this |
||
374 | */ |
||
375 | public function ignoreHeader($header_name) |
||
381 | |||
382 | /** |
||
383 | * Set the headers to sign. |
||
384 | * |
||
385 | * @param Swift_Mime_HeaderSet $headers |
||
386 | * |
||
387 | * @return $this |
||
388 | */ |
||
389 | public function setHeaders(Swift_Mime_HeaderSet $headers) |
||
390 | { |
||
391 | $this->_startHash(); |
||
392 | $this->_canonData = ''; |
||
393 | // Loop through Headers |
||
394 | $listHeaders = $headers->listAll(); |
||
395 | View Code Duplication | foreach ($listHeaders as $hName) { |
|
396 | // Check if we need to ignore Header |
||
397 | if (!isset($this->_ignoredHeaders[Swift::strtolowerWithStaticCache($hName)])) { |
||
398 | if ($headers->has($hName)) { |
||
399 | $tmp = $headers->getAll($hName); |
||
400 | foreach ($tmp as $header) { |
||
401 | if ($header->getFieldBody() != '') { |
||
402 | $this->_addHeader($header->toString()); |
||
403 | $this->_signedHeaders[] = $header->getFieldName(); |
||
404 | } |
||
405 | } |
||
406 | } |
||
407 | } |
||
408 | } |
||
409 | $this->_endOfHeaders(); |
||
410 | |||
411 | return $this; |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * Add the signature to the given Headers. |
||
416 | * |
||
417 | * @param Swift_Mime_HeaderSet $headers |
||
418 | * |
||
419 | * @return $this |
||
420 | */ |
||
421 | public function addSignature(Swift_Mime_HeaderSet $headers) |
||
434 | |||
435 | /* Private helpers */ |
||
436 | |||
437 | /** |
||
438 | * @param string $header |
||
439 | */ |
||
440 | View Code Duplication | protected function _addHeader($header) |
|
455 | |||
456 | protected function _endOfHeaders() |
||
460 | |||
461 | /** |
||
462 | * @param string $string |
||
463 | */ |
||
464 | protected function _canonicalizeBody($string) |
||
512 | |||
513 | protected function _endOfBody() |
||
521 | |||
522 | private function _addToHash($string) |
||
527 | |||
528 | private function _startHash() |
||
538 | |||
539 | /** |
||
540 | * @throws Swift_SwiftException |
||
541 | * |
||
542 | * @return string |
||
543 | */ |
||
544 | private function _getEncryptedHash() |
||
556 | } |
||
557 |
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.