| Conditions | 17 |
| Paths | 3456 |
| Total Lines | 125 |
| Code Lines | 71 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 204 | public static function checkConfiguration(array $fields) |
||
| 205 | { |
||
| 206 | $errors = array(); |
||
| 207 | |||
| 208 | // Testing the database connection |
||
| 209 | try { |
||
| 210 | Symphony::Database()->connect( |
||
| 211 | $fields['database']['host'], |
||
| 212 | $fields['database']['user'], |
||
| 213 | $fields['database']['password'], |
||
| 214 | $fields['database']['port'], |
||
| 215 | $fields['database']['db'] |
||
| 216 | ); |
||
| 217 | } catch (DatabaseException $e) { |
||
| 218 | // Invalid credentials |
||
| 219 | // @link http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html |
||
| 220 | if ($e->getDatabaseErrorCode() === 1044 || $e->getDatabaseErrorCode() === 1045) { |
||
| 221 | $errors['database-invalid-credentials'] = array( |
||
| 222 | 'msg' => 'Database credentials were denied', |
||
| 223 | 'details' => __('Symphony was unable to access the database with these credentials.') |
||
| 224 | ); |
||
| 225 | } // Connection related |
||
| 226 | else { |
||
| 227 | $errors['no-database-connection'] = array( |
||
| 228 | 'msg' => 'Could not establish database connection.', |
||
| 229 | 'details' => __('Symphony was unable to establish a valid database connection. You may need to modify host or port settings.') |
||
| 230 | ); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | try { |
||
| 235 | // Check the database table prefix is legal. #1815 |
||
| 236 | if (!preg_match('/^[0-9a-zA-Z\$_]*$/', $fields['database']['tbl_prefix'])) { |
||
| 237 | $errors['database-table-prefix'] = array( |
||
| 238 | 'msg' => 'Invalid database table prefix: ‘' . $fields['database']['tbl_prefix'] . '’', |
||
| 239 | 'details' => __('The table prefix %s is invalid. The table prefix must only contain numbers, letters or underscore characters.', |
||
| 240 | array('<code>' . $fields['database']['tbl_prefix'] . '</code>')) |
||
| 241 | ); |
||
| 242 | } // Check the database credentials |
||
| 243 | elseif (Symphony::Database()->isConnected()) { |
||
| 244 | // Incorrect MySQL version |
||
| 245 | $version = Symphony::Database()->fetchVar('version', 0, "SELECT VERSION() AS `version`;"); |
||
| 246 | if (version_compare($version, '5.5', '<')) { |
||
| 247 | $errors['database-incorrect-version'] = array( |
||
| 248 | 'msg' => 'MySQL Version is not correct. ' . $version . ' detected.', |
||
| 249 | 'details' => __('Symphony requires %1$s or greater to work, however version %2$s was detected. This requirement must be met before installation can proceed.', |
||
| 250 | array('<code>MySQL 5.5</code>', '<code>' . $version . '</code>')) |
||
| 251 | ); |
||
| 252 | } else { |
||
| 253 | // Existing table prefix |
||
| 254 | if (Symphony::Database()->tableExists($fields['database']['tbl_prefix'] . '%')) { |
||
| 255 | $errors['database-table-prefix'] = array( |
||
| 256 | 'msg' => 'Database table prefix clash with ‘' . $fields['database']['db'] . '’', |
||
| 257 | 'details' => __('The table prefix %s is already in use. Please choose a different prefix to use with Symphony.', |
||
| 258 | array( |
||
| 259 | |||
| 260 | '<code>' . $fields['database']['tbl_prefix'] . '</code>' |
||
| 261 | )) |
||
| 262 | ); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } catch (DatabaseException $e) { |
||
| 267 | $errors['unknown-database'] = array( |
||
| 268 | 'msg' => 'Database ‘' . $fields['database']['db'] . '’ not found.', |
||
| 269 | 'details' => __('Symphony was unable to connect to the specified database.') |
||
| 270 | ); |
||
| 271 | } |
||
| 272 | |||
| 273 | // Website name not entered |
||
| 274 | if (trim($fields['general']['sitename']) === '') { |
||
| 275 | $errors['general-no-sitename'] = array( |
||
| 276 | 'msg' => 'No sitename entered.', |
||
| 277 | 'details' => __('You must enter a Site name. This will be shown at the top of your backend.') |
||
| 278 | ); |
||
| 279 | } |
||
| 280 | |||
| 281 | // Username Not Entered |
||
| 282 | if (trim($fields['user']['username']) === '') { |
||
| 283 | $errors['user-no-username'] = array( |
||
| 284 | 'msg' => 'No username entered.', |
||
| 285 | 'details' => __('You must enter a Username. This will be your Symphony login information.') |
||
| 286 | ); |
||
| 287 | } |
||
| 288 | |||
| 289 | // Password Not Entered |
||
| 290 | if (trim($fields['user']['password']) === '') { |
||
| 291 | $errors['user-no-password'] = array( |
||
| 292 | 'msg' => 'No password entered.', |
||
| 293 | 'details' => __('You must enter a Password. This will be your Symphony login information.') |
||
| 294 | ); |
||
| 295 | } // Password mismatch |
||
| 296 | elseif ($fields['user']['password'] !== $fields['user']['confirm-password']) { |
||
| 297 | $errors['user-password-mismatch'] = array( |
||
| 298 | 'msg' => 'Passwords did not match.', |
||
| 299 | 'details' => __('The password and confirmation did not match. Please retype your password.') |
||
| 300 | ); |
||
| 301 | } |
||
| 302 | |||
| 303 | // No Name entered |
||
| 304 | if (trim($fields['user']['firstname']) === '' || trim($fields['user']['lastname']) === '') { |
||
| 305 | $errors['user-no-name'] = array( |
||
| 306 | 'msg' => 'Did not enter First and Last names.', |
||
| 307 | 'details' => __('You must enter your name.') |
||
| 308 | ); |
||
| 309 | } |
||
| 310 | |||
| 311 | // Invalid Email |
||
| 312 | if (!preg_match('/^\w(?:\.?[\w%+-]+)*@\w(?:[\w-]*\.)+?[a-z]{2,}$/i', $fields['user']['email'])) { |
||
| 313 | $errors['user-invalid-email'] = array( |
||
| 314 | 'msg' => 'Invalid email address supplied.', |
||
| 315 | 'details' => __('This is not a valid email address. You must provide an email address since you will need it if you forget your password.') |
||
| 316 | ); |
||
| 317 | } |
||
| 318 | |||
| 319 | // Admin path not entered |
||
| 320 | if (trim($fields['symphony']['admin-path']) === '') { |
||
| 321 | $errors['no-symphony-path'] = array( |
||
| 322 | 'msg' => 'No Symphony path entered.', |
||
| 323 | 'details' => __('You must enter a path for accessing Symphony, or leave the default. This will be used to access Symphony\'s backend.') |
||
| 324 | ); |
||
| 325 | } |
||
| 326 | |||
| 327 | return $errors; |
||
| 328 | } |
||
| 329 | |||
| 414 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: