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