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 INITIAL_SESSION_EXPIRATION = 300; |
||
44 | /** |
||
45 | * Id of current session |
||
46 | * |
||
47 | * @var false|string |
||
48 | */ |
||
49 | protected $session_id; |
||
50 | /** |
||
51 | * User id of current session |
||
52 | * |
||
53 | * @var false|int |
||
54 | */ |
||
55 | protected $user_id = false; |
||
56 | protected $is_admin = false; |
||
57 | protected $is_user = false; |
||
58 | protected $is_bot = false; |
||
59 | protected $is_guest = false; |
||
60 | /** |
||
61 | * @var Prefix |
||
62 | */ |
||
63 | protected $cache; |
||
64 | /** |
||
65 | * @var Prefix |
||
66 | */ |
||
67 | protected $users_cache; |
||
68 | protected $data_model = [ |
||
69 | 'id' => 'text', |
||
70 | 'user' => 'int:0', |
||
71 | 'created' => 'int:0', |
||
72 | 'expire' => 'int:0', |
||
73 | 'user_agent' => 'text', |
||
74 | 'remote_addr' => 'text', |
||
75 | 'ip' => 'text', |
||
76 | 'data' => 'json' |
||
77 | ]; |
||
78 | protected $table = '[prefix]sessions'; |
||
79 | protected function construct () { |
||
84 | /** |
||
85 | * Returns database index |
||
86 | * |
||
87 | * @return int |
||
88 | */ |
||
89 | protected function cdb () { |
||
92 | /** |
||
93 | * Use cookie as source of session id, load session |
||
94 | * |
||
95 | * Bots detection is also done here |
||
96 | */ |
||
97 | protected function initialize () { |
||
125 | /** |
||
126 | * Try to determine whether visitor is a known bot, bots have no sessions |
||
127 | */ |
||
128 | protected function bots_detection () { |
||
163 | /** |
||
164 | * Get list of all bots |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | protected function all_bots () { |
||
191 | /** |
||
192 | * Check whether user agent and IP (login and email for bots) corresponds to passed bot data |
||
193 | * |
||
194 | * @param array $bot |
||
195 | * @param string $login |
||
196 | * @param string $email |
||
197 | * |
||
198 | * @return bool |
||
199 | */ |
||
200 | protected function is_this_bot ($bot, $login, $email) { |
||
217 | /** |
||
218 | * Updates information about who is user accessed by methods ::guest() ::bot() ::user() admin() |
||
219 | */ |
||
220 | protected function update_user_is () { |
||
243 | /** |
||
244 | * Is admin |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | function admin () { |
||
251 | /** |
||
252 | * Is user |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | function user () { |
||
259 | /** |
||
260 | * Is guest |
||
261 | * |
||
262 | * @return bool |
||
263 | */ |
||
264 | function guest () { |
||
267 | /** |
||
268 | * Is bot |
||
269 | * |
||
270 | * @return bool |
||
271 | */ |
||
272 | function bot () { |
||
275 | /** |
||
276 | * Returns id of current session |
||
277 | * |
||
278 | * @return false|string |
||
279 | */ |
||
280 | function get_id () { |
||
286 | /** |
||
287 | * Returns user id of current session |
||
288 | * |
||
289 | * @return false|int |
||
290 | */ |
||
291 | function get_user () { |
||
294 | /** |
||
295 | * Returns session details by session id |
||
296 | * |
||
297 | * @param false|null|string $session_id If `null` - loaded from `$this->session_id`, and if that also empty - from cookies |
||
298 | * |
||
299 | * @return false|array |
||
300 | */ |
||
301 | function get ($session_id) { |
||
308 | /** |
||
309 | * @param false|null|string $session_id |
||
310 | * |
||
311 | * @return false|array |
||
312 | */ |
||
313 | protected function get_internal ($session_id) { |
||
336 | /** |
||
337 | * Check whether session was not expired, user agent and IP corresponds to what is expected and user is actually active |
||
338 | * |
||
339 | * @param mixed $session_data |
||
340 | * |
||
341 | * @return bool |
||
342 | */ |
||
343 | protected function is_good_session ($session_data) { |
||
349 | /** |
||
350 | * Whether session data belongs to current visitor (user agent, remote addr and ip check) |
||
351 | * |
||
352 | * @param string $session_id |
||
353 | * @param string $user_agent |
||
354 | * @param string $remote_addr |
||
355 | * @param string $ip |
||
356 | * |
||
357 | * @return bool |
||
358 | */ |
||
359 | function is_session_owner ($session_id, $user_agent, $remote_addr, $ip) { |
||
363 | /** |
||
364 | * Whether session data belongs to current visitor (user agent, remote addr and ip check) |
||
365 | * |
||
366 | * @param array $session_data |
||
367 | * @param string|null $user_agent |
||
368 | * @param string|null $remote_addr |
||
369 | * @param string|null $ip |
||
370 | * |
||
371 | * @return bool |
||
372 | */ |
||
373 | protected function is_session_owner_internal ($session_data, $user_agent = null, $remote_addr = null, $ip = null) { |
||
394 | /** |
||
395 | * Load session by id and return id of session owner (user), update session expiration |
||
396 | * |
||
397 | * @param false|null|string $session_id If not specified - loaded from `$this->session_id`, and if that also empty - from cookies |
||
398 | * |
||
399 | * @return int User id |
||
400 | */ |
||
401 | function load ($session_id = null) { |
||
429 | /** |
||
430 | * Initialize session (set user id, session id and update who user is) |
||
431 | * |
||
432 | * @param string $session_id |
||
433 | * @param int $user_id |
||
434 | * |
||
435 | * @return int User id |
||
436 | */ |
||
437 | protected function load_initialization ($session_id, $user_id) { |
||
443 | /** |
||
444 | * Whether profile is activated, not disabled and not blocked |
||
445 | * |
||
446 | * @param int $user |
||
447 | * |
||
448 | * @return bool |
||
449 | */ |
||
450 | protected function is_user_active ($user) { |
||
494 | /** |
||
495 | * Create the session for the user with specified id |
||
496 | * |
||
497 | * @param false|int $user |
||
498 | * @param bool $delete_current_session |
||
499 | * |
||
500 | * @return false|string Session id on success, `false` otherwise |
||
501 | */ |
||
502 | function add ($user = false, $delete_current_session = true) { |
||
531 | /** |
||
532 | * @param int $user |
||
533 | * |
||
534 | * @return array Session data |
||
535 | */ |
||
536 | protected function create_unique_session ($user) { |
||
566 | /** |
||
567 | * Destroying of the session |
||
568 | * |
||
569 | * @param null|string $session_id |
||
570 | * |
||
571 | * @return bool |
||
572 | */ |
||
573 | function del ($session_id = null) { |
||
576 | /** |
||
577 | * Deletion of the session |
||
578 | * |
||
579 | * @param string|null $session_id |
||
580 | * @param bool $create_guest_session |
||
581 | * |
||
582 | * @return bool |
||
583 | */ |
||
584 | protected function del_internal ($session_id = null, $create_guest_session = true) { |
||
612 | /** |
||
613 | * Delete all old sessions from DB |
||
614 | */ |
||
615 | protected function delete_old_sessions () { |
||
621 | /** |
||
622 | * Deletion of all user sessions |
||
623 | * |
||
624 | * @param false|int $user If not specified - current user assumed |
||
625 | * |
||
626 | * @return bool |
||
627 | */ |
||
628 | function del_all ($user = false) { |
||
651 | /** |
||
652 | * Get data, stored with session |
||
653 | * |
||
654 | * @param string $item |
||
655 | * @param null|string $session_id |
||
656 | * |
||
657 | * @return false|mixed |
||
658 | * |
||
659 | */ |
||
660 | function get_data ($item, $session_id = null) { |
||
664 | /* |
||
1 ignored issue
–
show
|
|||
665 | * @param null|string $session_id |
||
666 | * |
||
667 | * @return array|false |
||
668 | */ |
||
669 | protected function get_data_internal ($session_id) { |
||
673 | /** |
||
674 | * Store data with session |
||
675 | * |
||
676 | * @param string $item |
||
677 | * @param mixed $value |
||
678 | * @param null|string $session_id |
||
679 | * |
||
680 | * @return bool |
||
681 | * |
||
682 | */ |
||
683 | function set_data ($item, $value, $session_id = null) { |
||
691 | /** |
||
692 | * Delete data, stored with session |
||
693 | * |
||
694 | * @param string $item |
||
695 | * @param null|string $session_id |
||
696 | * |
||
697 | * @return bool |
||
698 | * |
||
699 | */ |
||
700 | function del_data ($item, $session_id = null) { |
||
710 | } |
||
711 |