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