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 |
||
25 | class EE_Session implements SessionIdentifierInterface |
||
26 | { |
||
27 | |||
28 | const session_id_prefix = 'ee_ssn_'; |
||
29 | |||
30 | const hash_check_prefix = 'ee_shc_'; |
||
31 | |||
32 | const OPTION_NAME_SETTINGS = 'ee_session_settings'; |
||
33 | |||
34 | const STATUS_CLOSED = 0; |
||
35 | |||
36 | const STATUS_OPEN = 1; |
||
37 | |||
38 | /** |
||
39 | * instance of the EE_Session object |
||
40 | * |
||
41 | * @var EE_Session |
||
42 | */ |
||
43 | private static $_instance; |
||
44 | |||
45 | /** |
||
46 | * @var CacheStorageInterface $cache_storage |
||
47 | */ |
||
48 | protected $cache_storage; |
||
49 | |||
50 | /** |
||
51 | * @var EE_Encryption $encryption |
||
52 | */ |
||
53 | protected $encryption; |
||
54 | |||
55 | /** |
||
56 | * @var SessionStartHandler $session_start_handler |
||
57 | */ |
||
58 | protected $session_start_handler; |
||
59 | |||
60 | /** |
||
61 | * the session id |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | private $_sid; |
||
66 | |||
67 | /** |
||
68 | * session id salt |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | private $_sid_salt; |
||
73 | |||
74 | /** |
||
75 | * session data |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | private $_session_data = array(); |
||
80 | |||
81 | /** |
||
82 | * how long an EE session lasts |
||
83 | * default session lifespan of 1 hour (for not so instant IPNs) |
||
84 | * |
||
85 | * @var SessionLifespan $session_lifespan |
||
86 | */ |
||
87 | private $session_lifespan; |
||
88 | |||
89 | /** |
||
90 | * session expiration time as Unix timestamp in GMT |
||
91 | * |
||
92 | * @var int |
||
93 | */ |
||
94 | private $_expiration; |
||
95 | |||
96 | /** |
||
97 | * whether or not session has expired at some point |
||
98 | * |
||
99 | * @var boolean |
||
100 | */ |
||
101 | private $_expired = false; |
||
102 | |||
103 | /** |
||
104 | * current time as Unix timestamp in GMT |
||
105 | * |
||
106 | * @var int |
||
107 | */ |
||
108 | private $_time; |
||
109 | |||
110 | /** |
||
111 | * whether to encrypt session data |
||
112 | * |
||
113 | * @var bool |
||
114 | */ |
||
115 | private $_use_encryption; |
||
116 | |||
117 | /** |
||
118 | * well... according to the server... |
||
119 | * |
||
120 | * @var null |
||
121 | */ |
||
122 | private $_user_agent; |
||
123 | |||
124 | /** |
||
125 | * do you really trust the server ? |
||
126 | * |
||
127 | * @var null |
||
128 | */ |
||
129 | private $_ip_address; |
||
130 | |||
131 | /** |
||
132 | * current WP user_id |
||
133 | * |
||
134 | * @var null |
||
135 | */ |
||
136 | private $_wp_user_id; |
||
137 | |||
138 | /** |
||
139 | * array for defining default session vars |
||
140 | * |
||
141 | * @var array |
||
142 | */ |
||
143 | private $_default_session_vars = array( |
||
144 | 'id' => null, |
||
145 | 'user_id' => null, |
||
146 | 'ip_address' => null, |
||
147 | 'user_agent' => null, |
||
148 | 'init_access' => null, |
||
149 | 'last_access' => null, |
||
150 | 'expiration' => null, |
||
151 | 'pages_visited' => array(), |
||
152 | ); |
||
153 | |||
154 | /** |
||
155 | * timestamp for when last garbage collection cycle was performed |
||
156 | * |
||
157 | * @var int $_last_gc |
||
158 | */ |
||
159 | private $_last_gc; |
||
160 | |||
161 | /** |
||
162 | * @var RequestInterface $request |
||
163 | */ |
||
164 | protected $request; |
||
165 | |||
166 | /** |
||
167 | * whether session is active or not |
||
168 | * |
||
169 | * @var int $status |
||
170 | */ |
||
171 | private $status = EE_Session::STATUS_CLOSED; |
||
172 | |||
173 | |||
174 | /** |
||
175 | * @singleton method used to instantiate class object |
||
176 | * @param CacheStorageInterface $cache_storage |
||
177 | * @param SessionLifespan|null $lifespan |
||
178 | * @param RequestInterface $request |
||
179 | * @param SessionStartHandler $session_start_handler |
||
180 | * @param EE_Encryption $encryption |
||
181 | * @return EE_Session |
||
182 | * @throws InvalidArgumentException |
||
183 | * @throws InvalidDataTypeException |
||
184 | * @throws InvalidInterfaceException |
||
185 | */ |
||
186 | public static function instance( |
||
187 | CacheStorageInterface $cache_storage = null, |
||
188 | SessionLifespan $lifespan = null, |
||
189 | RequestInterface $request = null, |
||
190 | SessionStartHandler $session_start_handler = null, |
||
191 | EE_Encryption $encryption = null |
||
192 | ) { |
||
193 | // check if class object is instantiated |
||
194 | // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: |
||
195 | // add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
||
196 | if (! self::$_instance instanceof EE_Session |
||
197 | && apply_filters('FHEE_load_EE_Session', true) |
||
198 | && $cache_storage instanceof CacheStorageInterface |
||
199 | && $lifespan instanceof SessionLifespan |
||
200 | && $request instanceof RequestInterface |
||
201 | && $session_start_handler instanceof SessionStartHandler |
||
202 | ) { |
||
203 | self::$_instance = new self( |
||
204 | $cache_storage, |
||
205 | $lifespan, |
||
206 | $request, |
||
207 | $session_start_handler, |
||
208 | $encryption |
||
209 | ); |
||
210 | } |
||
211 | return self::$_instance; |
||
212 | } |
||
213 | |||
214 | |||
215 | /** |
||
216 | * protected constructor to prevent direct creation |
||
217 | * |
||
218 | * @param CacheStorageInterface $cache_storage |
||
219 | * @param SessionLifespan $lifespan |
||
220 | * @param RequestInterface $request |
||
221 | * @param SessionStartHandler $session_start_handler |
||
222 | * @param EE_Encryption $encryption |
||
223 | * @throws InvalidArgumentException |
||
224 | * @throws InvalidDataTypeException |
||
225 | * @throws InvalidInterfaceException |
||
226 | */ |
||
227 | protected function __construct( |
||
228 | CacheStorageInterface $cache_storage, |
||
229 | SessionLifespan $lifespan, |
||
230 | RequestInterface $request, |
||
231 | SessionStartHandler $session_start_handler, |
||
232 | EE_Encryption $encryption = null |
||
233 | ) { |
||
234 | // session loading is turned ON by default, |
||
235 | // but prior to the 'AHEE__EE_System__core_loaded_and_ready' hook |
||
236 | // (which currently fires on the init hook at priority 9), |
||
237 | // can be turned back OFF via: add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
||
238 | if (! apply_filters('FHEE_load_EE_Session', true)) { |
||
239 | return; |
||
240 | } |
||
241 | $this->session_start_handler = $session_start_handler; |
||
242 | $this->session_lifespan = $lifespan; |
||
243 | $this->request = $request; |
||
244 | if (! defined('ESPRESSO_SESSION')) { |
||
245 | define('ESPRESSO_SESSION', true); |
||
246 | } |
||
247 | // retrieve session options from db |
||
248 | $session_settings = (array) get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
||
249 | if (! empty($session_settings)) { |
||
250 | // cycle though existing session options |
||
251 | foreach ($session_settings as $var_name => $session_setting) { |
||
252 | // set values for class properties |
||
253 | $var_name = '_' . $var_name; |
||
254 | $this->{$var_name} = $session_setting; |
||
255 | } |
||
256 | } |
||
257 | $this->cache_storage = $cache_storage; |
||
258 | // are we using encryption? |
||
259 | $this->_use_encryption = $encryption instanceof EE_Encryption |
||
260 | && EE_Registry::instance()->CFG->admin->encode_session_data(); |
||
261 | // encrypt data via: $this->encryption->encrypt(); |
||
262 | $this->encryption = $encryption; |
||
263 | // filter hook allows outside functions/classes/plugins to change default empty cart |
||
264 | $extra_default_session_vars = apply_filters('FHEE__EE_Session__construct__extra_default_session_vars', array()); |
||
265 | array_merge($this->_default_session_vars, $extra_default_session_vars); |
||
266 | // apply default session vars |
||
267 | $this->_set_defaults(); |
||
268 | add_action('AHEE__EE_System__initialize', array($this, 'open_session')); |
||
269 | // check request for 'clear_session' param |
||
270 | add_action('AHEE__EE_Request_Handler__construct__complete', array($this, 'wp_loaded')); |
||
271 | // once everything is all said and done, |
||
272 | add_action('shutdown', array($this, 'update'), 100); |
||
273 | add_action('shutdown', array($this, 'garbageCollection'), 1000); |
||
274 | $this->configure_garbage_collection_filters(); |
||
275 | } |
||
276 | |||
277 | |||
278 | /** |
||
279 | * @return bool |
||
280 | * @throws InvalidArgumentException |
||
281 | * @throws InvalidDataTypeException |
||
282 | * @throws InvalidInterfaceException |
||
283 | */ |
||
284 | public static function isLoadedAndActive() |
||
285 | { |
||
286 | return did_action('AHEE__EE_System__core_loaded_and_ready') |
||
287 | && EE_Session::instance() instanceof EE_Session |
||
288 | && EE_Session::instance()->isActive(); |
||
289 | } |
||
290 | |||
291 | |||
292 | /** |
||
293 | * @return bool |
||
294 | */ |
||
295 | public function isActive() |
||
296 | { |
||
297 | return $this->status === EE_Session::STATUS_OPEN; |
||
298 | } |
||
299 | |||
300 | |||
301 | /** |
||
302 | * @return void |
||
303 | * @throws EE_Error |
||
304 | * @throws InvalidArgumentException |
||
305 | * @throws InvalidDataTypeException |
||
306 | * @throws InvalidInterfaceException |
||
307 | * @throws InvalidSessionDataException |
||
308 | */ |
||
309 | public function open_session() |
||
310 | { |
||
311 | // check for existing session and retrieve it from db |
||
312 | if (! $this->_espresso_session()) { |
||
313 | // or just start a new one |
||
314 | $this->_create_espresso_session(); |
||
315 | } |
||
316 | } |
||
317 | |||
318 | |||
319 | /** |
||
320 | * @return bool |
||
321 | */ |
||
322 | public function expired() |
||
323 | { |
||
324 | return $this->_expired; |
||
325 | } |
||
326 | |||
327 | |||
328 | /** |
||
329 | * @return void |
||
330 | */ |
||
331 | public function reset_expired() |
||
332 | { |
||
333 | $this->_expired = false; |
||
334 | } |
||
335 | |||
336 | |||
337 | /** |
||
338 | * @return int |
||
339 | */ |
||
340 | public function expiration() |
||
341 | { |
||
342 | return $this->_expiration; |
||
343 | } |
||
344 | |||
345 | |||
346 | /** |
||
347 | * @return int |
||
348 | */ |
||
349 | public function extension() |
||
350 | { |
||
351 | return apply_filters('FHEE__EE_Session__extend_expiration__seconds_added', 10 * MINUTE_IN_SECONDS); |
||
352 | } |
||
353 | |||
354 | |||
355 | /** |
||
356 | * @param int $time number of seconds to add to session expiration |
||
357 | */ |
||
358 | public function extend_expiration($time = 0) |
||
359 | { |
||
360 | $time = $time ? $time : $this->extension(); |
||
361 | $this->_expiration += absint($time); |
||
362 | } |
||
363 | |||
364 | |||
365 | /** |
||
366 | * @return int |
||
367 | */ |
||
368 | public function lifespan() |
||
369 | { |
||
370 | return $this->session_lifespan->inSeconds(); |
||
371 | } |
||
372 | |||
373 | |||
374 | /** |
||
375 | * This just sets some defaults for the _session data property |
||
376 | * |
||
377 | * @access private |
||
378 | * @return void |
||
379 | */ |
||
380 | private function _set_defaults() |
||
381 | { |
||
382 | // set some defaults |
||
383 | foreach ($this->_default_session_vars as $key => $default_var) { |
||
384 | if (is_array($default_var)) { |
||
385 | $this->_session_data[ $key ] = array(); |
||
386 | } else { |
||
387 | $this->_session_data[ $key ] = ''; |
||
388 | } |
||
389 | } |
||
390 | } |
||
391 | |||
392 | |||
393 | /** |
||
394 | * @retrieve session data |
||
395 | * @access public |
||
396 | * @return string |
||
397 | */ |
||
398 | public function id() |
||
399 | { |
||
400 | return $this->_sid; |
||
401 | } |
||
402 | |||
403 | |||
404 | /** |
||
405 | * @param \EE_Cart $cart |
||
406 | * @return bool |
||
407 | */ |
||
408 | public function set_cart(EE_Cart $cart) |
||
409 | { |
||
410 | $this->_session_data['cart'] = $cart; |
||
411 | return true; |
||
412 | } |
||
413 | |||
414 | |||
415 | /** |
||
416 | * reset_cart |
||
417 | */ |
||
418 | public function reset_cart() |
||
419 | { |
||
420 | do_action('AHEE__EE_Session__reset_cart__before_reset', $this); |
||
421 | $this->_session_data['cart'] = null; |
||
422 | } |
||
423 | |||
424 | |||
425 | /** |
||
426 | * @return \EE_Cart |
||
427 | */ |
||
428 | public function cart() |
||
429 | { |
||
430 | return isset($this->_session_data['cart']) && $this->_session_data['cart'] instanceof EE_Cart |
||
431 | ? $this->_session_data['cart'] |
||
432 | : null; |
||
433 | } |
||
434 | |||
435 | |||
436 | /** |
||
437 | * @param \EE_Checkout $checkout |
||
438 | * @return bool |
||
439 | */ |
||
440 | public function set_checkout(EE_Checkout $checkout) |
||
441 | { |
||
442 | $this->_session_data['checkout'] = $checkout; |
||
443 | return true; |
||
444 | } |
||
445 | |||
446 | |||
447 | /** |
||
448 | * reset_checkout |
||
449 | */ |
||
450 | public function reset_checkout() |
||
451 | { |
||
452 | do_action('AHEE__EE_Session__reset_checkout__before_reset', $this); |
||
453 | $this->_session_data['checkout'] = null; |
||
454 | } |
||
455 | |||
456 | |||
457 | /** |
||
458 | * @return \EE_Checkout |
||
459 | */ |
||
460 | public function checkout() |
||
461 | { |
||
462 | return isset($this->_session_data['checkout']) && $this->_session_data['checkout'] instanceof EE_Checkout |
||
463 | ? $this->_session_data['checkout'] |
||
464 | : null; |
||
465 | } |
||
466 | |||
467 | |||
468 | /** |
||
469 | * @param \EE_Transaction $transaction |
||
470 | * @return bool |
||
471 | * @throws EE_Error |
||
472 | */ |
||
473 | public function set_transaction(EE_Transaction $transaction) |
||
474 | { |
||
475 | // first remove the session from the transaction before we save the transaction in the session |
||
476 | $transaction->set_txn_session_data(null); |
||
477 | $this->_session_data['transaction'] = $transaction; |
||
478 | return true; |
||
479 | } |
||
480 | |||
481 | |||
482 | /** |
||
483 | * reset_transaction |
||
484 | */ |
||
485 | public function reset_transaction() |
||
486 | { |
||
487 | do_action('AHEE__EE_Session__reset_transaction__before_reset', $this); |
||
488 | $this->_session_data['transaction'] = null; |
||
489 | } |
||
490 | |||
491 | |||
492 | /** |
||
493 | * @return \EE_Transaction |
||
494 | */ |
||
495 | public function transaction() |
||
502 | |||
503 | |||
504 | /** |
||
505 | * retrieve session data |
||
506 | * |
||
507 | * @param null $key |
||
508 | * @param bool $reset_cache |
||
509 | * @return array |
||
510 | */ |
||
511 | public function get_session_data($key = null, $reset_cache = false) |
||
523 | |||
524 | |||
525 | /** |
||
526 | * Returns TRUE on success, FALSE on fail |
||
527 | * |
||
528 | * @param array $data |
||
529 | * @return bool |
||
530 | */ |
||
531 | public function set_session_data($data) |
||
532 | { |
||
533 | // nothing ??? bad data ??? go home! |
||
534 | if (empty($data) || ! is_array($data)) { |
||
535 | EE_Error::add_error( |
||
536 | esc_html__( |
||
537 | 'No session data or invalid session data was provided.', |
||
538 | 'event_espresso' |
||
539 | ), |
||
540 | __FILE__, |
||
541 | __FUNCTION__, |
||
542 | __LINE__ |
||
543 | ); |
||
544 | return false; |
||
566 | |||
567 | |||
568 | /** |
||
569 | * @initiate session |
||
570 | * @access private |
||
571 | * @return TRUE on success, FALSE on fail |
||
572 | * @throws EE_Error |
||
573 | * @throws InvalidArgumentException |
||
574 | * @throws InvalidDataTypeException |
||
575 | * @throws InvalidInterfaceException |
||
576 | * @throws InvalidSessionDataException |
||
577 | */ |
||
578 | private function _espresso_session() |
||
623 | |||
624 | |||
625 | /** |
||
626 | * _get_session_data |
||
627 | * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup |
||
628 | * databases |
||
629 | * |
||
630 | * @return array |
||
631 | * @throws EE_Error |
||
632 | * @throws InvalidArgumentException |
||
633 | * @throws InvalidSessionDataException |
||
634 | * @throws InvalidDataTypeException |
||
635 | * @throws InvalidInterfaceException |
||
636 | */ |
||
637 | protected function _retrieve_session_data() |
||
731 | |||
732 | |||
733 | /** |
||
734 | * _generate_session_id |
||
735 | * Retrieves the PHP session id either directly from the PHP session, |
||
736 | * or from the $_REQUEST array if it was passed in from an AJAX request. |
||
737 | * The session id is then salted and hashed (mmm sounds tasty) |
||
738 | * so that it can be safely used as a $_REQUEST param |
||
739 | * |
||
740 | * @return string |
||
741 | */ |
||
742 | protected function _generate_session_id() |
||
752 | |||
753 | |||
754 | /** |
||
755 | * _get_sid_salt |
||
756 | * |
||
757 | * @return string |
||
758 | */ |
||
759 | protected function _get_sid_salt() |
||
777 | |||
778 | |||
779 | /** |
||
780 | * _set_init_access_and_expiration |
||
781 | * |
||
782 | * @return void |
||
783 | */ |
||
784 | protected function _set_init_access_and_expiration() |
||
793 | |||
794 | |||
795 | /** |
||
796 | * @update session data prior to saving to the db |
||
797 | * @access public |
||
798 | * @param bool $new_session |
||
799 | * @return TRUE on success, FALSE on fail |
||
800 | * @throws EE_Error |
||
801 | * @throws InvalidArgumentException |
||
802 | * @throws InvalidDataTypeException |
||
803 | * @throws InvalidInterfaceException |
||
804 | */ |
||
805 | public function update($new_session = false) |
||
873 | |||
874 | |||
875 | /** |
||
876 | * @create session data array |
||
877 | * @access public |
||
878 | * @return bool |
||
879 | * @throws EE_Error |
||
880 | * @throws InvalidArgumentException |
||
881 | * @throws InvalidDataTypeException |
||
882 | * @throws InvalidInterfaceException |
||
883 | */ |
||
884 | private function _create_espresso_session() |
||
890 | |||
891 | /** |
||
892 | * Detects if there is anything worth saving in the session (eg the cart is a good one, notices are pretty good |
||
893 | * too). This is used when determining if we want to save the session or not. |
||
894 | * @since 4.9.67.p |
||
895 | * @return bool |
||
896 | */ |
||
897 | private function sessionHasStuffWorthSaving() |
||
909 | /** |
||
910 | * _save_session_to_db |
||
911 | * |
||
912 | * @param bool $clear_session |
||
913 | * @return string |
||
914 | * @throws EE_Error |
||
915 | * @throws InvalidArgumentException |
||
916 | * @throws InvalidDataTypeException |
||
917 | * @throws InvalidInterfaceException |
||
918 | */ |
||
919 | private function _save_session_to_db($clear_session = false) |
||
960 | |||
961 | |||
962 | /** |
||
963 | * @get the full page request the visitor is accessing |
||
964 | * @access public |
||
965 | * @return string |
||
966 | */ |
||
967 | public function _get_page_visit() |
||
996 | |||
997 | |||
998 | /** |
||
999 | * @the current wp user id |
||
1000 | * @access public |
||
1001 | * @return int |
||
1002 | */ |
||
1003 | public function _wp_user_id() |
||
1009 | |||
1010 | |||
1011 | /** |
||
1012 | * Clear EE_Session data |
||
1013 | * |
||
1014 | * @access public |
||
1015 | * @param string $class |
||
1016 | * @param string $function |
||
1017 | * @return void |
||
1018 | * @throws EE_Error |
||
1019 | * @throws InvalidArgumentException |
||
1020 | * @throws InvalidDataTypeException |
||
1021 | * @throws InvalidInterfaceException |
||
1022 | */ |
||
1023 | public function clear_session($class = '', $function = '') |
||
1040 | |||
1041 | |||
1042 | /** |
||
1043 | * resets all non-default session vars. Returns TRUE on success, FALSE on fail |
||
1044 | * |
||
1045 | * @param array|mixed $data_to_reset |
||
1046 | * @param bool $show_all_notices |
||
1047 | * @return bool |
||
1048 | */ |
||
1049 | public function reset_data($data_to_reset = array(), $show_all_notices = false) |
||
1125 | |||
1126 | |||
1127 | /** |
||
1128 | * wp_loaded |
||
1129 | * |
||
1130 | * @access public |
||
1131 | * @throws EE_Error |
||
1132 | * @throws InvalidDataTypeException |
||
1133 | * @throws InvalidInterfaceException |
||
1134 | * @throws InvalidArgumentException |
||
1135 | */ |
||
1136 | public function wp_loaded() |
||
1142 | |||
1143 | |||
1144 | /** |
||
1145 | * Used to reset the entire object (for tests). |
||
1146 | * |
||
1147 | * @since 4.3.0 |
||
1148 | * @throws EE_Error |
||
1149 | * @throws InvalidDataTypeException |
||
1150 | * @throws InvalidInterfaceException |
||
1151 | * @throws InvalidArgumentException |
||
1152 | */ |
||
1153 | public function reset_instance() |
||
1158 | |||
1159 | |||
1160 | public function configure_garbage_collection_filters() |
||
1183 | |||
1184 | |||
1185 | /** |
||
1186 | * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
||
1187 | * @param $data1 |
||
1188 | * @return string |
||
1189 | */ |
||
1190 | private function find_serialize_error($data1) |
||
1246 | |||
1247 | |||
1248 | /** |
||
1249 | * Saves an array of settings used for configuring aspects of session behaviour |
||
1250 | * |
||
1251 | * @param array $updated_settings |
||
1252 | */ |
||
1253 | private function updateSessionSettings(array $updated_settings = array()) |
||
1259 | |||
1260 | |||
1261 | /** |
||
1262 | * garbage_collection |
||
1263 | */ |
||
1264 | public function garbageCollection() |
||
1308 | } |
||
1309 |