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() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Método estático que devuelve los perfiles de la plataforma |
||
| 58 | * @return array |
||
| 59 | */ |
||
| 60 | public static function getProfiles() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Method that returns all the available profiles |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | public function getAdminProfiles() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Método estático que devuelve los perfiles disponibles |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | public static function getCleanProfiles() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Método estático que devuelve los perfiles disponibles |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | public function getAdminCleanProfiles() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Método que guarda los administradores |
||
| 102 | * |
||
| 103 | * @param array $user |
||
| 104 | * |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | public static function save($user) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Method to save a new admin user |
||
| 121 | * @param array $user |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | public function saveUser($user) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Servicio que actualiza los datos del usuario |
||
| 135 | * |
||
| 136 | * @param $user |
||
| 137 | */ |
||
| 138 | public function updateUser($user) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Método que devuelve los administradores de una plataforma |
||
| 145 | * @return array|mixed |
||
| 146 | */ |
||
| 147 | public function getAdmins() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Método que devuelve si un usuario tiene privilegios para acceder a la zona de administración |
||
| 159 | * |
||
| 160 | * @param null $user |
||
| 161 | * @param null $pass |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | * @throws \HttpException |
||
| 165 | */ |
||
| 166 | public function checkAdmin($user = NULL, $pass = NULL) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | protected function getAdminFromCookie() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Método privado para la generación del hash de la cookie de administración |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getHash() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Método que devuelve el usuario logado |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | 1 | public function getUser() |
|
| 224 | { |
||
| 225 | 1 | return $this->user; |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Método que devuelve el usuario administrador logado |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | public function getAdmin() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Método que calcula si se está logado o para acceder a administración |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | 1 | public function canAccessRestrictedAdmin() |
|
| 242 | { |
||
| 243 | 1 | return $this->admin || preg_match('/^\/admin\/login/i', Request::requestUri()); |
|
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
| 248 | * |
||
| 249 | * @param string|null $route |
||
| 250 | * |
||
| 251 | * @return string|null |
||
| 252 | */ |
||
| 253 | public function notAuthorized($route) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Servicio que chequea si un usuario es super administrador o no |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | public function isSuperAdmin() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Servicio que devuelve un dato de sesión |
||
| 280 | * |
||
| 281 | * @param string $key |
||
| 282 | * |
||
| 283 | * @return mixed |
||
| 284 | */ |
||
| 285 | 2 | public function getSessionKey($key) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Servicio que setea una variable de sesión |
||
| 297 | * |
||
| 298 | * @param string $key |
||
| 299 | * @param mixed $data |
||
| 300 | * |
||
| 301 | * @return Security |
||
| 302 | */ |
||
| 303 | 2 | public function setSessionKey($key, $data = NULL) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Servicio que devuelve los mensajes flash de sesiones |
||
| 312 | * @return mixed |
||
| 313 | */ |
||
| 314 | public function getFlashes() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Servicio que limpia los mensajes flash |
||
| 323 | * @return $this |
||
| 324 | */ |
||
| 325 | 1 | public function clearFlashes() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Servicio que inserta un flash en sesión |
||
| 334 | * |
||
| 335 | * @param string $key |
||
| 336 | * @param mixed $data |
||
| 337 | */ |
||
| 338 | public function setFlash($key, $data = NULL) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Servicio que devuelve un flash de sesión |
||
| 350 | * |
||
| 351 | * @param string $key |
||
| 352 | * |
||
| 353 | * @return mixed |
||
| 354 | */ |
||
| 355 | public function getFlash($key) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Servicio que actualiza |
||
| 364 | * |
||
| 365 | * @param boolean $closeSession |
||
| 366 | * |
||
| 367 | * @return Security |
||
| 368 | */ |
||
| 369 | public function updateSession($closeSession = FALSE) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Servicio que limpia la sesión |
||
| 385 | */ |
||
| 386 | public function closeSession() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Extract parts from token |
||
| 394 | * @param string $token |
||
| 395 | * |
||
| 396 | * @return array |
||
| 397 | */ |
||
| 398 | private static function extractTokenParts($token) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Extract Ts and Module from token |
||
| 417 | * @param array $parts |
||
| 418 | * @param int $partLength |
||
| 419 | * |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | private static function extractTsAndMod(array &$parts, $partLength) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Decode token to check authorized request |
||
| 438 | * @param string $token |
||
| 439 | * @param string $module |
||
| 440 | * |
||
| 441 | * @return null|string |
||
| 442 | */ |
||
| 443 | private static function decodeToken($token, $module = 'PSFS') |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Generate a authorized token |
||
| 457 | * @param string $secret |
||
| 458 | * @param string $module |
||
| 459 | * |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public static function generateToken($secret, $module = 'PSFS') |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Checks if auth token is correct |
||
| 480 | * @param string $token |
||
| 481 | * @param string $secret |
||
| 482 | * @param string $module |
||
| 483 | * |
||
| 484 | * @return bool |
||
| 485 | */ |
||
| 486 | public static function checkToken($token, $secret, $module = 'PSFS') |
||
| 497 | |||
| 498 | } |
||
| 499 |
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.