Complex classes like WC_Session_Handler 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 WC_Session_Handler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class WC_Session_Handler extends WC_Session { |
||
19 | |||
20 | /** |
||
21 | * Cookie name used for the session. |
||
22 | * |
||
23 | * @var string cookie name |
||
24 | */ |
||
25 | protected $_cookie; |
||
26 | |||
27 | /** |
||
28 | * Stores session expiry. |
||
29 | * |
||
30 | * @var string session due to expire timestamp |
||
31 | */ |
||
32 | protected $_session_expiring; |
||
33 | |||
34 | /** |
||
35 | * Stores session due to expire timestamp. |
||
36 | * |
||
37 | * @var string session expiration timestamp |
||
38 | */ |
||
39 | protected $_session_expiration; |
||
40 | |||
41 | /** |
||
42 | * True when the cookie exists. |
||
43 | * |
||
44 | * @var bool Based on whether a cookie exists. |
||
45 | */ |
||
46 | protected $_has_cookie = false; |
||
47 | |||
48 | /** |
||
49 | * Table name for session data. |
||
50 | * |
||
51 | * @var string Custom session table name |
||
52 | */ |
||
53 | protected $_table; |
||
54 | |||
55 | /** |
||
56 | * Constructor for the session class. |
||
57 | */ |
||
58 | 7 | public function __construct() { |
|
62 | |||
63 | /** |
||
64 | * Init hooks and session data. |
||
65 | * |
||
66 | * @since 3.3.0 |
||
67 | */ |
||
68 | 7 | public function init() { |
|
80 | |||
81 | /** |
||
82 | * Setup cookie and customer ID. |
||
83 | 7 | * |
|
84 | 7 | * @since 3.6.0 |
|
85 | */ |
||
86 | public function init_session_cookie() { |
||
105 | |||
106 | /** |
||
107 | * Sets the session cookie on-demand (usually after adding an item to the cart). |
||
108 | * |
||
109 | * Since the cookie name (as of 2.1) is prepended with wp, cache systems like batcache will not cache pages when set. |
||
110 | * |
||
111 | * Warning: Cookies will only be set if this is called before the headers are sent. |
||
112 | * |
||
113 | * @param bool $set Should the session cookie be set. |
||
114 | */ |
||
115 | public function set_customer_session_cookie( $set ) { |
||
127 | |||
128 | /** |
||
129 | * Return true if the current user has an active session, i.e. a cookie to retrieve values. |
||
130 | 7 | * |
|
131 | 7 | * @return bool |
|
132 | 7 | */ |
|
133 | public function has_session() { |
||
136 | |||
137 | /** |
||
138 | * Set session expiration. |
||
139 | */ |
||
140 | public function set_session_expiration() { |
||
144 | |||
145 | 7 | /** |
|
146 | * Generate a unique customer ID for guests, or return user ID if logged in. |
||
147 | * |
||
148 | * Uses Portable PHP password hashing framework to generate a unique cryptographically strong ID. |
||
149 | 7 | * |
|
150 | 7 | * @return string |
|
151 | 7 | */ |
|
152 | 7 | public function generate_customer_id() { |
|
167 | |||
168 | 7 | /** |
|
169 | 7 | * Get the session cookie, if set. Otherwise return false. |
|
170 | * |
||
171 | * Session cookies without a customer ID are invalid. |
||
172 | * |
||
173 | * @return bool|array |
||
174 | */ |
||
175 | public function get_session_cookie() { |
||
198 | |||
199 | /** |
||
200 | * Get session data. |
||
201 | * |
||
202 | * @return array |
||
203 | 7 | */ |
|
204 | 7 | public function get_session_data() { |
|
207 | |||
208 | /** |
||
209 | * Gets a cache prefix. This is used in session names so the entire cache can be invalidated with 1 function call. |
||
210 | 7 | * |
|
211 | * @return string |
||
212 | 7 | */ |
|
213 | private function get_cache_prefix() { |
||
216 | 7 | ||
217 | 7 | /** |
|
218 | * Save data. |
||
219 | 7 | */ |
|
220 | 7 | public function save_data() { |
|
239 | |||
240 | /** |
||
241 | * Destroy all session data. |
||
242 | */ |
||
243 | public function destroy_session() { |
||
247 | |||
248 | /** |
||
249 | * Forget all session data without destroying it. |
||
250 | */ |
||
251 | 7 | public function forget_session() { |
|
260 | |||
261 | /** |
||
262 | * When a user is logged out, ensure they have a unique nonce by using the customer/session ID. |
||
263 | * |
||
264 | * @param int $uid User ID. |
||
265 | * @return string |
||
266 | */ |
||
267 | public function nonce_user_logged_out( $uid ) { |
||
270 | |||
271 | /** |
||
272 | * Cleanup session data from the database and clear caches. |
||
273 | */ |
||
274 | public function cleanup_sessions() { |
||
283 | 3 | ||
284 | /** |
||
285 | 3 | * Returns the session. |
|
286 | 2 | * |
|
287 | * @param string $customer_id Custo ID. |
||
288 | 2 | * @param mixed $default Default session value. |
|
289 | 1 | * @return string|array |
|
290 | */ |
||
291 | public function get_session( $customer_id, $default = false ) { |
||
313 | |||
314 | /** |
||
315 | * Delete the session from the cache and database. |
||
316 | * |
||
317 | * @param int $customer_id Customer ID. |
||
318 | */ |
||
319 | public function delete_session( $customer_id ) { |
||
331 | 1 | ||
332 | /** |
||
333 | * Update the session expiry timestamp. |
||
334 | 1 | * |
|
335 | * @param string $customer_id Customer ID. |
||
336 | * @param int $timestamp Timestamp to expire the cookie. |
||
337 | */ |
||
338 | public function update_session_timestamp( $customer_id, $timestamp ) { |
||
354 | } |
||
355 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.