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() |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Método estático que devuelve los perfiles de la plataforma |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | public static function getProfiles() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Method that returns all the available profiles |
||
| 62 | */ |
||
| 63 | public function getAdminProfiles() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Método estático que devuelve los perfiles disponibles |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public static function getCleanProfiles() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Método estático que devuelve los perfiles disponibles |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | public function getAdminCleanProfiles() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Método que guarda los administradores |
||
| 92 | * |
||
| 93 | * @param array $user |
||
| 94 | * |
||
| 95 | * @return bool |
||
| 96 | */ |
||
| 97 | public static function save($user) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Method to save a new admin user |
||
| 111 | * @param array $user |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public function saveUser($user) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Servicio que actualiza los datos del usuario |
||
| 124 | * |
||
| 125 | * @param $user |
||
| 126 | */ |
||
| 127 | public function updateUser($user) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Método que devuelve los administradores de una plataforma |
||
| 134 | * @return array|mixed |
||
| 135 | */ |
||
| 136 | public function getAdmins() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Método que devuelve si un usuario tiene privilegios para acceder a la zona de administración |
||
| 148 | * |
||
| 149 | * @param null $user |
||
| 150 | * @param null $pass |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | * @throws \HttpException |
||
| 154 | */ |
||
| 155 | public function checkAdmin($user = NULL, $pass = NULL) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | protected function getAdminFromCookie() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Método privado para la generación del hash de la cookie de administración |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function getHash() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Método que devuelve el usuario logado |
||
| 210 | * @return user |
||
| 211 | */ |
||
| 212 | public function getUser() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Método que devuelve el usuario administrador logado |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function getAdmin() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Método que calcula si se está logado o para acceder a administración |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public function canAccessRestrictedAdmin() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
| 237 | * |
||
| 238 | * @param string|null $route |
||
| 239 | * |
||
| 240 | * @return string|null |
||
| 241 | */ |
||
| 242 | public function notAuthorized($route) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Servicio que chequea si un usuario es super administrador o no |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | public function isSuperAdmin() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Servicio que devuelve un dato de sesión |
||
| 269 | * |
||
| 270 | * @param string $key |
||
| 271 | * |
||
| 272 | * @return mixed |
||
| 273 | */ |
||
| 274 | 1 | public function getSessionKey($key) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Servicio que setea una variable de sesión |
||
| 286 | * |
||
| 287 | * @param string $key |
||
| 288 | * @param mixed $data |
||
| 289 | * |
||
| 290 | * @return Security |
||
| 291 | */ |
||
| 292 | 1 | public function setSessionKey($key, $data = NULL) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Servicio que devuelve los mensajes flash de sesiones |
||
| 301 | * @return mixed |
||
| 302 | */ |
||
| 303 | public function getFlashes() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Servicio que limpia los mensajes flash |
||
| 312 | * @return $this |
||
| 313 | */ |
||
| 314 | 1 | public function clearFlashes() |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Servicio que inserta un flash en sesión |
||
| 323 | * |
||
| 324 | * @param string $key |
||
| 325 | * @param mixed $data |
||
| 326 | */ |
||
| 327 | public function setFlash($key, $data = NULL) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Servicio que devuelve un flash de sesión |
||
| 339 | * |
||
| 340 | * @param string $key |
||
| 341 | * |
||
| 342 | * @return mixed |
||
| 343 | */ |
||
| 344 | public function getFlash($key) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Servicio que actualiza |
||
| 353 | * |
||
| 354 | * @param boolean $closeSession |
||
| 355 | * |
||
| 356 | * @return Security |
||
| 357 | */ |
||
| 358 | public function updateSession($closeSession = FALSE) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Servicio que limpia la sesión |
||
| 374 | */ |
||
| 375 | public function closeSession() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Extract parts from token |
||
| 383 | * @param string $token |
||
| 384 | * |
||
| 385 | * @return array |
||
| 386 | */ |
||
| 387 | private static function extractTokenParts($token) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Extract Ts and Module from token |
||
| 406 | * @param array $parts |
||
| 407 | * @param int $partLength |
||
| 408 | * |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | private static function extractTsAndMod(array &$parts, $partLength) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Decode token to check authorized request |
||
| 427 | * @param string $token |
||
| 428 | * @param string $module |
||
| 429 | * |
||
| 430 | * @return null|string |
||
| 431 | */ |
||
| 432 | private static function decodeToken($token, $module = 'PSFS') |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Generate a authorized token |
||
| 446 | * @param string $secret |
||
| 447 | * @param string $module |
||
| 448 | * |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | public static function generateToken($secret, $module = 'PSFS') |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Checks if auth token is correct |
||
| 469 | * @param string $token |
||
| 470 | * @param string $secret |
||
| 471 | * @param string $module |
||
| 472 | * |
||
| 473 | * @return bool |
||
| 474 | */ |
||
| 475 | public static function checkToken($token, $secret, $module = 'PSFS') |
||
| 486 | |||
| 487 | } |
||
| 488 |
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.