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 int |
||
54 | */ |
||
55 | protected $user_id = User::GUEST_ID; |
||
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 () { |
||
113 | /** |
||
114 | * Try to determine whether visitor is a known bot, bots have no sessions |
||
115 | */ |
||
116 | protected function bots_detection () { |
||
152 | /** |
||
153 | * Get list of all bots |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | protected function all_bots () { |
||
180 | /** |
||
181 | * Check whether user agent and IP (login and email for bots) corresponds to passed bot data |
||
182 | * |
||
183 | * @param array $bot |
||
184 | * @param string $login |
||
185 | * @param string $email |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | protected function is_this_bot ($bot, $login, $email) { |
||
206 | /** |
||
207 | * Updates information about who is user accessed by methods ::guest() ::bot() ::user() admin() |
||
208 | */ |
||
209 | protected function update_user_is () { |
||
232 | /** |
||
233 | * Is admin |
||
234 | * |
||
235 | * @return bool |
||
236 | */ |
||
237 | function admin () { |
||
240 | /** |
||
241 | * Is user |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | function user () { |
||
248 | /** |
||
249 | * Is guest |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | function guest () { |
||
256 | /** |
||
257 | * Is bot |
||
258 | * |
||
259 | * @return bool |
||
260 | */ |
||
261 | function bot () { |
||
264 | /** |
||
265 | * Returns id of current session |
||
266 | * |
||
267 | * @return false|string |
||
268 | */ |
||
269 | function get_id () { |
||
275 | /** |
||
276 | * Returns user id of current session |
||
277 | * |
||
278 | * @return int |
||
279 | */ |
||
280 | function get_user () { |
||
283 | /** |
||
284 | * Returns session details by session id |
||
285 | * |
||
286 | * @param false|null|string $session_id If `null` - loaded from `$this->session_id`, and if that also empty - from cookies |
||
287 | * |
||
288 | * @return false|array |
||
289 | */ |
||
290 | function get ($session_id) { |
||
297 | /** |
||
298 | * @param false|null|string $session_id |
||
299 | * |
||
300 | * @return false|array |
||
301 | */ |
||
302 | protected function get_internal ($session_id) { |
||
325 | /** |
||
326 | * Check whether session was not expired, user agent and IP corresponds to what is expected and user is actually active |
||
327 | * |
||
328 | * @param mixed $session_data |
||
329 | * |
||
330 | * @return bool |
||
331 | */ |
||
332 | protected function is_good_session ($session_data) { |
||
338 | /** |
||
339 | * Whether session data belongs to current visitor (user agent, remote addr and ip check) |
||
340 | * |
||
341 | * @param string $session_id |
||
342 | * @param string $user_agent |
||
343 | * @param string $remote_addr |
||
344 | * @param string $ip |
||
345 | * |
||
346 | * @return bool |
||
347 | */ |
||
348 | function is_session_owner ($session_id, $user_agent, $remote_addr, $ip) { |
||
352 | /** |
||
353 | * Whether session data belongs to current visitor (user agent, remote addr and ip check) |
||
354 | * |
||
355 | * @param array $session_data |
||
356 | * @param string|null $user_agent |
||
357 | * @param string|null $remote_addr |
||
358 | * @param string|null $ip |
||
359 | * |
||
360 | * @return bool |
||
361 | */ |
||
362 | protected function is_session_owner_internal ($session_data, $user_agent = null, $remote_addr = null, $ip = null) { |
||
383 | /** |
||
384 | * Load session by id and return id of session owner (user), update session expiration |
||
385 | * |
||
386 | * @param false|null|string $session_id If not specified - loaded from `$this->session_id`, and if that also empty - from cookies |
||
387 | * |
||
388 | * @return int User id |
||
389 | */ |
||
390 | function load ($session_id = null) { |
||
418 | /** |
||
419 | * Initialize session (set user id, session id and update who user is) |
||
420 | * |
||
421 | * @param string $session_id |
||
422 | * @param int $user_id |
||
423 | * |
||
424 | * @return int User id |
||
425 | */ |
||
426 | protected function load_initialization ($session_id, $user_id) { |
||
432 | /** |
||
433 | * Whether profile is activated, not disabled and not blocked |
||
434 | * |
||
435 | * @param int $user |
||
436 | * |
||
437 | * @return bool |
||
438 | */ |
||
439 | protected function is_user_active ($user) { |
||
483 | /** |
||
484 | * Create the session for the user with specified id |
||
485 | * |
||
486 | * @param int $user |
||
487 | * @param bool $delete_current_session |
||
488 | * |
||
489 | * @return false|string Session id on success, `false` otherwise |
||
490 | */ |
||
491 | function add ($user, $delete_current_session = true) { |
||
523 | /** |
||
524 | * @param int $user |
||
525 | * |
||
526 | * @return array Session data |
||
527 | */ |
||
528 | protected function create_unique_session ($user) { |
||
558 | /** |
||
559 | * Destroying of the session |
||
560 | * |
||
561 | * @param null|string $session_id |
||
562 | * |
||
563 | * @return bool |
||
564 | */ |
||
565 | function del ($session_id = null) { |
||
568 | /** |
||
569 | * Deletion of the session |
||
570 | * |
||
571 | * @param string|null $session_id |
||
572 | * @param bool $create_guest_session |
||
573 | * |
||
574 | * @return bool |
||
575 | */ |
||
576 | protected function del_internal ($session_id = null, $create_guest_session = true) { |
||
604 | /** |
||
605 | * Delete all old sessions from DB |
||
606 | */ |
||
607 | protected function delete_old_sessions () { |
||
613 | /** |
||
614 | * Deletion of all user sessions |
||
615 | * |
||
616 | * @param false|int $user If not specified - current user assumed |
||
617 | * |
||
618 | * @return bool |
||
619 | */ |
||
620 | function del_all ($user = false) { |
||
643 | /** |
||
644 | * Get data, stored with session |
||
645 | * |
||
646 | * @param string $item |
||
647 | * @param null|string $session_id |
||
648 | * |
||
649 | * @return false|mixed |
||
650 | * |
||
651 | */ |
||
652 | function get_data ($item, $session_id = null) { |
||
656 | /* |
||
657 | * @param null|string $session_id |
||
658 | * |
||
659 | * @return array|false |
||
660 | */ |
||
661 | protected function get_data_internal ($session_id) { |
||
665 | /** |
||
666 | * Store data with session |
||
667 | * |
||
668 | * @param string $item |
||
669 | * @param mixed $value |
||
670 | * @param null|string $session_id |
||
671 | * |
||
672 | * @return bool |
||
673 | * |
||
674 | */ |
||
675 | function set_data ($item, $value, $session_id = null) { |
||
690 | /** |
||
691 | * Delete data, stored with session |
||
692 | * |
||
693 | * @param string $item |
||
694 | * @param null|string $session_id |
||
695 | * |
||
696 | * @return bool |
||
697 | * |
||
698 | */ |
||
699 | function del_data ($item, $session_id = null) { |
||
709 | } |
||
710 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.