| Conditions | 10 |
| Paths | 10 |
| Total Lines | 33 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 73 | public static function verify($token, $signature, $key, $algorithm = 'HS256') |
||
| 74 | { |
||
| 75 | $segments = explode('.', $token); |
||
| 76 | $segments = array_map('trim', $segments); |
||
| 77 | |||
| 78 | if (count($segments) == 3) { |
||
| 79 | array_pop($segments); |
||
| 80 | $data = implode('.', $segments); |
||
| 81 | |||
| 82 | if (Algorithm::validate($algorithm)) { |
||
| 83 | list($function, $algorithm) = Algorithm::map($algorithm); |
||
| 84 | |||
| 85 | switch ($function) { |
||
| 86 | case 'HMAC': |
||
| 87 | return Hmac::hash($algorithm, $data, $key, true) === $signature; |
||
| 88 | case 'hash_hmac': |
||
| 89 | return hash_hmac($algorithm, $data, $key, true) === $signature; |
||
| 90 | case 'openssl': |
||
| 91 | switch ($algorithm) { |
||
| 92 | case 'RS256': |
||
| 93 | return (bool)openssl_verify($data, $signature, $key, OPENSSL_ALGO_SHA1); |
||
| 94 | case 'RS256': |
||
| 95 | return (bool)openssl_verify($data, $signature, $key, OPENSSL_ALGO_SHA256); |
||
| 96 | case 'RS384': |
||
| 97 | return (bool)openssl_verify($data, $signature, $key, OPENSSL_ALGO_SHA384); |
||
| 98 | case 'RS512': |
||
| 99 | return (bool)openssl_verify($data, $signature, $key, OPENSSL_ALGO_SHA512); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | return false; |
||
| 106 | } |
||
| 107 | } |