| Conditions | 16 |
| Paths | 44 |
| Total Lines | 80 |
| 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 |
||
| 30 | protected function createConnection($databaseConfig, &$error) |
||
| 31 | { |
||
| 32 | $error = null; |
||
| 33 | try { |
||
| 34 | switch ($databaseConfig['type']) { |
||
| 35 | case 'MySQLDatabase': |
||
|
|
|||
| 36 | $conn = mysqli_init(); |
||
| 37 | |||
| 38 | // Set SSL parameters if they exist. All parameters are required. |
||
| 39 | if (array_key_exists('ssl_key', $databaseConfig) && |
||
| 40 | array_key_exists('ssl_cert', $databaseConfig) && |
||
| 41 | array_key_exists('ssl_ca', $databaseConfig) |
||
| 42 | ) { |
||
| 43 | $conn->ssl_set( |
||
| 44 | $databaseConfig['ssl_key'], |
||
| 45 | $databaseConfig['ssl_cert'], |
||
| 46 | $databaseConfig['ssl_ca'], |
||
| 47 | dirname($databaseConfig['ssl_ca']), |
||
| 48 | array_key_exists('ssl_cipher', $databaseConfig) |
||
| 49 | ? $databaseConfig['ssl_cipher'] |
||
| 50 | : Config::inst()->get(MySQLiConnector::class, 'ssl_cipher_default') |
||
| 51 | ); |
||
| 52 | } |
||
| 53 | @$conn->real_connect( |
||
| 54 | $databaseConfig['server'], |
||
| 55 | $databaseConfig['username'], |
||
| 56 | $databaseConfig['password'] |
||
| 57 | ); |
||
| 58 | |||
| 59 | if ($conn && empty($conn->connect_errno)) { |
||
| 60 | $conn->query("SET sql_mode = 'ANSI'"); |
||
| 61 | return $conn; |
||
| 62 | } else { |
||
| 63 | $error = ($conn->connect_errno) |
||
| 64 | ? $conn->connect_error |
||
| 65 | : 'Unknown connection error'; |
||
| 66 | return null; |
||
| 67 | } |
||
| 68 | case 'MySQLPDODatabase': |
||
| 69 | // May throw a PDOException if fails |
||
| 70 | |||
| 71 | // Set SSL parameters |
||
| 72 | $ssl = null; |
||
| 73 | |||
| 74 | if (array_key_exists('ssl_key', $databaseConfig) && |
||
| 75 | array_key_exists('ssl_cert', $databaseConfig) |
||
| 76 | ) { |
||
| 77 | $ssl = array( |
||
| 78 | PDO::MYSQL_ATTR_SSL_KEY => $databaseConfig['ssl_key'], |
||
| 79 | PDO::MYSQL_ATTR_SSL_CERT => $databaseConfig['ssl_cert'], |
||
| 80 | ); |
||
| 81 | if (array_key_exists('ssl_ca', $databaseConfig)) { |
||
| 82 | $ssl[PDO::MYSQL_ATTR_SSL_CA] = $databaseConfig['ssl_ca']; |
||
| 83 | } |
||
| 84 | // use default cipher if not provided |
||
| 85 | $ssl[PDO::MYSQL_ATTR_SSL_CA] = array_key_exists('ssl_ca', $databaseConfig) |
||
| 86 | ? $databaseConfig['ssl_ca'] |
||
| 87 | : Config::inst()->get(PDOConnector::class, 'ssl_cipher_default'); |
||
| 88 | } |
||
| 89 | |||
| 90 | $conn = @new PDO( |
||
| 91 | 'mysql:host=' . $databaseConfig['server'], |
||
| 92 | $databaseConfig['username'], |
||
| 93 | $databaseConfig['password'], |
||
| 94 | $ssl |
||
| 95 | ); |
||
| 96 | if ($conn) { |
||
| 97 | $conn->query("SET sql_mode = 'ANSI'"); |
||
| 98 | return $conn; |
||
| 99 | } else { |
||
| 100 | $error = 'Unknown connection error'; |
||
| 101 | return null; |
||
| 102 | } |
||
| 103 | default: |
||
| 104 | $error = 'Invalid connection type: ' . $databaseConfig['type']; |
||
| 105 | return null; |
||
| 106 | } |
||
| 107 | } catch (Exception $ex) { |
||
| 108 | $error = $ex->getMessage(); |
||
| 109 | return null; |
||
| 110 | } |
||
| 306 |