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 |
||
22 | class EE_Session implements SessionIdentifierInterface |
||
23 | { |
||
24 | |||
25 | const session_id_prefix = 'ee_ssn_'; |
||
26 | |||
27 | const hash_check_prefix = 'ee_shc_'; |
||
28 | |||
29 | const OPTION_NAME_SETTINGS = 'ee_session_settings'; |
||
30 | |||
31 | const STATUS_CLOSED = 0; |
||
32 | |||
33 | const STATUS_OPEN = 1; |
||
34 | |||
35 | /** |
||
36 | * instance of the EE_Session object |
||
37 | * |
||
38 | * @var EE_Session |
||
39 | */ |
||
40 | private static $_instance; |
||
41 | |||
42 | /** |
||
43 | * @var CacheStorageInterface $cache_storage |
||
44 | */ |
||
45 | protected $cache_storage; |
||
46 | |||
47 | /** |
||
48 | * EE_Encryption object |
||
49 | * |
||
50 | * @var EE_Encryption |
||
51 | */ |
||
52 | protected $encryption; |
||
53 | |||
54 | /** |
||
55 | * the session id |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | private $_sid; |
||
60 | |||
61 | /** |
||
62 | * session id salt |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | private $_sid_salt; |
||
67 | |||
68 | /** |
||
69 | * session data |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | private $_session_data = array(); |
||
74 | |||
75 | /** |
||
76 | * how long an EE session lasts |
||
77 | * default session lifespan of 1 hour (for not so instant IPNs) |
||
78 | * |
||
79 | * @var SessionLifespan $session_lifespan |
||
80 | */ |
||
81 | private $session_lifespan; |
||
82 | |||
83 | /** |
||
84 | * session expiration time as Unix timestamp in GMT |
||
85 | * |
||
86 | * @var int |
||
87 | */ |
||
88 | private $_expiration; |
||
89 | |||
90 | /** |
||
91 | * whether or not session has expired at some point |
||
92 | * |
||
93 | * @var boolean |
||
94 | */ |
||
95 | private $_expired = false; |
||
96 | |||
97 | /** |
||
98 | * current time as Unix timestamp in GMT |
||
99 | * |
||
100 | * @var int |
||
101 | */ |
||
102 | private $_time; |
||
103 | |||
104 | /** |
||
105 | * whether to encrypt session data |
||
106 | * |
||
107 | * @var bool |
||
108 | */ |
||
109 | private $_use_encryption; |
||
110 | |||
111 | /** |
||
112 | * well... according to the server... |
||
113 | * |
||
114 | * @var null |
||
115 | */ |
||
116 | private $_user_agent; |
||
117 | |||
118 | /** |
||
119 | * do you really trust the server ? |
||
120 | * |
||
121 | * @var null |
||
122 | */ |
||
123 | private $_ip_address; |
||
124 | |||
125 | /** |
||
126 | * current WP user_id |
||
127 | * |
||
128 | * @var null |
||
129 | */ |
||
130 | private $_wp_user_id; |
||
131 | |||
132 | /** |
||
133 | * array for defining default session vars |
||
134 | * |
||
135 | * @var array |
||
136 | */ |
||
137 | private $_default_session_vars = array( |
||
138 | 'id' => null, |
||
139 | 'user_id' => null, |
||
140 | 'ip_address' => null, |
||
141 | 'user_agent' => null, |
||
142 | 'init_access' => null, |
||
143 | 'last_access' => null, |
||
144 | 'expiration' => null, |
||
145 | 'pages_visited' => array(), |
||
146 | ); |
||
147 | |||
148 | /** |
||
149 | * timestamp for when last garbage collection cycle was performed |
||
150 | * |
||
151 | * @var int $_last_gc |
||
152 | */ |
||
153 | private $_last_gc; |
||
154 | |||
155 | /** |
||
156 | * @var RequestInterface $request |
||
157 | */ |
||
158 | protected $request; |
||
159 | |||
160 | /** |
||
161 | * whether session is active or not |
||
162 | * |
||
163 | * @var int $status |
||
164 | */ |
||
165 | private $status = EE_Session::STATUS_CLOSED; |
||
166 | |||
167 | |||
168 | |||
169 | /** |
||
170 | * @singleton method used to instantiate class object |
||
171 | * @param CacheStorageInterface $cache_storage |
||
172 | * @param SessionLifespan|null $lifespan |
||
173 | * @param RequestInterface $request |
||
174 | * @param EE_Encryption $encryption |
||
175 | * @return EE_Session |
||
176 | * @throws InvalidArgumentException |
||
177 | * @throws InvalidDataTypeException |
||
178 | * @throws InvalidInterfaceException |
||
179 | */ |
||
180 | public static function instance( |
||
199 | |||
200 | |||
201 | /** |
||
202 | * protected constructor to prevent direct creation |
||
203 | * |
||
204 | * @param CacheStorageInterface $cache_storage |
||
205 | * @param SessionLifespan $lifespan |
||
206 | * @param RequestInterface $request |
||
207 | * @param EE_Encryption $encryption |
||
208 | * @throws InvalidArgumentException |
||
209 | * @throws InvalidDataTypeException |
||
210 | * @throws InvalidInterfaceException |
||
211 | */ |
||
212 | protected function __construct( |
||
259 | |||
260 | |||
261 | /** |
||
262 | * @return bool |
||
263 | * @throws InvalidArgumentException |
||
264 | * @throws InvalidDataTypeException |
||
265 | * @throws InvalidInterfaceException |
||
266 | */ |
||
267 | public static function isLoadedAndActive() |
||
273 | |||
274 | |||
275 | /** |
||
276 | * @return bool |
||
277 | */ |
||
278 | public function isActive() |
||
282 | |||
283 | |||
284 | |||
285 | /** |
||
286 | * @return void |
||
287 | * @throws EE_Error |
||
288 | * @throws InvalidArgumentException |
||
289 | * @throws InvalidDataTypeException |
||
290 | * @throws InvalidInterfaceException |
||
291 | * @throws InvalidSessionDataException |
||
292 | */ |
||
293 | public function open_session() |
||
301 | |||
302 | |||
303 | |||
304 | /** |
||
305 | * @return bool |
||
306 | */ |
||
307 | public function expired() |
||
311 | |||
312 | |||
313 | |||
314 | /** |
||
315 | * @return void |
||
316 | */ |
||
317 | public function reset_expired() |
||
321 | |||
322 | |||
323 | /** |
||
324 | * @return int |
||
325 | */ |
||
326 | public function expiration() |
||
330 | |||
331 | |||
332 | |||
333 | /** |
||
334 | * @return int |
||
335 | */ |
||
336 | public function extension() |
||
340 | |||
341 | |||
342 | |||
343 | /** |
||
344 | * @param int $time number of seconds to add to session expiration |
||
345 | */ |
||
346 | public function extend_expiration($time = 0) |
||
351 | |||
352 | |||
353 | |||
354 | /** |
||
355 | * @return int |
||
356 | */ |
||
357 | public function lifespan() |
||
361 | |||
362 | |||
363 | |||
364 | /** |
||
365 | * This just sets some defaults for the _session data property |
||
366 | * |
||
367 | * @access private |
||
368 | * @return void |
||
369 | */ |
||
370 | private function _set_defaults() |
||
381 | |||
382 | |||
383 | |||
384 | /** |
||
385 | * @retrieve session data |
||
386 | * @access public |
||
387 | * @return string |
||
388 | */ |
||
389 | public function id() |
||
393 | |||
394 | |||
395 | |||
396 | /** |
||
397 | * @param \EE_Cart $cart |
||
398 | * @return bool |
||
399 | */ |
||
400 | public function set_cart(EE_Cart $cart) |
||
405 | |||
406 | |||
407 | |||
408 | /** |
||
409 | * reset_cart |
||
410 | */ |
||
411 | public function reset_cart() |
||
416 | |||
417 | |||
418 | |||
419 | /** |
||
420 | * @return \EE_Cart |
||
421 | */ |
||
422 | public function cart() |
||
428 | |||
429 | |||
430 | |||
431 | /** |
||
432 | * @param \EE_Checkout $checkout |
||
433 | * @return bool |
||
434 | */ |
||
435 | public function set_checkout(EE_Checkout $checkout) |
||
440 | |||
441 | |||
442 | |||
443 | /** |
||
444 | * reset_checkout |
||
445 | */ |
||
446 | public function reset_checkout() |
||
451 | |||
452 | |||
453 | |||
454 | /** |
||
455 | * @return \EE_Checkout |
||
456 | */ |
||
457 | public function checkout() |
||
463 | |||
464 | |||
465 | |||
466 | /** |
||
467 | * @param \EE_Transaction $transaction |
||
468 | * @return bool |
||
469 | * @throws EE_Error |
||
470 | */ |
||
471 | public function set_transaction(EE_Transaction $transaction) |
||
478 | |||
479 | |||
480 | |||
481 | /** |
||
482 | * reset_transaction |
||
483 | */ |
||
484 | public function reset_transaction() |
||
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__, __FUNCTION__, __LINE__ |
||
541 | ); |
||
542 | return false; |
||
543 | } |
||
544 | foreach ($data as $key => $value) { |
||
545 | View Code Duplication | if (isset($this->_default_session_vars[ $key ])) { |
|
546 | EE_Error::add_error( |
||
547 | sprintf( |
||
548 | esc_html__( |
||
549 | 'Sorry! %s is a default session datum and can not be reset.', |
||
550 | 'event_espresso' |
||
551 | ), |
||
552 | $key |
||
553 | ), |
||
554 | __FILE__, __FUNCTION__, __LINE__ |
||
555 | ); |
||
556 | return false; |
||
557 | } |
||
558 | $this->_session_data[ $key ] = $value; |
||
559 | } |
||
560 | return true; |
||
561 | } |
||
562 | |||
563 | |||
564 | |||
565 | /** |
||
566 | * @initiate session |
||
567 | * @access private |
||
568 | * @return TRUE on success, FALSE on fail |
||
569 | * @throws EE_Error |
||
570 | * @throws InvalidArgumentException |
||
571 | * @throws InvalidDataTypeException |
||
572 | * @throws InvalidInterfaceException |
||
573 | * @throws InvalidSessionDataException |
||
574 | */ |
||
575 | private function _espresso_session() |
||
624 | |||
625 | |||
626 | |||
627 | /** |
||
628 | * _get_session_data |
||
629 | * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup |
||
630 | * databases |
||
631 | * |
||
632 | * @return array |
||
633 | * @throws EE_Error |
||
634 | * @throws InvalidArgumentException |
||
635 | * @throws InvalidSessionDataException |
||
636 | * @throws InvalidDataTypeException |
||
637 | * @throws InvalidInterfaceException |
||
638 | */ |
||
639 | protected function _retrieve_session_data() |
||
732 | |||
733 | |||
734 | |||
735 | /** |
||
736 | * _generate_session_id |
||
737 | * Retrieves the PHP session id either directly from the PHP session, |
||
738 | * or from the $_REQUEST array if it was passed in from an AJAX request. |
||
739 | * The session id is then salted and hashed (mmm sounds tasty) |
||
740 | * so that it can be safely used as a $_REQUEST param |
||
741 | * |
||
742 | * @return string |
||
743 | */ |
||
744 | protected function _generate_session_id() |
||
754 | |||
755 | |||
756 | |||
757 | /** |
||
758 | * _get_sid_salt |
||
759 | * |
||
760 | * @return string |
||
761 | */ |
||
762 | protected function _get_sid_salt() |
||
780 | |||
781 | |||
782 | |||
783 | /** |
||
784 | * _set_init_access_and_expiration |
||
785 | * |
||
786 | * @return void |
||
787 | */ |
||
788 | protected function _set_init_access_and_expiration() |
||
797 | |||
798 | |||
799 | |||
800 | /** |
||
801 | * @update session data prior to saving to the db |
||
802 | * @access public |
||
803 | * @param bool $new_session |
||
804 | * @return TRUE on success, FALSE on fail |
||
805 | * @throws EE_Error |
||
806 | * @throws InvalidArgumentException |
||
807 | * @throws InvalidDataTypeException |
||
808 | * @throws InvalidInterfaceException |
||
809 | */ |
||
810 | public function update($new_session = false) |
||
880 | |||
881 | |||
882 | |||
883 | /** |
||
884 | * @create session data array |
||
885 | * @access public |
||
886 | * @return bool |
||
887 | * @throws EE_Error |
||
888 | * @throws InvalidArgumentException |
||
889 | * @throws InvalidDataTypeException |
||
890 | * @throws InvalidInterfaceException |
||
891 | */ |
||
892 | private function _create_espresso_session() |
||
898 | |||
899 | |||
900 | |||
901 | /** |
||
902 | * _save_session_to_db |
||
903 | * |
||
904 | * @param bool $clear_session |
||
905 | * @return string |
||
906 | * @throws EE_Error |
||
907 | * @throws InvalidArgumentException |
||
908 | * @throws InvalidDataTypeException |
||
909 | * @throws InvalidInterfaceException |
||
910 | */ |
||
911 | private function _save_session_to_db($clear_session = false) |
||
946 | |||
947 | |||
948 | /** |
||
949 | * @get the full page request the visitor is accessing |
||
950 | * @access public |
||
951 | * @return string |
||
952 | */ |
||
953 | public function _get_page_visit() |
||
982 | |||
983 | |||
984 | |||
985 | /** |
||
986 | * @the current wp user id |
||
987 | * @access public |
||
988 | * @return int |
||
989 | */ |
||
990 | public function _wp_user_id() |
||
996 | |||
997 | |||
998 | |||
999 | /** |
||
1000 | * Clear EE_Session data |
||
1001 | * |
||
1002 | * @access public |
||
1003 | * @param string $class |
||
1004 | * @param string $function |
||
1005 | * @return void |
||
1006 | * @throws EE_Error |
||
1007 | * @throws InvalidArgumentException |
||
1008 | * @throws InvalidDataTypeException |
||
1009 | * @throws InvalidInterfaceException |
||
1010 | */ |
||
1011 | public function clear_session($class = '', $function = '') |
||
1028 | |||
1029 | |||
1030 | /** |
||
1031 | * resets all non-default session vars. Returns TRUE on success, FALSE on fail |
||
1032 | * |
||
1033 | * @param array|mixed $data_to_reset |
||
1034 | * @param bool $show_all_notices |
||
1035 | * @return bool |
||
1036 | */ |
||
1037 | public function reset_data($data_to_reset = array(), $show_all_notices = false) |
||
1080 | |||
1081 | |||
1082 | |||
1083 | /** |
||
1084 | * wp_loaded |
||
1085 | * |
||
1086 | * @access public |
||
1087 | * @throws EE_Error |
||
1088 | * @throws InvalidDataTypeException |
||
1089 | * @throws InvalidInterfaceException |
||
1090 | * @throws InvalidArgumentException |
||
1091 | */ |
||
1092 | public function wp_loaded() |
||
1098 | |||
1099 | |||
1100 | |||
1101 | /** |
||
1102 | * Used to reset the entire object (for tests). |
||
1103 | * |
||
1104 | * @since 4.3.0 |
||
1105 | * @throws EE_Error |
||
1106 | * @throws InvalidDataTypeException |
||
1107 | * @throws InvalidInterfaceException |
||
1108 | * @throws InvalidArgumentException |
||
1109 | */ |
||
1110 | public function reset_instance() |
||
1115 | |||
1116 | |||
1117 | |||
1118 | public function configure_garbage_collection_filters() |
||
1142 | |||
1143 | |||
1144 | |||
1145 | /** |
||
1146 | * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
||
1147 | * @param $data1 |
||
1148 | * @return string |
||
1149 | */ |
||
1150 | private function find_serialize_error($data1) |
||
1207 | |||
1208 | |||
1209 | /** |
||
1210 | * Saves an array of settings used for configuring aspects of session behaviour |
||
1211 | * |
||
1212 | * @param array $updated_settings |
||
1213 | */ |
||
1214 | private function updateSessionSettings(array $updated_settings = array()) |
||
1220 | |||
1221 | |||
1222 | /** |
||
1223 | * garbage_collection |
||
1224 | */ |
||
1225 | public function garbageCollection() |
||
1269 | |||
1270 | |||
1271 | |||
1272 | } |
||
1273 | /* End of file EE_Session.class.php */ |
||
1275 |
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):