Complex classes like 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 User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class User { |
||
| 14 | |||
| 15 | const ACCESS_DISALLOWED = 0; |
||
| 16 | const ACCESS_ALLOWED = 1; |
||
| 17 | const ACCESS_ADMIN = 2; |
||
| 18 | |||
| 19 | private $id; |
||
| 20 | private $isActive; |
||
| 21 | private $isLogged; |
||
| 22 | private $accessLevel; |
||
| 23 | private $name; |
||
| 24 | private $email; |
||
| 25 | private $password; |
||
| 26 | private $passwordHash; |
||
| 27 | private $recoreryHash; |
||
| 28 | private $image; |
||
| 29 | private $lastLogin; |
||
| 30 | |||
| 31 | /** @var Group */ |
||
| 32 | private $group; |
||
| 33 | private $groupId; |
||
| 34 | |||
| 35 | /** @var Person */ |
||
| 36 | private $person; |
||
| 37 | |||
| 38 | public function __construct() { |
||
| 39 | $this->id = 0; |
||
| 40 | $this->isActive = true; |
||
| 41 | $this->isLogged = false; |
||
| 42 | $this->accessLevel = self::ACCESS_DISALLOWED; |
||
| 43 | $this->group = null; |
||
| 44 | $this->groupId = 0; |
||
| 45 | $this->person = null; |
||
| 46 | $this->name = ''; |
||
| 47 | $this->email = ''; |
||
| 48 | $this->password = '********'; |
||
| 49 | $this->passwordHash = ''; |
||
| 50 | $this->recoreryHash = null; |
||
| 51 | $this->image = null; |
||
| 52 | $this->lastLogin = null; |
||
| 53 | |||
| 54 | if (isset($_SESSION['user'])) { |
||
| 55 | $this->fromSession(); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | public function getId() { |
||
| 60 | return $this->id; |
||
| 61 | } |
||
| 62 | |||
| 63 | public function isActive() { |
||
| 64 | return $this->isActive; |
||
| 65 | } |
||
| 66 | |||
| 67 | public function isLogged() { |
||
| 68 | return $this->isLogged; |
||
| 69 | } |
||
| 70 | |||
| 71 | public function getAccessLevel() { |
||
| 72 | return $this->accessLevel; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** @return boolean */ |
||
| 76 | public function isMaster() { |
||
| 77 | return ($this->accessLevel == self::ACCESS_ADMIN); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function getGroup() { |
||
| 81 | if (is_null($this->group)) { |
||
|
|
|||
| 82 | // groupDAO |
||
| 83 | } |
||
| 84 | return $this->group; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function getGroupId() { |
||
| 88 | return $this->groupId; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function getPerson() { |
||
| 92 | if (is_null($this->person)) { |
||
| 93 | // personDAO |
||
| 94 | } |
||
| 95 | return $this->person; |
||
| 96 | } |
||
| 97 | |||
| 98 | public function getName() { |
||
| 99 | return $this->name; |
||
| 100 | } |
||
| 101 | |||
| 102 | public function getEmail() { |
||
| 103 | return $this->email; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function getPassword() { |
||
| 107 | return $this->password; |
||
| 108 | } |
||
| 109 | |||
| 110 | public function getPasswordHash() { |
||
| 111 | return $this->passwordHash; |
||
| 112 | } |
||
| 113 | |||
| 114 | public function getRecoreryHash() { |
||
| 115 | return $this->recoreryHash; |
||
| 116 | } |
||
| 117 | |||
| 118 | public function getImage() { |
||
| 119 | return $this->image; |
||
| 120 | } |
||
| 121 | |||
| 122 | public function getLastLogin() { |
||
| 123 | return $this->lastLogin; |
||
| 124 | } |
||
| 125 | |||
| 126 | public function setId($id) { |
||
| 127 | $this->id = (int) $id; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function setActive($active) { |
||
| 131 | $this->isActive = (boolean) $active; |
||
| 132 | } |
||
| 133 | |||
| 134 | public function setAccessLevel($accessLevel) { |
||
| 135 | $this->accessLevel = (int) $accessLevel; |
||
| 136 | } |
||
| 137 | |||
| 138 | public function setGroup(Group $group) { |
||
| 139 | $this->group = $group; |
||
| 140 | } |
||
| 141 | |||
| 142 | public function setGroupId($groupId) { |
||
| 143 | $this->groupId = (int) $groupId; |
||
| 144 | } |
||
| 145 | |||
| 146 | public function setPerson(Person $person) { |
||
| 147 | $this->person = $person; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function setName($name) { |
||
| 151 | $this->name = $name; |
||
| 152 | } |
||
| 153 | |||
| 154 | public function setEmail($email) { |
||
| 155 | $this->email = $email; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function setPassword($password) { |
||
| 159 | $this->password = $password; |
||
| 160 | $this->passwordHash = md5($password); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function setPasswordHash($passwordHash) { |
||
| 164 | $this->passwordHash = $passwordHash; |
||
| 165 | } |
||
| 166 | |||
| 167 | public function setRecoreryHash($recoreryHash) { |
||
| 168 | $this->recoreryHash = $recoreryHash; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function setLastLogin($lastLogin) { |
||
| 172 | $this->lastLogin = $lastLogin; |
||
| 173 | } |
||
| 174 | |||
| 175 | public function setImage($image) { |
||
| 176 | $this->image = $image; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Tenta realizar login |
||
| 181 | * @return boolean |
||
| 182 | */ |
||
| 183 | public function login() { |
||
| 184 | $filters = [ |
||
| 185 | 'is_active = ?' => true, |
||
| 186 | 'access_level > ?' => 0, |
||
| 187 | 'email = ?' => $this->email, |
||
| 188 | 'password_hash = ?' => $this->passwordHash |
||
| 189 | ]; |
||
| 190 | $uDAO = new UserDAO(); |
||
| 191 | $user = $uDAO->fetch($filters); |
||
| 192 | if ($user->getId() > 0) { |
||
| 193 | $this->toSession($user); |
||
| 194 | $this->fromSession(); |
||
| 195 | $uDAO->updateLastLogin($user); |
||
| 196 | } |
||
| 197 | return $this->isLogged; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Realiza logout |
||
| 202 | */ |
||
| 203 | public function logout() { |
||
| 207 | |||
| 208 | /** Objeto > Sessão */ |
||
| 209 | private function toSession(User $user) { |
||
| 222 | |||
| 223 | /** Objeto < Sessão */ |
||
| 224 | private function fromSession() { |
||
| 235 | |||
| 236 | /** Obriga o usuário a se logar */ |
||
| 237 | public function requireLogin() { |
||
| 238 | if (!$this->isLogged) { |
||
| 239 | Url::instance()->redirect('login'); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | /** Obriga o usuário a logar como ADMIN */ |
||
| 244 | public function requireMaster() { |
||
| 245 | if (!$this->isLogged || $this->getAccessLevel() != static::ACCESS_ADMIN) { |
||
| 246 | Url::instance()->redirect('login'); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Envia link de recuperacao de senha via Email |
||
| 252 | * @return boolean |
||
| 253 | */ |
||
| 254 | public function sendRecoveryHash() { |
||
| 255 | $success = false; |
||
| 256 | $filters = ['is_active = ?' => true, 'access_level > ?' => 0, 'email = ?' => $this->email]; |
||
| 257 | $uDAO = new UserDAO(); |
||
| 258 | $users = $uDAO->fetchAll($filters); |
||
| 259 | |||
| 260 | if (count($users) > 0) { |
||
| 261 | $success = true; |
||
| 262 | $user = $users[0]; |
||
| 263 | $uDAO->updateRecoveryHash($user); |
||
| 264 | $body = new Block('email/html/recovery-password', ['user' => $user]); |
||
| 265 | |||
| 266 | $mail = new Email(); |
||
| 267 | $mail->setSubject('Recuperação de Senha'); |
||
| 274 | |||
| 275 | } |
||
| 276 |
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.