| Conditions | 3 |
| Paths | 4 |
| Total Lines | 55 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 27 | public static function generateCsr( |
||
| 28 | array $domains, |
||
| 29 | OpenSSLAsymmetricKey $privateKey, |
||
| 30 | bool $isAssociative = false |
||
| 31 | ): string { |
||
| 32 | if ($isAssociative) { |
||
| 33 | $san = []; |
||
| 34 | |||
| 35 | self::extractKey($domains, $san, 'dns', 'DNS'); |
||
| 36 | self::extractKey($domains, $san, 'ip', 'IP Address'); |
||
| 37 | |||
| 38 | $san = implode(',', $san); |
||
| 39 | |||
| 40 | $dn = implode(',', array_map(function ($key, $value) { |
||
| 41 | return $key . ':' . $value; |
||
| 42 | }, $domains)); |
||
| 43 | } else { |
||
| 44 | $dn = ['commonName' => $domains[0]]; |
||
| 45 | |||
| 46 | $san = implode(',', array_map(function ($dns) { |
||
| 47 | return 'DNS:' . $dns; |
||
| 48 | }, $domains)); |
||
| 49 | } |
||
| 50 | |||
| 51 | $tempFile = tmpfile(); |
||
| 52 | |||
| 53 | fwrite( |
||
| 54 | $tempFile, |
||
| 55 | 'HOME = . |
||
| 56 | RANDFILE = $ENV::HOME/.rnd |
||
| 57 | [ req ] |
||
| 58 | default_bits = 4096 |
||
| 59 | default_keyfile = privkey.pem |
||
| 60 | distinguished_name = req_distinguished_name |
||
| 61 | req_extensions = v3_req |
||
| 62 | [ req_distinguished_name ] |
||
| 63 | countryName = Country Name (2 letter code) |
||
| 64 | [ v3_req ] |
||
| 65 | basicConstraints = CA:FALSE |
||
| 66 | subjectAltName = ' . $san . ' |
||
| 67 | keyUsage = nonRepudiation, digitalSignature, keyEncipherment' |
||
| 68 | ); |
||
| 69 | |||
| 70 | $csr = openssl_csr_new($dn, $privateKey, [ |
||
|
|
|||
| 71 | 'digest_alg' => 'sha256', |
||
| 72 | 'config' => stream_get_meta_data($tempFile)['uri'], |
||
| 73 | ]); |
||
| 74 | |||
| 75 | fclose($tempFile); |
||
| 76 | |||
| 77 | if (!openssl_csr_export($csr, $out)) { |
||
| 78 | throw new LetsEncryptClientException('Exporting CSR failed.'); |
||
| 79 | } |
||
| 80 | |||
| 81 | return trim($out); |
||
| 82 | } |
||
| 94 |