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 && apply_filters('FHEE_load_EE_Session', true)) { |
||
197 | self::$_instance = new self( |
||
198 | $cache_storage, |
||
|
|||
199 | $lifespan, |
||
200 | $request, |
||
201 | $session_start_handler, |
||
202 | $encryption |
||
203 | ); |
||
204 | } |
||
205 | return self::$_instance; |
||
206 | } |
||
207 | |||
208 | |||
209 | /** |
||
210 | * protected constructor to prevent direct creation |
||
211 | * |
||
212 | * @param CacheStorageInterface $cache_storage |
||
213 | * @param SessionLifespan $lifespan |
||
214 | * @param RequestInterface $request |
||
215 | * @param SessionStartHandler $session_start_handler |
||
216 | * @param EE_Encryption $encryption |
||
217 | * @throws InvalidArgumentException |
||
218 | * @throws InvalidDataTypeException |
||
219 | * @throws InvalidInterfaceException |
||
220 | */ |
||
221 | protected function __construct( |
||
222 | CacheStorageInterface $cache_storage, |
||
223 | SessionLifespan $lifespan, |
||
224 | RequestInterface $request, |
||
225 | SessionStartHandler $session_start_handler, |
||
226 | EE_Encryption $encryption = null |
||
227 | ) { |
||
228 | // session loading is turned ON by default, |
||
229 | // but prior to the 'AHEE__EE_System__core_loaded_and_ready' hook |
||
230 | // (which currently fires on the init hook at priority 9), |
||
231 | // can be turned back OFF via: add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
||
232 | if (! apply_filters('FHEE_load_EE_Session', true)) { |
||
233 | return; |
||
234 | } |
||
235 | $this->session_start_handler = $session_start_handler; |
||
236 | $this->session_lifespan = $lifespan; |
||
237 | $this->request = $request; |
||
238 | if (! defined('ESPRESSO_SESSION')) { |
||
239 | define('ESPRESSO_SESSION', true); |
||
240 | } |
||
241 | // retrieve session options from db |
||
242 | $session_settings = (array) get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
||
243 | if (! empty($session_settings)) { |
||
244 | // cycle though existing session options |
||
245 | foreach ($session_settings as $var_name => $session_setting) { |
||
246 | // set values for class properties |
||
247 | $var_name = '_' . $var_name; |
||
248 | $this->{$var_name} = $session_setting; |
||
249 | } |
||
250 | } |
||
251 | $this->cache_storage = $cache_storage; |
||
252 | // are we using encryption? |
||
253 | $this->_use_encryption = $encryption instanceof EE_Encryption |
||
254 | && EE_Registry::instance()->CFG->admin->encode_session_data(); |
||
255 | // encrypt data via: $this->encryption->encrypt(); |
||
256 | $this->encryption = $encryption; |
||
257 | // filter hook allows outside functions/classes/plugins to change default empty cart |
||
258 | $extra_default_session_vars = apply_filters('FHEE__EE_Session__construct__extra_default_session_vars', array()); |
||
259 | array_merge($this->_default_session_vars, $extra_default_session_vars); |
||
260 | // apply default session vars |
||
261 | $this->_set_defaults(); |
||
262 | add_action('AHEE__EE_System__initialize', array($this, 'open_session')); |
||
263 | // check request for 'clear_session' param |
||
264 | add_action('AHEE__EE_Request_Handler__construct__complete', array($this, 'wp_loaded')); |
||
265 | // once everything is all said and done, |
||
266 | add_action('shutdown', array($this, 'update'), 100); |
||
267 | add_action('shutdown', array($this, 'garbageCollection'), 1000); |
||
268 | $this->configure_garbage_collection_filters(); |
||
269 | } |
||
270 | |||
271 | |||
272 | /** |
||
273 | * @return bool |
||
274 | * @throws InvalidArgumentException |
||
275 | * @throws InvalidDataTypeException |
||
276 | * @throws InvalidInterfaceException |
||
277 | */ |
||
278 | public static function isLoadedAndActive() |
||
279 | { |
||
280 | return did_action('AHEE__EE_System__core_loaded_and_ready') |
||
281 | && EE_Session::instance() instanceof EE_Session |
||
282 | && EE_Session::instance()->isActive(); |
||
283 | } |
||
284 | |||
285 | |||
286 | /** |
||
287 | * @return bool |
||
288 | */ |
||
289 | public function isActive() |
||
290 | { |
||
291 | return $this->status === EE_Session::STATUS_OPEN; |
||
292 | } |
||
293 | |||
294 | |||
295 | /** |
||
296 | * @return void |
||
297 | * @throws EE_Error |
||
298 | * @throws InvalidArgumentException |
||
299 | * @throws InvalidDataTypeException |
||
300 | * @throws InvalidInterfaceException |
||
301 | * @throws InvalidSessionDataException |
||
302 | */ |
||
303 | public function open_session() |
||
304 | { |
||
305 | // check for existing session and retrieve it from db |
||
306 | if (! $this->_espresso_session()) { |
||
307 | // or just start a new one |
||
308 | $this->_create_espresso_session(); |
||
309 | } |
||
310 | } |
||
311 | |||
312 | |||
313 | /** |
||
314 | * @return bool |
||
315 | */ |
||
316 | public function expired() |
||
317 | { |
||
318 | return $this->_expired; |
||
319 | } |
||
320 | |||
321 | |||
322 | /** |
||
323 | * @return void |
||
324 | */ |
||
325 | public function reset_expired() |
||
326 | { |
||
327 | $this->_expired = false; |
||
328 | } |
||
329 | |||
330 | |||
331 | /** |
||
332 | * @return int |
||
333 | */ |
||
334 | public function expiration() |
||
335 | { |
||
336 | return $this->_expiration; |
||
337 | } |
||
338 | |||
339 | |||
340 | /** |
||
341 | * @return int |
||
342 | */ |
||
343 | public function extension() |
||
344 | { |
||
345 | return apply_filters('FHEE__EE_Session__extend_expiration__seconds_added', 10 * MINUTE_IN_SECONDS); |
||
346 | } |
||
347 | |||
348 | |||
349 | /** |
||
350 | * @param int $time number of seconds to add to session expiration |
||
351 | */ |
||
352 | public function extend_expiration($time = 0) |
||
353 | { |
||
354 | $time = $time ? $time : $this->extension(); |
||
355 | $this->_expiration += absint($time); |
||
356 | } |
||
357 | |||
358 | |||
359 | /** |
||
360 | * @return int |
||
361 | */ |
||
362 | public function lifespan() |
||
363 | { |
||
364 | return $this->session_lifespan->inSeconds(); |
||
365 | } |
||
366 | |||
367 | |||
368 | /** |
||
369 | * This just sets some defaults for the _session data property |
||
370 | * |
||
371 | * @access private |
||
372 | * @return void |
||
373 | */ |
||
374 | private function _set_defaults() |
||
375 | { |
||
376 | // set some defaults |
||
377 | foreach ($this->_default_session_vars as $key => $default_var) { |
||
378 | if (is_array($default_var)) { |
||
379 | $this->_session_data[ $key ] = array(); |
||
380 | } else { |
||
381 | $this->_session_data[ $key ] = ''; |
||
382 | } |
||
383 | } |
||
384 | } |
||
385 | |||
386 | |||
387 | /** |
||
388 | * @retrieve session data |
||
389 | * @access public |
||
390 | * @return string |
||
391 | */ |
||
392 | public function id() |
||
393 | { |
||
394 | return $this->_sid; |
||
395 | } |
||
396 | |||
397 | |||
398 | /** |
||
399 | * @param \EE_Cart $cart |
||
400 | * @return bool |
||
401 | */ |
||
402 | public function set_cart(EE_Cart $cart) |
||
403 | { |
||
404 | $this->_session_data['cart'] = $cart; |
||
405 | return true; |
||
406 | } |
||
407 | |||
408 | |||
409 | /** |
||
410 | * reset_cart |
||
411 | */ |
||
412 | public function reset_cart() |
||
413 | { |
||
414 | do_action('AHEE__EE_Session__reset_cart__before_reset', $this); |
||
415 | $this->_session_data['cart'] = null; |
||
416 | } |
||
417 | |||
418 | |||
419 | /** |
||
420 | * @return \EE_Cart |
||
421 | */ |
||
422 | public function cart() |
||
428 | |||
429 | |||
430 | /** |
||
431 | * @param \EE_Checkout $checkout |
||
432 | * @return bool |
||
433 | */ |
||
434 | public function set_checkout(EE_Checkout $checkout) |
||
435 | { |
||
436 | $this->_session_data['checkout'] = $checkout; |
||
437 | return true; |
||
438 | } |
||
439 | |||
440 | |||
441 | /** |
||
442 | * reset_checkout |
||
443 | */ |
||
444 | public function reset_checkout() |
||
449 | |||
450 | |||
451 | /** |
||
452 | * @return \EE_Checkout |
||
453 | */ |
||
454 | public function checkout() |
||
460 | |||
461 | |||
462 | /** |
||
463 | * @param \EE_Transaction $transaction |
||
464 | * @return bool |
||
465 | * @throws EE_Error |
||
466 | */ |
||
467 | public function set_transaction(EE_Transaction $transaction) |
||
474 | |||
475 | |||
476 | /** |
||
477 | * reset_transaction |
||
478 | */ |
||
479 | public function reset_transaction() |
||
484 | |||
485 | |||
486 | /** |
||
487 | * @return \EE_Transaction |
||
488 | */ |
||
489 | public function transaction() |
||
496 | |||
497 | |||
498 | /** |
||
499 | * retrieve session data |
||
500 | * |
||
501 | * @param null $key |
||
502 | * @param bool $reset_cache |
||
503 | * @return array |
||
504 | */ |
||
505 | public function get_session_data($key = null, $reset_cache = false) |
||
517 | |||
518 | |||
519 | /** |
||
520 | * Returns TRUE on success, FALSE on fail |
||
521 | * |
||
522 | * @param array $data |
||
523 | * @return bool |
||
524 | */ |
||
525 | public function set_session_data($data) |
||
526 | { |
||
527 | // nothing ??? bad data ??? go home! |
||
528 | if (empty($data) || ! is_array($data)) { |
||
529 | EE_Error::add_error( |
||
530 | esc_html__( |
||
531 | 'No session data or invalid session data was provided.', |
||
532 | 'event_espresso' |
||
533 | ), |
||
534 | __FILE__, |
||
535 | __FUNCTION__, |
||
536 | __LINE__ |
||
537 | ); |
||
538 | return false; |
||
539 | } |
||
540 | foreach ($data as $key => $value) { |
||
541 | View Code Duplication | if (isset($this->_default_session_vars[ $key ])) { |
|
542 | EE_Error::add_error( |
||
543 | sprintf( |
||
544 | esc_html__( |
||
545 | 'Sorry! %s is a default session datum and can not be reset.', |
||
546 | 'event_espresso' |
||
547 | ), |
||
548 | $key |
||
549 | ), |
||
550 | __FILE__, |
||
551 | __FUNCTION__, |
||
552 | __LINE__ |
||
553 | ); |
||
554 | return false; |
||
555 | } |
||
556 | $this->_session_data[ $key ] = $value; |
||
557 | } |
||
558 | return true; |
||
559 | } |
||
560 | |||
561 | |||
562 | /** |
||
563 | * @initiate session |
||
564 | * @access private |
||
565 | * @return TRUE on success, FALSE on fail |
||
566 | * @throws EE_Error |
||
567 | * @throws InvalidArgumentException |
||
568 | * @throws InvalidDataTypeException |
||
569 | * @throws InvalidInterfaceException |
||
570 | * @throws InvalidSessionDataException |
||
571 | */ |
||
572 | private function _espresso_session() |
||
617 | |||
618 | |||
619 | /** |
||
620 | * _get_session_data |
||
621 | * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup |
||
622 | * databases |
||
623 | * |
||
624 | * @return array |
||
625 | * @throws EE_Error |
||
626 | * @throws InvalidArgumentException |
||
627 | * @throws InvalidSessionDataException |
||
628 | * @throws InvalidDataTypeException |
||
629 | * @throws InvalidInterfaceException |
||
630 | */ |
||
631 | protected function _retrieve_session_data() |
||
725 | |||
726 | |||
727 | /** |
||
728 | * _generate_session_id |
||
729 | * Retrieves the PHP session id either directly from the PHP session, |
||
730 | * or from the $_REQUEST array if it was passed in from an AJAX request. |
||
731 | * The session id is then salted and hashed (mmm sounds tasty) |
||
732 | * so that it can be safely used as a $_REQUEST param |
||
733 | * |
||
734 | * @return string |
||
735 | */ |
||
736 | protected function _generate_session_id() |
||
746 | |||
747 | |||
748 | /** |
||
749 | * _get_sid_salt |
||
750 | * |
||
751 | * @return string |
||
752 | */ |
||
753 | protected function _get_sid_salt() |
||
771 | |||
772 | |||
773 | /** |
||
774 | * _set_init_access_and_expiration |
||
775 | * |
||
776 | * @return void |
||
777 | */ |
||
778 | protected function _set_init_access_and_expiration() |
||
787 | |||
788 | |||
789 | /** |
||
790 | * @update session data prior to saving to the db |
||
791 | * @access public |
||
792 | * @param bool $new_session |
||
793 | * @return TRUE on success, FALSE on fail |
||
794 | * @throws EE_Error |
||
795 | * @throws InvalidArgumentException |
||
796 | * @throws InvalidDataTypeException |
||
797 | * @throws InvalidInterfaceException |
||
798 | */ |
||
799 | public function update($new_session = false) |
||
867 | |||
868 | |||
869 | /** |
||
870 | * @create session data array |
||
871 | * @access public |
||
872 | * @return bool |
||
873 | * @throws EE_Error |
||
874 | * @throws InvalidArgumentException |
||
875 | * @throws InvalidDataTypeException |
||
876 | * @throws InvalidInterfaceException |
||
877 | */ |
||
878 | private function _create_espresso_session() |
||
884 | |||
885 | /** |
||
886 | * Detects if there is anything worth saving in the session (eg the cart is a good one, notices are pretty good |
||
887 | * too). This is used when determining if we want to save the session or not. |
||
888 | * @since 4.9.67.p |
||
889 | * @return bool |
||
890 | */ |
||
891 | private function sessionHasStuffWorthSaving() |
||
903 | /** |
||
904 | * _save_session_to_db |
||
905 | * |
||
906 | * @param bool $clear_session |
||
907 | * @return string |
||
908 | * @throws EE_Error |
||
909 | * @throws InvalidArgumentException |
||
910 | * @throws InvalidDataTypeException |
||
911 | * @throws InvalidInterfaceException |
||
912 | */ |
||
913 | private function _save_session_to_db($clear_session = false) |
||
954 | |||
955 | |||
956 | /** |
||
957 | * @get the full page request the visitor is accessing |
||
958 | * @access public |
||
959 | * @return string |
||
960 | */ |
||
961 | public function _get_page_visit() |
||
990 | |||
991 | |||
992 | /** |
||
993 | * @the current wp user id |
||
994 | * @access public |
||
995 | * @return int |
||
996 | */ |
||
997 | public function _wp_user_id() |
||
1003 | |||
1004 | |||
1005 | /** |
||
1006 | * Clear EE_Session data |
||
1007 | * |
||
1008 | * @access public |
||
1009 | * @param string $class |
||
1010 | * @param string $function |
||
1011 | * @return void |
||
1012 | * @throws EE_Error |
||
1013 | * @throws InvalidArgumentException |
||
1014 | * @throws InvalidDataTypeException |
||
1015 | * @throws InvalidInterfaceException |
||
1016 | */ |
||
1017 | public function clear_session($class = '', $function = '') |
||
1034 | |||
1035 | |||
1036 | /** |
||
1037 | * resets all non-default session vars. Returns TRUE on success, FALSE on fail |
||
1038 | * |
||
1039 | * @param array|mixed $data_to_reset |
||
1040 | * @param bool $show_all_notices |
||
1041 | * @return bool |
||
1042 | */ |
||
1043 | public function reset_data($data_to_reset = array(), $show_all_notices = false) |
||
1119 | |||
1120 | |||
1121 | /** |
||
1122 | * wp_loaded |
||
1123 | * |
||
1124 | * @access public |
||
1125 | * @throws EE_Error |
||
1126 | * @throws InvalidDataTypeException |
||
1127 | * @throws InvalidInterfaceException |
||
1128 | * @throws InvalidArgumentException |
||
1129 | */ |
||
1130 | public function wp_loaded() |
||
1136 | |||
1137 | |||
1138 | /** |
||
1139 | * Used to reset the entire object (for tests). |
||
1140 | * |
||
1141 | * @since 4.3.0 |
||
1142 | * @throws EE_Error |
||
1143 | * @throws InvalidDataTypeException |
||
1144 | * @throws InvalidInterfaceException |
||
1145 | * @throws InvalidArgumentException |
||
1146 | */ |
||
1147 | public function reset_instance() |
||
1152 | |||
1153 | |||
1154 | public function configure_garbage_collection_filters() |
||
1177 | |||
1178 | |||
1179 | /** |
||
1180 | * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
||
1181 | * @param $data1 |
||
1182 | * @return string |
||
1183 | */ |
||
1184 | private function find_serialize_error($data1) |
||
1240 | |||
1241 | |||
1242 | /** |
||
1243 | * Saves an array of settings used for configuring aspects of session behaviour |
||
1244 | * |
||
1245 | * @param array $updated_settings |
||
1246 | */ |
||
1247 | private function updateSessionSettings(array $updated_settings = array()) |
||
1253 | |||
1254 | |||
1255 | /** |
||
1256 | * garbage_collection |
||
1257 | */ |
||
1258 | public function garbageCollection() |
||
1302 | } |
||
1303 |
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):