| 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 |
||
| 158 | private function extractKeyData(array $data): array |
||
| 159 | { |
||
| 160 | if (!array_key_exists('registrationData', $data) || !is_string($data['registrationData'])) { |
||
| 161 | throw new \InvalidArgumentException('Invalid response.'); |
||
| 162 | } |
||
| 163 | $stream = fopen('php://memory', 'r+'); |
||
| 164 | if (false === $stream) { |
||
| 165 | throw new \InvalidArgumentException('Unable to load the registration data.'); |
||
| 166 | } |
||
| 167 | $registrationData = Base64Url::decode($data['registrationData']); |
||
| 168 | fwrite($stream, $registrationData); |
||
| 169 | rewind($stream); |
||
| 170 | |||
| 171 | $reservedByte = fread($stream, 1); |
||
| 172 | if ("\x05" !== $reservedByte) { // 1 byte reserved with value x05 |
||
| 173 | fclose($stream); |
||
| 174 | |||
| 175 | throw new \InvalidArgumentException('Bad reserved byte.'); |
||
| 176 | } |
||
| 177 | |||
| 178 | $publicKey = fread($stream, self::PUBLIC_KEY_LENGTH); // 65 bytes for the public key |
||
| 179 | if (mb_strlen($publicKey, '8bit') !== self::PUBLIC_KEY_LENGTH) { |
||
| 180 | fclose($stream); |
||
| 181 | |||
| 182 | throw new \InvalidArgumentException('Bad public key length.'); |
||
| 183 | } |
||
| 184 | |||
| 185 | $keyHandleLength = fread($stream, 1); // 1 byte for the key handle length |
||
| 186 | if (ord($keyHandleLength) === 0) { |
||
| 187 | fclose($stream); |
||
| 188 | |||
| 189 | throw new \InvalidArgumentException('Bad key handle length.'); |
||
| 190 | } |
||
| 191 | |||
| 192 | $keyHandle = fread($stream, ord($keyHandleLength)); // x bytes for the key handle |
||
| 193 | if (mb_strlen($keyHandle, '8bit') !== ord($keyHandleLength)) { |
||
| 194 | fclose($stream); |
||
| 195 | |||
| 196 | throw new \InvalidArgumentException('Bad key handle.'); |
||
| 197 | } |
||
| 198 | |||
| 199 | $certHeader = fread($stream, 4); // 4 bytes for the certificate header |
||
| 200 | if (mb_strlen($certHeader, '8bit') !== 4) { |
||
| 201 | fclose($stream); |
||
| 202 | |||
| 203 | throw new \InvalidArgumentException('Bad certificate header.'); |
||
| 204 | } |
||
| 205 | |||
| 206 | $highOrder = ord($certHeader[2]) << 8; |
||
| 207 | $lowOrder = ord($certHeader[3]); |
||
| 208 | $certLength = $highOrder + $lowOrder; |
||
| 209 | $certBody = fread($stream, $certLength); // x bytes for the certificate |
||
| 210 | if (mb_strlen($certBody, '8bit') !== $certLength) { |
||
| 211 | fclose($stream); |
||
| 212 | |||
| 213 | throw new \InvalidArgumentException('Bad certificate.'); |
||
| 214 | } |
||
| 215 | $derCertificate = $this->unusedBytesFix($certHeader.$certBody); |
||
| 216 | $pemCert = '-----BEGIN CERTIFICATE-----'.PHP_EOL; |
||
| 217 | $pemCert .= chunk_split(base64_encode($derCertificate), 64, PHP_EOL); |
||
| 218 | $pemCert .= '-----END CERTIFICATE-----'.PHP_EOL; |
||
| 219 | |||
| 220 | $signature = ''; // The rest is the signature |
||
| 221 | while (!feof($stream)) { |
||
| 222 | $signature .= fread($stream, 1024); |
||
| 223 | } |
||
| 224 | fclose($stream); |
||
| 225 | |||
| 226 | return [ |
||
| 227 | PublicKey::create($publicKey), |
||
| 228 | KeyHandle::create($keyHandle), |
||
| 229 | $pemCert, |
||
| 230 | $signature, |
||
| 231 | ]; |
||
| 277 |