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 use EventEspresso\core\exceptions\InvalidSessionDataException; |
||
12 | class EE_Session { |
||
13 | |||
14 | const session_id_prefix = 'ee_ssn_'; |
||
15 | const hash_check_prefix = 'ee_shc_'; |
||
16 | |||
17 | /** |
||
18 | * instance of the EE_Session object |
||
19 | * @var EE_Session |
||
20 | */ |
||
21 | private static $_instance; |
||
22 | |||
23 | /** |
||
24 | * the session id |
||
25 | * @var string |
||
26 | */ |
||
27 | private $_sid; |
||
28 | |||
29 | /** |
||
30 | * session id salt |
||
31 | * @var string |
||
32 | */ |
||
33 | private $_sid_salt; |
||
34 | |||
35 | /** |
||
36 | * session data |
||
37 | * @var array |
||
38 | */ |
||
39 | private $_session_data = array(); |
||
40 | |||
41 | /** |
||
42 | * how long an EE session lasts |
||
43 | * default session lifespan of 2 hours (for not so instant IPNs) |
||
44 | * @var int |
||
45 | */ |
||
46 | private $_lifespan; |
||
47 | |||
48 | /** |
||
49 | * session expiration time as Unix timestamp in GMT |
||
50 | * @var int |
||
51 | */ |
||
52 | private $_expiration; |
||
53 | |||
54 | /** |
||
55 | * current time as Unix timestamp in GMT |
||
56 | * @var int |
||
57 | */ |
||
58 | private $_time; |
||
59 | |||
60 | /** |
||
61 | * whether to encrypt session data |
||
62 | * @var bool |
||
63 | */ |
||
64 | private $_use_encryption = false; |
||
65 | |||
66 | /** |
||
67 | * EE_Encryption object |
||
68 | * @var EE_Encryption |
||
69 | */ |
||
70 | protected $encryption; |
||
71 | |||
72 | /** |
||
73 | * well... according to the server... |
||
74 | * @var null |
||
75 | */ |
||
76 | private $_user_agent; |
||
77 | |||
78 | /** |
||
79 | * do you really trust the server ? |
||
80 | * @var null |
||
81 | */ |
||
82 | private $_ip_address; |
||
83 | |||
84 | /** |
||
85 | * current WP user_id |
||
86 | * @var null |
||
87 | */ |
||
88 | private $_wp_user_id; |
||
89 | |||
90 | /** |
||
91 | * array for defining default session vars |
||
92 | * @var array |
||
93 | */ |
||
94 | private $_default_session_vars = array ( |
||
95 | 'id' => NULL, |
||
96 | 'user_id' => NULL, |
||
97 | 'ip_address' => NULL, |
||
98 | 'user_agent' => NULL, |
||
99 | 'init_access' => NULL, |
||
100 | 'last_access' => NULL, |
||
101 | 'expiration' => NULL, |
||
102 | 'pages_visited' => array() |
||
103 | ); |
||
104 | |||
105 | |||
106 | |||
107 | /** |
||
108 | * @singleton method used to instantiate class object |
||
109 | * @param \EE_Encryption $encryption |
||
110 | * @return EE_Session |
||
111 | * @throws InvalidSessionDataException |
||
112 | * @throws \EE_Error |
||
113 | */ |
||
114 | public static function instance( EE_Encryption $encryption = null ) { |
||
115 | // check if class object is instantiated |
||
116 | // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: |
||
117 | // add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
||
118 | if ( ! self::$_instance instanceof EE_Session && apply_filters( 'FHEE_load_EE_Session', true ) ) { |
||
119 | self::$_instance = new self( $encryption ); |
||
120 | } |
||
121 | return self::$_instance; |
||
122 | } |
||
123 | |||
124 | |||
125 | |||
126 | /** |
||
127 | * protected constructor to prevent direct creation |
||
128 | * |
||
129 | * @Constructor |
||
130 | * @access protected |
||
131 | * @param \EE_Encryption $encryption |
||
132 | * @throws \EE_Error |
||
133 | * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
||
134 | */ |
||
135 | protected function __construct( EE_Encryption $encryption = null ) { |
||
136 | |||
137 | // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
||
138 | if ( ! apply_filters( 'FHEE_load_EE_Session', TRUE ) ) { |
||
139 | return; |
||
140 | } |
||
141 | do_action( 'AHEE_log', __FILE__, __FUNCTION__, '' ); |
||
142 | if ( ! defined( 'ESPRESSO_SESSION' ) ) { |
||
143 | define( 'ESPRESSO_SESSION', true ); |
||
144 | } |
||
145 | // default session lifespan in seconds |
||
146 | $this->_lifespan = apply_filters( |
||
147 | 'FHEE__EE_Session__construct___lifespan', |
||
148 | 60 * MINUTE_IN_SECONDS |
||
149 | ) + 1; |
||
150 | /* |
||
151 | * do something like the following to adjust the session lifespan: |
||
152 | * public static function session_lifespan() { |
||
153 | * return 15 * MINUTE_IN_SECONDS; |
||
154 | * } |
||
155 | */ |
||
156 | // retrieve session options from db |
||
157 | $session_settings = get_option( 'ee_session_settings' ); |
||
158 | if ( $session_settings !== FALSE ) { |
||
159 | // cycle though existing session options |
||
160 | foreach ( $session_settings as $var_name => $session_setting ) { |
||
161 | // set values for class properties |
||
162 | $var_name = '_' . $var_name; |
||
163 | $this->{$var_name} = $session_setting; |
||
164 | } |
||
165 | } |
||
166 | // are we using encryption? |
||
167 | if ( $this->_use_encryption && $encryption instanceof EE_Encryption ) { |
||
168 | // encrypt data via: $this->encryption->encrypt(); |
||
169 | $this->encryption = $encryption; |
||
170 | } |
||
171 | // filter hook allows outside functions/classes/plugins to change default empty cart |
||
172 | $extra_default_session_vars = apply_filters( 'FHEE__EE_Session__construct__extra_default_session_vars', array() ); |
||
173 | array_merge( $this->_default_session_vars, $extra_default_session_vars ); |
||
174 | // apply default session vars |
||
175 | $this->_set_defaults(); |
||
176 | // check for existing session and retrieve it from db |
||
177 | if ( ! $this->_espresso_session() ) { |
||
178 | // or just start a new one |
||
179 | $this->_create_espresso_session(); |
||
180 | } |
||
181 | // check request for 'clear_session' param |
||
182 | add_action( 'AHEE__EE_Request_Handler__construct__complete', array( $this, 'wp_loaded' )); |
||
183 | // once everything is all said and done, |
||
184 | add_action( 'shutdown', array( $this, 'update' ), 100 ); |
||
185 | add_action( 'shutdown', array( $this, 'garbage_collection' ), 999 ); |
||
186 | add_filter( 'wp_redirect', array( $this, 'update_on_redirect' ), 100, 1 ); |
||
187 | } |
||
188 | |||
189 | |||
190 | |||
191 | /** |
||
192 | * @return int |
||
193 | */ |
||
194 | public function expiration() { |
||
197 | |||
198 | |||
199 | |||
200 | /** |
||
201 | * @return int |
||
202 | */ |
||
203 | public function lifespan() { |
||
206 | |||
207 | |||
208 | |||
209 | /** |
||
210 | * This just sets some defaults for the _session data property |
||
211 | * |
||
212 | * @access private |
||
213 | * @return void |
||
214 | */ |
||
215 | private function _set_defaults() { |
||
225 | |||
226 | |||
227 | |||
228 | /** |
||
229 | * @retrieve session data |
||
230 | * @access public |
||
231 | * @return string |
||
232 | */ |
||
233 | public function id() { |
||
236 | |||
237 | |||
238 | |||
239 | /** |
||
240 | * @param \EE_Cart $cart |
||
241 | * @return bool |
||
242 | */ |
||
243 | public function set_cart( EE_Cart $cart ) { |
||
247 | |||
248 | |||
249 | |||
250 | /** |
||
251 | * reset_cart |
||
252 | */ |
||
253 | public function reset_cart() { |
||
256 | |||
257 | |||
258 | |||
259 | /** |
||
260 | * @return \EE_Cart |
||
261 | */ |
||
262 | public function cart() { |
||
265 | |||
266 | |||
267 | |||
268 | /** |
||
269 | * @param \EE_Checkout $checkout |
||
270 | * @return bool |
||
271 | */ |
||
272 | public function set_checkout( EE_Checkout $checkout ) { |
||
276 | |||
277 | |||
278 | |||
279 | /** |
||
280 | * reset_checkout |
||
281 | */ |
||
282 | public function reset_checkout() { |
||
285 | |||
286 | |||
287 | |||
288 | /** |
||
289 | * @return \EE_Checkout |
||
290 | */ |
||
291 | public function checkout() { |
||
294 | |||
295 | |||
296 | |||
297 | /** |
||
298 | * @param \EE_Transaction $transaction |
||
299 | * @return bool |
||
300 | * @throws \EE_Error |
||
301 | */ |
||
302 | public function set_transaction( EE_Transaction $transaction ) { |
||
308 | |||
309 | |||
310 | |||
311 | /** |
||
312 | * reset_transaction |
||
313 | */ |
||
314 | public function reset_transaction() { |
||
317 | |||
318 | |||
319 | |||
320 | /** |
||
321 | * @return \EE_Transaction |
||
322 | */ |
||
323 | public function transaction() { |
||
326 | |||
327 | |||
328 | |||
329 | /** |
||
330 | * retrieve session data |
||
331 | * @access public |
||
332 | * @param null $key |
||
333 | * @param bool $reset_cache |
||
334 | * @return array |
||
335 | */ |
||
336 | public function get_session_data( $key = NULL, $reset_cache = FALSE ) { |
||
348 | |||
349 | |||
350 | |||
351 | /** |
||
352 | * set session data |
||
353 | * @access public |
||
354 | * @param array $data |
||
355 | * @return TRUE on success, FALSE on fail |
||
356 | */ |
||
357 | public function set_session_data( $data ) { |
||
377 | |||
378 | |||
379 | |||
380 | /** |
||
381 | * @initiate session |
||
382 | * @access private |
||
383 | * @return TRUE on success, FALSE on fail |
||
384 | * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
||
385 | * @throws \EE_Error |
||
386 | */ |
||
387 | private function _espresso_session() { |
||
474 | |||
475 | |||
476 | |||
477 | /** |
||
478 | * _generate_session_id |
||
479 | * Retrieves the PHP session id either directly from the PHP session, |
||
480 | * or from the $_REQUEST array if it was passed in from an AJAX request. |
||
481 | * The session id is then salted and hashed (mmm sounds tasty) |
||
482 | * so that it can be safely used as a $_REQUEST param |
||
483 | * |
||
484 | * @return string |
||
485 | */ |
||
486 | protected function _generate_session_id() { |
||
495 | |||
496 | |||
497 | |||
498 | /** |
||
499 | * _get_sid_salt |
||
500 | * |
||
501 | * @return string |
||
502 | */ |
||
503 | protected function _get_sid_salt() { |
||
522 | |||
523 | |||
524 | |||
525 | /** |
||
526 | * _set_init_access_and_expiration |
||
527 | * @return void |
||
528 | */ |
||
529 | protected function _set_init_access_and_expiration() { |
||
537 | |||
538 | |||
539 | |||
540 | /** |
||
541 | * @update session data prior to saving to the db |
||
542 | * @access public |
||
543 | * @param bool $new_session |
||
544 | * @return TRUE on success, FALSE on fail |
||
545 | */ |
||
546 | public function update( $new_session = FALSE ) { |
||
628 | |||
629 | |||
630 | |||
631 | /** |
||
632 | * since WordPress has no do_action()s within wp_safe_redirect, |
||
633 | * we have to hack into one of the supplied filters |
||
634 | * in order to make sure the session is updated prior to redirecting. |
||
635 | * This is a callback for the 'wp_redirect' filter |
||
636 | * |
||
637 | * @param string $location |
||
638 | * @return mixed |
||
639 | */ |
||
640 | public function update_on_redirect( $location ) { |
||
644 | |||
645 | |||
646 | /** |
||
647 | * @create session data array |
||
648 | * @access public |
||
649 | * @return bool |
||
650 | */ |
||
651 | private function _create_espresso_session( ) { |
||
656 | |||
657 | |||
658 | |||
659 | |||
660 | |||
661 | /** |
||
662 | * _save_session_to_db |
||
663 | * |
||
664 | * @access public |
||
665 | * @return string |
||
666 | */ |
||
667 | private function _save_session_to_db() { |
||
702 | |||
703 | |||
704 | |||
705 | |||
706 | |||
707 | /** |
||
708 | * _visitor_ip |
||
709 | * attempt to get IP address of current visitor from server |
||
710 | * plz see: http://stackoverflow.com/a/2031935/1475279 |
||
711 | * |
||
712 | * @access public |
||
713 | * @return string |
||
714 | */ |
||
715 | View Code Duplication | private function _visitor_ip() { |
|
737 | |||
738 | |||
739 | |||
740 | |||
741 | |||
742 | /** |
||
743 | * @get the full page request the visitor is accessing |
||
744 | * @access public |
||
745 | * @return string |
||
746 | */ |
||
747 | public function _get_page_visit() { |
||
776 | |||
777 | |||
778 | |||
779 | |||
780 | |||
781 | /** |
||
782 | * @the current wp user id |
||
783 | * @access public |
||
784 | * @return int |
||
785 | */ |
||
786 | public function _wp_user_id() { |
||
791 | |||
792 | |||
793 | |||
794 | /** |
||
795 | * Clear EE_Session data |
||
796 | * |
||
797 | * @access public |
||
798 | * @param string $class |
||
799 | * @param string $function |
||
800 | * @return void |
||
801 | */ |
||
802 | public function clear_session( $class = '', $function = '' ) { |
||
814 | |||
815 | |||
816 | |||
817 | /** |
||
818 | * @resets all non-default session vars |
||
819 | * @access public |
||
820 | * @param array $data_to_reset |
||
821 | * @param bool $show_all_notices |
||
822 | * @return TRUE on success, FALSE on fail |
||
823 | */ |
||
824 | public function reset_data( $data_to_reset = array(), $show_all_notices = FALSE ) { |
||
868 | |||
869 | |||
870 | |||
871 | |||
872 | |||
873 | |||
874 | /** |
||
875 | * wp_loaded |
||
876 | * @access public |
||
877 | */ |
||
878 | public function wp_loaded() { |
||
883 | |||
884 | |||
885 | |||
886 | /** |
||
887 | * Used to reset the entire object (for tests). |
||
888 | * |
||
889 | * @since 4.3.0 |
||
890 | * |
||
891 | */ |
||
892 | public function reset_instance() { |
||
896 | |||
897 | |||
898 | |||
899 | /** |
||
900 | * garbage_collection |
||
901 | * @since 4.3.0 |
||
902 | */ |
||
903 | public function garbage_collection() { |
||
963 | |||
964 | |||
965 | |||
966 | /** |
||
967 | * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
||
968 | * @param $string |
||
969 | * @return bool |
||
970 | */ |
||
971 | private function valid_base_64( $string ) { |
||
984 | |||
985 | |||
986 | |||
987 | /** |
||
988 | * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
||
989 | * @param $data1 |
||
990 | * @return string |
||
991 | */ |
||
992 | private function find_serialize_error( $data1 ) { |
||
1047 | |||
1048 | } |
||
1049 | /* End of file EE_Session.class.php */ |
||
1051 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: