| Conditions | 4 |
| Paths | 15 |
| Total Lines | 51 |
| Code Lines | 37 |
| 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 |
||
| 91 | private function extractKeyData(array $data): array |
||
| 92 | { |
||
| 93 | Assertion::false(!array_key_exists('registrationData', $data) || !\is_string($data['registrationData']), 'Invalid response.'); |
||
| 94 | $stream = \Safe\fopen('php://memory', 'r+'); |
||
| 95 | $registrationData = Base64Url::decode($data['registrationData']); |
||
| 96 | \Safe\fwrite($stream, $registrationData); |
||
| 97 | \Safe\rewind($stream); |
||
| 98 | |||
| 99 | $reservedByte = \Safe\fread($stream, 1); |
||
| 100 | try { |
||
| 101 | // 1 byte reserved with value x05 |
||
| 102 | Assertion::eq("\x05", $reservedByte, 'Bad reserved byte.'); |
||
| 103 | |||
| 104 | $publicKey = \Safe\fread($stream, self::PUBLIC_KEY_LENGTH); // 65 bytes for the public key |
||
| 105 | Assertion::eq(self::PUBLIC_KEY_LENGTH, mb_strlen($publicKey, '8bit'), 'Bad public key length.'); |
||
| 106 | |||
| 107 | $keyHandleLength = \Safe\fread($stream, 1); // 1 byte for the key handle length |
||
| 108 | Assertion::notEq(0, \ord($keyHandleLength), 'Bad key handle length.'); |
||
| 109 | |||
| 110 | $keyHandle = \Safe\fread($stream, \ord($keyHandleLength)); // x bytes for the key handle |
||
| 111 | Assertion::eq(mb_strlen($keyHandle, '8bit'), \ord($keyHandleLength), 'Bad key handle.'); |
||
| 112 | |||
| 113 | $certHeader = \Safe\fread($stream, 4); // 4 bytes for the certificate header |
||
| 114 | Assertion::eq(4, mb_strlen($certHeader, '8bit'), 'Bad certificate header.'); |
||
| 115 | |||
| 116 | $highOrder = \ord($certHeader[2]) << 8; |
||
| 117 | $lowOrder = \ord($certHeader[3]); |
||
| 118 | $certLength = $highOrder + $lowOrder; |
||
| 119 | $certBody = \Safe\fread($stream, $certLength); // x bytes for the certificate |
||
| 120 | Assertion::eq(mb_strlen($certBody, '8bit'), $certLength, 'Bad certificate.'); |
||
| 121 | } catch (\Throwable $throwable) { |
||
| 122 | \Safe\fclose($stream); |
||
| 123 | throw $throwable; |
||
| 124 | } |
||
| 125 | |||
| 126 | $derCertificate = $this->unusedBytesFix($certHeader.$certBody); |
||
| 127 | $pemCert = '-----BEGIN CERTIFICATE-----'.PHP_EOL; |
||
| 128 | $pemCert .= chunk_split(base64_encode($derCertificate), 64, PHP_EOL); |
||
| 129 | $pemCert .= '-----END CERTIFICATE-----'.PHP_EOL; |
||
| 130 | |||
| 131 | $signature = ''; // The rest is the signature |
||
| 132 | while (!feof($stream)) { |
||
| 133 | $signature .= \Safe\fread($stream, 1024); |
||
| 134 | } |
||
| 135 | \Safe\fclose($stream); |
||
| 136 | |||
| 137 | return [ |
||
| 138 | new PublicKey($publicKey), |
||
| 139 | new KeyHandler($keyHandle), |
||
| 140 | $pemCert, |
||
| 141 | $signature, |
||
| 142 | ]; |
||
| 180 |