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 |
||
| 13 | class Security |
||
| 14 | { |
||
| 15 | // sha1('user') |
||
| 16 | const USER_ID_TOKEN = '12dea96fec20593566ab75692c9949596833adc9'; |
||
| 17 | // sha1('admin') |
||
| 18 | const MANAGER_ID_TOKEN = 'd033e22ae348aeb5660fc2140aec35850c4da997'; |
||
| 19 | // sha1('superadmin') |
||
| 20 | const ADMIN_ID_TOKEN = '889a3a791b3875cfae413574b53da4bb8a90d53e'; |
||
| 21 | // sha1('FLASHES') |
||
| 22 | const FLASH_MESSAGE_TOKEN = '4680c68435db1bfbf17c3fcc4f7b39d2c6122504'; |
||
| 23 | |||
| 24 | use SingletonTrait; |
||
| 25 | /** |
||
| 26 | * @var array $user |
||
| 27 | */ |
||
| 28 | private $user; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array $admin |
||
| 32 | */ |
||
| 33 | private $admin; |
||
| 34 | |||
| 35 | private $authorized = FALSE; |
||
| 36 | |||
| 37 | protected $session; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Constructor por defecto |
||
| 41 | */ |
||
| 42 | 3 | public function __construct() |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Método estático que devuelve los perfiles de la plataforma |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | public static function getProfiles() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Method that returns all the available profiles |
||
| 74 | * @return array |
||
| 75 | */ |
||
| 76 | public function getAdminProfiles() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Método estático que devuelve los perfiles disponibles |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | public static function getCleanProfiles() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Método estático que devuelve los perfiles disponibles |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | public function getAdminCleanProfiles() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Método que guarda los administradores |
||
| 105 | * |
||
| 106 | * @param array $user |
||
| 107 | * |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | public static function save($user) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Method to save a new admin user |
||
| 124 | * @param array $user |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | public function saveUser($user) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Servicio que actualiza los datos del usuario |
||
| 138 | * |
||
| 139 | * @param $user |
||
| 140 | */ |
||
| 141 | public function updateUser($user) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Método que devuelve los administradores de una plataforma |
||
| 148 | * @return array|mixed |
||
| 149 | */ |
||
| 150 | public function getAdmins() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Método que devuelve si un usuario tiene privilegios para acceder a la zona de administración |
||
| 162 | * |
||
| 163 | * @param null $user |
||
| 164 | * @param null $pass |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | * @throws \HttpException |
||
| 168 | */ |
||
| 169 | public function checkAdmin($user = NULL, $pass = NULL) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | protected function getAdminFromCookie() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Método privado para la generación del hash de la cookie de administración |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | public function getHash() |
||
| 224 | |||
| 225 | 1 | /** |
|
| 226 | * Método que devuelve el usuario logado |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | public function getUser() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Método que devuelve el usuario administrador logado |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public function getAdmin() |
||
| 242 | |||
| 243 | 1 | /** |
|
| 244 | * Método que calcula si se está logado o para acceder a administración |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | public function canAccessRestrictedAdmin() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
| 254 | * |
||
| 255 | * @param string|null $route |
||
| 256 | * |
||
| 257 | * @return string|null |
||
| 258 | */ |
||
| 259 | public function notAuthorized($route) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Servicio que chequea si un usuario es super administrador o no |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | public function isSuperAdmin() |
||
| 283 | |||
| 284 | /** |
||
| 285 | 2 | * Servicio que devuelve un dato de sesión |
|
| 286 | * |
||
| 287 | 2 | * @param string $key |
|
| 288 | 2 | * |
|
| 289 | 1 | * @return mixed |
|
| 290 | 1 | */ |
|
| 291 | public function getSessionKey($key) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Servicio que setea una variable de sesión |
||
| 303 | 2 | * |
|
| 304 | * @param string $key |
||
| 305 | 2 | * @param mixed $data |
|
| 306 | * |
||
| 307 | 2 | * @return Security |
|
| 308 | */ |
||
| 309 | public function setSessionKey($key, $data = NULL) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Servicio que devuelve los mensajes flash de sesiones |
||
| 318 | * @return mixed |
||
| 319 | */ |
||
| 320 | public function getFlashes() |
||
| 326 | |||
| 327 | 1 | /** |
|
| 328 | * Servicio que limpia los mensajes flash |
||
| 329 | 1 | * @return $this |
|
| 330 | */ |
||
| 331 | public function clearFlashes() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Servicio que inserta un flash en sesión |
||
| 340 | * |
||
| 341 | * @param string $key |
||
| 342 | * @param mixed $data |
||
| 343 | */ |
||
| 344 | public function setFlash($key, $data = NULL) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Servicio que devuelve un flash de sesión |
||
| 356 | * |
||
| 357 | * @param string $key |
||
| 358 | * |
||
| 359 | * @return mixed |
||
| 360 | */ |
||
| 361 | public function getFlash($key) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Servicio que actualiza |
||
| 370 | * |
||
| 371 | * @param boolean $closeSession |
||
| 372 | * |
||
| 373 | * @return Security |
||
| 374 | */ |
||
| 375 | public function updateSession($closeSession = FALSE) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Servicio que limpia la sesión |
||
| 391 | */ |
||
| 392 | public function closeSession() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Extract parts from token |
||
| 400 | * @param string $token |
||
| 401 | * |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | private static function extractTokenParts($token) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Extract Ts and Module from token |
||
| 423 | * @param array $parts |
||
| 424 | * @param int $partLength |
||
| 425 | * |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | private static function extractTsAndMod(array &$parts, $partLength) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Decode token to check authorized request |
||
| 444 | * @param string $token |
||
| 445 | * @param string $module |
||
| 446 | * |
||
| 447 | * @return null|string |
||
| 448 | */ |
||
| 449 | private static function decodeToken($token, $module = 'PSFS') |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Generate a authorized token |
||
| 463 | * @param string $secret |
||
| 464 | * @param string $module |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | public static function generateToken($secret, $module = 'PSFS') |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Checks if auth token is correct |
||
| 486 | * @param string $token |
||
| 487 | * @param string $secret |
||
| 488 | * @param string $module |
||
| 489 | * |
||
| 490 | * @return bool |
||
| 491 | */ |
||
| 492 | public static function checkToken($token, $secret, $module = 'PSFS') |
||
| 503 | |||
| 504 | } |
||
| 505 |
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.