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 | 8 | $this->encryptCipher = OPENSSL_CIPHER_AES_128_CBC; |
|
66 | 8 | } else { |
|
67 | $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 Swift_Signers_SMimeSigner |
||
78 | */ |
||
79 | 1 | public static function newInstance($certificate = null, $privateKey = null) |
|
83 | |||
84 | /** |
||
85 | * Set the certificate location to use for signing. |
||
86 | * |
||
87 | * @link 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 Swift_Signers_SMimeSigner |
||
95 | */ |
||
96 | 6 | public function setSignCertificate($certificate, $privateKey = null, $signOptions = PKCS7_DETACHED, $extraCerts = null) |
|
97 | { |
||
98 | 6 | $this->signCertificate = 'file://' . str_replace('\\', '/', realpath($certificate)); |
|
99 | |||
100 | 6 | View Code Duplication | if (null !== $privateKey) { |
101 | 6 | if (is_array($privateKey)) { |
|
102 | $this->signPrivateKey = $privateKey; |
||
103 | $this->signPrivateKey[0] = 'file://' . str_replace('\\', '/', realpath($privateKey[0])); |
||
104 | } else { |
||
105 | 6 | $this->signPrivateKey = 'file://' . str_replace('\\', '/', realpath($privateKey)); |
|
106 | } |
||
107 | 6 | } |
|
108 | |||
109 | 6 | $this->signOptions = $signOptions; |
|
110 | 6 | if (null !== $extraCerts) { |
|
111 | 1 | $this->extraCerts = str_replace('\\', '/', realpath($extraCerts)); |
|
112 | 1 | } |
|
113 | |||
114 | 6 | return $this; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Set the certificate location to use for encryption. |
||
119 | * |
||
120 | * @link http://www.php.net/manual/en/openssl.pkcs7.flags.php |
||
121 | * @link 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 Swift_Signers_SMimeSigner |
||
127 | */ |
||
128 | 4 | public function setEncryptCertificate($recipientCerts, $cipher = null) |
|
129 | { |
||
130 | 4 | View Code Duplication | if (is_array($recipientCerts)) { |
131 | 1 | $this->encryptCert = array(); |
|
132 | |||
133 | 1 | foreach ($recipientCerts as $cert) { |
|
134 | 1 | $this->encryptCert[] = 'file://' . str_replace('\\', '/', realpath($cert)); |
|
135 | 1 | } |
|
136 | 1 | } else { |
|
137 | 3 | $this->encryptCert = 'file://' . str_replace('\\', '/', realpath($recipientCerts)); |
|
138 | } |
||
139 | |||
140 | 4 | if (null !== $cipher) { |
|
141 | $this->encryptCipher = $cipher; |
||
142 | } |
||
143 | |||
144 | 4 | return $this; |
|
145 | } |
||
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 Swift_Signers_SMimeSigner |
||
173 | */ |
||
174 | 1 | public function setSignThenEncrypt($signThenEncrypt = true) |
|
175 | { |
||
176 | 1 | $this->signThenEncrypt = $signThenEncrypt; |
|
177 | |||
178 | 1 | return $this; |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return bool |
||
183 | */ |
||
184 | public function isSignThenEncrypt() |
||
188 | |||
189 | /** |
||
190 | * Resets internal states. |
||
191 | * |
||
192 | * @return Swift_Signers_SMimeSigner |
||
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 Swift_Signers_SMimeSigner |
||
205 | */ |
||
206 | 8 | public function signMessage(Swift_Message $message) |
|
207 | { |
||
208 | 8 | if (null === $this->signCertificate && null === $this->encryptCert) { |
|
209 | return $this; |
||
210 | } |
||
211 | |||
212 | // Store the message using ByteStream to a file{1} |
||
213 | // Remove all Children |
||
214 | // Sign file{1}, parse the new MIME headers and set them on the primary MimeEntity |
||
215 | // Set the singed-body as the new body (without boundary) |
||
216 | |||
217 | 8 | $messageStream = new Swift_ByteStream_TemporaryFileByteStream(); |
|
218 | 8 | $this->toSMimeByteStream($messageStream, $message); |
|
219 | 8 | $message->setEncoder(Swift_DependencyContainer::getInstance()->lookup('mime.rawcontentencoder')); |
|
220 | |||
221 | 8 | $message->setChildren(array()); |
|
222 | 8 | $this->streamToMime($messageStream, $message); |
|
223 | 8 | } |
|
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) |
|
239 | { |
||
240 | 8 | $mimeEntity = $this->createMessage($message); |
|
241 | 8 | $messageStream = new Swift_ByteStream_TemporaryFileByteStream(); |
|
242 | |||
243 | 8 | $mimeEntity->toByteStream($messageStream); |
|
244 | 8 | $messageStream->commit(); |
|
245 | |||
246 | 8 | if (null !== $this->signCertificate && null !== $this->encryptCert) { |
|
247 | 2 | $temporaryStream = new Swift_ByteStream_TemporaryFileByteStream(); |
|
248 | |||
249 | 2 | if ($this->signThenEncrypt) { |
|
250 | 1 | $this->messageStreamToSignedByteStream($messageStream, $temporaryStream); |
|
251 | 1 | $this->messageStreamToEncryptedByteStream($temporaryStream, $inputStream); |
|
252 | 1 | } else { |
|
253 | 1 | $this->messageStreamToEncryptedByteStream($messageStream, $temporaryStream); |
|
254 | 1 | $this->messageStreamToSignedByteStream($temporaryStream, $inputStream); |
|
255 | } |
||
256 | 8 | } elseif ($this->signCertificate !== null) { |
|
257 | 4 | $this->messageStreamToSignedByteStream($messageStream, $inputStream); |
|
258 | 4 | } else { |
|
259 | 2 | $this->messageStreamToEncryptedByteStream($messageStream, $inputStream); |
|
260 | } |
||
261 | 8 | } |
|
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) |
|
291 | { |
||
292 | 6 | $signedMessageStream = new Swift_ByteStream_TemporaryFileByteStream(); |
|
293 | |||
294 | 6 | $args = array($outputStream->getPath(), $signedMessageStream->getPath(), $this->signCertificate, $this->signPrivateKey, array(), $this->signOptions); |
|
295 | 6 | if (null !== $this->extraCerts) { |
|
296 | 1 | $args[] = $this->extraCerts; |
|
297 | 1 | } |
|
298 | |||
299 | 6 | if (!call_user_func_array('openssl_pkcs7_sign', $args)) { |
|
300 | throw new Swift_IoException(sprintf('Failed to sign S/Mime message. Error: "%s".', openssl_error_string())); |
||
301 | } |
||
302 | |||
303 | 6 | $this->copyFromOpenSSLOutput($signedMessageStream, $inputStream); |
|
304 | 6 | } |
|
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.