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 |
||
18 | class User { |
||
19 | |||
20 | private static $accessLevels = [0, 1, 2]; |
||
21 | |||
22 | const ACCESS_DENIED = 0; |
||
23 | const ACCESS_ALLOWED = 1; |
||
24 | const ACCESS_ADMIN = 2; |
||
25 | |||
26 | /* Lock after many login fails */ |
||
27 | const LOCK_TRIES = 5; |
||
28 | const LOCK_TIME_MINUTES = 10; |
||
29 | |||
30 | private static $passwordSalt = 'E50H%gDui#'; |
||
31 | private $id; |
||
32 | private $isEnabled; |
||
33 | private $isLogged; |
||
34 | private $accessLevel; |
||
35 | private $name; |
||
36 | private $email; |
||
37 | private $confirmEmail; |
||
38 | private $password; |
||
39 | private $confirmPassword; |
||
40 | private $passwordHash; |
||
41 | private $recoreryHash; |
||
42 | |||
43 | /** @var Date */ |
||
44 | private $loginDate; |
||
45 | |||
46 | /** @var Date */ |
||
47 | private $loginLockDate; |
||
48 | public $loginFailCount = 0; |
||
49 | |||
50 | /** @var Image */ |
||
51 | private $image; |
||
52 | |||
53 | /** @var Group */ |
||
54 | private $group; |
||
55 | private $groupId; |
||
56 | |||
57 | /** @var Person */ |
||
58 | private $person; |
||
59 | |||
60 | public function __construct() { |
||
80 | |||
81 | public function getId() { |
||
84 | |||
85 | public function isEnabled() { |
||
88 | |||
89 | public function isLogged() { |
||
92 | |||
93 | public function getAccessLevel() { |
||
96 | |||
97 | public function accessIsDenied() { |
||
100 | |||
101 | /** @return boolean */ |
||
102 | public function isAdmin() { |
||
105 | |||
106 | public function getGroup() { |
||
112 | |||
113 | public function getGroupId() { |
||
116 | |||
117 | /** @return Person */ |
||
118 | public function getPerson() { |
||
125 | |||
126 | public function getName() { |
||
129 | |||
130 | public function getEmail() { |
||
133 | |||
134 | public function getConfirmEmail() { |
||
137 | |||
138 | public function getPassword() { |
||
141 | |||
142 | public function getConfirmPassword() { |
||
145 | |||
146 | public function getPasswordHash() { |
||
149 | |||
150 | public function getRecoreryHash() { |
||
153 | |||
154 | public function getImage() { |
||
157 | |||
158 | public function getLoginDate() { |
||
161 | |||
162 | public function getLoginLockDate() { |
||
165 | |||
166 | /** @return Date retorna data que poderá logar novamente sem bloqueio */ |
||
167 | public function getLoginUnlockDate() { |
||
172 | |||
173 | public function getLockedMsg() { |
||
176 | |||
177 | public function setId($id) { |
||
180 | |||
181 | public function setEnabled($enabled) { |
||
184 | |||
185 | public function setAccessLevel($accessLevel) { |
||
190 | |||
191 | public function setGroup(Group $group) { |
||
194 | |||
195 | public function setGroupId($groupId) { |
||
198 | |||
199 | public function setPerson(Person $person) { |
||
202 | |||
203 | public function setName($name) { |
||
206 | |||
207 | public function setEmail($email) { |
||
210 | |||
211 | public function setConfirmEmail($confirmEmail) { |
||
214 | |||
215 | public function setPassword($password) { |
||
219 | |||
220 | public function setConfirmPassword($confirmPassword) { |
||
223 | |||
224 | public function setPasswordHash($passwordHash) { |
||
227 | |||
228 | public function setRecoreryHash($recoreryHash) { |
||
231 | |||
232 | public function setLoginDate(Date $loginDate) { |
||
235 | |||
236 | public function setImage(Image $image) { |
||
239 | |||
240 | /** |
||
241 | * Tenta realizar login |
||
242 | * @return boolean |
||
243 | */ |
||
244 | public function login() { |
||
266 | |||
267 | /** Realiza logout */ |
||
268 | public function logout() { |
||
271 | |||
272 | private function incrementLoginFail() { |
||
279 | |||
280 | /** @return boolean retorna TRUE se está bloqueado por tentativas de login */ |
||
281 | public function isLocked() { |
||
285 | |||
286 | /** @return int total de tentativas restantes até ser bloqueado */ |
||
287 | public function getLoginTriesLeft() { |
||
290 | |||
291 | /** Objeto > Sessão */ |
||
292 | private function setCurrentUser(User $user) { |
||
300 | |||
301 | /** Objeto < Sessão */ |
||
302 | public static function getCurrentUser() { |
||
305 | |||
306 | /** Obriga o usuário a se logar */ |
||
307 | public function requireLogin() { |
||
312 | |||
313 | /** Obriga o usuário a logar como ADMIN */ |
||
314 | public function requireAdmin() { |
||
320 | |||
321 | /** |
||
322 | * Envia link de recuperacao de senha via Email |
||
323 | * @return string | null |
||
324 | */ |
||
325 | public function sendRecoveryHash() { |
||
344 | |||
345 | /** Define os atributos que são salvos na SESSAO */ |
||
346 | public function __sleep() { |
||
349 | |||
350 | /** |
||
351 | * Adiciona maior segura na senha/ utilizar esta função ao inves de um simples md5 |
||
352 | * @param string $password |
||
353 | */ |
||
354 | public static function encryptPassword($password) { |
||
357 | |||
358 | /** |
||
359 | * Retorna uma senha aleatoria |
||
360 | * A senha tem sempre pelo menos: 1 caracter especial e 2 numeros; |
||
361 | * @param int $length |
||
362 | * @return string |
||
363 | */ |
||
364 | public static function generatePassword($length = 6) { |
||
375 | |||
376 | } |
||
377 |
This check looks for the bodies of
if
statements 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
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.