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 = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array $admin |
||
| 31 | */ |
||
| 32 | private $admin = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var bool $authorized |
||
| 36 | */ |
||
| 37 | private $authorized = FALSE; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var bool $checked |
||
| 41 | */ |
||
| 42 | private $checked = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array $session |
||
| 46 | */ |
||
| 47 | protected $session; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Constructor por defecto |
||
| 51 | */ |
||
| 52 | 3 | public function __construct() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Método estático que devuelve los perfiles de la plataforma |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | 1 | public static function getProfiles() |
|
| 74 | { |
||
| 75 | return array( |
||
| 76 | 1 | self::ADMIN_ID_TOKEN => _('Administrador'), |
|
| 77 | 1 | self::MANAGER_ID_TOKEN => _('Gestor'), |
|
| 78 | 1 | self::USER_ID_TOKEN => _('Usuario'), |
|
| 79 | 1 | ); |
|
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Method that returns all the available profiles |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | public function getAdminProfiles() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Método estático que devuelve los perfiles disponibles |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | public static function getCleanProfiles() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Método estático que devuelve los perfiles disponibles |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | public function getAdminCleanProfiles() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Método que guarda los administradores |
||
| 115 | * |
||
| 116 | * @param array $user |
||
| 117 | * |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | public static function save($user) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Method to save a new admin user |
||
| 134 | * @param array $user |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | public function saveUser($user) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Servicio que actualiza los datos del usuario |
||
| 148 | * |
||
| 149 | * @param $user |
||
| 150 | */ |
||
| 151 | public function updateUser($user) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Método que devuelve los administradores de una plataforma |
||
| 158 | * @return array|mixed |
||
| 159 | */ |
||
| 160 | 1 | public function getAdmins() |
|
| 161 | { |
||
| 162 | 1 | $admins = array(); |
|
| 163 | 1 | View Code Duplication | if (file_exists(CONFIG_DIR . DIRECTORY_SEPARATOR . 'admins.json')) { |
| 164 | $admins = json_decode(file_get_contents(CONFIG_DIR . DIRECTORY_SEPARATOR . 'admins.json'), TRUE); |
||
| 165 | } |
||
| 166 | |||
| 167 | 1 | return $admins; |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Método que devuelve si un usuario tiene privilegios para acceder a la zona de administración |
||
| 172 | * |
||
| 173 | * @param null $user |
||
| 174 | * @param null $pass |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | * @throws \HttpException |
||
| 178 | */ |
||
| 179 | 1 | public function checkAdmin($user = NULL, $pass = NULL) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | protected function getAdminFromCookie() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Método privado para la generación del hash de la cookie de administración |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function getHash() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Método que devuelve el usuario logado |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public function getUser() |
||
| 239 | { |
||
| 240 | return $this->user; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Método que devuelve el usuario administrador logado |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | public function getAdmin() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Método que calcula si se está logado o para acceder a administración |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | 1 | public function canAccessRestrictedAdmin() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
| 263 | * |
||
| 264 | * @param string|null $route |
||
| 265 | * |
||
| 266 | * @return string|null |
||
| 267 | */ |
||
| 268 | public function notAuthorized($route) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Servicio que chequea si un usuario es super administrador o no |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | public function isSuperAdmin() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Servicio que devuelve un dato de sesión |
||
| 295 | * |
||
| 296 | * @param string $key |
||
| 297 | * |
||
| 298 | * @return mixed |
||
| 299 | */ |
||
| 300 | 2 | public function getSessionKey($key) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Servicio que setea una variable de sesión |
||
| 312 | * |
||
| 313 | * @param string $key |
||
| 314 | * @param mixed $data |
||
| 315 | * |
||
| 316 | * @return Security |
||
| 317 | */ |
||
| 318 | 2 | public function setSessionKey($key, $data = NULL) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Servicio que devuelve los mensajes flash de sesiones |
||
| 327 | * @return mixed |
||
| 328 | */ |
||
| 329 | public function getFlashes() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Servicio que limpia los mensajes flash |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | 1 | public function clearFlashes() |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Servicio que inserta un flash en sesión |
||
| 349 | * |
||
| 350 | * @param string $key |
||
| 351 | * @param mixed $data |
||
| 352 | */ |
||
| 353 | public function setFlash($key, $data = NULL) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Servicio que devuelve un flash de sesión |
||
| 365 | * |
||
| 366 | * @param string $key |
||
| 367 | * |
||
| 368 | * @return mixed |
||
| 369 | */ |
||
| 370 | public function getFlash($key) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Servicio que actualiza |
||
| 379 | * |
||
| 380 | * @param boolean $closeSession |
||
| 381 | * |
||
| 382 | * @return Security |
||
| 383 | */ |
||
| 384 | public function updateSession($closeSession = FALSE) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Servicio que limpia la sesión |
||
| 400 | */ |
||
| 401 | public function closeSession() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Extract parts from token |
||
| 409 | * @param string $token |
||
| 410 | * |
||
| 411 | * @return array |
||
| 412 | */ |
||
| 413 | private static function extractTokenParts($token) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Extract Ts and Module from token |
||
| 432 | * @param array $parts |
||
| 433 | * @param int $partLength |
||
| 434 | * |
||
| 435 | * @return array |
||
| 436 | */ |
||
| 437 | private static function extractTsAndMod(array &$parts, $partLength) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Decode token to check authorized request |
||
| 453 | * @param string $token |
||
| 454 | * @param string $module |
||
| 455 | * |
||
| 456 | * @return null|string |
||
| 457 | */ |
||
| 458 | private static function decodeToken($token, $module = 'PSFS') |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Generate a authorized token |
||
| 472 | * @param string $secret |
||
| 473 | * @param string $module |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | public static function generateToken($secret, $module = 'PSFS') |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Checks if auth token is correct |
||
| 495 | * @param string $token |
||
| 496 | * @param string $secret |
||
| 497 | * @param string $module |
||
| 498 | * |
||
| 499 | * @return bool |
||
| 500 | */ |
||
| 501 | public static function checkToken($token, $secret, $module = 'PSFS') |
||
| 512 | |||
| 513 | } |
||
| 514 |
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.