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( |
||
260 | |||
261 | |||
262 | /** |
||
263 | * @return bool |
||
264 | * @throws InvalidArgumentException |
||
265 | * @throws InvalidDataTypeException |
||
266 | * @throws InvalidInterfaceException |
||
267 | */ |
||
268 | public static function isLoadedAndActive() |
||
274 | |||
275 | |||
276 | /** |
||
277 | * @return bool |
||
278 | */ |
||
279 | public function isActive() |
||
283 | |||
284 | |||
285 | |||
286 | /** |
||
287 | * @return void |
||
288 | * @throws EE_Error |
||
289 | * @throws InvalidArgumentException |
||
290 | * @throws InvalidDataTypeException |
||
291 | * @throws InvalidInterfaceException |
||
292 | * @throws InvalidSessionDataException |
||
293 | */ |
||
294 | public function open_session() |
||
302 | |||
303 | |||
304 | |||
305 | /** |
||
306 | * @return bool |
||
307 | */ |
||
308 | public function expired() |
||
312 | |||
313 | |||
314 | |||
315 | /** |
||
316 | * @return void |
||
317 | */ |
||
318 | public function reset_expired() |
||
322 | |||
323 | |||
324 | /** |
||
325 | * @return int |
||
326 | */ |
||
327 | public function expiration() |
||
331 | |||
332 | |||
333 | |||
334 | /** |
||
335 | * @return int |
||
336 | */ |
||
337 | public function extension() |
||
341 | |||
342 | |||
343 | |||
344 | /** |
||
345 | * @param int $time number of seconds to add to session expiration |
||
346 | */ |
||
347 | public function extend_expiration($time = 0) |
||
352 | |||
353 | |||
354 | |||
355 | /** |
||
356 | * @return int |
||
357 | */ |
||
358 | public function lifespan() |
||
362 | |||
363 | |||
364 | |||
365 | /** |
||
366 | * This just sets some defaults for the _session data property |
||
367 | * |
||
368 | * @access private |
||
369 | * @return void |
||
370 | */ |
||
371 | private function _set_defaults() |
||
382 | |||
383 | |||
384 | |||
385 | /** |
||
386 | * @retrieve session data |
||
387 | * @access public |
||
388 | * @return string |
||
389 | */ |
||
390 | public function id() |
||
394 | |||
395 | |||
396 | |||
397 | /** |
||
398 | * @param \EE_Cart $cart |
||
399 | * @return bool |
||
400 | */ |
||
401 | public function set_cart(EE_Cart $cart) |
||
406 | |||
407 | |||
408 | |||
409 | /** |
||
410 | * reset_cart |
||
411 | */ |
||
412 | public function reset_cart() |
||
417 | |||
418 | |||
419 | |||
420 | /** |
||
421 | * @return \EE_Cart |
||
422 | */ |
||
423 | public function cart() |
||
429 | |||
430 | |||
431 | |||
432 | /** |
||
433 | * @param \EE_Checkout $checkout |
||
434 | * @return bool |
||
435 | */ |
||
436 | public function set_checkout(EE_Checkout $checkout) |
||
441 | |||
442 | |||
443 | |||
444 | /** |
||
445 | * reset_checkout |
||
446 | */ |
||
447 | public function reset_checkout() |
||
452 | |||
453 | |||
454 | |||
455 | /** |
||
456 | * @return \EE_Checkout |
||
457 | */ |
||
458 | public function checkout() |
||
464 | |||
465 | |||
466 | |||
467 | /** |
||
468 | * @param \EE_Transaction $transaction |
||
469 | * @return bool |
||
470 | * @throws EE_Error |
||
471 | */ |
||
472 | public function set_transaction(EE_Transaction $transaction) |
||
479 | |||
480 | |||
481 | |||
482 | /** |
||
483 | * reset_transaction |
||
484 | */ |
||
485 | public function reset_transaction() |
||
490 | |||
491 | |||
492 | |||
493 | /** |
||
494 | * @return \EE_Transaction |
||
495 | */ |
||
496 | public function transaction() |
||
503 | |||
504 | |||
505 | /** |
||
506 | * retrieve session data |
||
507 | * |
||
508 | * @param null $key |
||
509 | * @param bool $reset_cache |
||
510 | * @return array |
||
511 | */ |
||
512 | public function get_session_data($key = null, $reset_cache = false) |
||
524 | |||
525 | |||
526 | /** |
||
527 | * Returns TRUE on success, FALSE on fail |
||
528 | * |
||
529 | * @param array $data |
||
530 | * @return bool |
||
531 | */ |
||
532 | public function set_session_data($data) |
||
533 | { |
||
534 | |||
535 | // nothing ??? bad data ??? go home! |
||
536 | View Code Duplication | if (empty($data) || ! is_array($data)) { |
|
537 | EE_Error::add_error(__('No session data or invalid session data was provided.', 'event_espresso'), __FILE__, |
||
538 | __FUNCTION__, __LINE__); |
||
539 | return false; |
||
540 | } |
||
541 | foreach ($data as $key => $value) { |
||
542 | View Code Duplication | if (isset($this->_default_session_vars[ $key ])) { |
|
543 | EE_Error::add_error(sprintf(__('Sorry! %s is a default session datum and can not be reset.', |
||
544 | 'event_espresso'), $key), __FILE__, __FUNCTION__, __LINE__); |
||
545 | return false; |
||
546 | } |
||
547 | $this->_session_data[ $key ] = $value; |
||
548 | } |
||
549 | return true; |
||
550 | } |
||
551 | |||
552 | |||
553 | |||
554 | /** |
||
555 | * @initiate session |
||
556 | * @access private |
||
557 | * @return TRUE on success, FALSE on fail |
||
558 | * @throws EE_Error |
||
559 | * @throws InvalidArgumentException |
||
560 | * @throws InvalidDataTypeException |
||
561 | * @throws InvalidInterfaceException |
||
562 | * @throws InvalidSessionDataException |
||
563 | */ |
||
564 | private function _espresso_session() |
||
613 | |||
614 | |||
615 | |||
616 | /** |
||
617 | * _get_session_data |
||
618 | * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup |
||
619 | * databases |
||
620 | * |
||
621 | * @return array |
||
622 | * @throws EE_Error |
||
623 | * @throws InvalidArgumentException |
||
624 | * @throws InvalidSessionDataException |
||
625 | * @throws InvalidDataTypeException |
||
626 | * @throws InvalidInterfaceException |
||
627 | */ |
||
628 | protected function _retrieve_session_data() |
||
721 | |||
722 | |||
723 | |||
724 | /** |
||
725 | * _generate_session_id |
||
726 | * Retrieves the PHP session id either directly from the PHP session, |
||
727 | * or from the $_REQUEST array if it was passed in from an AJAX request. |
||
728 | * The session id is then salted and hashed (mmm sounds tasty) |
||
729 | * so that it can be safely used as a $_REQUEST param |
||
730 | * |
||
731 | * @return string |
||
732 | */ |
||
733 | protected function _generate_session_id() |
||
743 | |||
744 | |||
745 | |||
746 | /** |
||
747 | * _get_sid_salt |
||
748 | * |
||
749 | * @return string |
||
750 | */ |
||
751 | protected function _get_sid_salt() |
||
769 | |||
770 | |||
771 | |||
772 | /** |
||
773 | * _set_init_access_and_expiration |
||
774 | * |
||
775 | * @return void |
||
776 | */ |
||
777 | protected function _set_init_access_and_expiration() |
||
786 | |||
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) |
||
869 | |||
870 | |||
871 | |||
872 | /** |
||
873 | * @create session data array |
||
874 | * @access public |
||
875 | * @return bool |
||
876 | * @throws EE_Error |
||
877 | * @throws InvalidArgumentException |
||
878 | * @throws InvalidDataTypeException |
||
879 | * @throws InvalidInterfaceException |
||
880 | */ |
||
881 | private function _create_espresso_session() |
||
887 | |||
888 | |||
889 | |||
890 | /** |
||
891 | * _save_session_to_db |
||
892 | * |
||
893 | * @param bool $clear_session |
||
894 | * @return string |
||
895 | * @throws EE_Error |
||
896 | * @throws InvalidArgumentException |
||
897 | * @throws InvalidDataTypeException |
||
898 | * @throws InvalidInterfaceException |
||
899 | */ |
||
900 | private function _save_session_to_db($clear_session = false) |
||
935 | |||
936 | |||
937 | /** |
||
938 | * @get the full page request the visitor is accessing |
||
939 | * @access public |
||
940 | * @return string |
||
941 | */ |
||
942 | public function _get_page_visit() |
||
971 | |||
972 | |||
973 | |||
974 | /** |
||
975 | * @the current wp user id |
||
976 | * @access public |
||
977 | * @return int |
||
978 | */ |
||
979 | public function _wp_user_id() |
||
985 | |||
986 | |||
987 | |||
988 | /** |
||
989 | * Clear EE_Session data |
||
990 | * |
||
991 | * @access public |
||
992 | * @param string $class |
||
993 | * @param string $function |
||
994 | * @return void |
||
995 | * @throws EE_Error |
||
996 | * @throws InvalidArgumentException |
||
997 | * @throws InvalidDataTypeException |
||
998 | * @throws InvalidInterfaceException |
||
999 | */ |
||
1000 | public function clear_session($class = '', $function = '') |
||
1012 | |||
1013 | |||
1014 | /** |
||
1015 | * resets all non-default session vars. Returns TRUE on success, FALSE on fail |
||
1016 | * |
||
1017 | * @param array|mixed $data_to_reset |
||
1018 | * @param bool $show_all_notices |
||
1019 | * @return bool |
||
1020 | */ |
||
1021 | public function reset_data($data_to_reset = array(), $show_all_notices = false) |
||
1064 | |||
1065 | |||
1066 | |||
1067 | /** |
||
1068 | * wp_loaded |
||
1069 | * |
||
1070 | * @access public |
||
1071 | * @throws EE_Error |
||
1072 | * @throws InvalidDataTypeException |
||
1073 | * @throws InvalidInterfaceException |
||
1074 | * @throws InvalidArgumentException |
||
1075 | */ |
||
1076 | public function wp_loaded() |
||
1082 | |||
1083 | |||
1084 | |||
1085 | /** |
||
1086 | * Used to reset the entire object (for tests). |
||
1087 | * |
||
1088 | * @since 4.3.0 |
||
1089 | * @throws EE_Error |
||
1090 | * @throws InvalidDataTypeException |
||
1091 | * @throws InvalidInterfaceException |
||
1092 | * @throws InvalidArgumentException |
||
1093 | */ |
||
1094 | public function reset_instance() |
||
1099 | |||
1100 | |||
1101 | |||
1102 | public function configure_garbage_collection_filters() |
||
1126 | |||
1127 | |||
1128 | |||
1129 | /** |
||
1130 | * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
||
1131 | * @param $data1 |
||
1132 | * @return string |
||
1133 | */ |
||
1134 | private function find_serialize_error($data1) |
||
1191 | |||
1192 | |||
1193 | /** |
||
1194 | * Saves an array of settings used for configuring aspects of session behaviour |
||
1195 | * |
||
1196 | * @param array $updated_settings |
||
1197 | */ |
||
1198 | private function updateSessionSettings(array $updated_settings = array()) |
||
1204 | |||
1205 | |||
1206 | /** |
||
1207 | * garbage_collection |
||
1208 | */ |
||
1209 | public function garbageCollection() |
||
1253 | |||
1254 | |||
1255 | |||
1256 | } |
||
1257 | /* End of file EE_Session.class.php */ |
||
1259 |
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):