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 Security 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 Security, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Security |
||
13 | { |
||
14 | // sha1('user') |
||
15 | const USER_ID_TOKEN = '12dea96fec20593566ab75692c9949596833adc9'; |
||
16 | // sha1('admin') |
||
17 | const MANAGER_ID_TOKEN = 'd033e22ae348aeb5660fc2140aec35850c4da997'; |
||
18 | // sha1('superadmin') |
||
19 | const ADMIN_ID_TOKEN = '889a3a791b3875cfae413574b53da4bb8a90d53e'; |
||
20 | // sha1('FLASHES') |
||
21 | const FLASH_MESSAGE_TOKEN = '4680c68435db1bfbf17c3fcc4f7b39d2c6122504'; |
||
22 | |||
23 | use SingletonTrait; |
||
24 | /** |
||
25 | * @var array $user |
||
26 | */ |
||
27 | private $user; |
||
28 | |||
29 | /** |
||
30 | * @var array $admin |
||
31 | */ |
||
32 | private $admin; |
||
33 | |||
34 | private $authorized = FALSE; |
||
35 | |||
36 | protected $session; |
||
37 | |||
38 | /** |
||
39 | * Constructor por defecto |
||
40 | */ |
||
41 | 2 | public function __construct() |
|
54 | |||
55 | /** |
||
56 | * Método estático que devuelve los perfiles de la plataforma |
||
57 | * @return array |
||
58 | */ |
||
59 | public static function getProfiles() |
||
67 | |||
68 | /** |
||
69 | * Method that returns all the available profiles |
||
70 | * @return array |
||
71 | */ |
||
72 | public function getAdminProfiles() |
||
76 | |||
77 | /** |
||
78 | * Método estático que devuelve los perfiles disponibles |
||
79 | * @return array |
||
80 | */ |
||
81 | public static function getCleanProfiles() |
||
89 | |||
90 | /** |
||
91 | * Método estático que devuelve los perfiles disponibles |
||
92 | * @return array |
||
93 | */ |
||
94 | public function getAdminCleanProfiles() |
||
98 | |||
99 | /** |
||
100 | * Método que guarda los administradores |
||
101 | * |
||
102 | * @param array $user |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public static function save($user) |
||
117 | |||
118 | /** |
||
119 | * Method to save a new admin user |
||
120 | * @param array $user |
||
121 | * @return bool |
||
122 | */ |
||
123 | public function saveUser($user) |
||
131 | |||
132 | /** |
||
133 | * Servicio que actualiza los datos del usuario |
||
134 | * |
||
135 | * @param $user |
||
136 | */ |
||
137 | public function updateUser($user) |
||
141 | |||
142 | /** |
||
143 | * Método que devuelve los administradores de una plataforma |
||
144 | * @return array|mixed |
||
145 | */ |
||
146 | public function getAdmins() |
||
155 | |||
156 | /** |
||
157 | * Método que devuelve si un usuario tiene privilegios para acceder a la zona de administración |
||
158 | * |
||
159 | * @param null $user |
||
160 | * @param null $pass |
||
161 | * |
||
162 | * @return bool |
||
163 | * @throws \HttpException |
||
164 | */ |
||
165 | public function checkAdmin($user = NULL, $pass = NULL) |
||
193 | |||
194 | /** |
||
195 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
196 | * @return array |
||
197 | */ |
||
198 | protected function getAdminFromCookie() |
||
208 | |||
209 | /** |
||
210 | * Método privado para la generación del hash de la cookie de administración |
||
211 | * @return string |
||
212 | */ |
||
213 | public function getHash() |
||
217 | |||
218 | /** |
||
219 | * Método que devuelve el usuario logado |
||
220 | * @return array |
||
221 | */ |
||
222 | public function getUser() |
||
226 | |||
227 | /** |
||
228 | * Método que devuelve el usuario administrador logado |
||
229 | * @return array |
||
230 | */ |
||
231 | public function getAdmin() |
||
235 | |||
236 | /** |
||
237 | * Método que calcula si se está logado o para acceder a administración |
||
238 | * @return bool |
||
239 | */ |
||
240 | public function canAccessRestrictedAdmin() |
||
244 | |||
245 | /** |
||
246 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
247 | * |
||
248 | * @param string|null $route |
||
249 | * |
||
250 | * @return string|null |
||
251 | */ |
||
252 | public function notAuthorized($route) |
||
258 | |||
259 | /** |
||
260 | * Servicio que chequea si un usuario es super administrador o no |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function isSuperAdmin() |
||
276 | |||
277 | /** |
||
278 | * Servicio que devuelve un dato de sesión |
||
279 | * |
||
280 | * @param string $key |
||
281 | * |
||
282 | * @return mixed |
||
283 | */ |
||
284 | 2 | public function getSessionKey($key) |
|
293 | |||
294 | /** |
||
295 | * Servicio que setea una variable de sesión |
||
296 | * |
||
297 | * @param string $key |
||
298 | * @param mixed $data |
||
299 | * |
||
300 | * @return Security |
||
301 | */ |
||
302 | 2 | public function setSessionKey($key, $data = NULL) |
|
308 | |||
309 | /** |
||
310 | * Servicio que devuelve los mensajes flash de sesiones |
||
311 | * @return mixed |
||
312 | */ |
||
313 | public function getFlashes() |
||
319 | |||
320 | /** |
||
321 | * Servicio que limpia los mensajes flash |
||
322 | * @return $this |
||
323 | */ |
||
324 | 1 | public function clearFlashes() |
|
330 | |||
331 | /** |
||
332 | * Servicio que inserta un flash en sesión |
||
333 | * |
||
334 | * @param string $key |
||
335 | * @param mixed $data |
||
336 | */ |
||
337 | public function setFlash($key, $data = NULL) |
||
346 | |||
347 | /** |
||
348 | * Servicio que devuelve un flash de sesión |
||
349 | * |
||
350 | * @param string $key |
||
351 | * |
||
352 | * @return mixed |
||
353 | */ |
||
354 | public function getFlash($key) |
||
360 | |||
361 | /** |
||
362 | * Servicio que actualiza |
||
363 | * |
||
364 | * @param boolean $closeSession |
||
365 | * |
||
366 | * @return Security |
||
367 | */ |
||
368 | public function updateSession($closeSession = FALSE) |
||
381 | |||
382 | /** |
||
383 | * Servicio que limpia la sesión |
||
384 | */ |
||
385 | public function closeSession() |
||
390 | |||
391 | /** |
||
392 | * Extract parts from token |
||
393 | * @param string $token |
||
394 | * |
||
395 | * @return array |
||
396 | */ |
||
397 | private static function extractTokenParts($token) |
||
413 | |||
414 | /** |
||
415 | * Extract Ts and Module from token |
||
416 | * @param array $parts |
||
417 | * @param int $partLength |
||
418 | * |
||
419 | * @return array |
||
420 | */ |
||
421 | private static function extractTsAndMod(array &$parts, $partLength) |
||
434 | |||
435 | /** |
||
436 | * Decode token to check authorized request |
||
437 | * @param string $token |
||
438 | * @param string $module |
||
439 | * |
||
440 | * @return null|string |
||
441 | */ |
||
442 | private static function decodeToken($token, $module = 'PSFS') |
||
453 | |||
454 | /** |
||
455 | * Generate a authorized token |
||
456 | * @param string $secret |
||
457 | * @param string $module |
||
458 | * |
||
459 | * @return string |
||
460 | */ |
||
461 | public static function generateToken($secret, $module = 'PSFS') |
||
476 | |||
477 | /** |
||
478 | * Checks if auth token is correct |
||
479 | * @param string $token |
||
480 | * @param string $secret |
||
481 | * @param string $module |
||
482 | * |
||
483 | * @return bool |
||
484 | */ |
||
485 | public static function checkToken($token, $secret, $module = 'PSFS') |
||
496 | |||
497 | } |
||
498 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.