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 SecureString 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 SecureString, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class SecureString { |
||
| 15 | const SEP = '.'; |
||
| 16 | const EOS = '-'; |
||
| 17 | const STRICT = 'strict'; |
||
| 18 | |||
| 19 | protected $timestampExpiry; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Initialize an instance of the {@link SecureString} class. |
||
| 23 | */ |
||
| 24 | 440 | public function __construct() { |
|
| 27 | |||
| 28 | /** |
||
| 29 | * Get or set the timestamp expiry in seconds. |
||
| 30 | * |
||
| 31 | * @param int $value Set a new expiry value in seconds. |
||
| 32 | * @return int Returns the current timestamp expiry. |
||
| 33 | */ |
||
| 34 | 1 | public function timestampExpiry($value = 0) { |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Encode a string using a secure specification. |
||
| 43 | * |
||
| 44 | * @param mixed $data The data to encode. This must be data that supports json serialization. |
||
| 45 | * @param array $spec An array specifying how the string should be encoded. |
||
| 46 | * The array should be in the form ['spec1' => 'password', 'spec2' => 'password']. |
||
| 47 | * @param bool $throw Whether or not to throw an exception on error. |
||
| 48 | * @return string|null Returns the encoded string or null if there is an error. |
||
| 49 | */ |
||
| 50 | 438 | public function encode($data, array $spec, $throw = false) { |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Decode a string that was encoded using {@link SecureString::encode()}. |
||
| 99 | * |
||
| 100 | * @param string $str The encoded string. |
||
| 101 | * @param array $spec An array specifying how the string should be encoded. |
||
| 102 | * @param bool $throw Whether or not to throw an exception on error. |
||
| 103 | * @return string|null Returns the decoded string or null of there was a problem. |
||
| 104 | */ |
||
| 105 | 439 | public function decode($str, array $spec, $throw = false) { |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Generate a random string suitable for use as an encryption or signature key. |
||
| 171 | * |
||
| 172 | * @param int $len The number of characters in the key. |
||
| 173 | * @return string Returns a base64url encoded string representing the random key. |
||
| 174 | */ |
||
| 175 | 2 | public static function generateRandomKey($len = 32) { |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Base64 Encode a string, but make it suitable to be passed in a url. |
||
| 183 | * |
||
| 184 | * @param string $str The string to encode. |
||
| 185 | * @return string The encoded string. |
||
| 186 | */ |
||
| 187 | 437 | protected static function base64urlEncode($str) { |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Decode a string that was encoded using base64UrlEncode(). |
||
| 193 | * |
||
| 194 | * @param string $str The encoded string. |
||
| 195 | * @return string The decoded string. |
||
| 196 | */ |
||
| 197 | 293 | protected static function base64urlDecode($str) { |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Encrypt a string with {@link openssl_encrypt()}. |
||
| 203 | * |
||
| 204 | * @param string $str The string to encrypt. |
||
| 205 | * @param string $method The encryption cipher. |
||
| 206 | * @param string $password The encryption password. |
||
| 207 | * @param string $iv The input vector. |
||
| 208 | * @param bool $throw Whether or not to throw an exception on error. |
||
| 209 | * @return string Returns the encrypted string. |
||
| 210 | */ |
||
| 211 | 290 | View Code Duplication | protected function encrypt($str, $method, $password, $iv = '', $throw = false) { |
| 227 | |||
| 228 | /** |
||
| 229 | * Decrypt a string with {@link openssl_decrypt()}. |
||
| 230 | * |
||
| 231 | * @param string $str The base64 url encoded encrypted string. |
||
| 232 | * @param string $method The encryption cipher. |
||
| 233 | * @param string $password The password to decyrpt the string. |
||
| 234 | * @param bool $throw Whether or not to decode the string on exception. |
||
| 235 | * @return string|null Returns the decrypted string or null on error. |
||
| 236 | */ |
||
| 237 | 162 | protected function decrypt($str, $method, $password, $throw = false) { |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Get information about a supported spec name. |
||
| 256 | * |
||
| 257 | * @param string $name The name of the spec. |
||
| 258 | * @param bool $throw Whether or not throw an exception on error. |
||
| 259 | * @return array|null Returns an array in the form [encode, decode, method, encodeFirst]. |
||
| 260 | * Returns null on error. |
||
| 261 | */ |
||
| 262 | 439 | protected function supportedInfo($name, $throw = false) { |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Sign a string with hmac and a hash method. |
||
| 278 | * |
||
| 279 | * @param string $str The string to sign. |
||
| 280 | * @param string $method The hash method used to sign the string. |
||
| 281 | * @param string $password The password used to hmac hash the string with. |
||
| 282 | * @param int $timestamp The timestamp used to sign the string with or 0 to use the current time. |
||
| 283 | * @param bool $throw Whether or not the throw an exception on error. |
||
| 284 | * @return string Returns the string with signing information or null on error. |
||
| 285 | */ |
||
| 286 | 292 | View Code Duplication | protected function hmac($str, $method, $password, $timestamp = 0, $throw = false) { |
| 304 | |||
| 305 | /** |
||
| 306 | * Verify the signature on a secure string. |
||
| 307 | * |
||
| 308 | * @param string $str The string to verify. |
||
| 309 | * @param string $method The hashing algorithm that the string was signed with. |
||
| 310 | * @param string $password The password used to sign the string. |
||
| 311 | * @param bool $throw Whether or not to throw an exeptio on error. |
||
| 312 | * @return bool Returns the string without the signing information or null on error. |
||
| 313 | */ |
||
| 314 | 180 | protected function verifyHmac($str, $method, $password, $throw = false) { |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Verify that a timestamp hasn't expired. |
||
| 349 | * |
||
| 350 | * @param string $timestamp The unix timestamp to verify. |
||
| 351 | * @param bool $throw Whether or not to throw an exception on error. |
||
| 352 | * @return bool Returns true if the timestamp is valid or false otherwise. |
||
| 353 | */ |
||
| 354 | 99 | protected function verifyTimestamp($timestamp, $throw = false) { |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Pop a string off of the end of an encoded secure string. |
||
| 369 | * |
||
| 370 | * @param string &$str The main string to pop. |
||
| 371 | * @return string|null Returns the popped string or null if {@link $str} is empty. |
||
| 372 | */ |
||
| 373 | 439 | protected function popString(&$str) { |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Pushes a string on to the end of an encoded secure string. |
||
| 391 | * |
||
| 392 | * @param string &$str The main string to push to. |
||
| 393 | * @param string|array $item The string or array of strings to push on to the end of {@link $str}. |
||
| 394 | */ |
||
| 395 | 437 | protected function pushString(&$str, $item) { |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Throw an exception or return false. |
||
| 404 | * |
||
| 405 | * @param bool $throw Whether or not to throw an exception. |
||
| 406 | * @param string $message The exception message. |
||
| 407 | * @param int $code The exception code. |
||
| 408 | * @return bool Returns false if {@link $throw} is false. |
||
| 409 | * @throws \Exception Throws an exception with {@link $message} and {@link $code}. |
||
| 410 | */ |
||
| 411 | 295 | protected function exception($throw, $message, $code) { |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Twiddle a value in an encoded secure string to another value. |
||
| 420 | * |
||
| 421 | * This method is mainly for testing so that an invalid string can be created. |
||
| 422 | * |
||
| 423 | * @param string $string A valid cookie to twiddle. |
||
| 424 | * @param int $index The index of the new value. |
||
| 425 | * @param string $value The new value. This will be base64url encoded. |
||
| 426 | * @param bool $encode Whether or not to base64 url encode the value. |
||
| 427 | * @return string Returns the new encoded cookie. |
||
| 428 | */ |
||
| 429 | 2 | public function twiddle($string, $index, $value, $encode = false) { |
|
| 439 | } |
||
| 440 |