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 EE_Encryption 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 EE_Encryption, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class EE_Encryption |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * key used for saving the encryption key to the wp_options table |
||
21 | */ |
||
22 | const ENCRYPTION_OPTION_KEY = 'ee_encryption_key'; |
||
23 | |||
24 | /** |
||
25 | * the OPENSSL cipher method used |
||
26 | */ |
||
27 | const OPENSSL_CIPHER_METHOD = 'aes-256-ctr'; |
||
28 | |||
29 | /** |
||
30 | * the OPENSSL digest method used |
||
31 | */ |
||
32 | const OPENSSL_DIGEST_METHOD = 'sha512'; |
||
33 | |||
34 | /** |
||
35 | * separates the encrypted text from the initialization vector |
||
36 | */ |
||
37 | const OPENSSL_IV_DELIMITER = ':iv:'; |
||
38 | |||
39 | /** |
||
40 | * appended to text encrypted using the acme encryption |
||
41 | */ |
||
42 | const ACME_ENCRYPTION_FLAG = '::ae'; |
||
43 | |||
44 | |||
45 | |||
46 | /** |
||
47 | * instance of the EE_Encryption object |
||
48 | */ |
||
49 | protected static $_instance; |
||
50 | |||
51 | /** |
||
52 | * @var string $_encryption_key |
||
53 | */ |
||
54 | protected $_encryption_key; |
||
55 | |||
56 | /** |
||
57 | * @var boolean $_use_openssl_encrypt |
||
58 | */ |
||
59 | protected $_use_openssl_encrypt = false; |
||
60 | |||
61 | /** |
||
62 | * @var boolean $_use_mcrypt |
||
63 | */ |
||
64 | protected $_use_mcrypt = false; |
||
65 | |||
66 | /** |
||
67 | * @var boolean $_use_base64_encode |
||
68 | */ |
||
69 | protected $_use_base64_encode = false; |
||
70 | |||
71 | |||
72 | |||
73 | /** |
||
74 | * protected constructor to prevent direct creation |
||
75 | */ |
||
76 | protected function __construct() |
||
90 | |||
91 | |||
92 | |||
93 | /** |
||
94 | * singleton method used to instantiate class object |
||
95 | * |
||
96 | * @return EE_Encryption |
||
97 | */ |
||
98 | public static function instance() |
||
106 | |||
107 | |||
108 | |||
109 | /** |
||
110 | * get encryption key |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | public function get_encryption_key() |
||
132 | |||
133 | |||
134 | |||
135 | /** |
||
136 | * encrypts data |
||
137 | * |
||
138 | * @param string $text_string - the text to be encrypted |
||
139 | * @return string |
||
140 | * @throws RuntimeException |
||
141 | */ |
||
142 | public function encrypt($text_string = '') |
||
155 | |||
156 | |||
157 | |||
158 | /** |
||
159 | * decrypts data |
||
160 | * |
||
161 | * @param string $encrypted_text - the text to be decrypted |
||
162 | * @return string |
||
163 | * @throws RuntimeException |
||
164 | */ |
||
165 | public function decrypt($encrypted_text = '') |
||
179 | |||
180 | |||
181 | |||
182 | /** |
||
183 | * encodes string with PHP's base64 encoding |
||
184 | * |
||
185 | * @see http://php.net/manual/en/function.base64-encode.php |
||
186 | * @param string $text_string the text to be encoded |
||
187 | * @return string |
||
188 | */ |
||
189 | public function base64_string_encode($text_string = '') |
||
198 | |||
199 | |||
200 | |||
201 | /** |
||
202 | * decodes string that has been encoded with PHP's base64 encoding |
||
203 | * |
||
204 | * @see http://php.net/manual/en/function.base64-encode.php |
||
205 | * @param string $encoded_string the text to be decoded |
||
206 | * @return string |
||
207 | */ |
||
208 | public function base64_string_decode($encoded_string = '') |
||
217 | |||
218 | |||
219 | |||
220 | /** |
||
221 | * encodes url string with PHP's base64 encoding |
||
222 | * |
||
223 | * @see http://php.net/manual/en/function.base64-encode.php |
||
224 | * @param string $text_string the text to be encoded |
||
225 | * @return string |
||
226 | */ |
||
227 | public function base64_url_encode($text_string = '') |
||
238 | |||
239 | |||
240 | |||
241 | /** |
||
242 | * decodes url string that has been encoded with PHP's base64 encoding |
||
243 | * |
||
244 | * @see http://php.net/manual/en/function.base64-encode.php |
||
245 | * @param string $encoded_string the text to be decoded |
||
246 | * @return string |
||
247 | */ |
||
248 | public function base64_url_decode($encoded_string = '') |
||
259 | |||
260 | |||
261 | |||
262 | /** |
||
263 | * encrypts data using PHP's openssl functions |
||
264 | * |
||
265 | * @param string $text_string the text to be encrypted |
||
266 | * @return string |
||
267 | * @throws RuntimeException |
||
268 | */ |
||
269 | protected function openssl_encrypt($text_string = '') |
||
299 | |||
300 | |||
301 | |||
302 | /** |
||
303 | * decrypts data that has been encrypted with PHP's openssl functions |
||
304 | * |
||
305 | * @param string $encrypted_text the text to be decrypted |
||
306 | * @return string |
||
307 | * @throws RuntimeException |
||
308 | */ |
||
309 | protected function openssl_decrypt($encrypted_text = '') |
||
339 | |||
340 | |||
341 | |||
342 | /** |
||
343 | * encrypts data for acme servers that didn't bother to install PHP mcrypt |
||
344 | * |
||
345 | * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
||
346 | * @param string $text_string the text to be decrypted |
||
347 | * @return string |
||
348 | */ |
||
349 | protected function acme_encrypt($text_string = '') |
||
369 | |||
370 | |||
371 | |||
372 | /** |
||
373 | * decrypts data for acme servers that didn't bother to install PHP mcrypt |
||
374 | * |
||
375 | * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
||
376 | * @param string $encrypted_text the text to be decrypted |
||
377 | * @return string |
||
378 | */ |
||
379 | protected function acme_decrypt($encrypted_text = '') |
||
403 | |||
404 | |||
405 | |||
406 | /** |
||
407 | * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
||
408 | * @param $string |
||
409 | * @return bool |
||
410 | */ |
||
411 | protected function valid_base_64($string) |
||
429 | |||
430 | |||
431 | |||
432 | /** |
||
433 | * generate random string |
||
434 | * |
||
435 | * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
||
436 | * @param int $length number of characters for random string |
||
437 | * @return string |
||
438 | */ |
||
439 | public function generate_random_string($length = 40) |
||
449 | |||
450 | |||
451 | |||
452 | /** |
||
453 | * encrypts data using PHP's mcrypt functions |
||
454 | * |
||
455 | * @deprecated 4.9.39 |
||
456 | * @param string $text_string |
||
457 | * @internal param $string - the text to be encrypted |
||
458 | * @return string |
||
459 | * @throws RuntimeException |
||
460 | */ |
||
461 | protected function m_encrypt($text_string = '') |
||
489 | |||
490 | |||
491 | |||
492 | /** |
||
493 | * decrypts data that has been encrypted with PHP's mcrypt functions |
||
494 | * |
||
495 | * @deprecated 4.9.39 |
||
496 | * @param string $encrypted_text the text to be decrypted |
||
497 | * @return string |
||
498 | * @throws RuntimeException |
||
499 | */ |
||
500 | protected function m_decrypt($encrypted_text = '') |
||
529 | |||
530 | } |
||
531 | /* End of file EE_Encryption.class.php */ |
||
532 | /* Location: /includes/core/EE_Encryption.core.php */ |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.