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 |
||
| 18 | class EE_Encryption { |
||
| 19 | |||
| 20 | // instance of the EE_Encryption object |
||
| 21 | protected static $_instance; |
||
| 22 | |||
| 23 | protected $_encryption_key; |
||
| 24 | |||
| 25 | protected $_use_mcrypt = true; |
||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | /** |
||
| 30 | * private constructor to prevent direct creation |
||
| 31 | * |
||
| 32 | */ |
||
| 33 | private function __construct() { |
||
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * singleton method used to instantiate class object |
||
| 44 | * @access public |
||
| 45 | * @return \EE_Encryption |
||
| 46 | */ |
||
| 47 | public static function instance ( ) { |
||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * get encryption key |
||
| 59 | * @access public |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | public function get_encryption_key() { |
||
| 79 | |||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * encrypts data |
||
| 84 | * @access public |
||
| 85 | * @param string $text_string - the text to be encrypted |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public function encrypt ( $text_string = '' ) { |
||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * decrypts data |
||
| 105 | * @access public |
||
| 106 | * @param string $encrypted_text - the text to be decrypted |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function decrypt ( $encrypted_text = '' ) { |
||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * encodes string with PHP's base64 encoding |
||
| 127 | * |
||
| 128 | * @source http://php.net/manual/en/function.base64-encode.php |
||
| 129 | * @param string $text_string |
||
| 130 | * @internal param $string - the text to be encoded |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function base64_string_encode ( $text_string = '' ) { |
||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * decodes string that has been encoded with PHP's base64 encoding |
||
| 146 | * |
||
| 147 | * @source http://php.net/manual/en/function.base64-encode.php |
||
| 148 | * @param string $encoded_string |
||
| 149 | * @internal param $string - the text to be decoded |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function base64_string_decode ( $encoded_string = '' ) { |
||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * encodes url string with PHP's base64 encoding |
||
| 165 | * @source http://php.net/manual/en/function.base64-encode.php |
||
| 166 | * @access public |
||
| 167 | * @param string $text_string |
||
| 168 | * @internal param $string - the text to be encoded |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function base64_url_encode ( $text_string = '' ) { |
||
| 181 | |||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * decodes url string that has been encoded with PHP's base64 encoding |
||
| 186 | * @source http://php.net/manual/en/function.base64-encode.php |
||
| 187 | * @access public |
||
| 188 | * @param string $encoded_string |
||
| 189 | * @internal param $string - the text to be decoded |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function base64_url_decode ( $encoded_string = '' ) { |
||
| 202 | |||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * encrypts data using PHP's mcrypt functions |
||
| 207 | * @access private |
||
| 208 | * @param string $text_string |
||
| 209 | * @internal param $string - the text to be encrypted |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | private function m_encrypt ( $text_string = '' ) { |
||
| 226 | |||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * decrypts data that has been encrypted with PHP's mcrypt functions |
||
| 231 | * @access private |
||
| 232 | * @param string $encrypted_text |
||
| 233 | * @internal param $string - the text to be decrypted |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | private function m_decrypt ( $encrypted_text = '' ) { |
||
| 251 | |||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * encrypts data for acme servers that didn't bother to install PHP mcrypt |
||
| 256 | * @source : http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
||
| 257 | * @access private |
||
| 258 | * @param string $text_string |
||
| 259 | * @internal param $string - the text to be decrypted |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | private function acme_encrypt ( $text_string = '' ) { |
||
| 275 | |||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * decrypts data for acme servers that didn't bother to install PHP mcrypt |
||
| 280 | * |
||
| 281 | * @source : http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
||
| 282 | * @param string $encrypted_text the text to be decrypted |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | private function acme_decrypt ( $encrypted_text = '' ) { |
||
| 300 | |||
| 301 | |||
| 302 | |||
| 303 | /** |
||
| 304 | * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
||
| 305 | * @param $string |
||
| 306 | * @return bool |
||
| 307 | */ |
||
| 308 | private function valid_base_64($string) |
||
| 326 | |||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * generate random string |
||
| 331 | * @source : http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
||
| 332 | * @access public |
||
| 333 | * @param int $length |
||
| 334 | * @internal param $string - number of characters for random string |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | public function generate_random_string ( $length = 40 ) { |
||
| 346 | |||
| 347 | |||
| 348 | |||
| 349 | } |
||
| 350 | /* End of file EE_Encryption.class.php */ |
||
| 351 | /* Location: /includes/core/EE_Encryption.core.php */ |