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 |
||
| 38 | class Session { |
||
| 39 | use |
||
| 40 | CRUD, |
||
| 41 | Singleton; |
||
| 42 | const INITIAL_SESSION_EXPIRATION = 300; |
||
| 43 | /** |
||
| 44 | * Id of current session |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $session_id; |
||
| 49 | /** |
||
| 50 | * User id of current session |
||
| 51 | * |
||
| 52 | * @var false|int |
||
| 53 | */ |
||
| 54 | protected $user_id = false; |
||
| 55 | protected $is_admin = false; |
||
| 56 | protected $is_user = false; |
||
| 57 | protected $is_bot = false; |
||
| 58 | protected $is_guest = false; |
||
| 59 | /** |
||
| 60 | * @var Prefix |
||
| 61 | */ |
||
| 62 | protected $cache; |
||
| 63 | /** |
||
| 64 | * @var Prefix |
||
| 65 | */ |
||
| 66 | protected $users_cache; |
||
| 67 | protected $data_model = [ |
||
| 68 | 'id' => 'text', |
||
| 69 | 'user' => 'int:0', |
||
| 70 | 'created' => 'int:0', |
||
| 71 | 'expire' => 'int:0', |
||
| 72 | 'user_agent' => 'text', |
||
| 73 | 'remote_addr' => 'text', |
||
| 74 | 'ip' => 'text', |
||
| 75 | 'data' => 'json' |
||
| 76 | ]; |
||
| 77 | protected $table = '[prefix]sessions'; |
||
| 78 | protected function construct () { |
||
| 83 | /** |
||
| 84 | * Returns database index |
||
| 85 | * |
||
| 86 | * @return int |
||
| 87 | */ |
||
| 88 | protected function cdb () { |
||
| 91 | /** |
||
| 92 | * Use cookie as source of session id, load session |
||
| 93 | * |
||
| 94 | * Bots detection is also done here |
||
| 95 | */ |
||
| 96 | protected function initialize () { |
||
| 124 | /** |
||
| 125 | * Try to determine whether visitor is a known bot, bots have no sessions |
||
| 126 | */ |
||
| 127 | protected function bots_detection () { |
||
| 162 | /** |
||
| 163 | * Get list of all bots |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | protected function all_bots () { |
||
| 190 | /** |
||
| 191 | * Check whether user agent and IP (login and email for bots) corresponds to passed bot data |
||
| 192 | * |
||
| 193 | * @param array $bot |
||
| 194 | * @param string $login |
||
| 195 | * @param string $email |
||
| 196 | * |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | protected function is_this_bot ($bot, $login, $email) { |
||
| 216 | /** |
||
| 217 | * Updates information about who is user accessed by methods ::guest() ::bot() ::user() admin() |
||
| 218 | */ |
||
| 219 | protected function update_user_is () { |
||
| 242 | /** |
||
| 243 | * Is admin |
||
| 244 | * |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | function admin () { |
||
| 250 | /** |
||
| 251 | * Is user |
||
| 252 | * |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | function user () { |
||
| 258 | /** |
||
| 259 | * Is guest |
||
| 260 | * |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | function guest () { |
||
| 266 | /** |
||
| 267 | * Is bot |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | function bot () { |
||
| 274 | /** |
||
| 275 | * Returns id of current session |
||
| 276 | * |
||
| 277 | * @return false|string |
||
| 278 | */ |
||
| 279 | function get_id () { |
||
| 285 | /** |
||
| 286 | * Returns user id of current session |
||
| 287 | * |
||
| 288 | * @return false|int |
||
| 289 | */ |
||
| 290 | function get_user () { |
||
| 293 | /** |
||
| 294 | * Returns session details by session id |
||
| 295 | * |
||
| 296 | * @param false|null|string $session_id If `null` - loaded from `$this->session_id`, and if that also empty - from cookies |
||
| 297 | * |
||
| 298 | * @return false|array |
||
| 299 | */ |
||
| 300 | function get ($session_id) { |
||
| 307 | /** |
||
| 308 | * @param false|null|string $session_id |
||
| 309 | * |
||
| 310 | * @return false|array |
||
| 311 | */ |
||
| 312 | protected function get_internal ($session_id) { |
||
| 335 | /** |
||
| 336 | * Check whether session was not expired, user agent and IP corresponds to what is expected and user is actually active |
||
| 337 | * |
||
| 338 | * @param mixed $session_data |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | protected function is_good_session ($session_data) { |
||
| 348 | /** |
||
| 349 | * Whether session data belongs to current visitor (user agent, remote addr and ip check) |
||
| 350 | * |
||
| 351 | * @param string $session_id |
||
| 352 | * @param string $user_agent |
||
| 353 | * @param string $remote_addr |
||
| 354 | * @param string $ip |
||
| 355 | * |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | function is_session_owner ($session_id, $user_agent, $remote_addr, $ip) { |
||
| 366 | /** |
||
| 367 | * Whether session data belongs to current visitor (user agent, remote addr and ip check) |
||
| 368 | * |
||
| 369 | * @param array $session_data |
||
| 370 | * @param string|null $user_agent |
||
| 371 | * @param string|null $remote_addr |
||
| 372 | * @param string|null $ip |
||
| 373 | * |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | protected function is_session_owner_internal ($session_data, $user_agent = null, $remote_addr = null, $ip = null) { |
||
| 401 | /** |
||
| 402 | * Load session by id and return id of session owner (user), update session expiration |
||
| 403 | * |
||
| 404 | * @param false|null|string $session_id If not specified - loaded from `$this->session_id`, and if that also empty - from cookies |
||
| 405 | * |
||
| 406 | * @return int User id |
||
| 407 | */ |
||
| 408 | function load ($session_id = null) { |
||
| 436 | /** |
||
| 437 | * Initialize session (set user id, session id and update who user is) |
||
| 438 | * |
||
| 439 | * @param string $session_id |
||
| 440 | * @param int $user_id |
||
| 441 | * |
||
| 442 | * @return int User id |
||
| 443 | */ |
||
| 444 | protected function load_initialization ($session_id, $user_id) { |
||
| 450 | /** |
||
| 451 | * Whether profile is activated, not disabled and not blocked |
||
| 452 | * |
||
| 453 | * @param int $user |
||
| 454 | * |
||
| 455 | * @return bool |
||
| 456 | */ |
||
| 457 | protected function is_user_active ($user) { |
||
| 501 | /** |
||
| 502 | * Create the session for the user with specified id |
||
| 503 | * |
||
| 504 | * @param false|int $user |
||
| 505 | * @param bool $delete_current_session |
||
| 506 | * |
||
| 507 | * @return false|string Session id on success, `false` otherwise |
||
| 508 | */ |
||
| 509 | function add ($user = false, $delete_current_session = true) { |
||
| 538 | /** |
||
| 539 | * @param int $user |
||
| 540 | * |
||
| 541 | * @return array Session data |
||
| 542 | */ |
||
| 543 | protected function create_unique_session ($user) { |
||
| 573 | /** |
||
| 574 | * Destroying of the session |
||
| 575 | * |
||
| 576 | * @param null|string $session_id |
||
| 577 | * |
||
| 578 | * @return bool |
||
| 579 | */ |
||
| 580 | function del ($session_id = null) { |
||
| 583 | /** |
||
| 584 | * Deletion of the session |
||
| 585 | * |
||
| 586 | * @param string|null $session_id |
||
| 587 | * @param bool $create_guest_session |
||
| 588 | * |
||
| 589 | * @return bool |
||
| 590 | */ |
||
| 591 | protected function del_internal ($session_id = null, $create_guest_session = true) { |
||
| 619 | /** |
||
| 620 | * Delete all old sessions from DB |
||
| 621 | */ |
||
| 622 | protected function delete_old_sessions () { |
||
| 628 | /** |
||
| 629 | * Deletion of all user sessions |
||
| 630 | * |
||
| 631 | * @param false|int $user If not specified - current user assumed |
||
| 632 | * |
||
| 633 | * @return bool |
||
| 634 | */ |
||
| 635 | function del_all ($user = false) { |
||
| 658 | /** |
||
| 659 | * Get data, stored with session |
||
| 660 | * |
||
| 661 | * @param string $item |
||
| 662 | * @param null|string $session_id |
||
| 663 | * |
||
| 664 | * @return false|mixed |
||
| 665 | * |
||
| 666 | */ |
||
| 667 | function get_data ($item, $session_id = null) { |
||
| 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) { |
||
| 697 | /** |
||
| 698 | * Delete data, stored with session |
||
| 699 | * |
||
| 700 | * @param string $item |
||
| 701 | * @param null|string $session_id |
||
| 702 | * |
||
| 703 | * @return bool |
||
| 704 | * |
||
| 705 | */ |
||
| 706 | function del_data ($item, $session_id = null) { |
||
| 720 | } |
||
| 721 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.