Complex classes like Session 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 Session, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class Session { |
||
| 40 | use |
||
| 41 | CRUD, |
||
| 42 | Singleton; |
||
| 43 | const INIT_STATE_METHOD = 'init'; |
||
| 44 | const INITIAL_SESSION_EXPIRATION = 300; |
||
| 45 | /** |
||
| 46 | * Id of current session |
||
| 47 | * |
||
| 48 | * @var false|string |
||
| 49 | */ |
||
| 50 | protected $session_id; |
||
| 51 | /** |
||
| 52 | * User id of current session |
||
| 53 | * |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | protected $user_id; |
||
| 57 | /** |
||
| 58 | * @var bool |
||
| 59 | */ |
||
| 60 | protected $is_admin; |
||
| 61 | /** |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | protected $is_user; |
||
| 65 | /** |
||
| 66 | * @var bool |
||
| 67 | */ |
||
| 68 | protected $is_bot; |
||
| 69 | /** |
||
| 70 | * @var bool |
||
| 71 | */ |
||
| 72 | protected $is_guest; |
||
| 73 | /** |
||
| 74 | * @var Cache_prefix |
||
| 75 | */ |
||
| 76 | protected $cache; |
||
| 77 | /** |
||
| 78 | * @var Cache_prefix |
||
| 79 | */ |
||
| 80 | protected $users_cache; |
||
| 81 | protected $data_model = [ |
||
| 82 | 'id' => 'text', |
||
| 83 | 'user' => 'int:0', |
||
| 84 | 'created' => 'int:0', |
||
| 85 | 'expire' => 'int:0', |
||
| 86 | 'user_agent' => 'text', |
||
| 87 | 'remote_addr' => 'text', |
||
| 88 | 'ip' => 'text', |
||
| 89 | 'data' => 'json' |
||
| 90 | ]; |
||
| 91 | protected $table = '[prefix]sessions'; |
||
| 92 | /** |
||
| 93 | * Returns database index |
||
| 94 | * |
||
| 95 | * @return int |
||
| 96 | */ |
||
| 97 | protected function cdb () { |
||
| 100 | /** |
||
| 101 | * Use cookie as source of session id, load session |
||
| 102 | * |
||
| 103 | * Bots detection is also done here |
||
| 104 | */ |
||
| 105 | protected function init () { |
||
| 128 | /** |
||
| 129 | * Try to determine whether visitor is a known bot, bots have no sessions |
||
| 130 | */ |
||
| 131 | protected function bots_detection () { |
||
| 165 | /** |
||
| 166 | * Get list of all bots |
||
| 167 | * |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | protected function all_bots () { |
||
| 193 | /** |
||
| 194 | * Check whether user agent and IP (login and email for bots) corresponds to passed bot data |
||
| 195 | * |
||
| 196 | * @param array $bot |
||
| 197 | * @param string $login |
||
| 198 | * @param string $email |
||
| 199 | * |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | protected function is_this_bot ($bot, $login, $email) { |
||
| 219 | /** |
||
| 220 | * Updates information about who is user accessed by methods ::guest() ::bot() ::user() admin() |
||
| 221 | */ |
||
| 222 | protected function update_user_is () { |
||
| 245 | /** |
||
| 246 | * Is admin |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | function admin () { |
||
| 253 | /** |
||
| 254 | * Is user |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | function user () { |
||
| 261 | /** |
||
| 262 | * Is guest |
||
| 263 | * |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | function guest () { |
||
| 269 | /** |
||
| 270 | * Is bot |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | function bot () { |
||
| 277 | /** |
||
| 278 | * Returns id of current session |
||
| 279 | * |
||
| 280 | * @return false|string |
||
| 281 | */ |
||
| 282 | function get_id () { |
||
| 288 | /** |
||
| 289 | * Returns user id of current session |
||
| 290 | * |
||
| 291 | * @return int |
||
| 292 | */ |
||
| 293 | function get_user () { |
||
| 296 | /** |
||
| 297 | * Returns session details by session id |
||
| 298 | * |
||
| 299 | * @param false|null|string $session_id If `null` - loaded from `$this->session_id`, and if that also empty - from cookies |
||
| 300 | * |
||
| 301 | * @return false|array |
||
| 302 | */ |
||
| 303 | function get ($session_id) { |
||
| 310 | /** |
||
| 311 | * @param false|null|string $session_id |
||
| 312 | * |
||
| 313 | * @return false|array |
||
| 314 | */ |
||
| 315 | protected function get_internal ($session_id) { |
||
| 338 | /** |
||
| 339 | * Check whether session was not expired, user agent and IP corresponds to what is expected and user is actually active |
||
| 340 | * |
||
| 341 | * @param mixed $session_data |
||
| 342 | * |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | protected function is_good_session ($session_data) { |
||
| 351 | /** |
||
| 352 | * Whether session data belongs to current visitor (user agent, remote addr and ip check) |
||
| 353 | * |
||
| 354 | * @param string $session_id |
||
| 355 | * @param string $user_agent |
||
| 356 | * @param string $remote_addr |
||
| 357 | * @param string $ip |
||
| 358 | * |
||
| 359 | * @return bool |
||
| 360 | */ |
||
| 361 | function is_session_owner ($session_id, $user_agent, $remote_addr, $ip) { |
||
| 365 | /** |
||
| 366 | * Whether session data belongs to current visitor (user agent, remote addr and ip check) |
||
| 367 | * |
||
| 368 | * @param array $session_data |
||
| 369 | * @param string|null $user_agent |
||
| 370 | * @param string|null $remote_addr |
||
| 371 | * @param string|null $ip |
||
| 372 | * |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | protected function is_session_owner_internal ($session_data, $user_agent = null, $remote_addr = null, $ip = null) { |
||
| 395 | /** |
||
| 396 | * Load session by id and return id of session owner (user), update session expiration |
||
| 397 | * |
||
| 398 | * @param false|null|string $session_id If not specified - loaded from `$this->session_id`, and if that also empty - from cookies |
||
| 399 | * |
||
| 400 | * @return int User id |
||
| 401 | */ |
||
| 402 | function load ($session_id = null) { |
||
| 430 | /** |
||
| 431 | * Initialize session (set user id, session id and update who user is) |
||
| 432 | * |
||
| 433 | * @param string $session_id |
||
| 434 | * @param int $user_id |
||
| 435 | * |
||
| 436 | * @return int User id |
||
| 437 | */ |
||
| 438 | protected function load_initialization ($session_id, $user_id) { |
||
| 444 | /** |
||
| 445 | * Whether profile is activated, not disabled and not blocked |
||
| 446 | * |
||
| 447 | * @param int $user |
||
| 448 | * |
||
| 449 | * @return bool |
||
| 450 | */ |
||
| 451 | protected function is_user_active ($user) { |
||
| 495 | /** |
||
| 496 | * Create the session for the user with specified id |
||
| 497 | * |
||
| 498 | * @param int $user |
||
| 499 | * @param bool $delete_current_session |
||
| 500 | * |
||
| 501 | * @return false|string Session id on success, `false` otherwise |
||
| 502 | */ |
||
| 503 | function add ($user, $delete_current_session = true) { |
||
| 535 | /** |
||
| 536 | * @param int $user |
||
| 537 | * |
||
| 538 | * @return array Session data |
||
| 539 | */ |
||
| 540 | protected function create_unique_session ($user) { |
||
| 568 | /** |
||
| 569 | * Destroying of the session |
||
| 570 | * |
||
| 571 | * @param null|string $session_id |
||
| 572 | * |
||
| 573 | * @return bool |
||
| 574 | */ |
||
| 575 | function del ($session_id = null) { |
||
| 578 | /** |
||
| 579 | * Deletion of the session |
||
| 580 | * |
||
| 581 | * @param string|null $session_id |
||
| 582 | * @param bool $create_guest_session |
||
| 583 | * |
||
| 584 | * @return bool |
||
| 585 | */ |
||
| 586 | protected function del_internal ($session_id = null, $create_guest_session = true) { |
||
| 614 | /** |
||
| 615 | * Delete all old sessions from DB |
||
| 616 | */ |
||
| 617 | protected function delete_old_sessions () { |
||
| 623 | /** |
||
| 624 | * Deletion of all user sessions |
||
| 625 | * |
||
| 626 | * @param false|int $user If not specified - current user assumed |
||
| 627 | * |
||
| 628 | * @return bool |
||
| 629 | */ |
||
| 630 | function del_all ($user = false) { |
||
| 653 | /** |
||
| 654 | * Get data, stored with session |
||
| 655 | * |
||
| 656 | * @param string $item |
||
| 657 | * @param null|string $session_id |
||
| 658 | * |
||
| 659 | * @return false|mixed |
||
| 660 | * |
||
| 661 | */ |
||
| 662 | function get_data ($item, $session_id = null) { |
||
| 666 | /* |
||
| 667 | * @param null|string $session_id |
||
| 668 | * |
||
| 669 | * @return array|false |
||
| 670 | */ |
||
| 671 | protected function get_data_internal ($session_id) { |
||
| 675 | /** |
||
| 676 | * Store data with session |
||
| 677 | * |
||
| 678 | * @param string $item |
||
| 679 | * @param mixed $value |
||
| 680 | * @param null|string $session_id |
||
| 681 | * |
||
| 682 | * @return bool |
||
| 683 | * |
||
| 684 | */ |
||
| 685 | function set_data ($item, $value, $session_id = null) { |
||
| 700 | /** |
||
| 701 | * Delete data, stored with session |
||
| 702 | * |
||
| 703 | * @param string $item |
||
| 704 | * @param null|string $session_id |
||
| 705 | * |
||
| 706 | * @return bool |
||
| 707 | * |
||
| 708 | */ |
||
| 709 | function del_data ($item, $session_id = null) { |
||
| 716 | } |
||
| 717 |