| Conditions | 12 |
| Paths | 19 |
| Total Lines | 74 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 163 | protected function login(string $username, string $password): array |
||
| 164 | { |
||
| 165 | $db = $this->connect(); |
||
| 166 | $params = ['username' => $username, 'password' => $password]; |
||
| 167 | $attributes = []; |
||
| 168 | |||
| 169 | $numQueries = count($this->query); |
||
| 170 | for ($x = 0; $x < $numQueries; $x++) { |
||
| 171 | try { |
||
| 172 | $sth = $db->prepare($this->query[$x]); |
||
| 173 | } catch (PDOException $e) { |
||
| 174 | throw new Exception('sqlauth:' . $this->authId . |
||
| 175 | ': - Failed to prepare query: ' . $e->getMessage()); |
||
| 176 | } |
||
| 177 | |||
| 178 | try { |
||
| 179 | $sth->execute($params); |
||
| 180 | } catch (PDOException $e) { |
||
| 181 | throw new Exception('sqlauth:' . $this->authId . |
||
| 182 | ': - Failed to execute query: ' . $e->getMessage()); |
||
| 183 | } |
||
| 184 | |||
| 185 | try { |
||
| 186 | $data = $sth->fetchAll(PDO::FETCH_ASSOC); |
||
| 187 | } catch (PDOException $e) { |
||
| 188 | throw new Exception('sqlauth:' . $this->authId . |
||
| 189 | ': - Failed to fetch result set: ' . $e->getMessage()); |
||
| 190 | } |
||
| 191 | |||
| 192 | Logger::info('sqlauth:' . $this->authId . ': Got ' . count($data) . |
||
| 193 | ' rows from database'); |
||
| 194 | |||
| 195 | if ($x === 0) { |
||
| 196 | if (count($data) === 0) { |
||
| 197 | // No rows returned from first query - invalid username/password |
||
| 198 | Logger::error('sqlauth:' . $this->authId . |
||
| 199 | ': No rows in result set. Probably wrong username/password.'); |
||
| 200 | throw new Error\Error('WRONGUSERPASS'); |
||
| 201 | } |
||
| 202 | /* Only the first query should be passed the password, as that is the only |
||
| 203 | * one used for authentication. Subsequent queries are only used for |
||
| 204 | * getting attribute lists, so only need the username. */ |
||
| 205 | unset($params['password']); |
||
| 206 | } |
||
| 207 | |||
| 208 | /* Extract attributes. We allow the resultset to consist of multiple rows. Attributes |
||
| 209 | * which are present in more than one row will become multivalued. null values and |
||
| 210 | * duplicate values will be skipped. All values will be converted to strings. |
||
| 211 | */ |
||
| 212 | foreach ($data as $row) { |
||
| 213 | foreach ($row as $name => $value) { |
||
| 214 | if ($value === null) { |
||
| 215 | continue; |
||
| 216 | } |
||
| 217 | |||
| 218 | $value = (string) $value; |
||
| 219 | |||
| 220 | if (!array_key_exists($name, $attributes)) { |
||
| 221 | $attributes[$name] = []; |
||
| 222 | } |
||
| 223 | |||
| 224 | if (in_array($value, $attributes[$name], true)) { |
||
| 225 | // Value already exists in attribute |
||
| 226 | continue; |
||
| 227 | } |
||
| 228 | |||
| 229 | $attributes[$name][] = $value; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | Logger::info('sqlauth:' . $this->authId . ': Attributes: ' . implode(',', array_keys($attributes))); |
||
| 235 | |||
| 236 | return $attributes; |
||
| 237 | } |
||
| 239 |