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 use EventEspresso\core\exceptions\InvalidSessionDataException; |
||
13 | class EE_Session { |
||
14 | |||
15 | const session_id_prefix = 'ee_ssn_'; |
||
16 | |||
17 | const hash_check_prefix = 'ee_shc_'; |
||
18 | |||
19 | /** |
||
20 | * instance of the EE_Session object |
||
21 | * @var EE_Session |
||
22 | */ |
||
23 | private static $_instance; |
||
24 | |||
25 | /** |
||
26 | * @var CacheStorageInterface $cache_storage |
||
27 | */ |
||
28 | protected $cache_storage; |
||
29 | |||
30 | /** |
||
31 | * EE_Encryption object |
||
32 | * |
||
33 | * @var EE_Encryption |
||
34 | */ |
||
35 | protected $encryption; |
||
36 | |||
37 | /** |
||
38 | * the session id |
||
39 | * @var string |
||
40 | */ |
||
41 | private $_sid; |
||
42 | |||
43 | /** |
||
44 | * session id salt |
||
45 | * @var string |
||
46 | */ |
||
47 | private $_sid_salt; |
||
48 | |||
49 | /** |
||
50 | * session data |
||
51 | * @var array |
||
52 | */ |
||
53 | private $_session_data = array(); |
||
54 | |||
55 | /** |
||
56 | * how long an EE session lasts |
||
57 | * default session lifespan of 2 hours (for not so instant IPNs) |
||
58 | * @var int |
||
59 | */ |
||
60 | private $_lifespan; |
||
61 | |||
62 | /** |
||
63 | * session expiration time as Unix timestamp in GMT |
||
64 | * @var int |
||
65 | */ |
||
66 | private $_expiration; |
||
67 | |||
68 | /** |
||
69 | * whether or not session has expired at some point |
||
70 | * |
||
71 | * @var boolean |
||
72 | */ |
||
73 | private $_expired = false; |
||
74 | |||
75 | /** |
||
76 | * current time as Unix timestamp in GMT |
||
77 | * @var int |
||
78 | */ |
||
79 | private $_time; |
||
80 | |||
81 | /** |
||
82 | * whether to encrypt session data |
||
83 | * @var bool |
||
84 | */ |
||
85 | private $_use_encryption = false; |
||
86 | |||
87 | /** |
||
88 | * well... according to the server... |
||
89 | * @var null |
||
90 | */ |
||
91 | private $_user_agent; |
||
92 | |||
93 | /** |
||
94 | * do you really trust the server ? |
||
95 | * @var null |
||
96 | */ |
||
97 | private $_ip_address; |
||
98 | |||
99 | /** |
||
100 | * current WP user_id |
||
101 | * @var null |
||
102 | */ |
||
103 | private $_wp_user_id; |
||
104 | |||
105 | /** |
||
106 | * array for defining default session vars |
||
107 | * @var array |
||
108 | */ |
||
109 | private $_default_session_vars = array ( |
||
110 | 'id' => null, |
||
111 | 'user_id' => null, |
||
112 | 'ip_address' => null, |
||
113 | 'user_agent' => null, |
||
114 | 'init_access' => null, |
||
115 | 'last_access' => null, |
||
116 | 'expiration' => null, |
||
117 | 'pages_visited' => array(), |
||
118 | ); |
||
119 | |||
120 | |||
121 | |||
122 | /** |
||
123 | * @singleton method used to instantiate class object |
||
124 | * @param CacheStorageInterface $cache_storage |
||
125 | * @param \EE_Encryption $encryption |
||
126 | * @return EE_Session |
||
127 | * @throws InvalidSessionDataException |
||
128 | * @throws \EE_Error |
||
129 | */ |
||
130 | public static function instance( |
||
142 | |||
143 | |||
144 | |||
145 | /** |
||
146 | * protected constructor to prevent direct creation |
||
147 | * |
||
148 | * @param CacheStorageInterface $cache_storage |
||
149 | * @param \EE_Encryption $encryption |
||
150 | * @throws \EE_Error |
||
151 | * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
||
152 | */ |
||
153 | protected function __construct(CacheStorageInterface $cache_storage, EE_Encryption $encryption = null ) { |
||
202 | |||
203 | |||
204 | |||
205 | /** |
||
206 | * @return void |
||
207 | * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
||
208 | * @throws \EE_Error |
||
209 | */ |
||
210 | public function open_session() { |
||
217 | |||
218 | |||
219 | |||
220 | /** |
||
221 | * @return bool |
||
222 | */ |
||
223 | public function expired() |
||
227 | |||
228 | |||
229 | |||
230 | /** |
||
231 | * @return void |
||
232 | */ |
||
233 | public function reset_expired() |
||
237 | |||
238 | |||
239 | /** |
||
240 | * @return int |
||
241 | */ |
||
242 | public function expiration() { |
||
245 | |||
246 | |||
247 | |||
248 | /** |
||
249 | * @return int |
||
250 | */ |
||
251 | public function extension() |
||
255 | |||
256 | |||
257 | |||
258 | /** |
||
259 | * @param int $time number of seconds to add to session expiration |
||
260 | */ |
||
261 | public function extend_expiration($time = 0) |
||
266 | |||
267 | |||
268 | |||
269 | |||
270 | /** |
||
271 | * @return int |
||
272 | */ |
||
273 | public function lifespan() { |
||
276 | |||
277 | |||
278 | |||
279 | /** |
||
280 | * This just sets some defaults for the _session data property |
||
281 | * |
||
282 | * @access private |
||
283 | * @return void |
||
284 | */ |
||
285 | private function _set_defaults() { |
||
295 | |||
296 | |||
297 | |||
298 | /** |
||
299 | * @retrieve session data |
||
300 | * @access public |
||
301 | * @return string |
||
302 | */ |
||
303 | public function id() { |
||
306 | |||
307 | |||
308 | |||
309 | /** |
||
310 | * @param \EE_Cart $cart |
||
311 | * @return bool |
||
312 | */ |
||
313 | public function set_cart(EE_Cart $cart) |
||
318 | |||
319 | |||
320 | |||
321 | /** |
||
322 | * reset_cart |
||
323 | */ |
||
324 | public function reset_cart() { |
||
328 | |||
329 | |||
330 | |||
331 | /** |
||
332 | * @return \EE_Cart |
||
333 | */ |
||
334 | public function cart() { |
||
339 | |||
340 | |||
341 | |||
342 | /** |
||
343 | * @param \EE_Checkout $checkout |
||
344 | * @return bool |
||
345 | */ |
||
346 | public function set_checkout( EE_Checkout $checkout ) { |
||
350 | |||
351 | |||
352 | |||
353 | /** |
||
354 | * reset_checkout |
||
355 | */ |
||
356 | public function reset_checkout() { |
||
360 | |||
361 | |||
362 | |||
363 | /** |
||
364 | * @return \EE_Checkout |
||
365 | */ |
||
366 | public function checkout() { |
||
371 | |||
372 | |||
373 | |||
374 | /** |
||
375 | * @param \EE_Transaction $transaction |
||
376 | * @return bool |
||
377 | * @throws \EE_Error |
||
378 | */ |
||
379 | public function set_transaction( EE_Transaction $transaction ) { |
||
385 | |||
386 | |||
387 | |||
388 | /** |
||
389 | * reset_transaction |
||
390 | */ |
||
391 | public function reset_transaction() { |
||
395 | |||
396 | |||
397 | |||
398 | /** |
||
399 | * @return \EE_Transaction |
||
400 | */ |
||
401 | public function transaction() { |
||
407 | |||
408 | |||
409 | |||
410 | /** |
||
411 | * retrieve session data |
||
412 | * @access public |
||
413 | * @param null $key |
||
414 | * @param bool $reset_cache |
||
415 | * @return array |
||
416 | */ |
||
417 | public function get_session_data( $key = NULL, $reset_cache = FALSE ) { |
||
429 | |||
430 | |||
431 | |||
432 | /** |
||
433 | * set session data |
||
434 | * @access public |
||
435 | * @param array $data |
||
436 | * @return TRUE on success, FALSE on fail |
||
437 | */ |
||
438 | public function set_session_data( $data ) { |
||
458 | |||
459 | |||
460 | |||
461 | /** |
||
462 | * @initiate session |
||
463 | * @access private |
||
464 | * @return TRUE on success, FALSE on fail |
||
465 | * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
||
466 | * @throws \EE_Error |
||
467 | */ |
||
468 | private function _espresso_session() { |
||
516 | |||
517 | |||
518 | |||
519 | /** |
||
520 | * _get_session_data |
||
521 | * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup databases |
||
522 | * |
||
523 | * @return array |
||
524 | * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
||
525 | */ |
||
526 | protected function _retrieve_session_data() |
||
614 | |||
615 | |||
616 | |||
617 | /** |
||
618 | * _generate_session_id |
||
619 | * Retrieves the PHP session id either directly from the PHP session, |
||
620 | * or from the $_REQUEST array if it was passed in from an AJAX request. |
||
621 | * The session id is then salted and hashed (mmm sounds tasty) |
||
622 | * so that it can be safely used as a $_REQUEST param |
||
623 | * |
||
624 | * @return string |
||
625 | */ |
||
626 | protected function _generate_session_id() { |
||
635 | |||
636 | |||
637 | |||
638 | /** |
||
639 | * _get_sid_salt |
||
640 | * |
||
641 | * @return string |
||
642 | */ |
||
643 | protected function _get_sid_salt() { |
||
644 | // was session id salt already saved to db ? |
||
645 | if ( empty( $this->_sid_salt ) ) { |
||
646 | // no? then maybe use WP defined constant |
||
647 | if ( defined( 'AUTH_SALT' ) ) { |
||
648 | $this->_sid_salt = AUTH_SALT; |
||
649 | } |
||
650 | // if salt doesn't exist or is too short |
||
651 | if ( strlen( $this->_sid_salt ) < 32 ) { |
||
652 | // create a new one |
||
653 | $this->_sid_salt = wp_generate_password( 64 ); |
||
654 | } |
||
655 | // and save it as a permanent session setting |
||
656 | $session_settings = get_option( 'ee_session_settings' ); |
||
657 | $session_settings[ 'sid_salt' ] = $this->_sid_salt; |
||
658 | update_option( 'ee_session_settings', $session_settings ); |
||
659 | } |
||
660 | return $this->_sid_salt; |
||
661 | } |
||
662 | |||
663 | |||
664 | |||
665 | /** |
||
666 | * _set_init_access_and_expiration |
||
667 | * @return void |
||
668 | */ |
||
669 | protected function _set_init_access_and_expiration() { |
||
677 | |||
678 | |||
679 | |||
680 | /** |
||
681 | * @update session data prior to saving to the db |
||
682 | * @access public |
||
683 | * @param bool $new_session |
||
684 | * @return TRUE on success, FALSE on fail |
||
685 | * @throws \EE_Error |
||
686 | */ |
||
687 | public function update( $new_session = FALSE ) { |
||
769 | |||
770 | |||
771 | |||
772 | /** |
||
773 | * @create session data array |
||
774 | * @access public |
||
775 | * @return bool |
||
776 | * @throws \EE_Error |
||
777 | */ |
||
778 | private function _create_espresso_session( ) { |
||
783 | |||
784 | |||
785 | |||
786 | /** |
||
787 | * _save_session_to_db |
||
788 | * |
||
789 | * @access public |
||
790 | * @return string |
||
791 | * @throws \EE_Error |
||
792 | */ |
||
793 | private function _save_session_to_db() { |
||
841 | |||
842 | |||
843 | |||
844 | |||
845 | |||
846 | /** |
||
847 | * _visitor_ip |
||
848 | * attempt to get IP address of current visitor from server |
||
849 | * plz see: http://stackoverflow.com/a/2031935/1475279 |
||
850 | * |
||
851 | * @access public |
||
852 | * @return string |
||
853 | */ |
||
854 | View Code Duplication | private function _visitor_ip() { |
|
876 | |||
877 | |||
878 | |||
879 | |||
880 | |||
881 | /** |
||
882 | * @get the full page request the visitor is accessing |
||
883 | * @access public |
||
884 | * @return string |
||
885 | */ |
||
886 | public function _get_page_visit() { |
||
915 | |||
916 | |||
917 | |||
918 | |||
919 | |||
920 | /** |
||
921 | * @the current wp user id |
||
922 | * @access public |
||
923 | * @return int |
||
924 | */ |
||
925 | public function _wp_user_id() { |
||
930 | |||
931 | |||
932 | |||
933 | /** |
||
934 | * Clear EE_Session data |
||
935 | * |
||
936 | * @access public |
||
937 | * @param string $class |
||
938 | * @param string $function |
||
939 | * @return void |
||
940 | * @throws \EE_Error |
||
941 | */ |
||
942 | public function clear_session( $class = '', $function = '' ) { |
||
954 | |||
955 | |||
956 | |||
957 | /** |
||
958 | * @resets all non-default session vars |
||
959 | * @access public |
||
960 | * @param array $data_to_reset |
||
961 | * @param bool $show_all_notices |
||
962 | * @return TRUE on success, FALSE on fail |
||
963 | */ |
||
964 | public function reset_data( $data_to_reset = array(), $show_all_notices = FALSE ) { |
||
1008 | |||
1009 | |||
1010 | |||
1011 | /** |
||
1012 | * wp_loaded |
||
1013 | * |
||
1014 | * @access public |
||
1015 | * @throws \EE_Error |
||
1016 | */ |
||
1017 | public function wp_loaded() { |
||
1022 | |||
1023 | |||
1024 | |||
1025 | /** |
||
1026 | * Used to reset the entire object (for tests). |
||
1027 | * |
||
1028 | * @since 4.3.0 |
||
1029 | * @throws \EE_Error |
||
1030 | */ |
||
1031 | public function reset_instance() { |
||
1035 | |||
1036 | |||
1037 | |||
1038 | public function configure_garbage_collection_filters() |
||
1039 | { |
||
1040 | // run old filter we had for controlling session cleanup |
||
1041 | $expired_session_transient_delete_query_limit = absint( |
||
1042 | apply_filters( |
||
1043 | 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
||
1044 | 50 |
||
1045 | ) |
||
1046 | ); |
||
1047 | // is there a value? or one that is different than the default 50 records? |
||
1048 | if ($expired_session_transient_delete_query_limit === 0) { |
||
1049 | // hook into TransientCacheStorage in case Session cleanup was turned off |
||
1050 | add_filter('FHEE__TransientCacheStorage__transient_cleanup_schedule', '__return_zero'); |
||
1051 | } else if ($expired_session_transient_delete_query_limit !== 50) { |
||
1052 | // or use that for the new transient cleanup query limit |
||
1053 | add_filter( |
||
1054 | 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
||
1055 | function () use ($expired_session_transient_delete_query_limit) { |
||
1056 | return $expired_session_transient_delete_query_limit; |
||
1057 | } |
||
1058 | ); |
||
1059 | } |
||
1060 | } |
||
1061 | |||
1062 | |||
1063 | |||
1064 | /** |
||
1065 | * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
||
1066 | * @param $data1 |
||
1067 | * @return string |
||
1068 | */ |
||
1069 | private function find_serialize_error( $data1 ) { |
||
1124 | |||
1125 | } |
||
1126 | /* End of file EE_Session.class.php */ |
||
1128 |
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):