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_SMimeSigner 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_SMimeSigner, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Swift_Signers_SMimeSigner implements Swift_Signers_BodySigner |
||
|
|||
19 | { |
||
20 | protected $signCertificate; |
||
21 | protected $signPrivateKey; |
||
22 | protected $encryptCert; |
||
23 | protected $signThenEncrypt = true; |
||
24 | protected $signLevel; |
||
25 | protected $encryptLevel; |
||
26 | protected $signOptions; |
||
27 | protected $encryptOptions; |
||
28 | protected $encryptCipher; |
||
29 | protected $extraCerts = null; |
||
30 | |||
31 | /** |
||
32 | * @var Swift_StreamFilters_StringReplacementFilterFactory |
||
33 | */ |
||
34 | protected $replacementFactory; |
||
35 | |||
36 | /** |
||
37 | * @var Swift_Mime_HeaderFactory |
||
38 | */ |
||
39 | protected $headerFactory; |
||
40 | |||
41 | /** |
||
42 | * Constructor. |
||
43 | * |
||
44 | * @param string|null $signCertificate |
||
45 | * @param string|null $signPrivateKey |
||
46 | * @param string|null $encryptCertificate |
||
47 | */ |
||
48 | 8 | public function __construct($signCertificate = null, $signPrivateKey = null, $encryptCertificate = null) |
|
49 | { |
||
50 | 8 | if (null !== $signPrivateKey) { |
|
51 | $this->setSignCertificate($signCertificate, $signPrivateKey); |
||
52 | } |
||
53 | |||
54 | 8 | if (null !== $encryptCertificate) { |
|
55 | $this->setEncryptCertificate($encryptCertificate); |
||
56 | } |
||
57 | |||
58 | 8 | $this->replacementFactory = Swift_DependencyContainer::getInstance() |
|
59 | 8 | ->lookup('transport.replacementfactory'); |
|
60 | |||
61 | 8 | $this->signOptions = PKCS7_DETACHED; |
|
62 | |||
63 | // Supported since php5.4 |
||
64 | 8 | if (defined('OPENSSL_CIPHER_AES_128_CBC')) { |
|
65 | $this->encryptCipher = OPENSSL_CIPHER_AES_128_CBC; |
||
66 | } else { |
||
67 | 8 | $this->encryptCipher = OPENSSL_CIPHER_RC2_128; |
|
68 | } |
||
69 | 8 | } |
|
70 | |||
71 | /** |
||
72 | * Returns an new Swift_Signers_SMimeSigner instance. |
||
73 | * |
||
74 | * @param string $certificate |
||
75 | * @param string $privateKey |
||
76 | * |
||
77 | * @return self |
||
78 | */ |
||
79 | public static function newInstance($certificate = null, $privateKey = null) |
||
80 | { |
||
81 | return new self($certificate, $privateKey); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Set the certificate location to use for signing. |
||
86 | * |
||
87 | * @see http://www.php.net/manual/en/openssl.pkcs7.flags.php |
||
88 | * |
||
89 | * @param string $certificate |
||
90 | * @param string|array $privateKey If the key needs an passphrase use array('file-location', 'passphrase') instead |
||
91 | * @param int $signOptions Bitwise operator options for openssl_pkcs7_sign() |
||
92 | * @param string $extraCerts A file containing intermediate certificates needed by the signing certificate |
||
93 | * |
||
94 | * @return $this |
||
95 | */ |
||
96 | 6 | public function setSignCertificate($certificate, $privateKey = null, $signOptions = PKCS7_DETACHED, $extraCerts = null) |
|
116 | |||
117 | /** |
||
118 | * Set the certificate location to use for encryption. |
||
119 | * |
||
120 | * @see http://www.php.net/manual/en/openssl.pkcs7.flags.php |
||
121 | * @see http://nl3.php.net/manual/en/openssl.ciphers.php |
||
122 | * |
||
123 | * @param string $recipientCerts Either an single X.509 certificate, or an assoc array of X.509 certificates. |
||
124 | * @param int $cipher |
||
125 | * |
||
126 | * @return $this |
||
127 | */ |
||
128 | 4 | public function setEncryptCertificate($recipientCerts, $cipher = null) |
|
146 | |||
147 | /** |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getSignCertificate() |
||
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getSignPrivateKey() |
||
162 | |||
163 | /** |
||
164 | * Set perform signing before encryption. |
||
165 | * |
||
166 | * The default is to first sign the message and then encrypt. |
||
167 | * But some older mail clients, namely Microsoft Outlook 2000 will work when the message first encrypted. |
||
168 | * As this goes against the official specs, its recommended to only use 'encryption -> signing' when specifically targeting these 'broken' clients. |
||
169 | * |
||
170 | * @param bool $signThenEncrypt |
||
171 | * |
||
172 | * @return $this |
||
173 | */ |
||
174 | 1 | public function setSignThenEncrypt($signThenEncrypt = true) |
|
180 | |||
181 | /** |
||
182 | * @return bool |
||
183 | */ |
||
184 | public function isSignThenEncrypt() |
||
188 | |||
189 | /** |
||
190 | * Resets internal states. |
||
191 | * |
||
192 | * @return $this |
||
193 | */ |
||
194 | public function reset() |
||
198 | |||
199 | /** |
||
200 | * Change the Swift_Message to apply the signing. |
||
201 | * |
||
202 | * @param Swift_Message $message |
||
203 | * |
||
204 | * @return $this |
||
205 | */ |
||
206 | 8 | public function signMessage(Swift_Message $message) |
|
224 | |||
225 | /** |
||
226 | * Return the list of header a signer might tamper. |
||
227 | * |
||
228 | * @return string[] |
||
229 | */ |
||
230 | 8 | public function getAlteredHeaders() |
|
234 | |||
235 | /** |
||
236 | * @param Swift_InputByteStream $inputStream |
||
237 | */ |
||
238 | 8 | protected function toSMimeByteStream(Swift_InputByteStream $inputStream, Swift_Message $message) |
|
262 | |||
263 | /** |
||
264 | * @param Swift_Message $message |
||
265 | * |
||
266 | * @return Swift_Message |
||
267 | */ |
||
268 | 8 | protected function createMessage(Swift_Message $message) |
|
283 | |||
284 | /** |
||
285 | * @param Swift_FileStream $outputStream |
||
286 | * @param Swift_InputByteStream $inputStream |
||
287 | * |
||
288 | * @throws Swift_IoException |
||
289 | */ |
||
290 | 6 | protected function messageStreamToSignedByteStream(Swift_FileStream $outputStream, Swift_InputByteStream $inputStream) |
|
305 | |||
306 | /** |
||
307 | * @param Swift_FileStream $outputStream |
||
308 | * @param Swift_InputByteStream $is |
||
309 | * |
||
310 | * @throws Swift_IoException |
||
311 | */ |
||
312 | 4 | protected function messageStreamToEncryptedByteStream(Swift_FileStream $outputStream, Swift_InputByteStream $is) |
|
322 | |||
323 | /** |
||
324 | * @param Swift_OutputByteStream $fromStream |
||
325 | * @param Swift_InputByteStream $toStream |
||
326 | */ |
||
327 | 8 | protected function copyFromOpenSSLOutput(Swift_OutputByteStream $fromStream, Swift_InputByteStream $toStream) |
|
346 | |||
347 | /** |
||
348 | * Merges an OutputByteStream to Swift_Message. |
||
349 | * |
||
350 | * @param Swift_OutputByteStream $fromStream |
||
351 | * @param Swift_Message $message |
||
352 | * |
||
353 | * @throws Swift_DependencyException |
||
354 | * @throws Swift_SwiftException |
||
355 | */ |
||
356 | 8 | protected function streamToMime(Swift_OutputByteStream $fromStream, Swift_Message $message) |
|
438 | } |
||
439 |
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.