| Conditions | 11 | 
| Paths | 10 | 
| Total Lines | 73 | 
| Code Lines | 48 | 
| 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 | ||
| 107 | private function extractKeyData(array $data): array | ||
| 108 |     { | ||
| 109 |         if (!array_key_exists('registrationData', $data) || !\is_string($data['registrationData'])) { | ||
| 110 |             throw new \InvalidArgumentException('Invalid response.'); | ||
| 111 | } | ||
| 112 |         $stream = fopen('php://memory', 'r+'); | ||
| 113 |         if (false === $stream) { | ||
| 114 |             throw new \InvalidArgumentException('Unable to load the registration data.'); | ||
| 115 | } | ||
| 116 | $registrationData = Base64Url::decode($data['registrationData']); | ||
| 117 | fwrite($stream, $registrationData); | ||
| 118 | rewind($stream); | ||
| 119 | |||
| 120 | $reservedByte = fread($stream, 1); | ||
| 121 |         if ("\x05" !== $reservedByte) { // 1 byte reserved with value x05 | ||
| 122 | fclose($stream); | ||
| 123 | |||
| 124 |             throw new \InvalidArgumentException('Bad reserved byte.'); | ||
| 125 | } | ||
| 126 | |||
| 127 | $publicKey = fread($stream, self::PUBLIC_KEY_LENGTH); // 65 bytes for the public key | ||
| 128 |         if (self::PUBLIC_KEY_LENGTH !== mb_strlen($publicKey, '8bit')) { | ||
| 129 | fclose($stream); | ||
| 130 | |||
| 131 |             throw new \InvalidArgumentException('Bad public key length.'); | ||
| 132 | } | ||
| 133 | |||
| 134 | $keyHandleLength = fread($stream, 1); // 1 byte for the key handle length | ||
| 135 |         if (0 === \ord($keyHandleLength)) { | ||
| 136 | fclose($stream); | ||
| 137 | |||
| 138 |             throw new \InvalidArgumentException('Bad key handle length.'); | ||
| 139 | } | ||
| 140 | |||
| 141 | $keyHandle = fread($stream, \ord($keyHandleLength)); // x bytes for the key handle | ||
| 142 |         if (mb_strlen($keyHandle, '8bit') !== \ord($keyHandleLength)) { | ||
| 143 | fclose($stream); | ||
| 144 | |||
| 145 |             throw new \InvalidArgumentException('Bad key handle.'); | ||
| 146 | } | ||
| 147 | |||
| 148 | $certHeader = fread($stream, 4); // 4 bytes for the certificate header | ||
| 149 |         if (4 !== mb_strlen($certHeader, '8bit')) { | ||
| 150 | fclose($stream); | ||
| 151 | |||
| 152 |             throw new \InvalidArgumentException('Bad certificate header.'); | ||
| 153 | } | ||
| 154 | |||
| 155 | $highOrder = \ord($certHeader[2]) << 8; | ||
| 156 | $lowOrder = \ord($certHeader[3]); | ||
| 157 | $certLength = $highOrder + $lowOrder; | ||
| 158 | $certBody = fread($stream, $certLength); // x bytes for the certificate | ||
| 159 |         if (mb_strlen($certBody, '8bit') !== $certLength) { | ||
| 160 | fclose($stream); | ||
| 161 | |||
| 162 |             throw new \InvalidArgumentException('Bad certificate.'); | ||
| 163 | } | ||
| 164 | $derCertificate = $this->unusedBytesFix($certHeader.$certBody); | ||
| 165 | $pemCert = '-----BEGIN CERTIFICATE-----'.PHP_EOL; | ||
| 166 | $pemCert .= chunk_split(base64_encode($derCertificate), 64, PHP_EOL); | ||
| 167 | $pemCert .= '-----END CERTIFICATE-----'.PHP_EOL; | ||
| 168 | |||
| 169 | $signature = ''; // The rest is the signature | ||
| 170 |         while (!feof($stream)) { | ||
| 171 | $signature .= fread($stream, 1024); | ||
| 172 | } | ||
| 173 | fclose($stream); | ||
| 174 | |||
| 175 | return [ | ||
| 176 | new PublicKey($publicKey), | ||
| 177 | new KeyHandler($keyHandle), | ||
| 178 | $pemCert, | ||
| 179 | $signature, | ||
| 180 | ]; | ||
| 218 |