| Conditions | 11 |
| Paths | 12 |
| Total Lines | 27 |
| Code Lines | 16 |
| 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 |
||
| 20 | private static function getSecretKey(): string { |
||
| 21 | $keyHex = null; |
||
| 22 | if (\defined('FILES_ACCOUNTSTORE_V1_SECRET_KEY')) { |
||
| 23 | $keyHex = (string) \constant('FILES_ACCOUNTSTORE_V1_SECRET_KEY'); |
||
| 24 | } |
||
| 25 | elseif (is_string(getenv('FILES_ACCOUNTSTORE_V1_SECRET_KEY')) && getenv('FILES_ACCOUNTSTORE_V1_SECRET_KEY') !== false) { |
||
| 26 | $keyHex = (string) getenv('FILES_ACCOUNTSTORE_V1_SECRET_KEY'); |
||
| 27 | } |
||
| 28 | |||
| 29 | if (!is_string($keyHex) || $keyHex === '') { |
||
| 30 | throw new \RuntimeException('FILES_ACCOUNTSTORE_V1_SECRET_KEY not configured. Define it as a hex-encoded key.'); |
||
| 31 | } |
||
| 32 | |||
| 33 | if (!ctype_xdigit($keyHex) || (strlen($keyHex) % 2) !== 0) { |
||
| 34 | throw new \UnexpectedValueException('FILES_ACCOUNTSTORE_V1_SECRET_KEY must be hex-encoded.'); |
||
| 35 | } |
||
| 36 | |||
| 37 | $key = hex2bin($keyHex); |
||
| 38 | if (!is_string($key) || strlen($key) !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) { |
||
|
|
|||
| 39 | throw new \UnexpectedValueException(sprintf( |
||
| 40 | 'FILES_ACCOUNTSTORE_V1_SECRET_KEY must decode to %d bytes; got %d.', |
||
| 41 | SODIUM_CRYPTO_SECRETBOX_KEYBYTES, |
||
| 42 | is_string($key) ? strlen($key) : -1 |
||
| 43 | )); |
||
| 44 | } |
||
| 45 | |||
| 46 | return $key; |
||
| 47 | } |
||
| 92 |