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 EE_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 EE_Session, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class EE_Session implements SessionIdentifierInterface |
||
21 | { |
||
22 | |||
23 | const session_id_prefix = 'ee_ssn_'; |
||
24 | |||
25 | const hash_check_prefix = 'ee_shc_'; |
||
26 | |||
27 | const OPTION_NAME_SETTINGS = 'ee_session_settings'; |
||
28 | |||
29 | /** |
||
30 | * instance of the EE_Session object |
||
31 | * |
||
32 | * @var EE_Session |
||
33 | */ |
||
34 | private static $_instance; |
||
35 | |||
36 | /** |
||
37 | * @var CacheStorageInterface $cache_storage |
||
38 | */ |
||
39 | protected $cache_storage; |
||
40 | |||
41 | /** |
||
42 | * EE_Encryption object |
||
43 | * |
||
44 | * @var EE_Encryption |
||
45 | */ |
||
46 | protected $encryption; |
||
47 | |||
48 | /** |
||
49 | * the session id |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | private $_sid; |
||
54 | |||
55 | /** |
||
56 | * session id salt |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | private $_sid_salt; |
||
61 | |||
62 | /** |
||
63 | * session data |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | private $_session_data = array(); |
||
68 | |||
69 | /** |
||
70 | * how long an EE session lasts |
||
71 | * default session lifespan of 2 hours (for not so instant IPNs) |
||
72 | * |
||
73 | * @var int |
||
74 | */ |
||
75 | private $_lifespan; |
||
76 | |||
77 | /** |
||
78 | * session expiration time as Unix timestamp in GMT |
||
79 | * |
||
80 | * @var int |
||
81 | */ |
||
82 | private $_expiration; |
||
83 | |||
84 | /** |
||
85 | * whether or not session has expired at some point |
||
86 | * |
||
87 | * @var boolean |
||
88 | */ |
||
89 | private $_expired = false; |
||
90 | |||
91 | /** |
||
92 | * current time as Unix timestamp in GMT |
||
93 | * |
||
94 | * @var int |
||
95 | */ |
||
96 | private $_time; |
||
97 | |||
98 | /** |
||
99 | * whether to encrypt session data |
||
100 | * |
||
101 | * @var bool |
||
102 | */ |
||
103 | private $_use_encryption; |
||
104 | |||
105 | /** |
||
106 | * well... according to the server... |
||
107 | * |
||
108 | * @var null |
||
109 | */ |
||
110 | private $_user_agent; |
||
111 | |||
112 | /** |
||
113 | * do you really trust the server ? |
||
114 | * |
||
115 | * @var null |
||
116 | */ |
||
117 | private $_ip_address; |
||
118 | |||
119 | /** |
||
120 | * current WP user_id |
||
121 | * |
||
122 | * @var null |
||
123 | */ |
||
124 | private $_wp_user_id; |
||
125 | |||
126 | /** |
||
127 | * array for defining default session vars |
||
128 | * |
||
129 | * @var array |
||
130 | */ |
||
131 | private $_default_session_vars = array( |
||
132 | 'id' => null, |
||
133 | 'user_id' => null, |
||
134 | 'ip_address' => null, |
||
135 | 'user_agent' => null, |
||
136 | 'init_access' => null, |
||
137 | 'last_access' => null, |
||
138 | 'expiration' => null, |
||
139 | 'pages_visited' => array(), |
||
140 | ); |
||
141 | |||
142 | /** |
||
143 | * timestamp for when last garbage collection cycle was performed |
||
144 | * |
||
145 | * @var int $_last_gc |
||
146 | */ |
||
147 | private $_last_gc; |
||
148 | |||
149 | |||
150 | |||
151 | /** |
||
152 | * @singleton method used to instantiate class object |
||
153 | * @param CacheStorageInterface $cache_storage |
||
154 | * @param EE_Encryption $encryption |
||
155 | * @return EE_Session |
||
156 | * @throws InvalidArgumentException |
||
157 | * @throws InvalidDataTypeException |
||
158 | * @throws InvalidInterfaceException |
||
159 | */ |
||
160 | public static function instance( |
||
172 | |||
173 | |||
174 | |||
175 | /** |
||
176 | * protected constructor to prevent direct creation |
||
177 | * |
||
178 | * @param CacheStorageInterface $cache_storage |
||
179 | * @param EE_Encryption $encryption |
||
180 | * @throws InvalidArgumentException |
||
181 | * @throws InvalidDataTypeException |
||
182 | * @throws InvalidInterfaceException |
||
183 | */ |
||
184 | protected function __construct(CacheStorageInterface $cache_storage, EE_Encryption $encryption = null) |
||
236 | |||
237 | |||
238 | |||
239 | /** |
||
240 | * @return void |
||
241 | * @throws EE_Error |
||
242 | * @throws InvalidArgumentException |
||
243 | * @throws InvalidDataTypeException |
||
244 | * @throws InvalidInterfaceException |
||
245 | * @throws InvalidSessionDataException |
||
246 | */ |
||
247 | public function open_session() |
||
255 | |||
256 | |||
257 | |||
258 | /** |
||
259 | * @return bool |
||
260 | */ |
||
261 | public function expired() |
||
265 | |||
266 | |||
267 | |||
268 | /** |
||
269 | * @return void |
||
270 | */ |
||
271 | public function reset_expired() |
||
275 | |||
276 | |||
277 | /** |
||
278 | * @return int |
||
279 | */ |
||
280 | public function expiration() |
||
284 | |||
285 | |||
286 | |||
287 | /** |
||
288 | * @return int |
||
289 | */ |
||
290 | public function extension() |
||
294 | |||
295 | |||
296 | |||
297 | /** |
||
298 | * @param int $time number of seconds to add to session expiration |
||
299 | */ |
||
300 | public function extend_expiration($time = 0) |
||
305 | |||
306 | |||
307 | |||
308 | /** |
||
309 | * @return int |
||
310 | */ |
||
311 | public function lifespan() |
||
315 | |||
316 | |||
317 | |||
318 | /** |
||
319 | * This just sets some defaults for the _session data property |
||
320 | * |
||
321 | * @access private |
||
322 | * @return void |
||
323 | */ |
||
324 | private function _set_defaults() |
||
335 | |||
336 | |||
337 | |||
338 | /** |
||
339 | * @retrieve session data |
||
340 | * @access public |
||
341 | * @return string |
||
342 | */ |
||
343 | public function id() |
||
347 | |||
348 | |||
349 | |||
350 | /** |
||
351 | * @param \EE_Cart $cart |
||
352 | * @return bool |
||
353 | */ |
||
354 | public function set_cart(EE_Cart $cart) |
||
359 | |||
360 | |||
361 | |||
362 | /** |
||
363 | * reset_cart |
||
364 | */ |
||
365 | public function reset_cart() |
||
370 | |||
371 | |||
372 | |||
373 | /** |
||
374 | * @return \EE_Cart |
||
375 | */ |
||
376 | public function cart() |
||
382 | |||
383 | |||
384 | |||
385 | /** |
||
386 | * @param \EE_Checkout $checkout |
||
387 | * @return bool |
||
388 | */ |
||
389 | public function set_checkout(EE_Checkout $checkout) |
||
394 | |||
395 | |||
396 | |||
397 | /** |
||
398 | * reset_checkout |
||
399 | */ |
||
400 | public function reset_checkout() |
||
405 | |||
406 | |||
407 | |||
408 | /** |
||
409 | * @return \EE_Checkout |
||
410 | */ |
||
411 | public function checkout() |
||
417 | |||
418 | |||
419 | |||
420 | /** |
||
421 | * @param \EE_Transaction $transaction |
||
422 | * @return bool |
||
423 | * @throws EE_Error |
||
424 | */ |
||
425 | public function set_transaction(EE_Transaction $transaction) |
||
432 | |||
433 | |||
434 | |||
435 | /** |
||
436 | * reset_transaction |
||
437 | */ |
||
438 | public function reset_transaction() |
||
443 | |||
444 | |||
445 | |||
446 | /** |
||
447 | * @return \EE_Transaction |
||
448 | */ |
||
449 | public function transaction() |
||
456 | |||
457 | |||
458 | |||
459 | /** |
||
460 | * retrieve session data |
||
461 | * |
||
462 | * @access public |
||
463 | * @param null $key |
||
464 | * @param bool $reset_cache |
||
465 | * @return array |
||
466 | */ |
||
467 | public function get_session_data($key = null, $reset_cache = false) |
||
479 | |||
480 | |||
481 | |||
482 | /** |
||
483 | * set session data |
||
484 | * |
||
485 | * @access public |
||
486 | * @param array $data |
||
487 | * @return TRUE on success, FALSE on fail |
||
488 | */ |
||
489 | public function set_session_data($data) |
||
490 | { |
||
491 | |||
492 | // nothing ??? bad data ??? go home! |
||
493 | View Code Duplication | if (empty($data) || ! is_array($data)) { |
|
494 | EE_Error::add_error(__('No session data or invalid session data was provided.', 'event_espresso'), __FILE__, |
||
495 | __FUNCTION__, __LINE__); |
||
496 | return false; |
||
497 | } |
||
498 | foreach ($data as $key => $value) { |
||
499 | View Code Duplication | if (isset($this->_default_session_vars[ $key ])) { |
|
500 | EE_Error::add_error(sprintf(__('Sorry! %s is a default session datum and can not be reset.', |
||
501 | 'event_espresso'), $key), __FILE__, __FUNCTION__, __LINE__); |
||
502 | return false; |
||
503 | } |
||
504 | $this->_session_data[ $key ] = $value; |
||
505 | } |
||
506 | return true; |
||
507 | } |
||
508 | |||
509 | |||
510 | |||
511 | /** |
||
512 | * @initiate session |
||
513 | * @access private |
||
514 | * @return TRUE on success, FALSE on fail |
||
515 | * @throws EE_Error |
||
516 | * @throws InvalidArgumentException |
||
517 | * @throws InvalidDataTypeException |
||
518 | * @throws InvalidInterfaceException |
||
519 | * @throws InvalidSessionDataException |
||
520 | */ |
||
521 | private function _espresso_session() |
||
569 | |||
570 | |||
571 | |||
572 | /** |
||
573 | * _get_session_data |
||
574 | * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup |
||
575 | * databases |
||
576 | * |
||
577 | * @return array |
||
578 | * @throws EE_Error |
||
579 | * @throws InvalidArgumentException |
||
580 | * @throws InvalidSessionDataException |
||
581 | * @throws InvalidDataTypeException |
||
582 | * @throws InvalidInterfaceException |
||
583 | */ |
||
584 | protected function _retrieve_session_data() |
||
585 | { |
||
586 | $ssn_key = EE_Session::session_id_prefix . $this->_sid; |
||
587 | try { |
||
588 | // we're using WP's Transient API to store session data using the PHP session ID as the option name |
||
589 | $session_data = $this->cache_storage->get($ssn_key, false); |
||
590 | if (empty($session_data)) { |
||
591 | return array(); |
||
592 | } |
||
593 | if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
||
594 | $hash_check = $this->cache_storage->get( |
||
595 | EE_Session::hash_check_prefix . $this->_sid, |
||
596 | false |
||
597 | ); |
||
598 | View Code Duplication | if ($hash_check && $hash_check !== md5($session_data)) { |
|
599 | EE_Error::add_error( |
||
600 | sprintf( |
||
601 | __( |
||
602 | 'The stored data for session %1$s failed to pass a hash check and therefore appears to be invalid.', |
||
603 | 'event_espresso' |
||
604 | ), |
||
605 | EE_Session::session_id_prefix . $this->_sid |
||
606 | ), |
||
607 | __FILE__, __FUNCTION__, __LINE__ |
||
608 | ); |
||
609 | } |
||
610 | } |
||
611 | } catch (Exception $e) { |
||
612 | // let's just eat that error for now and attempt to correct any corrupted data |
||
613 | global $wpdb; |
||
614 | $row = $wpdb->get_row( |
||
615 | $wpdb->prepare( |
||
616 | "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
||
617 | '_transient_' . $ssn_key |
||
618 | ) |
||
619 | ); |
||
620 | $session_data = is_object($row) ? $row->option_value : null; |
||
621 | if ($session_data) { |
||
622 | $session_data = preg_replace_callback( |
||
623 | '!s:(d+):"(.*?)";!', |
||
624 | View Code Duplication | function ($match) |
|
625 | { |
||
626 | return $match[1] === strlen($match[2]) |
||
627 | ? $match[0] |
||
628 | : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; |
||
629 | }, |
||
630 | $session_data |
||
631 | ); |
||
632 | } |
||
633 | $session_data = maybe_unserialize($session_data); |
||
634 | } |
||
635 | // in case the data is encoded... try to decode it |
||
636 | $session_data = $this->encryption instanceof EE_Encryption |
||
637 | ? $this->encryption->base64_string_decode($session_data) |
||
638 | : $session_data; |
||
639 | if (! is_array($session_data)) { |
||
640 | try { |
||
641 | $session_data = maybe_unserialize($session_data); |
||
642 | } catch (Exception $e) { |
||
643 | $msg = esc_html__( |
||
644 | 'An error occurred while attempting to unserialize the session data.', |
||
645 | 'event_espresso' |
||
646 | ); |
||
647 | $msg .= WP_DEBUG |
||
648 | ? '<br><pre>' |
||
649 | . print_r($session_data, true) |
||
650 | . '</pre><br>' |
||
651 | . $this->find_serialize_error($session_data) |
||
652 | : ''; |
||
653 | $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
||
654 | throw new InvalidSessionDataException($msg, 0, $e); |
||
655 | } |
||
656 | } |
||
657 | // just a check to make sure the session array is indeed an array |
||
658 | if (! is_array($session_data)) { |
||
659 | // no?!?! then something is wrong |
||
660 | $msg = esc_html__( |
||
661 | 'The session data is missing, invalid, or corrupted.', |
||
662 | 'event_espresso' |
||
663 | ); |
||
664 | $msg .= WP_DEBUG |
||
665 | ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
||
666 | : ''; |
||
667 | $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
||
668 | throw new InvalidSessionDataException($msg); |
||
669 | } |
||
670 | View Code Duplication | if (isset($session_data['transaction']) && absint($session_data['transaction']) !== 0) { |
|
671 | $session_data['transaction'] = EEM_Transaction::instance()->get_one_by_ID( |
||
672 | $session_data['transaction'] |
||
673 | ); |
||
674 | } |
||
675 | return $session_data; |
||
676 | } |
||
677 | |||
678 | |||
679 | |||
680 | /** |
||
681 | * _generate_session_id |
||
682 | * Retrieves the PHP session id either directly from the PHP session, |
||
683 | * or from the $_REQUEST array if it was passed in from an AJAX request. |
||
684 | * The session id is then salted and hashed (mmm sounds tasty) |
||
685 | * so that it can be safely used as a $_REQUEST param |
||
686 | * |
||
687 | * @return string |
||
688 | */ |
||
689 | protected function _generate_session_id() |
||
699 | |||
700 | |||
701 | |||
702 | /** |
||
703 | * _get_sid_salt |
||
704 | * |
||
705 | * @return string |
||
706 | */ |
||
707 | protected function _get_sid_salt() |
||
725 | |||
726 | |||
727 | |||
728 | /** |
||
729 | * _set_init_access_and_expiration |
||
730 | * |
||
731 | * @return void |
||
732 | */ |
||
733 | protected function _set_init_access_and_expiration() |
||
742 | |||
743 | |||
744 | |||
745 | /** |
||
746 | * @update session data prior to saving to the db |
||
747 | * @access public |
||
748 | * @param bool $new_session |
||
749 | * @return TRUE on success, FALSE on fail |
||
750 | * @throws EE_Error |
||
751 | * @throws InvalidArgumentException |
||
752 | * @throws InvalidDataTypeException |
||
753 | * @throws InvalidInterfaceException |
||
754 | */ |
||
755 | public function update($new_session = false) |
||
825 | |||
826 | |||
827 | |||
828 | /** |
||
829 | * @create session data array |
||
830 | * @access public |
||
831 | * @return bool |
||
832 | * @throws EE_Error |
||
833 | * @throws InvalidArgumentException |
||
834 | * @throws InvalidDataTypeException |
||
835 | * @throws InvalidInterfaceException |
||
836 | */ |
||
837 | private function _create_espresso_session() |
||
843 | |||
844 | |||
845 | |||
846 | /** |
||
847 | * _save_session_to_db |
||
848 | * |
||
849 | * @access public |
||
850 | * @return string |
||
851 | * @throws EE_Error |
||
852 | * @throws InvalidArgumentException |
||
853 | * @throws InvalidDataTypeException |
||
854 | * @throws InvalidInterfaceException |
||
855 | */ |
||
856 | private function _save_session_to_db() |
||
905 | |||
906 | |||
907 | |||
908 | /** |
||
909 | * _visitor_ip |
||
910 | * attempt to get IP address of current visitor from server |
||
911 | * plz see: http://stackoverflow.com/a/2031935/1475279 |
||
912 | * |
||
913 | * @access public |
||
914 | * @return string |
||
915 | */ |
||
916 | View Code Duplication | private function _visitor_ip() |
|
939 | |||
940 | |||
941 | |||
942 | /** |
||
943 | * @get the full page request the visitor is accessing |
||
944 | * @access public |
||
945 | * @return string |
||
946 | */ |
||
947 | public function _get_page_visit() |
||
976 | |||
977 | |||
978 | |||
979 | /** |
||
980 | * @the current wp user id |
||
981 | * @access public |
||
982 | * @return int |
||
983 | */ |
||
984 | public function _wp_user_id() |
||
990 | |||
991 | |||
992 | |||
993 | /** |
||
994 | * Clear EE_Session data |
||
995 | * |
||
996 | * @access public |
||
997 | * @param string $class |
||
998 | * @param string $function |
||
999 | * @return void |
||
1000 | * @throws EE_Error |
||
1001 | * @throws InvalidArgumentException |
||
1002 | * @throws InvalidDataTypeException |
||
1003 | * @throws InvalidInterfaceException |
||
1004 | */ |
||
1005 | public function clear_session($class = '', $function = '') |
||
1018 | |||
1019 | |||
1020 | |||
1021 | /** |
||
1022 | * @resets all non-default session vars |
||
1023 | * @access public |
||
1024 | * @param array|mixed $data_to_reset |
||
1025 | * @param bool $show_all_notices |
||
1026 | * @return TRUE on success, FALSE on fail |
||
1027 | */ |
||
1028 | public function reset_data($data_to_reset = array(), $show_all_notices = false) |
||
1071 | |||
1072 | |||
1073 | |||
1074 | /** |
||
1075 | * wp_loaded |
||
1076 | * |
||
1077 | * @access public |
||
1078 | * @throws EE_Error |
||
1079 | * @throws InvalidDataTypeException |
||
1080 | * @throws InvalidInterfaceException |
||
1081 | * @throws InvalidArgumentException |
||
1082 | */ |
||
1083 | public function wp_loaded() |
||
1092 | |||
1093 | |||
1094 | |||
1095 | /** |
||
1096 | * Used to reset the entire object (for tests). |
||
1097 | * |
||
1098 | * @since 4.3.0 |
||
1099 | * @throws EE_Error |
||
1100 | * @throws InvalidDataTypeException |
||
1101 | * @throws InvalidInterfaceException |
||
1102 | * @throws InvalidArgumentException |
||
1103 | */ |
||
1104 | public function reset_instance() |
||
1109 | |||
1110 | |||
1111 | |||
1112 | public function configure_garbage_collection_filters() |
||
1136 | |||
1137 | |||
1138 | |||
1139 | /** |
||
1140 | * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
||
1141 | * @param $data1 |
||
1142 | * @return string |
||
1143 | */ |
||
1144 | private function find_serialize_error($data1) |
||
1201 | |||
1202 | |||
1203 | /** |
||
1204 | * Saves an array of settings used for configuring aspects of session behaviour |
||
1205 | * |
||
1206 | * @param array $updated_settings |
||
1207 | */ |
||
1208 | private function updateSessionSettings(array $updated_settings = array()) |
||
1214 | |||
1215 | |||
1216 | /** |
||
1217 | * garbage_collection |
||
1218 | */ |
||
1219 | public function garbageCollection() |
||
1263 | |||
1264 | |||
1265 | |||
1266 | } |
||
1267 | /* End of file EE_Session.class.php */ |
||
1269 |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):