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 | |||
15 | use SingletonTrait; |
||
16 | /** |
||
17 | * @var array $user |
||
18 | */ |
||
19 | private $user; |
||
20 | |||
21 | /** |
||
22 | * @var array $admin |
||
23 | */ |
||
24 | private $admin; |
||
25 | |||
26 | private $authorized = FALSE; |
||
27 | |||
28 | protected $session; |
||
29 | |||
30 | /** |
||
31 | * Constructor por defecto |
||
32 | */ |
||
33 | 2 | public function __construct() |
|
34 | { |
||
35 | 2 | if (PHP_SESSION_NONE === session_status()) { |
|
36 | 1 | session_start(); |
|
37 | } |
||
38 | 2 | $this->session = (is_null($_SESSION)) ? array() : $_SESSION; |
|
39 | 2 | if (NULL === $this->getSessionKey('__FLASH_CLEAR__')) { |
|
40 | 2 | $this->clearFlashes(); |
|
41 | 2 | $this->setSessionKey('__FLASH_CLEAR__', microtime(TRUE)); |
|
42 | 2 | } |
|
43 | 2 | $this->user = (array_key_exists(sha1('USER'), $this->session)) ? unserialize($this->session[sha1('USER')]) : NULL; |
|
|
|||
44 | 2 | $this->admin = (array_key_exists(sha1('ADMIN'), $this->session)) ? unserialize($this->session[sha1('ADMIN')]) : NULL; |
|
45 | 2 | } |
|
46 | |||
47 | /** |
||
48 | * Método estático que devuelve los perfiles de la plataforma |
||
49 | * @return array |
||
50 | */ |
||
51 | public static function getProfiles() |
||
58 | |||
59 | /** |
||
60 | * Método estático que devuelve los perfiles disponibles |
||
61 | * @return array |
||
62 | */ |
||
63 | public static function getCleanProfiles() |
||
70 | |||
71 | /** |
||
72 | * Método que guarda los administradores |
||
73 | * |
||
74 | * @param $user |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | public static function save($user) |
||
89 | |||
90 | /** |
||
91 | * Servicio que actualiza los datos del usuario |
||
92 | * |
||
93 | * @param $user |
||
94 | */ |
||
95 | public function updateUser($user) |
||
99 | |||
100 | /** |
||
101 | * Método que devuelve los administradores de una plataforma |
||
102 | * @return array|mixed |
||
103 | */ |
||
104 | public function getAdmins() |
||
113 | |||
114 | /** |
||
115 | * Método que devuelve si un usuario tiene privilegios para acceder a la zona de administración |
||
116 | * |
||
117 | * @param null $user |
||
118 | * @param null $pass |
||
119 | * |
||
120 | * @return bool |
||
121 | * @throws \HttpException |
||
122 | */ |
||
123 | public function checkAdmin($user = NULL, $pass = NULL) |
||
151 | |||
152 | /** |
||
153 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
154 | * @return array |
||
155 | */ |
||
156 | protected function getAdminFromCookie() |
||
166 | |||
167 | /** |
||
168 | * Método privado para la generación del hash de la cookie de administración |
||
169 | * @return string |
||
170 | */ |
||
171 | public function getHash() |
||
175 | |||
176 | /** |
||
177 | * Método que devuelve el usuario logado |
||
178 | * @return user |
||
179 | */ |
||
180 | public function getUser() |
||
184 | |||
185 | /** |
||
186 | * Método que devuelve el usuario administrador logado |
||
187 | * @return array |
||
188 | */ |
||
189 | public function getAdmin() |
||
193 | |||
194 | /** |
||
195 | * Método que calcula si se está logado o para acceder a administración |
||
196 | * @return bool |
||
197 | */ |
||
198 | public function canAccessRestrictedAdmin() |
||
202 | |||
203 | /** |
||
204 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
205 | * |
||
206 | * @param string|null $route |
||
207 | * |
||
208 | * @return string|null |
||
209 | */ |
||
210 | public function notAuthorized($route) |
||
216 | |||
217 | /** |
||
218 | * Servicio que chequea si un usuario es super administrador o no |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function isSuperAdmin() |
||
234 | |||
235 | /** |
||
236 | * Servicio que devuelve un dato de sesión |
||
237 | * |
||
238 | * @param string $key |
||
239 | * |
||
240 | * @return mixed |
||
241 | */ |
||
242 | 1 | public function getSessionKey($key) |
|
251 | |||
252 | /** |
||
253 | * Servicio que setea una variable de sesión |
||
254 | * |
||
255 | * @param string $key |
||
256 | * @param mixed $data |
||
257 | * |
||
258 | * @return Security |
||
259 | */ |
||
260 | 1 | public function setSessionKey($key, $data = NULL) |
|
266 | |||
267 | /** |
||
268 | * Servicio que devuelve los mensajes flash de sesiones |
||
269 | * @return mixed |
||
270 | */ |
||
271 | public function getFlashes() |
||
277 | |||
278 | /** |
||
279 | * Servicio que limpia los mensajes flash |
||
280 | * @return $this |
||
281 | */ |
||
282 | 1 | public function clearFlashes() |
|
288 | |||
289 | /** |
||
290 | * Servicio que inserta un flash en sesión |
||
291 | * |
||
292 | * @param string $key |
||
293 | * @param mixed $data |
||
294 | */ |
||
295 | public function setFlash($key, $data = NULL) |
||
304 | |||
305 | /** |
||
306 | * Servicio que devuelve un flash de sesión |
||
307 | * |
||
308 | * @param string $key |
||
309 | * |
||
310 | * @return mixed |
||
311 | */ |
||
312 | public function getFlash($key) |
||
318 | |||
319 | /** |
||
320 | * Servicio que actualiza |
||
321 | * |
||
322 | * @param boolean $closeSession |
||
323 | * |
||
324 | * @return Security |
||
325 | */ |
||
326 | public function updateSession($closeSession = FALSE) |
||
339 | |||
340 | /** |
||
341 | * Servicio que limpia la sesión |
||
342 | */ |
||
343 | public function closeSession() |
||
348 | |||
349 | /** |
||
350 | * Extract parts from token |
||
351 | * @param string $token |
||
352 | * |
||
353 | * @return array |
||
354 | */ |
||
355 | private static function extractTokenParts($token) |
||
371 | |||
372 | /** |
||
373 | * Extract Ts and Module from token |
||
374 | * @param array $parts |
||
375 | * @param int $partLength |
||
376 | * |
||
377 | * @return array |
||
378 | */ |
||
379 | private static function extractTsAndMod(array &$parts, $partLength) |
||
392 | |||
393 | /** |
||
394 | * Decode token to check authorized request |
||
395 | * @param string $token |
||
396 | * @param string $module |
||
397 | * |
||
398 | * @return null|string |
||
399 | */ |
||
400 | private static function decodeToken($token, $module = 'PSFS') |
||
411 | |||
412 | /** |
||
413 | * Generate a authorized token |
||
414 | * @param string $secret |
||
415 | * @param string $module |
||
416 | * |
||
417 | * @return string |
||
418 | */ |
||
419 | public static function generateToken($secret, $module = 'PSFS') |
||
434 | |||
435 | /** |
||
436 | * Checks if auth token is correct |
||
437 | * @param string $token |
||
438 | * @param string $secret |
||
439 | * @param string $module |
||
440 | * |
||
441 | * @return bool |
||
442 | */ |
||
443 | public static function checkToken($token, $secret, $module = 'PSFS') |
||
454 | |||
455 | } |
||
456 |
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.