Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PMF_User_CurrentUser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PMF_User_CurrentUser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class PMF_User_CurrentUser extends PMF_User |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * true if CurrentUser is logged in, otherwise false. |
||
| 53 | * |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | private $loggedIn = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Specifies the timeout for the session in minutes. If the session ID was |
||
| 60 | * not updated for the last $this->_sessionTimeout minutes, the CurrentUser |
||
| 61 | * will be logged out automatically if no cookie was set. |
||
| 62 | * |
||
| 63 | * @var int |
||
| 64 | */ |
||
| 65 | private $sessionTimeout = PMF_AUTH_TIMEOUT; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Specifies the timeout for the session-ID in minutes. If the session ID |
||
| 69 | * was not updated for the last $this->_sessionIdTimeout minutes, it will |
||
| 70 | * be updated. If set to 0, the session ID will be updated on every click. |
||
| 71 | * The session ID timeout must not be greater than Session timeout. |
||
| 72 | * |
||
| 73 | * @var int |
||
| 74 | */ |
||
| 75 | private $sessionIdTimeout = 1; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * LDAP configuration if available. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private $ldapConfig = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Remember me activated or deactivated. |
||
| 86 | * |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | private $rememberMe = false; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Login successful or auth failure: |
||
| 93 | * 1 -> success |
||
| 94 | * 0 -> failure. |
||
| 95 | * |
||
| 96 | * @var int |
||
| 97 | */ |
||
| 98 | private $loginState = 1; |
||
| 99 | |||
| 100 | private $lockoutTime = 600; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Constructor. |
||
| 104 | * |
||
| 105 | * @param PMF_Configuration $config |
||
| 106 | */ |
||
| 107 | public function __construct(PMF_Configuration $config) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Checks the given login and password in all auth-objects. Returns true |
||
| 115 | * on success, otherwise false. Raises errors that can be checked using |
||
| 116 | * the error() method. On success, the CurrentUser instance will be |
||
| 117 | * labeled as logged in. The name of the successful auth container will |
||
| 118 | * be stored in the user table. A new auth object may be added by using |
||
| 119 | * addAuth() method. The given password must not be encrypted, since the |
||
| 120 | * auth object takes care about the encryption method. |
||
| 121 | * |
||
| 122 | * @param string $login Login name |
||
| 123 | * @param string $password Password |
||
| 124 | * |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | public function login($login, $password) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Returns true if CurrentUser is logged in, otherwise false. |
||
| 248 | * |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public function isLoggedIn() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns false if the CurrentUser object stored in the |
||
| 258 | * session is valid and not timed out. There are two |
||
| 259 | * parameters for session timeouts: $this->_sessionTimeout |
||
| 260 | * and $this->_sessionIdTimeout. |
||
| 261 | * |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | public function sessionIsTimedOut() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Returns false if the session-ID is not timed out. |
||
| 275 | * |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | public function sessionIdIsTimedOut() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Returns the age of the current session-ID in minutes. |
||
| 289 | * |
||
| 290 | * @return float |
||
| 291 | */ |
||
| 292 | public function sessionAge() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Returns an associative array with session information stored |
||
| 303 | * in the user table. The array has the following keys: |
||
| 304 | * session_id, session_timestamp and ip. |
||
| 305 | * |
||
| 306 | * @return array |
||
| 307 | */ |
||
| 308 | public function getSessionInfo() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Updates the session-ID, does not care about time outs. |
||
| 334 | * Stores session information in the user table: session_id, |
||
| 335 | * session_timestamp and ip. |
||
| 336 | * Optionally it should update the 'last login' time. |
||
| 337 | * Returns true on success, otherwise false. |
||
| 338 | * |
||
| 339 | * @param bool $updateLastlogin Update the last login time? |
||
| 340 | * |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | public function updateSessionId($updateLastlogin = false) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Saves the CurrentUser into the session. This method |
||
| 390 | * may be called after a successful login. |
||
| 391 | */ |
||
| 392 | public function saveToSession() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Deletes the CurrentUser from the session. The user |
||
| 399 | * will be logged out. Return true on success, otherwise false. |
||
| 400 | * |
||
| 401 | * @param bool $deleteCookie |
||
| 402 | * |
||
| 403 | * @return bool |
||
| 404 | */ |
||
| 405 | public function deleteFromSession($deleteCookie = false) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * This static method returns a valid CurrentUser object if there is one |
||
| 450 | * in the session that is not timed out. The session-ID is updated if |
||
| 451 | * necessary. The CurrentUser will be removed from the session, if it is |
||
| 452 | * timed out. If there is no valid CurrentUser in the session or the |
||
| 453 | * session is timed out, null will be returned. If the session data is |
||
| 454 | * correct, but there is no user found in the user table, false will be |
||
| 455 | * returned. On success, a valid CurrentUser object is returned. |
||
| 456 | * |
||
| 457 | * @static |
||
| 458 | * |
||
| 459 | * @param PMF_Configuration $config |
||
| 460 | * |
||
| 461 | * @return null|PMF_User_CurrentUser |
||
| 462 | */ |
||
| 463 | public static function getFromSession(PMF_Configuration $config) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * This static method returns a valid CurrentUser object if there is one |
||
| 506 | * in the cookie that is not timed out. The session-ID is updated then. |
||
| 507 | * The CurrentUser will be removed from the session, if it is |
||
| 508 | * timed out. If there is no valid CurrentUser in the cookie or the |
||
| 509 | * cookie is timed out, null will be returned. If the cookie is correct, |
||
| 510 | * but there is no user found in the user table, false will be returned. |
||
| 511 | * On success, a valid CurrentUser object is returned. |
||
| 512 | * |
||
| 513 | * @static |
||
| 514 | * |
||
| 515 | * @param PMF_Configuration $config |
||
| 516 | * |
||
| 517 | * @return null|PMF_User_CurrentUser |
||
| 518 | */ |
||
| 519 | public static function getFromCookie(PMF_Configuration $config) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Sets the number of minutes when the current user stored in |
||
| 547 | * the session gets invalid. |
||
| 548 | * |
||
| 549 | * @param float $timeout Timeout |
||
| 550 | */ |
||
| 551 | public function setSessionTimeout($timeout) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Sets the number of minutes when the session-ID needs to be |
||
| 558 | * updated. By setting the session-ID timeout to zero, the |
||
| 559 | * session-ID will be updated on each click. |
||
| 560 | * |
||
| 561 | * @param float $timeout Timeout |
||
| 562 | */ |
||
| 563 | public function setSessionIdTimeout($timeout) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Enables the remember me decision. |
||
| 570 | */ |
||
| 571 | public function enableRememberMe() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Saves remember me token in the database. |
||
| 578 | * |
||
| 579 | * @param string $rememberMe |
||
| 580 | * |
||
| 581 | * @return bool |
||
| 582 | */ |
||
| 583 | View Code Duplication | protected function setRememberMe($rememberMe) |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Sets login success/failure. |
||
| 602 | * |
||
| 603 | * @param bool $success |
||
| 604 | * |
||
| 605 | * @return bool |
||
| 606 | */ |
||
| 607 | View Code Duplication | protected function setSuccess($success) |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Sets IP and session timestamp, success flag to false. |
||
| 628 | * |
||
| 629 | * @return mixed |
||
| 630 | */ |
||
| 631 | View Code Duplication | protected function setLoginAttempt() |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Checks if the last login attempt from current user failed. |
||
| 653 | * |
||
| 654 | * @return bool |
||
| 655 | */ |
||
| 656 | protected function isFailedLastLoginAttempt() |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Returns the CSRF token from session. |
||
| 689 | * |
||
| 690 | * @return string |
||
| 691 | */ |
||
| 692 | public function getCsrfTokenFromSession() |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Save CSRF token to session. |
||
| 699 | */ |
||
| 700 | public function saveCrsfTokenToSession() |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Deletes CSRF token from session. |
||
| 709 | */ |
||
| 710 | protected function deleteCsrfTokenFromSession() |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Creates a CSRF token. |
||
| 717 | * |
||
| 718 | * @return string |
||
| 719 | */ |
||
| 720 | private function createCsrfToken() |
||
| 724 | } |
||
| 725 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: