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 JWT 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 JWT, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class JWT |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * When checking nbf, iat or expiration times, |
||
| 27 | * we want to provide some extra leeway time to |
||
| 28 | * account for clock skew. |
||
| 29 | */ |
||
| 30 | public static $leeway = 0; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Allow the current timestamp to be specified. |
||
| 34 | * Useful for fixing a value within unit testing. |
||
| 35 | * |
||
| 36 | * Will default to PHP time() value if null. |
||
| 37 | */ |
||
| 38 | public static $timestamp = null; |
||
| 39 | |||
| 40 | public static $supported_algs = array( |
||
| 41 | 'HS256' => array('hash_hmac', 'SHA256'), |
||
| 42 | 'HS512' => array('hash_hmac', 'SHA512'), |
||
| 43 | 'HS384' => array('hash_hmac', 'SHA384'), |
||
| 44 | 'RS256' => array('openssl', 'SHA256'), |
||
| 45 | ); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Decodes a JWT string into a PHP object. |
||
| 49 | * |
||
| 50 | * @param string $jwt The JWT |
||
| 51 | * @param string|array $key The key, or map of keys. |
||
| 52 | * If the algorithm used is asymmetric, this is the public key |
||
| 53 | * @param array $allowed_algs List of supported verification algorithms |
||
| 54 | * Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256' |
||
| 55 | * |
||
| 56 | * @return object The JWT's payload as a PHP object |
||
| 57 | * |
||
| 58 | * @throws UnexpectedValueException Provided JWT was invalid |
||
| 59 | * @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed |
||
| 60 | * @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf' |
||
| 61 | * @throws BeforeValidException Provided JWT is trying to be used before it's been created as defined by 'iat' |
||
| 62 | * @throws ExpiredException Provided JWT has since expired, as defined by the 'exp' claim |
||
| 63 | * |
||
| 64 | * @uses jsonDecode |
||
| 65 | * @uses urlsafeB64Decode |
||
| 66 | */ |
||
| 67 | public static function decode($jwt, $key, $allowed_algs = array()) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Converts and signs a PHP object or array into a JWT string. |
||
| 139 | * |
||
| 140 | * @param object|array $payload PHP object or array |
||
| 141 | * @param string $key The secret key. |
||
| 142 | * If the algorithm used is asymmetric, this is the private key |
||
| 143 | * @param string $alg The signing algorithm. |
||
| 144 | * Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256' |
||
| 145 | * @param mixed $keyId |
||
| 146 | * @param array $head An array with header elements to attach |
||
| 147 | * |
||
| 148 | * @return string A signed JWT |
||
| 149 | * |
||
| 150 | * @uses jsonEncode |
||
| 151 | * @uses urlsafeB64Encode |
||
| 152 | */ |
||
| 153 | public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $head = null) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Sign a string with a given key and algorithm. |
||
| 175 | * |
||
| 176 | * @param string $msg The message to sign |
||
| 177 | * @param string|resource $key The secret key |
||
| 178 | * @param string $alg The signing algorithm. |
||
| 179 | * Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256' |
||
| 180 | * |
||
| 181 | * @return string An encrypted message |
||
| 182 | * |
||
| 183 | * @throws DomainException Unsupported algorithm was specified |
||
| 184 | */ |
||
| 185 | public static function sign($msg, $key, $alg = 'HS256') |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Verify a signature with the message, key and method. Not all methods |
||
| 207 | * are symmetric, so we must have a separate verify and sign method. |
||
| 208 | * |
||
| 209 | * @param string $msg The original message (header and body) |
||
| 210 | * @param string $signature The original signature |
||
| 211 | * @param string|resource $key For HS*, a string key works. for RS*, must be a resource of an openssl public key |
||
| 212 | * @param string $alg The algorithm |
||
| 213 | * |
||
| 214 | * @return bool |
||
| 215 | * |
||
| 216 | * @throws DomainException Invalid Algorithm or OpenSSL failure |
||
| 217 | */ |
||
| 218 | private static function verify($msg, $signature, $key, $alg) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Decode a JSON string into a PHP object. |
||
| 253 | * |
||
| 254 | * @param string $input JSON string |
||
| 255 | * |
||
| 256 | * @return object Object representation of JSON string |
||
| 257 | * |
||
| 258 | * @throws DomainException Provided string was invalid JSON |
||
| 259 | */ |
||
| 260 | public static function jsonDecode($input) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Encode a PHP object into a JSON string. |
||
| 288 | * |
||
| 289 | * @param object|array $input A PHP object or array |
||
| 290 | * |
||
| 291 | * @return string JSON representation of the PHP object or array |
||
| 292 | * |
||
| 293 | * @throws DomainException Provided object could not be encoded to valid JSON |
||
| 294 | */ |
||
| 295 | public static function jsonEncode($input) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Decode a string with URL-safe Base64. |
||
| 308 | * |
||
| 309 | * @param string $input A Base64 encoded string |
||
| 310 | * |
||
| 311 | * @return string A decoded string |
||
| 312 | */ |
||
| 313 | public static function urlsafeB64Decode($input) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Encode a string with URL-safe Base64. |
||
| 325 | * |
||
| 326 | * @param string $input The string you want encoded |
||
| 327 | * |
||
| 328 | * @return string The base64 encode of what you passed in |
||
| 329 | */ |
||
| 330 | public static function urlsafeB64Encode($input) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Helper method to create a JSON error. |
||
| 337 | * |
||
| 338 | * @param int $errno An error number from json_last_error() |
||
| 339 | * |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | private static function handleJsonError($errno) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get the number of bytes in cryptographic strings. |
||
| 358 | * |
||
| 359 | * @param string |
||
| 360 | * |
||
| 361 | * @return int |
||
| 362 | */ |
||
| 363 | private static function safeStrlen($str) |
||
| 370 | } |
||
| 371 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: