| Conditions | 12 |
| Paths | 18 |
| Total Lines | 35 |
| 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 |
||
| 46 | private static function checkOperation(JWK $key, string $usage): bool |
||
| 47 | { |
||
| 48 | $ops = $key->get('key_ops'); |
||
| 49 | if (!is_array($ops)) { |
||
| 50 | $ops = [$ops]; |
||
| 51 | } |
||
| 52 | switch ($usage) { |
||
| 53 | case 'verification': |
||
| 54 | if (!in_array('verify', $ops)) { |
||
| 55 | throw new \InvalidArgumentException('Key cannot be used to verify a signature'); |
||
| 56 | } |
||
| 57 | |||
| 58 | return true; |
||
| 59 | case 'signature': |
||
| 60 | if (!in_array('sign', $ops)) { |
||
| 61 | throw new \InvalidArgumentException('Key cannot be used to sign'); |
||
| 62 | } |
||
| 63 | |||
| 64 | return true; |
||
| 65 | case 'encryption': |
||
| 66 | if (!in_array('encrypt', $ops) && !in_array('wrapKey', $ops)) { |
||
| 67 | throw new \InvalidArgumentException('Key cannot be used to encrypt'); |
||
| 68 | } |
||
| 69 | |||
| 70 | return true; |
||
| 71 | case 'decryption': |
||
| 72 | if (!in_array('decrypt', $ops) && !in_array('unwrapKey', $ops)) { |
||
| 73 | throw new \InvalidArgumentException('Key cannot be used to decrypt'); |
||
| 74 | } |
||
| 75 | |||
| 76 | return true; |
||
| 77 | default: |
||
| 78 | throw new \InvalidArgumentException('Unsupported key usage.'); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 126 |