| Conditions | 11 |
| Paths | 7 |
| Total Lines | 71 |
| Code Lines | 42 |
| 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 |
||
| 45 | public function getUserAuth(string $login, string $password, string $apikey): array |
||
| 46 | { |
||
| 47 | // Sanitize |
||
| 48 | include_once API_ROOT_PATH . '/../sources/main.functions.php'; |
||
| 49 | $inputData = dataSanitizer( |
||
| 50 | [ |
||
| 51 | 'login' => isset($login) === true ? $login : '', |
||
| 52 | 'password' => isset($password) === true ? $password : '', |
||
| 53 | 'apikey' => isset($apikey) === true ? $apikey : '', |
||
| 54 | ], |
||
| 55 | [ |
||
| 56 | 'login' => 'trim|escape', |
||
| 57 | 'password' => 'trim|escape', |
||
| 58 | 'apikey' => 'trim|escape', |
||
| 59 | ], |
||
| 60 | API_ROOT_PATH . '/..' |
||
|
|
|||
| 61 | ); |
||
| 62 | if (empty($inputData['login']) === true || empty($inputData['apikey']) === true) { |
||
| 63 | return ["error" => "Login failed0.", "info" => "Empty entry"]; |
||
| 64 | } |
||
| 65 | |||
| 66 | // Check apikey |
||
| 67 | if (empty($inputData['password']) === true) { |
||
| 68 | // case where it is a generic key |
||
| 69 | $apiInfo = $this->select("SELECT count(*) FROM " . prefixTable('api') . " WHERE value='".$inputData['apikey']."' AND label='".$inputData['login']."'"); |
||
| 70 | if ((int) $apiInfo[0]['count(*)'] === 0) { |
||
| 71 | return ["error" => "Login failed1.", "info" => "apikey : Not valid"]; |
||
| 72 | } |
||
| 73 | |||
| 74 | return ["error" => "Login failed2.", "info" => "Not managed."]; |
||
| 75 | } else { |
||
| 76 | // case where it is a user api key |
||
| 77 | // Check if user exists |
||
| 78 | $userInfoRes = $this->select( |
||
| 79 | "SELECT u.id, u.pw, u.public_key, u.private_key, u.personal_folder, u.fonction_id, u.groupes_visibles, u.groupes_interdits, a.value AS user_api_key |
||
| 80 | FROM " . prefixTable('users') . " AS u |
||
| 81 | INNER JOIN " . prefixTable('api') . " AS a ON (a.user_id=u.id) |
||
| 82 | WHERE login='".$inputData['login']."'"); |
||
| 83 | if (count($userInfoRes) === 0) { |
||
| 84 | return ["error" => "Login failed3.", "info" => "apikey : Not valid"]; |
||
| 85 | } |
||
| 86 | $userInfoRes[0]['special'] = ''; |
||
| 87 | $userInfo = $userInfoRes[0]; |
||
| 88 | |||
| 89 | // Check password |
||
| 90 | $pwdlib = new PasswordLib(); |
||
| 91 | if ($pwdlib->verifyPasswordHash($inputData['password'], $userInfo['pw']) === true) { |
||
| 92 | // Correct credentials |
||
| 93 | // get user keys |
||
| 94 | $privateKeyClear = decryptPrivateKey($inputData['password'], (string) $userInfo['private_key']); |
||
| 95 | |||
| 96 | // check API key |
||
| 97 | if ($inputData['apikey'] !== base64_decode(decryptUserObjectKey($userInfo['user_api_key'], $privateKeyClear))) { |
||
| 98 | return ["error" => "Login failed4.", "apikey" => "Not valid"]; |
||
| 99 | } |
||
| 100 | |||
| 101 | // get user folders list |
||
| 102 | $ret = $this->buildUserFoldersList($userInfo); |
||
| 103 | |||
| 104 | // create JWT |
||
| 105 | return $this->createUserJWT( |
||
| 106 | $userInfo['id'], |
||
| 107 | $inputData['login'], |
||
| 108 | $userInfo['personal_folder'], |
||
| 109 | $userInfo['public_key'], |
||
| 110 | $privateKeyClear, |
||
| 111 | implode(",", $ret['folders']), |
||
| 112 | implode(",", $ret['items']) |
||
| 113 | ); |
||
| 114 | } else { |
||
| 115 | return ["error" => "Login failed5.", "info" => "password : Not valid"]; |
||
| 116 | } |
||
| 256 | } |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.