| Conditions | 17 |
| Paths | 36 |
| Total Lines | 126 |
| Code Lines | 90 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 394 | public function registerJson() |
||
| 395 | { |
||
| 396 | $user = new UserModel(); |
||
| 397 | |||
| 398 | $returnto = Ajde::app()->getRequest()->getPostParam('returnto', false); |
||
| 399 | |||
| 400 | $username = Ajde::app()->getRequest()->getPostParam($user->usernameField); |
||
| 401 | $password = Ajde::app()->getRequest()->getPostParam('password', ''); |
||
| 402 | $passwordCheck = Ajde::app()->getRequest()->getPostParam('passwordCheck', ''); |
||
| 403 | $providername = Ajde::app()->getRequest()->getPostParam('provider', false); |
||
| 404 | $email = Ajde::app()->getRequest()->getPostParam('email', false); |
||
| 405 | $fullname = Ajde::app()->getRequest()->getPostParam('fullname', false); |
||
| 406 | |||
| 407 | $return = [false]; |
||
| 408 | |||
| 409 | $shadowUser = new UserModel(); |
||
| 410 | |||
| 411 | $provider = false; |
||
| 412 | if ($providername) { |
||
| 413 | $sso = Config::get('ssoProviders'); |
||
| 414 | if (!in_array($providername, $sso)) { |
||
| 415 | Ajde_Http_Response::redirectNotFound(); |
||
| 416 | } |
||
| 417 | |||
| 418 | $classname = 'Ajde_User_Sso_' . ucfirst($providername); |
||
| 419 | /* @var $provider Ajde_User_SSO_Interface */ |
||
| 420 | $provider = new $classname; |
||
| 421 | } |
||
| 422 | |||
| 423 | if (empty($username)) { |
||
| 424 | $return = [ |
||
| 425 | 'success' => false, |
||
| 426 | 'message' => __("Please provide a " . $user->usernameField . "") |
||
| 427 | ]; |
||
| 428 | } else { |
||
| 429 | if (!$provider && empty($password)) { |
||
| 430 | $return = [ |
||
| 431 | 'success' => false, |
||
| 432 | 'message' => __("Please provide a password") |
||
| 433 | ]; |
||
| 434 | } else { |
||
| 435 | if ($shadowUser->loadByField($shadowUser->usernameField, $username)) { |
||
| 436 | $return = [ |
||
| 437 | 'success' => false, |
||
| 438 | 'message' => __(ucfirst($user->usernameField) . " already exist") |
||
| 439 | ]; |
||
| 440 | } else { |
||
| 441 | if (!$provider && $password !== $passwordCheck) { |
||
| 442 | $return = [ |
||
| 443 | 'success' => false, |
||
| 444 | 'message' => __("Passwords do not match") |
||
| 445 | ]; |
||
| 446 | } else { |
||
| 447 | if (empty($email)) { |
||
| 448 | $return = [ |
||
| 449 | 'success' => false, |
||
| 450 | 'message' => __("Please provide an e-mail address") |
||
| 451 | ]; |
||
| 452 | } else { |
||
| 453 | if (Ajde_Component_String::validEmail($email) === false) { |
||
| 454 | $return = [ |
||
| 455 | 'success' => false, |
||
| 456 | 'message' => __('Please provide a valid e-mail address') |
||
| 457 | ]; |
||
| 458 | } else { |
||
| 459 | if ($shadowUser->loadByField('email', $email)) { |
||
| 460 | $return = [ |
||
| 461 | 'success' => false, |
||
| 462 | 'message' => __("A user with this e-mail address already exist") |
||
| 463 | ]; |
||
| 464 | } else { |
||
| 465 | if (empty($fullname)) { |
||
| 466 | $return = [ |
||
| 467 | 'success' => false, |
||
| 468 | 'message' => __("Please provide a full name") |
||
| 469 | ]; |
||
| 470 | } else { |
||
| 471 | if ($provider && !$provider->getData()) { |
||
| 472 | $return = [ |
||
| 473 | 'success' => false, |
||
| 474 | 'message' => __("Something went wrong with fetching your credentials from an external service") |
||
| 475 | ]; |
||
| 476 | } else { |
||
| 477 | $user->set('email', $email); |
||
| 478 | $user->set('fullname', $fullname); |
||
| 479 | if ($user->add($username, $password)) { |
||
| 480 | if ($provider) { |
||
| 481 | $sso = new SsoModel(); |
||
| 482 | $sso->populate([ |
||
| 483 | 'user' => $user->getPK(), |
||
| 484 | 'provider' => $providername, |
||
| 485 | 'username' => $provider->getUsernameSuggestion(), |
||
| 486 | 'avatar' => $provider->getAvatarSuggestion(), |
||
| 487 | 'profile' => $provider->getProfileSuggestion(), |
||
| 488 | 'uid' => $provider->getUidHash(), |
||
| 489 | 'data' => serialize($provider->getData()) |
||
| 490 | ]); |
||
| 491 | $sso->insert(); |
||
| 492 | $user->copyAvatarFromSso($sso); |
||
| 493 | } |
||
| 494 | $user->login(); |
||
| 495 | $user->storeCookie($this->includeDomain); |
||
| 496 | Ajde_Session_Flash::alert(sprintf(__('Welcome %s, you are now logged in'), |
||
| 497 | $fullname)); |
||
| 498 | $return = [ |
||
| 499 | 'success' => true, |
||
| 500 | 'returnto' => $returnto |
||
| 501 | ]; |
||
| 502 | } else { |
||
| 503 | $return = [ |
||
| 504 | 'success' => false, |
||
| 505 | 'message' => __("Something went wrong") |
||
| 506 | ]; |
||
| 507 | } |
||
| 508 | } |
||
| 509 | } |
||
| 510 | } |
||
| 511 | } |
||
| 512 | } |
||
| 513 | } |
||
| 514 | } |
||
| 515 | } |
||
| 516 | } |
||
| 517 | |||
| 518 | return $return; |
||
| 519 | } |
||
| 520 | } |
||
| 521 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: