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 |
||
19 | class EE_Encryption implements InterminableInterface |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * key used for saving the encryption key to the wp_options table |
||
24 | */ |
||
25 | const ENCRYPTION_OPTION_KEY = 'ee_encryption_key'; |
||
26 | |||
27 | /** |
||
28 | * the OPENSSL cipher method used |
||
29 | */ |
||
30 | const OPENSSL_CIPHER_METHOD = 'AES-128-CBC'; |
||
31 | |||
32 | /** |
||
33 | * WP "options_name" used to store a verified available cipher method |
||
34 | */ |
||
35 | const OPENSSL_CIPHER_METHOD_OPTION_NAME = 'ee_openssl_cipher_method'; |
||
36 | |||
37 | /** |
||
38 | * the OPENSSL digest method used |
||
39 | */ |
||
40 | const OPENSSL_DIGEST_METHOD = 'sha512'; |
||
41 | |||
42 | /** |
||
43 | * separates the encrypted text from the initialization vector |
||
44 | */ |
||
45 | const OPENSSL_IV_DELIMITER = ':iv:'; |
||
46 | |||
47 | /** |
||
48 | * appended to text encrypted using the acme encryption |
||
49 | */ |
||
50 | const ACME_ENCRYPTION_FLAG = '::ae'; |
||
51 | |||
52 | |||
53 | |||
54 | /** |
||
55 | * instance of the EE_Encryption object |
||
56 | */ |
||
57 | protected static $_instance; |
||
58 | |||
59 | /** |
||
60 | * @var string $_encryption_key |
||
61 | */ |
||
62 | protected $_encryption_key; |
||
63 | |||
64 | /** |
||
65 | * @var string $cipher_method |
||
66 | */ |
||
67 | private $cipher_method = ''; |
||
68 | |||
69 | /** |
||
70 | * @var array $cipher_methods |
||
71 | */ |
||
72 | private $cipher_methods = array(); |
||
73 | |||
74 | /** |
||
75 | * @var array $digest_methods |
||
76 | */ |
||
77 | private $digest_methods = array(); |
||
78 | |||
79 | /** |
||
80 | * @var boolean $_use_openssl_encrypt |
||
81 | */ |
||
82 | protected $_use_openssl_encrypt = false; |
||
83 | |||
84 | /** |
||
85 | * @var boolean $_use_mcrypt |
||
86 | */ |
||
87 | protected $_use_mcrypt = false; |
||
88 | |||
89 | /** |
||
90 | * @var boolean $_use_base64_encode |
||
91 | */ |
||
92 | protected $_use_base64_encode = false; |
||
93 | |||
94 | |||
95 | |||
96 | /** |
||
97 | * protected constructor to prevent direct creation |
||
98 | */ |
||
99 | protected function __construct() |
||
113 | |||
114 | |||
115 | |||
116 | /** |
||
117 | * singleton method used to instantiate class object |
||
118 | * |
||
119 | * @return EE_Encryption |
||
120 | */ |
||
121 | public static function instance() |
||
129 | |||
130 | |||
131 | |||
132 | /** |
||
133 | * get encryption key |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | public function get_encryption_key() |
||
155 | |||
156 | |||
157 | |||
158 | /** |
||
159 | * encrypts data |
||
160 | * |
||
161 | * @param string $text_string - the text to be encrypted |
||
162 | * @return string |
||
163 | * @throws RuntimeException |
||
164 | */ |
||
165 | public function encrypt($text_string = '') |
||
178 | |||
179 | |||
180 | |||
181 | /** |
||
182 | * decrypts data |
||
183 | * |
||
184 | * @param string $encrypted_text - the text to be decrypted |
||
185 | * @return string |
||
186 | * @throws RuntimeException |
||
187 | */ |
||
188 | public function decrypt($encrypted_text = '') |
||
202 | |||
203 | |||
204 | |||
205 | /** |
||
206 | * encodes string with PHP's base64 encoding |
||
207 | * |
||
208 | * @see http://php.net/manual/en/function.base64-encode.php |
||
209 | * @param string $text_string the text to be encoded |
||
210 | * @return string |
||
211 | */ |
||
212 | public function base64_string_encode($text_string = '') |
||
221 | |||
222 | |||
223 | /** |
||
224 | * decodes string that has been encoded with PHP's base64 encoding |
||
225 | * |
||
226 | * @see http://php.net/manual/en/function.base64-encode.php |
||
227 | * @param string $encoded_string the text to be decoded |
||
228 | * @return string |
||
229 | * @throws RuntimeException |
||
230 | */ |
||
231 | public function base64_string_decode($encoded_string = '') |
||
246 | |||
247 | |||
248 | |||
249 | /** |
||
250 | * encodes url string with PHP's base64 encoding |
||
251 | * |
||
252 | * @see http://php.net/manual/en/function.base64-encode.php |
||
253 | * @param string $text_string the text to be encoded |
||
254 | * @return string |
||
255 | */ |
||
256 | public function base64_url_encode($text_string = '') |
||
267 | |||
268 | |||
269 | /** |
||
270 | * decodes url string that has been encoded with PHP's base64 encoding |
||
271 | * |
||
272 | * @see http://php.net/manual/en/function.base64-encode.php |
||
273 | * @param string $encoded_string the text to be decoded |
||
274 | * @return string |
||
275 | * @throws RuntimeException |
||
276 | */ |
||
277 | public function base64_url_decode($encoded_string = '') |
||
294 | |||
295 | |||
296 | /** |
||
297 | * encrypts data using PHP's openssl functions |
||
298 | * |
||
299 | * @param string $text_string the text to be encrypted |
||
300 | * @param string $cipher_method |
||
301 | * @param string $encryption_key |
||
302 | * @return string |
||
303 | * @throws RuntimeException |
||
304 | */ |
||
305 | protected function openssl_encrypt( |
||
342 | |||
343 | |||
344 | |||
345 | /** |
||
346 | * Returns a cipher method that has been verified to work. |
||
347 | * First checks if the cached cipher has been set already and if so, returns that. |
||
348 | * Then tests the incoming default and returns that if it's good. |
||
349 | * If not, then it retrieves the previously tested and saved cipher method. |
||
350 | * But if that doesn't exist, then calls getAvailableCipherMethod() |
||
351 | * to see what is available on the server, and returns the results. |
||
352 | * |
||
353 | * @param string $cipher_method |
||
354 | * @return string |
||
355 | * @throws RuntimeException |
||
356 | */ |
||
357 | protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD) |
||
373 | |||
374 | |||
375 | |||
376 | /** |
||
377 | * @param string $cipher_method |
||
378 | * @return string |
||
379 | * @throws \RuntimeException |
||
380 | */ |
||
381 | protected function getAvailableCipherMethod($cipher_method) |
||
412 | |||
413 | |||
414 | /** |
||
415 | * decrypts data that has been encrypted with PHP's openssl functions |
||
416 | * |
||
417 | * @param string $encrypted_text the text to be decrypted |
||
418 | * @param string $cipher_method |
||
419 | * @param string $encryption_key |
||
420 | * @return string |
||
421 | * @throws RuntimeException |
||
422 | */ |
||
423 | protected function openssl_decrypt( |
||
456 | |||
457 | |||
458 | /** |
||
459 | * Computes the digest hash value using the specified digest method. |
||
460 | * If that digest method fails to produce a valid hash value, |
||
461 | * then we'll grab the next digest method and recursively try again until something works. |
||
462 | * |
||
463 | * @param string $digest_method |
||
464 | * @param string $encryption_key |
||
465 | * @return string |
||
466 | * @throws RuntimeException |
||
467 | */ |
||
468 | protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = ''){ |
||
478 | |||
479 | |||
480 | |||
481 | /** |
||
482 | * Returns the NEXT element in the $digest_methods array. |
||
483 | * If the $digest_methods array is empty, then we populate it |
||
484 | * with the available values returned from openssl_get_md_methods(). |
||
485 | * |
||
486 | * @return string |
||
487 | * @throws \RuntimeException |
||
488 | */ |
||
489 | protected function getDigestMethod(){ |
||
505 | |||
506 | |||
507 | /** |
||
508 | * encrypts data for acme servers that didn't bother to install PHP mcrypt |
||
509 | * |
||
510 | * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
||
511 | * @param string $text_string the text to be decrypted |
||
512 | * @return string |
||
513 | */ |
||
514 | protected function acme_encrypt($text_string = '') |
||
539 | |||
540 | |||
541 | |||
542 | /** |
||
543 | * decrypts data for acme servers that didn't bother to install PHP mcrypt |
||
544 | * |
||
545 | * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
||
546 | * @param string $encrypted_text the text to be decrypted |
||
547 | * @return string |
||
548 | * @throws RuntimeException |
||
549 | */ |
||
550 | protected function acme_decrypt($encrypted_text = '') |
||
582 | |||
583 | |||
584 | |||
585 | /** |
||
586 | * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
||
587 | * @param $string |
||
588 | * @return bool |
||
589 | */ |
||
590 | protected function valid_base_64($string) |
||
608 | |||
609 | |||
610 | |||
611 | /** |
||
612 | * generate random string |
||
613 | * |
||
614 | * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
||
615 | * @param int $length number of characters for random string |
||
616 | * @return string |
||
617 | */ |
||
618 | public function generate_random_string($length = 40) |
||
628 | |||
629 | |||
630 | |||
631 | /** |
||
632 | * encrypts data using PHP's mcrypt functions |
||
633 | * |
||
634 | * @deprecated 4.9.39 |
||
635 | * @param string $text_string |
||
636 | * @internal param $string - the text to be encrypted |
||
637 | * @return string |
||
638 | * @throws RuntimeException |
||
639 | */ |
||
640 | protected function m_encrypt($text_string = '') |
||
668 | |||
669 | |||
670 | |||
671 | /** |
||
672 | * decrypts data that has been encrypted with PHP's mcrypt functions |
||
673 | * |
||
674 | * @deprecated 4.9.39 |
||
675 | * @param string $encrypted_text the text to be decrypted |
||
676 | * @return string |
||
677 | * @throws RuntimeException |
||
678 | */ |
||
679 | protected function m_decrypt($encrypted_text = '') |
||
708 | |||
709 | } |
||
710 | /* End of file EE_Encryption.class.php */ |
||
712 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.