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 |
||
17 | class User { |
||
18 | |||
19 | const ACCESS_DENIED = 0; |
||
20 | const ACCESS_ALLOWED = 1; |
||
21 | const ACCESS_ADMIN = 2; |
||
22 | |||
23 | /* Lock after many login fails */ |
||
24 | const LOCK_TRIES = 5; |
||
25 | const LOCK_TIME_MINUTES = 10; |
||
26 | |||
27 | public $id; |
||
28 | public $isEnabled; |
||
29 | private $isLogged; |
||
30 | public $accessLevel; |
||
31 | public $name; |
||
32 | private $email; |
||
33 | private $confirmEmail; |
||
34 | private $password; |
||
35 | private $confirmPassword; |
||
36 | public $passwordHash; |
||
37 | public $recoreryHash; |
||
38 | |||
39 | /** @var Date */ |
||
40 | public $loginDate; |
||
41 | |||
42 | /** @var Date */ |
||
43 | public $loginLockDate; |
||
44 | public $loginFailCount = 0; |
||
45 | |||
46 | /** @var Image */ |
||
47 | public $image; |
||
48 | |||
49 | /** @var Group */ |
||
50 | private $group; |
||
51 | public $groupId; |
||
52 | |||
53 | /** @var Person */ |
||
54 | private $person; |
||
55 | |||
56 | public function __construct() { |
||
76 | |||
77 | public function getId() { |
||
80 | |||
81 | public function isLogged() { |
||
84 | |||
85 | public function accessIsDenied() { |
||
88 | |||
89 | /** @return boolean */ |
||
90 | public function isAdmin() { |
||
93 | |||
94 | public function getGroup() { |
||
100 | |||
101 | public function getPassword() { |
||
104 | |||
105 | public function getEmail() { |
||
108 | |||
109 | /** @return Person */ |
||
110 | public function getPerson() { |
||
117 | |||
118 | /** @return Date retorna data que poderá logar novamente sem bloqueio */ |
||
119 | public function getLoginUnlockDate() { |
||
124 | |||
125 | public function getLockedMsg() { |
||
128 | |||
129 | public function setId($id) { |
||
132 | |||
133 | public function setPerson(Person $person) { |
||
136 | |||
137 | public function setEmail($email, $confirmEmail = null) { |
||
141 | |||
142 | public function setPassword($password, $confirmPassword = null) { |
||
147 | |||
148 | /** |
||
149 | * Tenta realizar login |
||
150 | * @return boolean |
||
151 | */ |
||
152 | public function login() { |
||
174 | |||
175 | /** @return boolean TRUE se preencheu os emails iguais */ |
||
176 | public function confirmEmail() { |
||
179 | |||
180 | /** @return boolean TRUE se preencheu as senhas iguais */ |
||
181 | public function confirmPassword() { |
||
184 | |||
185 | /** Realiza logout */ |
||
186 | public function logout() { |
||
189 | |||
190 | private function incrementLoginFail() { |
||
197 | |||
198 | /** @return boolean retorna TRUE se está bloqueado por tentativas de login */ |
||
199 | public function isLocked() { |
||
203 | |||
204 | /** @return int total de tentativas restantes até ser bloqueado */ |
||
205 | public function getLoginTriesLeft() { |
||
208 | |||
209 | /** Objeto > Sessão */ |
||
210 | private function setCurrentUser(User $user) { |
||
218 | |||
219 | /** Objeto < Sessão */ |
||
220 | public static function getCurrentUser() { |
||
226 | |||
227 | /** |
||
228 | * Evita que um usuário seja removido e continue logado |
||
229 | * @param User $user |
||
230 | */ |
||
231 | private static function preventDeletedAndLogged(User $user) { |
||
241 | |||
242 | /** Obriga o usuário a se logar */ |
||
243 | public function requireLogin() { |
||
248 | |||
249 | /** Obriga o usuário a logar como ADMIN */ |
||
250 | public function requireAdmin() { |
||
256 | |||
257 | /** Define os atributos que são salvos na SESSAO */ |
||
258 | public function __sleep() { |
||
261 | |||
262 | } |
||
263 |
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.