| Conditions | 28 |
| Paths | 32 |
| Total Lines | 100 |
| Code Lines | 68 |
| Lines | 3 |
| Ratio | 3 % |
| 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 |
||
| 37 | private function registerNewUser() |
||
| 38 | { |
||
| 39 | $settings = require('config/settings.php'); |
||
| 40 | |||
| 41 | if (empty($_POST['user_name'])) { |
||
| 42 | $this->errors[] = "Empty Username"; |
||
| 43 | } elseif (empty($_POST['user_password_new']) || empty($_POST['user_password_repeat'])) { |
||
| 44 | $this->errors[] = "Empty Password"; |
||
| 45 | } elseif ($_POST['user_password_new'] !== $_POST['user_password_repeat']) { |
||
| 46 | $this->errors[] = "Password and password repeat are not the same"; |
||
| 47 | } elseif (strlen($_POST['user_password_new']) < 6) { |
||
| 48 | $this->errors[] = "Password has a minimum length of 6 characters"; |
||
| 49 | View Code Duplication | } elseif (strlen($_POST['user_name']) > 64 || strlen($_POST['user_name']) < 2) { |
|
| 50 | $this->errors[] = "Username cannot be shorter than 2 or longer than 30 characters"; |
||
| 51 | } elseif (!preg_match('/^[a-z\d]{2,30}$/i', $_POST['user_name'])) { |
||
| 52 | $this->errors[] = "Username does not fit the name scheme: only a-Z and numbers are allowed, 2 to 64 characters"; |
||
| 53 | } elseif (empty($_POST['user_email'])) { |
||
| 54 | $this->errors[] = "Email cannot be empty"; |
||
| 55 | } elseif (strlen($_POST['user_email']) > 64) { |
||
| 56 | $this->errors[] = "Email cannot be longer than 64 characters"; |
||
| 57 | } elseif (!filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL)) { |
||
| 58 | $this->errors[] = "Your email address is not in a valid email format"; |
||
| 59 | } elseif (!empty($_POST['user_name']) |
||
| 60 | && strlen($_POST['user_name']) <= 64 |
||
| 61 | && strlen($_POST['user_name']) >= 2 |
||
| 62 | && preg_match('/^[a-z\d]{2,64}$/i', $_POST['user_name']) |
||
| 63 | && !empty($_POST['user_email']) |
||
| 64 | && strlen($_POST['user_email']) <= 64 |
||
| 65 | && filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL) |
||
| 66 | && !empty($_POST['user_password_new']) |
||
| 67 | && !empty($_POST['user_password_repeat']) |
||
| 68 | && ($_POST['user_password_new'] === $_POST['user_password_repeat']) |
||
| 69 | ) { |
||
| 70 | $temp_host = decrypt($settings['db']['host']); |
||
| 71 | $temp_user = decrypt($settings['db']['user']); |
||
| 72 | $temp_pass = decrypt($settings['db']['pass']); |
||
| 73 | $temp_name = decrypt($settings['db']['name']); |
||
| 74 | |||
| 75 | // create a database connection, using the constants from config/config.php (which we loaded in index.php) |
||
| 76 | $this->db_connection = new mysqli($temp_host, $temp_user, $temp_pass, $temp_name); |
||
| 77 | |||
| 78 | // change character set to utf8 and check it |
||
| 79 | if (!$this->db_connection->set_charset("utf8")) { |
||
| 80 | $this->errors[] = $this->db_connection->error; |
||
| 81 | } |
||
| 82 | |||
| 83 | // if no connection errors (= working database connection) |
||
| 84 | if (!$this->db_connection->connect_errno) { |
||
| 85 | |||
| 86 | // escaping, additionally removing everything that could be (html/javascript-) code |
||
| 87 | $user_name = $this->db_connection->real_escape_string(strip_tags($_POST['user_name'], ENT_QUOTES)); |
||
| 88 | $user_email = $this->db_connection->real_escape_string(strip_tags($_POST['user_email'], ENT_QUOTES)); |
||
| 89 | if(isset($_POST['player_id'])) { |
||
| 90 | $playerid = $this->db_connection->real_escape_string(strip_tags($_POST['player_id'], ENT_QUOTES)); |
||
| 91 | } |
||
| 92 | $user_password = $_POST['user_password_new']; |
||
| 93 | $user_pic = $_POST['profile_pic']; |
||
| 94 | $user_lvl = $_POST['user_lvl']; |
||
| 95 | |||
| 96 | // Ecrypt the user's password with PHP 5.5's password_hash() function, results in a 60 character |
||
| 97 | // hash string. the PASSWORD_DEFAULT constant is defined by the PHP 5.5, or if you are using |
||
| 98 | // PHP 5.3/5.4, by the password hashing compatibility library |
||
| 99 | $user_password_hash = password_hash($user_password, PASSWORD_DEFAULT); |
||
| 100 | |||
| 101 | // check if user or email address already exists |
||
| 102 | $sql = "SELECT * FROM `users` WHERE `user_name` = '" . $user_name . "' OR `user_email = '" . $user_email . "';"; |
||
| 103 | $query_check_user_name = $this->db_connection->query($sql); |
||
| 104 | |||
| 105 | if ($query_check_user_name->num_rows == 1) { |
||
| 106 | $this->errors[] = "Sorry, that username / email address is already taken."; |
||
| 107 | |||
| 108 | } else { |
||
| 109 | |||
| 110 | $permissions = include 'config/permissions.php'; |
||
| 111 | $userPerms = json_encode($permissions[$user_lvl]); |
||
| 112 | // write new user's data into database |
||
| 113 | if (!empty($playerid)) { |
||
| 114 | |||
| 115 | $sql = "INSERT INTO `users` (`user_name`, `user_password_hash`, `user_email`, `playerid`, `user_level`, `permissions`, `user_profile`) VALUES |
||
| 116 | ('" . $user_name . "', '" . $user_password_hash . "', '" . $user_email . "', '" . $playerid . "', '" . $user_lvl . "', '" . $userPerms . "', '" . $user_pic . "');"; |
||
| 117 | } else { |
||
| 118 | $sql = "INSERT INTO `users` (`user_name`, `user_password_hash`, `user_email`, `user_level`, `permissions`, `user_profile`) VALUES |
||
| 119 | ('" . $user_name . "', '" . $user_password_hash . "', '" . $user_email . "', '" . $user_lvl . "', '" . $userPerms . "', '" . $user_pic . "');"; |
||
| 120 | } |
||
| 121 | $query_new_user_insert = $this->db_connection->query($sql); |
||
| 122 | |||
| 123 | // if user has been added successfully |
||
| 124 | if ($query_new_user_insert) { |
||
| 125 | $this->messages[] = "Your account has been created"; |
||
| 126 | } else { |
||
| 127 | $this->errors[] = "Sorry, your registration failed. Please go back and try again."; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } else { |
||
| 131 | $this->errors[] = "Sorry, no database connection."; |
||
| 132 | } |
||
| 133 | } else { |
||
| 134 | $this->errors[] = "An unknown error occurred."; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.