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 | const ACCESS_DENIED = 0; |
||
21 | const ACCESS_ALLOWED = 1; |
||
22 | const ACCESS_ADMIN = 2; |
||
23 | |||
24 | private static $passwordSalt = 'E50H%gDui#'; |
||
25 | private $id; |
||
26 | private $isEnabled; |
||
27 | private $isLogged; |
||
28 | private $accessLevel; |
||
29 | private $name; |
||
30 | private $email; |
||
31 | private $password; |
||
32 | private $passwordHash; |
||
33 | private $recoreryHash; |
||
34 | |||
35 | /** @var Date */ |
||
36 | private $loginDate; |
||
37 | |||
38 | /** @var Image */ |
||
39 | private $image; |
||
40 | |||
41 | /** @var Group */ |
||
42 | private $group; |
||
43 | private $groupId; |
||
44 | |||
45 | /** @var Person */ |
||
46 | private $person; |
||
47 | |||
48 | public function __construct() { |
||
65 | |||
66 | public function getId() { |
||
69 | |||
70 | public function isEnabled() { |
||
73 | |||
74 | public function isLogged() { |
||
77 | |||
78 | public function getAccessLevel() { |
||
81 | |||
82 | /** @return boolean */ |
||
83 | public function isAdmin() { |
||
84 | return ($this->accessLevel == self::ACCESS_ADMIN); |
||
85 | } |
||
86 | |||
87 | public function getGroup() { |
||
93 | |||
94 | public function getGroupId() { |
||
97 | |||
98 | /** @return Person */ |
||
99 | public function getPerson() { |
||
106 | |||
107 | public function getName() { |
||
110 | |||
111 | public function getEmail() { |
||
114 | |||
115 | public function getPassword() { |
||
118 | |||
119 | public function getPasswordHash() { |
||
122 | |||
123 | public function getRecoreryHash() { |
||
126 | |||
127 | public function getImage() { |
||
130 | |||
131 | /** @return Date */ |
||
132 | public function getLoginDate() { |
||
135 | |||
136 | public function setId($id) { |
||
139 | |||
140 | public function setEnabled($enabled) { |
||
143 | |||
144 | public function setAccessLevel($accessLevel) { |
||
147 | |||
148 | public function setGroup(Group $group) { |
||
151 | |||
152 | public function setGroupId($groupId) { |
||
155 | |||
156 | public function setPerson(Person $person) { |
||
159 | |||
160 | public function setName($name) { |
||
163 | |||
164 | public function setEmail($email) { |
||
167 | |||
168 | public function setPassword($password) { |
||
172 | |||
173 | public function setPasswordHash($passwordHash) { |
||
176 | |||
177 | public function setRecoreryHash($recoreryHash) { |
||
180 | |||
181 | public function setLoginDate($loginDate) { |
||
184 | |||
185 | public function setImage($image) { |
||
188 | |||
189 | /** |
||
190 | * Tenta realizar login |
||
191 | * @return boolean |
||
192 | */ |
||
193 | public function login() { |
||
209 | |||
210 | /** Realiza logout */ |
||
211 | public function logout() { |
||
214 | |||
215 | /** Objeto > Sessão */ |
||
216 | private function setCurrentUser(User $user) { |
||
226 | |||
227 | /** Objeto < Sessão */ |
||
228 | public static function getCurrentUser() { |
||
231 | |||
232 | /** Obriga o usuário a se logar */ |
||
233 | public function requireLogin() { |
||
238 | |||
239 | /** Obriga o usuário a logar como ADMIN */ |
||
240 | public function requireAdmin() { |
||
241 | $this->requireLogin(); |
||
242 | if ($this->getAccessLevel() != static::ACCESS_ADMIN) { |
||
243 | Application::app()->errorPage(403); |
||
244 | } |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Envia link de recuperacao de senha via Email |
||
249 | * @return string | null |
||
250 | */ |
||
251 | public function sendRecoveryHash() { |
||
270 | |||
271 | /** Define os atributos que são salvos na SESSAO */ |
||
272 | public function __sleep() { |
||
275 | |||
276 | /** |
||
277 | * Adiciona maior segura na senha/ utilizar esta função ao inves de um simples md5 |
||
278 | * @param string $password |
||
279 | */ |
||
280 | public static function encryptPassword($password) { |
||
283 | |||
284 | /** @return boolean Retorna true se já existe este email no sistema */ |
||
285 | public function emailIsDuplicated() { |
||
289 | |||
290 | /** |
||
291 | * Retorna uma senha aleatoria |
||
292 | * A senha tem sempre pelo menos: 1 caracter especial e 2 numeros; |
||
293 | * @param int $length |
||
294 | * @return string |
||
295 | */ |
||
296 | public static function generatePassword($length = 6) { |
||
307 | |||
308 | } |
||
309 |
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.