| Conditions | 10 |
| Paths | 9 |
| Total Lines | 38 |
| Code Lines | 20 |
| 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 |
||
| 35 | public function __construct($p12, $password) |
||
| 36 | { |
||
| 37 | if (!function_exists('openssl_x509_read')) { |
||
| 38 | throw new Google_Exception( |
||
| 39 | 'The Google PHP API library needs the openssl PHP extension' |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | |||
| 43 | // If the private key is provided directly, then this isn't in the p12 |
||
| 44 | // format. Different versions of openssl support different p12 formats |
||
| 45 | // and the key from google wasn't being accepted by the version available |
||
| 46 | // at the time. |
||
| 47 | if (!$password && strpos($p12, "-----BEGIN RSA PRIVATE KEY-----") !== false) { |
||
| 48 | $this->privateKey = openssl_pkey_get_private($p12); |
||
| 49 | } elseif ($password === 'notasecret' && strpos($p12, "-----BEGIN PRIVATE KEY-----") !== false) { |
||
| 50 | $this->privateKey = openssl_pkey_get_private($p12); |
||
| 51 | } else { |
||
| 52 | // This throws on error |
||
| 53 | $certs = array(); |
||
| 54 | if (!openssl_pkcs12_read($p12, $certs, $password)) { |
||
| 55 | throw new Google_Auth_Exception( |
||
| 56 | "Unable to parse the p12 file. " . |
||
| 57 | "Is this a .p12 file? Is the password correct? OpenSSL error: " . |
||
| 58 | openssl_error_string() |
||
| 59 | ); |
||
| 60 | } |
||
| 61 | // TODO(beaton): is this part of the contract for the openssl_pkcs12_read |
||
| 62 | // method? What happens if there are multiple private keys? Do we care? |
||
| 63 | if (!array_key_exists("pkey", $certs) || !$certs["pkey"]) { |
||
| 64 | throw new Google_Auth_Exception("No private key found in p12 file."); |
||
| 65 | } |
||
| 66 | $this->privateKey = openssl_pkey_get_private($certs['pkey']); |
||
| 67 | } |
||
| 68 | |||
| 69 | if (!$this->privateKey) { |
||
| 70 | throw new Google_Auth_Exception("Unable to load private key"); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 95 |