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 | /** |
||
32 | * instance of the EE_Session object |
||
33 | * |
||
34 | * @var EE_Session |
||
35 | */ |
||
36 | private static $_instance; |
||
37 | |||
38 | /** |
||
39 | * @var CacheStorageInterface $cache_storage |
||
40 | */ |
||
41 | protected $cache_storage; |
||
42 | |||
43 | /** |
||
44 | * EE_Encryption object |
||
45 | * |
||
46 | * @var EE_Encryption |
||
47 | */ |
||
48 | protected $encryption; |
||
49 | |||
50 | /** |
||
51 | * the session id |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | private $_sid; |
||
56 | |||
57 | /** |
||
58 | * session id salt |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | private $_sid_salt; |
||
63 | |||
64 | /** |
||
65 | * session data |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | private $_session_data = array(); |
||
70 | |||
71 | /** |
||
72 | * how long an EE session lasts |
||
73 | * default session lifespan of 1 hour (for not so instant IPNs) |
||
74 | * |
||
75 | * @var SessionLifespan $session_lifespan |
||
76 | */ |
||
77 | private $session_lifespan; |
||
78 | |||
79 | /** |
||
80 | * session expiration time as Unix timestamp in GMT |
||
81 | * |
||
82 | * @var int |
||
83 | */ |
||
84 | private $_expiration; |
||
85 | |||
86 | /** |
||
87 | * whether or not session has expired at some point |
||
88 | * |
||
89 | * @var boolean |
||
90 | */ |
||
91 | private $_expired = false; |
||
92 | |||
93 | /** |
||
94 | * current time as Unix timestamp in GMT |
||
95 | * |
||
96 | * @var int |
||
97 | */ |
||
98 | private $_time; |
||
99 | |||
100 | /** |
||
101 | * whether to encrypt session data |
||
102 | * |
||
103 | * @var bool |
||
104 | */ |
||
105 | private $_use_encryption; |
||
106 | |||
107 | /** |
||
108 | * well... according to the server... |
||
109 | * |
||
110 | * @var null |
||
111 | */ |
||
112 | private $_user_agent; |
||
113 | |||
114 | /** |
||
115 | * do you really trust the server ? |
||
116 | * |
||
117 | * @var null |
||
118 | */ |
||
119 | private $_ip_address; |
||
120 | |||
121 | /** |
||
122 | * current WP user_id |
||
123 | * |
||
124 | * @var null |
||
125 | */ |
||
126 | private $_wp_user_id; |
||
127 | |||
128 | /** |
||
129 | * array for defining default session vars |
||
130 | * |
||
131 | * @var array |
||
132 | */ |
||
133 | private $_default_session_vars = array( |
||
134 | 'id' => null, |
||
135 | 'user_id' => null, |
||
136 | 'ip_address' => null, |
||
137 | 'user_agent' => null, |
||
138 | 'init_access' => null, |
||
139 | 'last_access' => null, |
||
140 | 'expiration' => null, |
||
141 | 'pages_visited' => array(), |
||
142 | ); |
||
143 | |||
144 | /** |
||
145 | * timestamp for when last garbage collection cycle was performed |
||
146 | * |
||
147 | * @var int $_last_gc |
||
148 | */ |
||
149 | private $_last_gc; |
||
150 | |||
151 | /** |
||
152 | * @var RequestInterface $request |
||
153 | */ |
||
154 | protected $request; |
||
155 | |||
156 | |||
157 | /** |
||
158 | * @singleton method used to instantiate class object |
||
159 | * @param CacheStorageInterface $cache_storage |
||
160 | * @param SessionLifespan|null $lifespan |
||
161 | * @param RequestInterface $request |
||
162 | * @param EE_Encryption $encryption |
||
163 | * @return EE_Session |
||
164 | * @throws InvalidArgumentException |
||
165 | * @throws InvalidDataTypeException |
||
166 | * @throws InvalidInterfaceException |
||
167 | */ |
||
168 | public static function instance( |
||
187 | |||
188 | |||
189 | /** |
||
190 | * protected constructor to prevent direct creation |
||
191 | * |
||
192 | * @param CacheStorageInterface $cache_storage |
||
193 | * @param SessionLifespan $lifespan |
||
194 | * @param RequestInterface $request |
||
195 | * @param EE_Encryption $encryption |
||
196 | * @throws InvalidArgumentException |
||
197 | * @throws InvalidDataTypeException |
||
198 | * @throws InvalidInterfaceException |
||
199 | */ |
||
200 | protected function __construct( |
||
246 | |||
247 | |||
248 | |||
249 | /** |
||
250 | * @return void |
||
251 | * @throws EE_Error |
||
252 | * @throws InvalidArgumentException |
||
253 | * @throws InvalidDataTypeException |
||
254 | * @throws InvalidInterfaceException |
||
255 | * @throws InvalidSessionDataException |
||
256 | */ |
||
257 | public function open_session() |
||
265 | |||
266 | |||
267 | |||
268 | /** |
||
269 | * @return bool |
||
270 | */ |
||
271 | public function expired() |
||
275 | |||
276 | |||
277 | |||
278 | /** |
||
279 | * @return void |
||
280 | */ |
||
281 | public function reset_expired() |
||
285 | |||
286 | |||
287 | /** |
||
288 | * @return int |
||
289 | */ |
||
290 | public function expiration() |
||
294 | |||
295 | |||
296 | |||
297 | /** |
||
298 | * @return int |
||
299 | */ |
||
300 | public function extension() |
||
304 | |||
305 | |||
306 | |||
307 | /** |
||
308 | * @param int $time number of seconds to add to session expiration |
||
309 | */ |
||
310 | public function extend_expiration($time = 0) |
||
315 | |||
316 | |||
317 | |||
318 | /** |
||
319 | * @return int |
||
320 | */ |
||
321 | public function lifespan() |
||
325 | |||
326 | |||
327 | |||
328 | /** |
||
329 | * This just sets some defaults for the _session data property |
||
330 | * |
||
331 | * @access private |
||
332 | * @return void |
||
333 | */ |
||
334 | private function _set_defaults() |
||
345 | |||
346 | |||
347 | |||
348 | /** |
||
349 | * @retrieve session data |
||
350 | * @access public |
||
351 | * @return string |
||
352 | */ |
||
353 | public function id() |
||
357 | |||
358 | |||
359 | |||
360 | /** |
||
361 | * @param \EE_Cart $cart |
||
362 | * @return bool |
||
363 | */ |
||
364 | public function set_cart(EE_Cart $cart) |
||
369 | |||
370 | |||
371 | |||
372 | /** |
||
373 | * reset_cart |
||
374 | */ |
||
375 | public function reset_cart() |
||
380 | |||
381 | |||
382 | |||
383 | /** |
||
384 | * @return \EE_Cart |
||
385 | */ |
||
386 | public function cart() |
||
392 | |||
393 | |||
394 | |||
395 | /** |
||
396 | * @param \EE_Checkout $checkout |
||
397 | * @return bool |
||
398 | */ |
||
399 | public function set_checkout(EE_Checkout $checkout) |
||
404 | |||
405 | |||
406 | |||
407 | /** |
||
408 | * reset_checkout |
||
409 | */ |
||
410 | public function reset_checkout() |
||
415 | |||
416 | |||
417 | |||
418 | /** |
||
419 | * @return \EE_Checkout |
||
420 | */ |
||
421 | public function checkout() |
||
427 | |||
428 | |||
429 | |||
430 | /** |
||
431 | * @param \EE_Transaction $transaction |
||
432 | * @return bool |
||
433 | * @throws EE_Error |
||
434 | */ |
||
435 | public function set_transaction(EE_Transaction $transaction) |
||
442 | |||
443 | |||
444 | |||
445 | /** |
||
446 | * reset_transaction |
||
447 | */ |
||
448 | public function reset_transaction() |
||
453 | |||
454 | |||
455 | |||
456 | /** |
||
457 | * @return \EE_Transaction |
||
458 | */ |
||
459 | public function transaction() |
||
466 | |||
467 | |||
468 | |||
469 | /** |
||
470 | * retrieve session data |
||
471 | * |
||
472 | * @access public |
||
473 | * @param null $key |
||
474 | * @param bool $reset_cache |
||
475 | * @return array |
||
476 | */ |
||
477 | public function get_session_data($key = null, $reset_cache = false) |
||
489 | |||
490 | |||
491 | |||
492 | /** |
||
493 | * set session data |
||
494 | * |
||
495 | * @access public |
||
496 | * @param array $data |
||
497 | * @return TRUE on success, FALSE on fail |
||
498 | */ |
||
499 | public function set_session_data($data) |
||
500 | { |
||
501 | |||
502 | // nothing ??? bad data ??? go home! |
||
503 | View Code Duplication | if (empty($data) || ! is_array($data)) { |
|
504 | EE_Error::add_error(__('No session data or invalid session data was provided.', 'event_espresso'), __FILE__, |
||
505 | __FUNCTION__, __LINE__); |
||
506 | return false; |
||
507 | } |
||
508 | foreach ($data as $key => $value) { |
||
509 | if (isset($this->_default_session_vars[ $key ])) { |
||
510 | EE_Error::add_error(sprintf(__('Sorry! %s is a default session datum and can not be reset.', |
||
511 | 'event_espresso'), $key), __FILE__, __FUNCTION__, __LINE__); |
||
512 | return false; |
||
513 | } |
||
514 | $this->_session_data[ $key ] = $value; |
||
515 | } |
||
516 | return true; |
||
517 | } |
||
518 | |||
519 | |||
520 | |||
521 | /** |
||
522 | * @initiate session |
||
523 | * @access private |
||
524 | * @return TRUE on success, FALSE on fail |
||
525 | * @throws EE_Error |
||
526 | * @throws InvalidArgumentException |
||
527 | * @throws InvalidDataTypeException |
||
528 | * @throws InvalidInterfaceException |
||
529 | * @throws InvalidSessionDataException |
||
530 | */ |
||
531 | private function _espresso_session() |
||
579 | |||
580 | |||
581 | |||
582 | /** |
||
583 | * _get_session_data |
||
584 | * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup |
||
585 | * databases |
||
586 | * |
||
587 | * @return array |
||
588 | * @throws EE_Error |
||
589 | * @throws InvalidArgumentException |
||
590 | * @throws InvalidSessionDataException |
||
591 | * @throws InvalidDataTypeException |
||
592 | * @throws InvalidInterfaceException |
||
593 | */ |
||
594 | protected function _retrieve_session_data() |
||
687 | |||
688 | |||
689 | |||
690 | /** |
||
691 | * _generate_session_id |
||
692 | * Retrieves the PHP session id either directly from the PHP session, |
||
693 | * or from the $_REQUEST array if it was passed in from an AJAX request. |
||
694 | * The session id is then salted and hashed (mmm sounds tasty) |
||
695 | * so that it can be safely used as a $_REQUEST param |
||
696 | * |
||
697 | * @return string |
||
698 | */ |
||
699 | protected function _generate_session_id() |
||
709 | |||
710 | |||
711 | |||
712 | /** |
||
713 | * _get_sid_salt |
||
714 | * |
||
715 | * @return string |
||
716 | */ |
||
717 | protected function _get_sid_salt() |
||
735 | |||
736 | |||
737 | |||
738 | /** |
||
739 | * _set_init_access_and_expiration |
||
740 | * |
||
741 | * @return void |
||
742 | */ |
||
743 | protected function _set_init_access_and_expiration() |
||
752 | |||
753 | |||
754 | |||
755 | /** |
||
756 | * @update session data prior to saving to the db |
||
757 | * @access public |
||
758 | * @param bool $new_session |
||
759 | * @return TRUE on success, FALSE on fail |
||
760 | * @throws EE_Error |
||
761 | * @throws InvalidArgumentException |
||
762 | * @throws InvalidDataTypeException |
||
763 | * @throws InvalidInterfaceException |
||
764 | */ |
||
765 | public function update($new_session = false) |
||
835 | |||
836 | |||
837 | |||
838 | /** |
||
839 | * @create session data array |
||
840 | * @access public |
||
841 | * @return bool |
||
842 | * @throws EE_Error |
||
843 | * @throws InvalidArgumentException |
||
844 | * @throws InvalidDataTypeException |
||
845 | * @throws InvalidInterfaceException |
||
846 | */ |
||
847 | private function _create_espresso_session() |
||
853 | |||
854 | |||
855 | |||
856 | /** |
||
857 | * _save_session_to_db |
||
858 | * |
||
859 | * @param bool $clear_session |
||
860 | * @return string |
||
861 | * @throws EE_Error |
||
862 | * @throws InvalidArgumentException |
||
863 | * @throws InvalidDataTypeException |
||
864 | * @throws InvalidInterfaceException |
||
865 | */ |
||
866 | private function _save_session_to_db($clear_session = false) |
||
901 | |||
902 | |||
903 | /** |
||
904 | * @get the full page request the visitor is accessing |
||
905 | * @access public |
||
906 | * @return string |
||
907 | */ |
||
908 | public function _get_page_visit() |
||
937 | |||
938 | |||
939 | |||
940 | /** |
||
941 | * @the current wp user id |
||
942 | * @access public |
||
943 | * @return int |
||
944 | */ |
||
945 | public function _wp_user_id() |
||
951 | |||
952 | |||
953 | |||
954 | /** |
||
955 | * Clear EE_Session data |
||
956 | * |
||
957 | * @access public |
||
958 | * @param string $class |
||
959 | * @param string $function |
||
960 | * @return void |
||
961 | * @throws EE_Error |
||
962 | * @throws InvalidArgumentException |
||
963 | * @throws InvalidDataTypeException |
||
964 | * @throws InvalidInterfaceException |
||
965 | */ |
||
966 | public function clear_session($class = '', $function = '') |
||
978 | |||
979 | |||
980 | |||
981 | /** |
||
982 | * @resets all non-default session vars |
||
983 | * @access public |
||
984 | * @param array|mixed $data_to_reset |
||
985 | * @param bool $show_all_notices |
||
986 | * @return TRUE on success, FALSE on fail |
||
987 | */ |
||
988 | public function reset_data($data_to_reset = array(), $show_all_notices = false) |
||
1031 | |||
1032 | |||
1033 | |||
1034 | /** |
||
1035 | * wp_loaded |
||
1036 | * |
||
1037 | * @access public |
||
1038 | * @throws EE_Error |
||
1039 | * @throws InvalidDataTypeException |
||
1040 | * @throws InvalidInterfaceException |
||
1041 | * @throws InvalidArgumentException |
||
1042 | */ |
||
1043 | public function wp_loaded() |
||
1049 | |||
1050 | |||
1051 | |||
1052 | /** |
||
1053 | * Used to reset the entire object (for tests). |
||
1054 | * |
||
1055 | * @since 4.3.0 |
||
1056 | * @throws EE_Error |
||
1057 | * @throws InvalidDataTypeException |
||
1058 | * @throws InvalidInterfaceException |
||
1059 | * @throws InvalidArgumentException |
||
1060 | */ |
||
1061 | public function reset_instance() |
||
1066 | |||
1067 | |||
1068 | |||
1069 | public function configure_garbage_collection_filters() |
||
1093 | |||
1094 | |||
1095 | |||
1096 | /** |
||
1097 | * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
||
1098 | * @param $data1 |
||
1099 | * @return string |
||
1100 | */ |
||
1101 | private function find_serialize_error($data1) |
||
1158 | |||
1159 | |||
1160 | /** |
||
1161 | * Saves an array of settings used for configuring aspects of session behaviour |
||
1162 | * |
||
1163 | * @param array $updated_settings |
||
1164 | */ |
||
1165 | private function updateSessionSettings(array $updated_settings = array()) |
||
1171 | |||
1172 | |||
1173 | /** |
||
1174 | * garbage_collection |
||
1175 | */ |
||
1176 | public function garbageCollection() |
||
1220 | |||
1221 | |||
1222 | |||
1223 | } |
||
1224 | /* End of file EE_Session.class.php */ |
||
1226 |
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):