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 | */ |
||
| 71 | public function getAdminProfiles() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Método estático que devuelve los perfiles disponibles |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | public static function getCleanProfiles() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Método estático que devuelve los perfiles disponibles |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | public function getAdminCleanProfiles() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Método que guarda los administradores |
||
| 100 | * |
||
| 101 | * @param array $user |
||
| 102 | * |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | public static function save($user) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Method to save a new admin user |
||
| 119 | * @param array $user |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | public function saveUser($user) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Servicio que actualiza los datos del usuario |
||
| 133 | * |
||
| 134 | * @param $user |
||
| 135 | */ |
||
| 136 | public function updateUser($user) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Método que devuelve los administradores de una plataforma |
||
| 143 | * @return array|mixed |
||
| 144 | */ |
||
| 145 | public function getAdmins() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Método que devuelve si un usuario tiene privilegios para acceder a la zona de administración |
||
| 157 | * |
||
| 158 | * @param null $user |
||
| 159 | * @param null $pass |
||
| 160 | * |
||
| 161 | * @return bool |
||
| 162 | * @throws \HttpException |
||
| 163 | */ |
||
| 164 | public function checkAdmin($user = NULL, $pass = NULL) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | protected function getAdminFromCookie() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Método privado para la generación del hash de la cookie de administración |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function getHash() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Método que devuelve el usuario logado |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function getUser() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Método que devuelve el usuario administrador logado |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | public function getAdmin() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Método que calcula si se está logado o para acceder a administración |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | public function canAccessRestrictedAdmin() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
| 246 | * |
||
| 247 | * @param string|null $route |
||
| 248 | * |
||
| 249 | * @return string|null |
||
| 250 | */ |
||
| 251 | public function notAuthorized($route) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Servicio que chequea si un usuario es super administrador o no |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | public function isSuperAdmin() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Servicio que devuelve un dato de sesión |
||
| 278 | * |
||
| 279 | * @param string $key |
||
| 280 | * |
||
| 281 | * @return mixed |
||
| 282 | */ |
||
| 283 | 2 | public function getSessionKey($key) |
|
| 284 | { |
||
| 285 | 2 | $data = NULL; |
|
| 286 | 2 | if (array_key_exists($key, $this->session)) { |
|
| 287 | 1 | $data = $this->session[$key]; |
|
| 288 | 1 | } |
|
| 289 | |||
| 290 | 2 | return $data; |
|
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Servicio que setea una variable de sesión |
||
| 295 | * |
||
| 296 | * @param string $key |
||
| 297 | * @param mixed $data |
||
| 298 | * |
||
| 299 | * @return Security |
||
| 300 | */ |
||
| 301 | 2 | public function setSessionKey($key, $data = NULL) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Servicio que devuelve los mensajes flash de sesiones |
||
| 310 | * @return mixed |
||
| 311 | */ |
||
| 312 | public function getFlashes() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Servicio que limpia los mensajes flash |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | 1 | public function clearFlashes() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Servicio que inserta un flash en sesión |
||
| 332 | * |
||
| 333 | * @param string $key |
||
| 334 | * @param mixed $data |
||
| 335 | */ |
||
| 336 | public function setFlash($key, $data = NULL) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Servicio que devuelve un flash de sesión |
||
| 348 | * |
||
| 349 | * @param string $key |
||
| 350 | * |
||
| 351 | * @return mixed |
||
| 352 | */ |
||
| 353 | public function getFlash($key) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Servicio que actualiza |
||
| 362 | * |
||
| 363 | * @param boolean $closeSession |
||
| 364 | * |
||
| 365 | * @return Security |
||
| 366 | */ |
||
| 367 | public function updateSession($closeSession = FALSE) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Servicio que limpia la sesión |
||
| 383 | */ |
||
| 384 | public function closeSession() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Extract parts from token |
||
| 392 | * @param string $token |
||
| 393 | * |
||
| 394 | * @return array |
||
| 395 | */ |
||
| 396 | private static function extractTokenParts($token) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Extract Ts and Module from token |
||
| 415 | * @param array $parts |
||
| 416 | * @param int $partLength |
||
| 417 | * |
||
| 418 | * @return array |
||
| 419 | */ |
||
| 420 | private static function extractTsAndMod(array &$parts, $partLength) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Decode token to check authorized request |
||
| 436 | * @param string $token |
||
| 437 | * @param string $module |
||
| 438 | * |
||
| 439 | * @return null|string |
||
| 440 | */ |
||
| 441 | private static function decodeToken($token, $module = 'PSFS') |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Generate a authorized token |
||
| 455 | * @param string $secret |
||
| 456 | * @param string $module |
||
| 457 | * |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | public static function generateToken($secret, $module = 'PSFS') |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Checks if auth token is correct |
||
| 478 | * @param string $token |
||
| 479 | * @param string $secret |
||
| 480 | * @param string $module |
||
| 481 | * |
||
| 482 | * @return bool |
||
| 483 | */ |
||
| 484 | public static function checkToken($token, $secret, $module = 'PSFS') |
||
| 495 | |||
| 496 | } |
||
| 497 |
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.