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 string 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() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Init hooks and session data. |
||
| 61 | * |
||
| 62 | * @since 3.3.0 |
||
| 63 | */ |
||
| 64 | public function init() { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Setup cookie and customer ID. |
||
| 78 | * |
||
| 79 | * @since 3.6.0 |
||
| 80 | */ |
||
| 81 | public function init_session_cookie() { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Sets the session cookie on-demand (usually after adding an item to the cart). |
||
| 113 | * |
||
| 114 | * Since the cookie name (as of 2.1) is prepended with wp, cache systems like batcache will not cache pages when set. |
||
| 115 | * |
||
| 116 | * Warning: Cookies will only be set if this is called before the headers are sent. |
||
| 117 | * |
||
| 118 | * @param bool $set Should the session cookie be set. |
||
| 119 | */ |
||
| 120 | public function set_customer_session_cookie( $set ) { |
||
| 132 | |||
| 133 | public function setcookie($name, $value, $expire = 0, $secure = false, $httponly = false){ |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Should the session cookie be secure? |
||
| 144 | * |
||
| 145 | * @since 3.6.0 |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | protected function use_secure_cookie() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Return true if the current user has an active session, i.e. a cookie to retrieve values. |
||
| 155 | * |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | public function has_session() { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Set session expiration. |
||
| 164 | */ |
||
| 165 | public function set_session_expiration() { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Generate a unique customer ID for guests, or return user ID if logged in. |
||
| 172 | * |
||
| 173 | * Uses Portable PHP password hashing framework to generate a unique cryptographically strong ID. |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function generate_customer_id() { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the session cookie, if set. Otherwise return false. |
||
| 193 | * |
||
| 194 | * Session cookies without a customer ID are invalid. |
||
| 195 | * |
||
| 196 | * @return bool|array |
||
| 197 | */ |
||
| 198 | public function get_session_cookie() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get session data. |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | public function get_session_data() { |
||
| 230 | |||
| 231 | public function generate_key($customer_id){ |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Save data. |
||
| 241 | */ |
||
| 242 | public function save_data() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Destroy all session data. |
||
| 254 | */ |
||
| 255 | public function destroy_session() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Forget all session data without destroying it. |
||
| 262 | */ |
||
| 263 | public function forget_session() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * When a user is logged out, ensure they have a unique nonce by using the customer/session ID. |
||
| 275 | * |
||
| 276 | * @param int $uid User ID. |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | public function nonce_user_logged_out( $uid ) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns the session. |
||
| 285 | * |
||
| 286 | * @param string $customer_id Customer ID. |
||
| 287 | * @param mixed $default Default session value. |
||
| 288 | * @return string|array |
||
| 289 | */ |
||
| 290 | public function get_session( $customer_id, $default = false ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Delete the session from the cache and database. |
||
| 314 | * |
||
| 315 | * @param int $customer_id Customer ID. |
||
| 316 | */ |
||
| 317 | public function delete_session( $customer_id ) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Update the session expiry timestamp. |
||
| 326 | * |
||
| 327 | * @param string $customer_id Customer ID. |
||
| 328 | * @param int $timestamp Timestamp to expire the cookie. |
||
| 329 | */ |
||
| 330 | public function update_session_timestamp( $customer_id, $timestamp ) { |
||
| 335 | } |
||
| 336 | |||
| 338 | $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.