| Conditions | 13 |
| Paths | 21 |
| Total Lines | 115 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 131 | protected function login(string $username, string $password): array |
||
| 132 | { |
||
| 133 | $db = $this->connect(); |
||
| 134 | |||
| 135 | try { |
||
| 136 | $sth = $db->prepare($this->query); |
||
| 137 | } catch (PDOException $e) { |
||
| 138 | throw new Exception(sprintf( |
||
| 139 | 'sqlauth:%s: - Failed to prepare query: %s', |
||
| 140 | $this->authId, |
||
| 141 | $e->getMessage(), |
||
| 142 | )); |
||
| 143 | } |
||
| 144 | |||
| 145 | |||
| 146 | try { |
||
| 147 | $sth->execute(['username' => $username]); |
||
| 148 | } catch (PDOException $e) { |
||
| 149 | throw new Exception(sprintf( |
||
| 150 | 'sqlauth:%s: - Failed to execute sql: %s query: %s', |
||
| 151 | $this->authId, |
||
| 152 | $this->query, |
||
| 153 | $e->getMessage(), |
||
| 154 | )); |
||
| 155 | } |
||
| 156 | |||
| 157 | try { |
||
| 158 | $data = $sth->fetchAll(PDO::FETCH_ASSOC); |
||
| 159 | } catch (PDOException $e) { |
||
| 160 | throw new Exception(sprintf( |
||
| 161 | 'sqlauth:%s: - Failed to fetch result set: %s', |
||
| 162 | $this->authId, |
||
| 163 | $e->getMessage(), |
||
| 164 | )); |
||
| 165 | } |
||
| 166 | |||
| 167 | Logger::info(sprintf( |
||
| 168 | 'sqlauth:%s : Got %d rows from database', |
||
| 169 | $this->authId, |
||
| 170 | count($data), |
||
| 171 | )); |
||
| 172 | |||
| 173 | if (count($data) === 0) { |
||
| 174 | // No rows returned - invalid username/password |
||
| 175 | Logger::error(sprintf( |
||
| 176 | 'sqlauth:%s: No rows in result set. Probably wrong username/password.', |
||
| 177 | $this->authId, |
||
| 178 | )); |
||
| 179 | throw new Error\Error('WRONGUSERPASS'); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Sanity check, passwordhash must be in each resulting tuple and must have |
||
| 184 | * the same value in every tuple. |
||
| 185 | * |
||
| 186 | * Note that $pwhash will contain the passwordhash value after this loop. |
||
| 187 | */ |
||
| 188 | $pwhash = null; |
||
| 189 | foreach ($data as $row) { |
||
| 190 | if ( |
||
| 191 | !array_key_exists($this->passwordhashcolumn, $row) |
||
| 192 | || is_null($row[$this->passwordhashcolumn]) |
||
| 193 | ) { |
||
| 194 | Logger::error(sprintf( |
||
| 195 | 'sqlauth:%s: column %s must be in every result tuple.', |
||
| 196 | $this->authId, |
||
| 197 | $this->passwordhashcolumn, |
||
| 198 | )); |
||
| 199 | throw new Error\Error('WRONGUSERPASS'); |
||
| 200 | } |
||
| 201 | if ($pwhash) { |
||
| 202 | if ($pwhash !== $row[$this->passwordhashcolumn]) { |
||
| 203 | Logger::error(sprintf( |
||
| 204 | 'sqlauth:%s: column %s must be THE SAME in every result tuple.', |
||
| 205 | $this->authId, |
||
| 206 | $this->passwordhashcolumn, |
||
| 207 | )); |
||
| 208 | throw new Error\Error('WRONGUSERPASS'); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | $pwhash = $row[$this->passwordhashcolumn]; |
||
| 212 | } |
||
| 213 | /** |
||
| 214 | * This should never happen as the count(data) test above would have already thrown. |
||
| 215 | * But checking twice doesn't hurt. |
||
| 216 | */ |
||
| 217 | if (is_null($pwhash)) { |
||
| 218 | if ($pwhash !== $row[$this->passwordhashcolumn]) { |
||
|
|
|||
| 219 | Logger::error(sprintf( |
||
| 220 | 'sqlauth:%s: column %s does not contain a password hash.', |
||
| 221 | $this->authId, |
||
| 222 | $this->passwordhashcolumn, |
||
| 223 | )); |
||
| 224 | throw new Error\Error('WRONGUSERPASS'); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * VERIFICATION! |
||
| 230 | * Now to check if the password the user supplied is actually valid |
||
| 231 | */ |
||
| 232 | if (!password_verify($password, $pwhash)) { |
||
| 233 | Logger::error(sprintf('sqlauth:%s: password is incorrect.', $this->authId)); |
||
| 234 | throw new Error\Error('WRONGUSERPASS'); |
||
| 235 | } |
||
| 236 | |||
| 237 | $attributes = $this->extractAttributes($data, [$this->passwordhashcolumn]); |
||
| 238 | |||
| 239 | Logger::info(sprintf( |
||
| 240 | 'sqlauth:%s: Attributes: %s', |
||
| 241 | $this->authId, |
||
| 242 | implode(',', array_keys($attributes)), |
||
| 243 | )); |
||
| 244 | |||
| 245 | return $attributes; |
||
| 246 | } |
||
| 248 |