| Conditions | 20 |
| Paths | 476 |
| Total Lines | 209 |
| Code Lines | 111 |
| 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 |
||
| 147 | public function attemp() |
||
| 148 | { |
||
| 149 | # data to send |
||
| 150 | $data = []; |
||
| 151 | |||
| 152 | $post = $this->getPost(); |
||
| 153 | $this->setTerminal(true); |
||
| 154 | |||
| 155 | # TRY-CATCH-BLOCK |
||
| 156 | try { |
||
| 157 | |||
| 158 | # STANDARD VALIDATIONS [check method] |
||
| 159 | if (!$this->isPost()) |
||
| 160 | { |
||
| 161 | $http = new Http(); |
||
| 162 | $http->writeStatus($http::HTTP_METHOD_NOT_ALLOWED); |
||
| 163 | |||
| 164 | die('Error ' . $http::HTTP_METHOD_NOT_ALLOWED .' (' . $http->getStatusText($http::HTTP_METHOD_NOT_ALLOWED) . ')!!'); |
||
| 165 | } |
||
| 166 | |||
| 167 | # STANDARD VALIDATIONS [check needed arguments] |
||
| 168 | $needles = ['username', 'password']; |
||
| 169 | |||
| 170 | array_walk($needles, function(&$item) use ($post) { |
||
| 171 | if (!array_key_exists($item, $post)) |
||
| 172 | { |
||
| 173 | $http = new Http(); |
||
| 174 | $http->writeStatus($http::HTTP_BAD_REQUEST); |
||
| 175 | |||
| 176 | die('Error ' . $http::HTTP_BAD_REQUEST .' (' . $http->getStatusText($http::HTTP_BAD_REQUEST) . ')!!'); |
||
| 177 | } |
||
| 178 | }); |
||
| 179 | |||
| 180 | $this->checkSession(); |
||
| 181 | |||
| 182 | $components = [ |
||
| 183 | "attributes" => [ |
||
| 184 | "username" => [ |
||
| 185 | "required" => true, |
||
| 186 | "type" => "text", |
||
| 187 | "minlength" => 4, |
||
| 188 | "maxlength" => 20 |
||
| 189 | ], |
||
| 190 | "password" => [ |
||
| 191 | "required" => true, |
||
| 192 | "type" => "text", |
||
| 193 | "minlength" => 4, |
||
| 194 | "maxlength" => 20 |
||
| 195 | ] |
||
| 196 | ], |
||
| 197 | ]; |
||
| 198 | |||
| 199 | $options = [ |
||
| 200 | "username" => [ |
||
| 201 | "label" => "Username", |
||
| 202 | "validators" => [ |
||
| 203 | "Alnum" => ["allowWhiteSpace" => false] |
||
| 204 | ] |
||
| 205 | ], |
||
| 206 | "password" => [ |
||
| 207 | "label" => "Password" |
||
| 208 | ] |
||
| 209 | ]; |
||
| 210 | |||
| 211 | $form = new Form($components); |
||
| 212 | $form->fill($post); |
||
| 213 | |||
| 214 | $validator = new FormValidator($form, $options); |
||
| 215 | $validator->validate(); |
||
| 216 | |||
| 217 | $data["validator"] = $validator; |
||
| 218 | |||
| 219 | # STANDARD VALIDATIONS [check argument constraints] |
||
| 220 | if (!$validator->isValid()) |
||
| 221 | { |
||
| 222 | $data["messages"] = $validator->getMessages(); |
||
| 223 | throw new \Drone\Exception\Exception("Form validation errors!"); |
||
| 224 | } |
||
| 225 | |||
| 226 | $config = include 'module/Auth/config/user.config.php'; |
||
| 227 | |||
| 228 | $authorization = $config["authorization"]; |
||
| 229 | $username_str = $config["authentication"]["gateway"]["credentials"]["username"]; |
||
| 230 | $password_str = $config["authentication"]["gateway"]["credentials"]["password"]; |
||
| 231 | |||
| 232 | switch ($config["authentication"]["type"]) |
||
| 233 | { |
||
| 234 | case 'db_user': |
||
| 235 | |||
| 236 | try |
||
| 237 | { |
||
| 238 | if ($authorization["enabled"]) |
||
| 239 | { |
||
| 240 | $rowset = $this->getDbUserAdapter()->select([ |
||
| 241 | $username_str => strtoupper($post["username"]) |
||
| 242 | ]); |
||
| 243 | |||
| 244 | if (!count($rowset)) |
||
| 245 | throw new \Drone\Exception\Exception("Your user is not authorized to use this application!"); |
||
| 246 | } |
||
| 247 | |||
| 248 | $auth = new Authentication("default", false); |
||
| 249 | $result = $auth->authenticate($post["username"], $post["password"]); |
||
| 250 | } |
||
| 251 | catch (\Drone\Db\Driver\Exception\ConnectionException $e) |
||
| 252 | { |
||
| 253 | throw new \Drone\Exception\Exception("Wrong user or password"); |
||
| 254 | } |
||
| 255 | |||
| 256 | break; |
||
| 257 | |||
| 258 | case 'db_table': |
||
| 259 | |||
| 260 | $rowset = $this->getUserAdapter()->select([ |
||
| 261 | $username_str => $post["username"] |
||
| 262 | ]); |
||
| 263 | |||
| 264 | if (!count($rowset)) |
||
| 265 | throw new \Drone\Exception\Exception("Username or password are incorrect"); |
||
| 266 | |||
| 267 | $user = array_shift($rowset); |
||
| 268 | |||
| 269 | if ($authorization["enabled"] == "Y") |
||
| 270 | { |
||
| 271 | $id_field = $config["authentication"]["gateway"]["table_info"]["columns"]["id_field"]; |
||
| 272 | |||
| 273 | $rowset = $this->getUserRoleAdapter()->select([ |
||
| 274 | $id_field => $user->{$id_field} |
||
| 275 | ]); |
||
| 276 | |||
| 277 | if (!count($rowset)) |
||
| 278 | throw new \Drone\Exception\Exception("Your user is not authorized to use this application!"); |
||
| 279 | } |
||
| 280 | |||
| 281 | $state_field = $config["authentication"]["gateway"]["table_info"]["columns"]["state_field"]; |
||
| 282 | $state_pending_value = $config["authentication"]["gateway"]["table_info"]["column_values"]["state_field"]["pending_email"]; |
||
| 283 | |||
| 284 | if ($user->{$state_field} == $state_pending_value) |
||
| 285 | throw new \Drone\Exception\Exception("User pending of email checking"); |
||
| 286 | |||
| 287 | $securePass = $user->{$password_str}; |
||
| 288 | $password = $post["password"]; |
||
| 289 | |||
| 290 | $bcrypt = new Bcrypt(); |
||
| 291 | |||
| 292 | if (!$bcrypt->verify($password, $securePass)) |
||
| 293 | throw new \Drone\Exception\Exception("Username or password are incorrect"); |
||
| 294 | |||
| 295 | break; |
||
| 296 | |||
| 297 | default: |
||
| 298 | # code... |
||
| 299 | break; |
||
| 300 | } |
||
| 301 | |||
| 302 | $key = $config["authentication"]["key"]; |
||
| 303 | $method = $config["authentication"]["method"]; |
||
| 304 | |||
| 305 | switch ($method) |
||
| 306 | { |
||
| 307 | case '_COOKIE': |
||
| 308 | setcookie($key, $post["username"], time() + 2000000000, '/'); |
||
| 309 | break; |
||
| 310 | |||
| 311 | case '_SESSION': |
||
| 312 | $_SESSION[$key] = $post["username"]; |
||
| 313 | break; |
||
| 314 | } |
||
| 315 | |||
| 316 | # SUCCESS-MESSAGE |
||
| 317 | $data["process"] = "success"; |
||
| 318 | } |
||
| 319 | catch (\Drone\Exception\Exception $e) |
||
| 320 | { |
||
| 321 | # ERROR-MESSAGE |
||
| 322 | $data["process"] = "warning"; |
||
| 323 | $data["message"] = $e->getMessage(); |
||
| 324 | } |
||
| 325 | catch (\Exception $e) |
||
| 326 | { |
||
| 327 | $file = str_replace('\\', '', __CLASS__); |
||
| 328 | $storage = new \Drone\Exception\Storage("cache/$file.json"); |
||
| 329 | |||
| 330 | # stores the error code |
||
| 331 | if (($errorCode = $storage->store($e)) === false) |
||
| 332 | { |
||
| 333 | $errors = $storage->getErrors(); |
||
| 334 | |||
| 335 | # if error storing is not possible, handle it (internal app error) |
||
| 336 | $this->handleErrors($errors, __METHOD__); |
||
| 337 | } |
||
| 338 | |||
| 339 | # errors retrived by the use of ErrorTrait |
||
| 340 | if (count($this->getErrors())) |
||
| 341 | $this->handleErrors($this->getErrors(), __METHOD__); |
||
| 342 | |||
| 343 | $data["code"] = $errorCode; |
||
| 344 | $data["message"] = $e->getMessage(); |
||
| 345 | |||
| 346 | $config = include 'config/application.config.php'; |
||
| 347 | $data["dev_mode"] = $config["environment"]["dev_mode"]; |
||
| 348 | |||
| 349 | # redirect view |
||
| 350 | $this->setMethod('error'); |
||
| 351 | |||
| 352 | return $data; |
||
| 353 | } |
||
| 354 | |||
| 355 | return $data; |
||
| 356 | } |
||
| 388 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths