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 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, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 47 | class PMF_User | ||
| 48 | { | ||
| 49 | const ERROR_UNDEFINED_PARAMETER = 'Following parameter must to be defined: '; | ||
| 50 | const ERROR_USER_ADD = 'Account could not be created. '; | ||
| 51 | const ERROR_USER_CANNOT_CREATE_USER = 'User account could not be created. '; | ||
| 52 | const ERROR_USER_CANNOT_CREATE_USERDATA = 'Entry for user data could not be created. '; | ||
| 53 | const ERROR_USER_CANNOT_DELETE_USER = 'User account could not be deleted. '; | ||
| 54 | const ERROR_USER_CANNOT_DELETE_USERDATA = 'Entry for user data could not be deleted. '; | ||
| 55 | const ERROR_USER_CANNOT_UPDATE_USERDATA = 'Entry for user data could not be updated. '; | ||
| 56 | const ERROR_USER_CHANGE = 'Account could not be updated. '; | ||
| 57 | const ERROR_USER_DELETE = 'Account could not be deleted. '; | ||
| 58 | const ERROR_USER_INCORRECT_LOGIN = 'Specified login could not be found. '; | ||
| 59 | const ERROR_USER_INCORRECT_PASSWORD = 'Specified password is not correct.'; | ||
| 60 | const ERROR_USER_INVALID_STATUS = 'Undefined user status.'; | ||
| 61 | const ERROR_USER_LOGINNAME_TOO_SHORT = 'The chosen loginname is too short.'; | ||
| 62 | const ERROR_USER_LOGIN_NOT_UNIQUE = 'Specified login name already exists. '; | ||
| 63 | const ERROR_USER_LOGIN_INVALID = 'The chosen login is invalid. A valid login has at least four characters. Only letters, numbers and underscore _ are allowed. The first letter must be a letter. '; | ||
| 64 | const ERROR_USER_NO_AUTH = 'No authentication method specified. '; | ||
| 65 | const ERROR_USER_NO_DB = 'No database specified.'; | ||
| 66 | const ERROR_USER_NO_PERM = 'No permission container specified.'; | ||
| 67 | const ERROR_USER_NO_USERID = 'No user-ID found. '; | ||
| 68 | const ERROR_USER_NO_USERLOGINDATA = 'No user login data found. '; | ||
| 69 | const ERROR_USER_NOT_FOUND = 'User account could not be found. '; | ||
| 70 | const ERROR_USER_NOWRITABLE = 'No authentication object is writable. '; | ||
| 71 | const ERROR_USER_NO_LOGIN_DATA = 'A username and password must be provided. '; | ||
| 72 | |||
| 73 | const STATUS_USER_PROTECTED = 'User account is protected. '; | ||
| 74 | const STATUS_USER_BLOCKED = 'User account is blocked. '; | ||
| 75 | const STATUS_USER_ACTIVE = 'User account is active. '; | ||
| 76 | |||
| 77 | // --- ATTRIBUTES --- | ||
| 78 | |||
| 79 | /** | ||
| 80 | * Permission container | ||
| 81 | * | ||
| 82 | * @var PMF_Perm_Basic|PMF_Perm_Medium | ||
| 83 | */ | ||
| 84 | public $perm = null; | ||
| 85 | |||
| 86 | /** | ||
| 87 | * User-data storage container | ||
| 88 | * | ||
| 89 | * @var PMF_User_UserData | ||
| 90 | */ | ||
| 91 | public $userdata = null; | ||
| 92 | |||
| 93 | /** | ||
| 94 | * Default Authentication properties | ||
| 95 | * | ||
| 96 | * @var array | ||
| 97 | */ | ||
| 98 | private $authData = array( | ||
| 99 | 'authSource' => array( | ||
| 100 | 'name' => 'db', | ||
| 101 | 'type' => 'local' | ||
| 102 | ), | ||
| 103 | 'encType' => PMF_ENCRYPTION_TYPE, | ||
| 104 | 'readOnly' => false | ||
| 105 | ); | ||
| 106 | |||
| 107 | /** | ||
| 108 | * Public array that contains error messages. | ||
| 109 | * | ||
| 110 | * @var array | ||
| 111 | */ | ||
| 112 | public $errors = []; | ||
| 113 | |||
| 114 | /** | ||
| 115 | * authentication container | ||
| 116 | * | ||
| 117 | * @var array | ||
| 118 | */ | ||
| 119 | protected $authContainer = []; | ||
| 120 | |||
| 121 | /** | ||
| 122 | * login string | ||
| 123 | * | ||
| 124 | * @var string | ||
| 125 | */ | ||
| 126 | private $login = ''; | ||
| 127 | |||
| 128 | /** | ||
| 129 | * minimum length of login string (default: 2) | ||
| 130 | * | ||
| 131 | * @var int | ||
| 132 | */ | ||
| 133 | private $loginMinLength = 2; | ||
| 134 | |||
| 135 | /** | ||
| 136 | * regular expression to find invalid login strings | ||
| 137 | * (default: /^[a-z0-9][\w\.\-@]+/i ) | ||
| 138 | * | ||
| 139 | * @var string | ||
| 140 | */ | ||
| 141 | private $validUsername = '/^[a-z0-9][\w\.\-@]+/i'; | ||
| 142 | |||
| 143 | /** | ||
| 144 | * user ID | ||
| 145 | * | ||
| 146 | * @var integer | ||
| 147 | */ | ||
| 148 | private $userId = -1; | ||
| 149 | |||
| 150 | /** | ||
| 151 | * Status of user | ||
| 152 | * | ||
| 153 | * @var string | ||
| 154 | */ | ||
| 155 | private $status = ''; | ||
| 156 | |||
| 157 | /** | ||
| 158 | * array of allowed values for status | ||
| 159 | * | ||
| 160 | * @var array | ||
| 161 | */ | ||
| 162 | private $allowedStatus = array( | ||
| 163 | 'active' => self::STATUS_USER_ACTIVE, | ||
| 164 | 'blocked' => self::STATUS_USER_BLOCKED, | ||
| 165 | 'protected' => self::STATUS_USER_PROTECTED | ||
| 166 | ); | ||
| 167 | |||
| 168 | /** | ||
| 169 | * Configuration | ||
| 170 | * | ||
| 171 | * @var PMF_Configuration | ||
| 172 | */ | ||
| 173 | protected $config = null; | ||
| 174 | |||
| 175 | /** | ||
| 176 | * Constructor | ||
| 177 | * | ||
| 178 | * @param PMF_Configuration $config | ||
| 179 | * | ||
| 180 | * @return PMF_User | ||
| 181 | */ | ||
| 182 | public function __construct(PMF_Configuration $config) | ||
| 215 | |||
| 216 | |||
| 217 | // --- OPERATIONS --- | ||
| 218 | |||
| 219 | /** | ||
| 220 | * adds a permission object to the user. | ||
| 221 | * | ||
| 222 | * @param PMF_Perm $perm Permission object | ||
| 223 | * @return boolean | ||
| 224 | */ | ||
| 225 | public function addPerm(PMF_Perm $perm) | ||
| 234 | |||
| 235 | /** | ||
| 236 | * Returns the User ID of the user. | ||
| 237 | * | ||
| 238 | * @return integer | ||
| 239 | */ | ||
| 240 | public function getUserId() | ||
| 250 | |||
| 251 | /** | ||
| 252 | * Loads basic user information from the database selecting the user with | ||
| 253 | * specified user-ID. | ||
| 254 | * | ||
| 255 | * @param integer $userId User ID | ||
| 256 | * @return bool | ||
| 257 | */ | ||
| 258 | public function getUserById($userId) | ||
| 309 | |||
| 310 | /** | ||
| 311 | * loads basic user information from the database selecting the user with | ||
| 312 | * specified login. | ||
| 313 | * | ||
| 314 | * @param string $login Login name | ||
| 315 | * @param bool $raiseError Raise error? | ||
| 316 | * @return bool | ||
| 317 | */ | ||
| 318 | View Code Duplication | public function getUserByLogin($login, $raiseError = true) | |
| 353 | |||
| 354 | /** | ||
| 355 | * loads basic user information from the database selecting the user with | ||
| 356 | * specified cookie information. | ||
| 357 | * | ||
| 358 | * @param string $cookie | ||
| 359 | * | ||
| 360 | * @return boolean | ||
| 361 | */ | ||
| 362 | View Code Duplication | public function getUserByCookie($cookie) | |
| 400 | |||
| 401 | /** | ||
| 402 | * search users by login | ||
| 403 | * | ||
| 404 | * @param string $search Login name | ||
| 405 | * @return array | ||
| 406 | */ | ||
| 407 | public function searchUsers($search) | ||
| 434 | |||
| 435 | /** | ||
| 436 | * creates a new user and stores basic data in the database. | ||
| 437 | * | ||
| 438 | * @param string $login Login name | ||
| 439 | * @param string $pass Password | ||
| 440 | * @param integer $userId User ID | ||
| 441 | * @return mixed | ||
| 442 | */ | ||
| 443 | public function createUser($login, $pass = '', $userId = 0) | ||
| 518 | |||
| 519 | /** | ||
| 520 | * deletes the user from the database. | ||
| 521 | * | ||
| 522 | * @return boolean | ||
| 523 | */ | ||
| 524 | public function deleteUser() | ||
| 587 | |||
| 588 | /** | ||
| 589 | * changes the user's password. If $pass is omitted, a new | ||
| 590 | * password is generated using the createPassword() method. | ||
| 591 | * | ||
| 592 | * @param string $pass Password | ||
| 593 | * @return boolean | ||
| 594 | */ | ||
| 595 | public function changePassword($pass = '') | ||
| 622 | |||
| 623 | /** | ||
| 624 | * returns the user's status. | ||
| 625 | * | ||
| 626 | * @return string | ||
| 627 | */ | ||
| 628 | public function getStatus() | ||
| 635 | |||
| 636 | /** | ||
| 637 | * sets the user's status and updates the database entry. | ||
| 638 | * | ||
| 639 | * @param string $status Status | ||
| 640 | * @return boolean | ||
| 641 | */ | ||
| 642 | public function setStatus($status) | ||
| 672 | |||
| 673 | /** | ||
| 674 | * Returns a string with error messages. | ||
| 675 | * | ||
| 676 | * The string returned by error() contains messages for all errors that | ||
| 677 | * during object procesing. Messages are separated by new lines. | ||
| 678 | * | ||
| 679 | * Error messages are stored in the public array errors. | ||
| 680 | * | ||
| 681 | * @return string | ||
| 682 | */ | ||
| 683 | public function error() | ||
| 692 | |||
| 693 | /** | ||
| 694 | * returns true if login is a valid login string. | ||
| 695 | * | ||
| 696 | * $this->loginMinLength defines the minimum length the | ||
| 697 | * login string. If login has more characters than allowed, | ||
| 698 | * false is returned. | ||
| 699 | * $this->login_invalidRegExp is a regular expression. | ||
| 700 | * If login matches this false is returned. | ||
| 701 | * | ||
| 702 | * @param string $login Login name | ||
| 703 | * @return boolean | ||
| 704 | */ | ||
| 705 | public function isValidLogin($login) | ||
| 715 | |||
| 716 | /** | ||
| 717 | * adds a new authentication object to the user object. | ||
| 718 | * | ||
| 719 | * @param PMF_Auth_Driver $auth PMF_Auth_Driver object | ||
| 720 | * @param string $name Auth name | ||
| 721 | * @return boolean | ||
| 722 | */ | ||
| 723 | public function addAuth($auth, $name) | ||
| 731 | |||
| 732 | /** | ||
| 733 | * returns true if auth is a valid authentication object. | ||
| 734 | * | ||
| 735 | * @param PMF_Auth $auth Auth object | ||
| 736 | * @return bool | ||
| 737 | */ | ||
| 738 | protected function checkAuth($auth) | ||
| 750 | |||
| 751 | /** | ||
| 752 | * Returns the data aof the auth container | ||
| 753 | * @return array | ||
| 754 | */ | ||
| 755 | public function getAuthContainer() | ||
| 759 | |||
| 760 | /** | ||
| 761 | * Returns a specific entry from the auth data source array | ||
| 762 | * | ||
| 763 | * @param string $key | ||
| 764 | * | ||
| 765 | * @return string|null | ||
| 766 | */ | ||
| 767 | public function getAuthSource($key) | ||
| 775 | |||
| 776 | /** | ||
| 777 | * Returns a specific entry from the auth data array | ||
| 778 | * | ||
| 779 | * @param string $key | ||
| 780 | * | ||
| 781 | * @return string|null | ||
| 782 | */ | ||
| 783 | public function getAuthData($key) | ||
| 791 | |||
| 792 | /** | ||
| 793 | * returns true if perm is a valid permission object. | ||
| 794 | * | ||
| 795 | * @param PMF_Perm $perm Perm object | ||
| 796 | * | ||
| 797 | * @return bool | ||
| 798 | */ | ||
| 799 | private function checkPerm($perm) | ||
| 807 | |||
| 808 | /** | ||
| 809 | * returns the user's login. | ||
| 810 | * | ||
| 811 | * @return string | ||
| 812 | */ | ||
| 813 | public function getLogin() | ||
| 817 | |||
| 818 | /** | ||
| 819 | * returns a new password. | ||
| 820 | * | ||
| 821 | * @return string | ||
| 822 | */ | ||
| 823 | private function createPassword() | ||
| 828 | |||
| 829 | /** | ||
| 830 | * Returns the data of the current user | ||
| 831 | * | ||
| 832 | * @param string $field Field | ||
| 833 | * @return array | ||
| 834 | */ | ||
| 835 | public function getUserData($field = '*') | ||
| 842 | |||
| 843 | /** | ||
| 844 | * Adds user data | ||
| 845 | * | ||
| 846 | * @param array $data Array with user data | ||
| 847 | * @return bool | ||
| 848 | */ | ||
| 849 | public function setUserData(Array $data) | ||
| 857 | |||
| 858 | /** | ||
| 859 | * Returns an array with the user-IDs of all users found in | ||
| 860 | * the database. By default, the Anonymous User will not be returned. | ||
| 861 | * | ||
| 862 | * @param boolean $withoutAnonymous Without anonymous? | ||
| 863 | * @return array | ||
| 864 | */ | ||
| 865 | View Code Duplication | public function getAllUsers($withoutAnonymous = true) | |
| 890 | |||
| 891 | /** | ||
| 892 | * Returns an array of all users found in the database. By default, the | ||
| 893 | * anonymous User will not be returned. The returned array contains the | ||
| 894 | * user ID as key, the values are login name, account status, authentication | ||
| 895 | * source and the user creation date. | ||
| 896 | * | ||
| 897 | * @param boolean $withoutAnonymous Without anonymous? | ||
| 898 | * @return array | ||
| 899 | */ | ||
| 900 | View Code Duplication | public function getAllUserData($withoutAnonymous = true) | |
| 925 | |||
| 926 | /** | ||
| 927 | * Get all users in <option> tags | ||
| 928 | * | ||
| 929 | * @param integer $id Selected user ID | ||
| 930 | * | ||
| 931 | * @return string | ||
| 932 | */ | ||
| 933 | public function getAllUserOptions($id = 1) | ||
| 951 | |||
| 952 | /** | ||
| 953 | * sets the minimum login string length | ||
| 954 | * | ||
| 955 | * @param integer $loginMinLength Minimum length of login name | ||
| 956 | * | ||
| 957 | * @return void | ||
| 958 | */ | ||
| 959 | public function setLoginMinLength($loginMinLength) | ||
| 965 | } | ||
| 966 | 
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.