Complex classes like WPInv_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 WPInv_Session_Handler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class WPInv_Session_Handler extends WPInv_Session { |
||
14 | |||
15 | /** |
||
16 | * Cookie name used for the session. |
||
17 | * |
||
18 | * @var string cookie name |
||
19 | */ |
||
20 | protected $_cookie; |
||
21 | |||
22 | /** |
||
23 | * Stores session expiry. |
||
24 | * |
||
25 | * @var int session due to expire timestamp |
||
26 | */ |
||
27 | protected $_session_expiring; |
||
28 | |||
29 | /** |
||
30 | * Stores session due to expire timestamp. |
||
31 | * |
||
32 | * @var string session expiration timestamp |
||
33 | */ |
||
34 | protected $_session_expiration; |
||
35 | |||
36 | /** |
||
37 | * True when the cookie exists. |
||
38 | * |
||
39 | * @var bool Based on whether a cookie exists. |
||
40 | */ |
||
41 | protected $_has_cookie = false; |
||
42 | |||
43 | /** |
||
44 | * Table name for session data. |
||
45 | * |
||
46 | * @var string Custom session table name |
||
47 | */ |
||
48 | protected $_table; |
||
49 | |||
50 | /** |
||
51 | * Constructor for the session class. |
||
52 | */ |
||
53 | public function __construct() { |
||
61 | |||
62 | /** |
||
63 | * Init hooks and session data. |
||
64 | * |
||
65 | * @since 3.3.0 |
||
66 | */ |
||
67 | public function init() { |
||
74 | |||
75 | /** |
||
76 | * Setup cookie and customer ID. |
||
77 | * |
||
78 | * @since 3.6.0 |
||
79 | */ |
||
80 | public function init_session_cookie() { |
||
109 | |||
110 | /** |
||
111 | * Sets the session cookie on-demand (usually after adding an item to the cart). |
||
112 | * |
||
113 | * Since the cookie name (as of 2.1) is prepended with wp, cache systems like batcache will not cache pages when set. |
||
114 | * |
||
115 | * Warning: Cookies will only be set if this is called before the headers are sent. |
||
116 | * |
||
117 | * @param bool $set Should the session cookie be set. |
||
118 | */ |
||
119 | public function set_customer_session_cookie( $set ) { |
||
131 | |||
132 | public function setcookie($name, $value, $expire = 0, $secure = false, $httponly = false){ |
||
140 | |||
141 | /** |
||
142 | * Should the session cookie be secure? |
||
143 | * |
||
144 | * @since 3.6.0 |
||
145 | * @return bool |
||
146 | */ |
||
147 | protected function use_secure_cookie() { |
||
151 | |||
152 | /** |
||
153 | * Return true if the current user has an active session, i.e. a cookie to retrieve values. |
||
154 | * |
||
155 | * @return bool |
||
156 | */ |
||
157 | public function has_session() { |
||
160 | |||
161 | /** |
||
162 | * Set session expiration. |
||
163 | */ |
||
164 | public function set_session_expiration() { |
||
168 | |||
169 | /** |
||
170 | * Generate a unique customer ID for guests, or return user ID if logged in. |
||
171 | * |
||
172 | * Uses Portable PHP password hashing framework to generate a unique cryptographically strong ID. |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | public function generate_customer_id() { |
||
189 | |||
190 | /** |
||
191 | * Get the session cookie, if set. Otherwise return false. |
||
192 | * |
||
193 | * Session cookies without a customer ID are invalid. |
||
194 | * |
||
195 | * @return bool|array |
||
196 | */ |
||
197 | public function get_session_cookie() { |
||
220 | |||
221 | /** |
||
222 | * Get session data. |
||
223 | * |
||
224 | * @return array |
||
225 | */ |
||
226 | public function get_session_data() { |
||
229 | |||
230 | public function generate_key($customer_id){ |
||
237 | |||
238 | /** |
||
239 | * Save data. |
||
240 | */ |
||
241 | public function save_data() { |
||
250 | |||
251 | /** |
||
252 | * Destroy all session data. |
||
253 | */ |
||
254 | public function destroy_session() { |
||
258 | |||
259 | /** |
||
260 | * Forget all session data without destroying it. |
||
261 | */ |
||
262 | public function forget_session() { |
||
271 | |||
272 | /** |
||
273 | * When a user is logged out, ensure they have a unique nonce by using the customer/session ID. |
||
274 | * |
||
275 | * @param int $uid User ID. |
||
276 | * @return string |
||
277 | */ |
||
278 | public function nonce_user_logged_out( $uid ) { |
||
281 | |||
282 | /** |
||
283 | * Returns the session. |
||
284 | * |
||
285 | * @param string $customer_id Customer ID. |
||
286 | * @param mixed $default Default session value. |
||
287 | * @return string|array |
||
288 | */ |
||
289 | public function get_session( $customer_id, $default = false ) { |
||
310 | |||
311 | /** |
||
312 | * Delete the session from the cache and database. |
||
313 | * |
||
314 | * @param int $customer_id Customer ID. |
||
315 | */ |
||
316 | public function delete_session( $customer_id ) { |
||
322 | |||
323 | /** |
||
324 | * Update the session expiry timestamp. |
||
325 | * |
||
326 | * @param string $customer_id Customer ID. |
||
327 | * @param int $timestamp Timestamp to expire the cookie. |
||
328 | */ |
||
329 | public function update_session_timestamp( $customer_id, $timestamp ) { |
||
334 | } |
||
335 | |||
337 | $wpi_session = new WPInv_Session_Handler(); |
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.