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 |
||
| 25 | class EE_Encryption implements InterminableInterface |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * key used for saving the encryption key to the wp_options table |
||
| 30 | */ |
||
| 31 | const ENCRYPTION_OPTION_KEY = 'ee_encryption_key'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * the OPENSSL cipher method used |
||
| 35 | */ |
||
| 36 | const OPENSSL_CIPHER_METHOD = 'AES-128-CBC'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * WP "options_name" used to store a verified available cipher method |
||
| 40 | */ |
||
| 41 | const OPENSSL_CIPHER_METHOD_OPTION_NAME = 'ee_openssl_cipher_method'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * the OPENSSL digest method used |
||
| 45 | */ |
||
| 46 | const OPENSSL_DIGEST_METHOD = 'sha512'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * separates the encrypted text from the initialization vector |
||
| 50 | */ |
||
| 51 | const OPENSSL_IV_DELIMITER = ':iv:'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * appended to text encrypted using the acme encryption |
||
| 55 | */ |
||
| 56 | const ACME_ENCRYPTION_FLAG = '::ae'; |
||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * instance of the EE_Encryption object |
||
| 61 | */ |
||
| 62 | protected static $_instance; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string $_encryption_key |
||
| 66 | */ |
||
| 67 | protected $_encryption_key; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string $cipher_method |
||
| 71 | */ |
||
| 72 | private $cipher_method = ''; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var array $cipher_methods |
||
| 76 | */ |
||
| 77 | private $cipher_methods = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array $digest_methods |
||
| 81 | */ |
||
| 82 | private $digest_methods = array(); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var boolean $_use_openssl_encrypt |
||
| 86 | */ |
||
| 87 | protected $_use_openssl_encrypt = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var boolean $_use_base64_encode |
||
| 91 | */ |
||
| 92 | protected $_use_base64_encode = false; |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * protected constructor to prevent direct creation |
||
| 97 | */ |
||
| 98 | protected function __construct() |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * singleton method used to instantiate class object |
||
| 114 | * |
||
| 115 | * @return EE_Encryption |
||
| 116 | */ |
||
| 117 | public static function instance() |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * get encryption key |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public function get_encryption_key() |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * encrypts data |
||
| 154 | * |
||
| 155 | * @param string $text_string - the text to be encrypted |
||
| 156 | * @return string |
||
| 157 | * @throws RuntimeException |
||
| 158 | */ |
||
| 159 | public function encrypt($text_string = '') |
||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * decrypts data |
||
| 176 | * |
||
| 177 | * @param string $encrypted_text - the text to be decrypted |
||
| 178 | * @return string |
||
| 179 | * @throws RuntimeException |
||
| 180 | */ |
||
| 181 | public function decrypt($encrypted_text = '') |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * encodes string with PHP's base64 encoding |
||
| 199 | * |
||
| 200 | * @see http://php.net/manual/en/function.base64-encode.php |
||
| 201 | * @param string $text_string the text to be encoded |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function base64_string_encode($text_string = '') |
||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * decodes string that has been encoded with PHP's base64 encoding |
||
| 217 | * |
||
| 218 | * @see http://php.net/manual/en/function.base64-encode.php |
||
| 219 | * @param string $encoded_string the text to be decoded |
||
| 220 | * @return string |
||
| 221 | * @throws RuntimeException |
||
| 222 | */ |
||
| 223 | public function base64_string_decode($encoded_string = '') |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * encodes url string with PHP's base64 encoding |
||
| 242 | * |
||
| 243 | * @see http://php.net/manual/en/function.base64-encode.php |
||
| 244 | * @param string $text_string the text to be encoded |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function base64_url_encode($text_string = '') |
||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * decodes url string that has been encoded with PHP's base64 encoding |
||
| 262 | * |
||
| 263 | * @see http://php.net/manual/en/function.base64-encode.php |
||
| 264 | * @param string $encoded_string the text to be decoded |
||
| 265 | * @return string |
||
| 266 | * @throws RuntimeException |
||
| 267 | */ |
||
| 268 | public function base64_url_decode($encoded_string = '') |
||
| 285 | |||
| 286 | |||
| 287 | /** |
||
| 288 | * encrypts data using PHP's openssl functions |
||
| 289 | * |
||
| 290 | * @param string $text_string the text to be encrypted |
||
| 291 | * @param string $cipher_method |
||
| 292 | * @param string $encryption_key |
||
| 293 | * @return string |
||
| 294 | * @throws RuntimeException |
||
| 295 | */ |
||
| 296 | protected function openssl_encrypt( |
||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * Returns a cipher method that has been verified to work. |
||
| 337 | * First checks if the cached cipher has been set already and if so, returns that. |
||
| 338 | * Then tests the incoming default and returns that if it's good. |
||
| 339 | * If not, then it retrieves the previously tested and saved cipher method. |
||
| 340 | * But if that doesn't exist, then calls getAvailableCipherMethod() |
||
| 341 | * to see what is available on the server, and returns the results. |
||
| 342 | * |
||
| 343 | * @param string $cipher_method |
||
| 344 | * @return string |
||
| 345 | * @throws RuntimeException |
||
| 346 | */ |
||
| 347 | protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD) |
||
| 363 | |||
| 364 | |||
| 365 | /** |
||
| 366 | * @param string $cipher_method |
||
| 367 | * @return string |
||
| 368 | * @throws \RuntimeException |
||
| 369 | */ |
||
| 370 | protected function getAvailableCipherMethod($cipher_method) |
||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * decrypts data that has been encrypted with PHP's openssl functions |
||
| 405 | * |
||
| 406 | * @param string $encrypted_text the text to be decrypted |
||
| 407 | * @param string $cipher_method |
||
| 408 | * @param string $encryption_key |
||
| 409 | * @return string |
||
| 410 | * @throws RuntimeException |
||
| 411 | */ |
||
| 412 | protected function openssl_decrypt( |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * Computes the digest hash value using the specified digest method. |
||
| 445 | * If that digest method fails to produce a valid hash value, |
||
| 446 | * then we'll grab the next digest method and recursively try again until something works. |
||
| 447 | * |
||
| 448 | * @param string $digest_method |
||
| 449 | * @param string $encryption_key |
||
| 450 | * @return string |
||
| 451 | * @throws RuntimeException |
||
| 452 | */ |
||
| 453 | protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = '') |
||
| 464 | |||
| 465 | |||
| 466 | /** |
||
| 467 | * Returns the NEXT element in the $digest_methods array. |
||
| 468 | * If the $digest_methods array is empty, then we populate it |
||
| 469 | * with the available values returned from openssl_get_md_methods(). |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | * @throws \RuntimeException |
||
| 473 | */ |
||
| 474 | protected function getDigestMethod() |
||
| 491 | |||
| 492 | |||
| 493 | /** |
||
| 494 | * encrypts data for acme servers that didn't bother to install PHP mcrypt |
||
| 495 | * |
||
| 496 | * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
||
| 497 | * @param string $text_string the text to be decrypted |
||
| 498 | * @return string |
||
| 499 | */ |
||
| 500 | protected function acme_encrypt($text_string = '') |
||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * decrypts data for acme servers that didn't bother to install PHP mcrypt |
||
| 529 | * |
||
| 530 | * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
||
| 531 | * @param string $encrypted_text the text to be decrypted |
||
| 532 | * @return string |
||
| 533 | * @throws RuntimeException |
||
| 534 | */ |
||
| 535 | protected function acme_decrypt($encrypted_text = '') |
||
| 561 | |||
| 562 | |||
| 563 | /** |
||
| 564 | * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
||
| 565 | * @param $string |
||
| 566 | * @return bool |
||
| 567 | */ |
||
| 568 | protected function valid_base_64($string) |
||
| 586 | |||
| 587 | |||
| 588 | /** |
||
| 589 | * generate random string |
||
| 590 | * |
||
| 591 | * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
||
| 592 | * @param int $length number of characters for random string |
||
| 593 | * @return string |
||
| 594 | */ |
||
| 595 | public function generate_random_string($length = 40) |
||
| 605 | } |
||
| 606 |