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 |
||
| 24 | class EE_Session implements SessionIdentifierInterface |
||
| 25 | { |
||
| 26 | |||
| 27 | const session_id_prefix = 'ee_ssn_'; |
||
| 28 | |||
| 29 | const hash_check_prefix = 'ee_shc_'; |
||
| 30 | |||
| 31 | const OPTION_NAME_SETTINGS = 'ee_session_settings'; |
||
| 32 | |||
| 33 | const STATUS_CLOSED = 0; |
||
| 34 | |||
| 35 | const STATUS_OPEN = 1; |
||
| 36 | |||
| 37 | const SAVE_STATE_CLEAN = 'clean'; |
||
| 38 | const SAVE_STATE_DIRTY = 'dirty'; |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * instance of the EE_Session object |
||
| 43 | * |
||
| 44 | * @var EE_Session |
||
| 45 | */ |
||
| 46 | private static $_instance; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var CacheStorageInterface $cache_storage |
||
| 50 | */ |
||
| 51 | protected $cache_storage; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var EE_Encryption $encryption |
||
| 55 | */ |
||
| 56 | protected $encryption; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var SessionStartHandler $session_start_handler |
||
| 60 | */ |
||
| 61 | protected $session_start_handler; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * the session id |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $_sid; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * session id salt |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | private $_sid_salt; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * session data |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private $_session_data = array(); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * how long an EE session lasts |
||
| 86 | * default session lifespan of 1 hour (for not so instant IPNs) |
||
| 87 | * |
||
| 88 | * @var SessionLifespan $session_lifespan |
||
| 89 | */ |
||
| 90 | private $session_lifespan; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * session expiration time as Unix timestamp in GMT |
||
| 94 | * |
||
| 95 | * @var int |
||
| 96 | */ |
||
| 97 | private $_expiration; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * whether or not session has expired at some point |
||
| 101 | * |
||
| 102 | * @var boolean |
||
| 103 | */ |
||
| 104 | private $_expired = false; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * current time as Unix timestamp in GMT |
||
| 108 | * |
||
| 109 | * @var int |
||
| 110 | */ |
||
| 111 | private $_time; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * whether to encrypt session data |
||
| 115 | * |
||
| 116 | * @var bool |
||
| 117 | */ |
||
| 118 | private $_use_encryption; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * well... according to the server... |
||
| 122 | * |
||
| 123 | * @var null |
||
| 124 | */ |
||
| 125 | private $_user_agent; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * do you really trust the server ? |
||
| 129 | * |
||
| 130 | * @var null |
||
| 131 | */ |
||
| 132 | private $_ip_address; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * current WP user_id |
||
| 136 | * |
||
| 137 | * @var null |
||
| 138 | */ |
||
| 139 | private $_wp_user_id; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * array for defining default session vars |
||
| 143 | * |
||
| 144 | * @var array |
||
| 145 | */ |
||
| 146 | private $_default_session_vars = array( |
||
| 147 | 'id' => null, |
||
| 148 | 'user_id' => null, |
||
| 149 | 'ip_address' => null, |
||
| 150 | 'user_agent' => null, |
||
| 151 | 'init_access' => null, |
||
| 152 | 'last_access' => null, |
||
| 153 | 'expiration' => null, |
||
| 154 | 'pages_visited' => array(), |
||
| 155 | ); |
||
| 156 | |||
| 157 | /** |
||
| 158 | * timestamp for when last garbage collection cycle was performed |
||
| 159 | * |
||
| 160 | * @var int $_last_gc |
||
| 161 | */ |
||
| 162 | private $_last_gc; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var RequestInterface $request |
||
| 166 | */ |
||
| 167 | protected $request; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * whether session is active or not |
||
| 171 | * |
||
| 172 | * @var int $status |
||
| 173 | */ |
||
| 174 | private $status = EE_Session::STATUS_CLOSED; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * whether session data has changed therefore requiring a session save |
||
| 178 | * |
||
| 179 | * @var string $save_state |
||
| 180 | */ |
||
| 181 | private $save_state = EE_Session::SAVE_STATE_CLEAN; |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * @singleton method used to instantiate class object |
||
| 186 | * @param CacheStorageInterface $cache_storage |
||
| 187 | * @param SessionLifespan|null $lifespan |
||
| 188 | * @param RequestInterface $request |
||
| 189 | * @param SessionStartHandler $session_start_handler |
||
| 190 | * @param EE_Encryption $encryption |
||
| 191 | * @return EE_Session |
||
| 192 | * @throws InvalidArgumentException |
||
| 193 | * @throws InvalidDataTypeException |
||
| 194 | * @throws InvalidInterfaceException |
||
| 195 | */ |
||
| 196 | public static function instance( |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * protected constructor to prevent direct creation |
||
| 227 | * |
||
| 228 | * @param CacheStorageInterface $cache_storage |
||
| 229 | * @param SessionLifespan $lifespan |
||
| 230 | * @param RequestInterface $request |
||
| 231 | * @param SessionStartHandler $session_start_handler |
||
| 232 | * @param EE_Encryption $encryption |
||
| 233 | * @throws InvalidArgumentException |
||
| 234 | * @throws InvalidDataTypeException |
||
| 235 | * @throws InvalidInterfaceException |
||
| 236 | */ |
||
| 237 | protected function __construct( |
||
| 286 | |||
| 287 | |||
| 288 | /** |
||
| 289 | * @return bool |
||
| 290 | * @throws InvalidArgumentException |
||
| 291 | * @throws InvalidDataTypeException |
||
| 292 | * @throws InvalidInterfaceException |
||
| 293 | */ |
||
| 294 | public static function isLoadedAndActive() |
||
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | public function isActive() |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * @return void |
||
| 313 | * @throws EE_Error |
||
| 314 | * @throws InvalidArgumentException |
||
| 315 | * @throws InvalidDataTypeException |
||
| 316 | * @throws InvalidInterfaceException |
||
| 317 | * @throws InvalidSessionDataException |
||
| 318 | * @throws RuntimeException |
||
| 319 | * @throws ReflectionException |
||
| 320 | */ |
||
| 321 | public function open_session() |
||
| 329 | |||
| 330 | |||
| 331 | /** |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | public function expired() |
||
| 338 | |||
| 339 | |||
| 340 | /** |
||
| 341 | * @return void |
||
| 342 | */ |
||
| 343 | public function reset_expired() |
||
| 347 | |||
| 348 | |||
| 349 | /** |
||
| 350 | * @return int |
||
| 351 | */ |
||
| 352 | public function expiration() |
||
| 356 | |||
| 357 | |||
| 358 | /** |
||
| 359 | * @return int |
||
| 360 | */ |
||
| 361 | public function extension() |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * @param int $time number of seconds to add to session expiration |
||
| 369 | */ |
||
| 370 | public function extend_expiration($time = 0) |
||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * @return int |
||
| 379 | */ |
||
| 380 | public function lifespan() |
||
| 384 | |||
| 385 | |||
| 386 | /** |
||
| 387 | * Marks whether the session data has been updated or not. |
||
| 388 | * Valid options are: |
||
| 389 | * EE_Session::SAVE_STATE_CLEAN - session data remains unchanged and updating is not necessary |
||
| 390 | * EE_Session::SAVE_STATE_DIRTY - session data has changed since last save and needs to be updated |
||
| 391 | * default value is EE_Session::SAVE_STATE_DIRTY |
||
| 392 | * |
||
| 393 | * @param string $save_state |
||
| 394 | */ |
||
| 395 | public function setSaveState($save_state = EE_Session::SAVE_STATE_DIRTY) |
||
| 406 | |||
| 407 | |||
| 408 | |||
| 409 | /** |
||
| 410 | * This just sets some defaults for the _session data property |
||
| 411 | * |
||
| 412 | * @access private |
||
| 413 | * @return void |
||
| 414 | */ |
||
| 415 | private function _set_defaults() |
||
| 426 | |||
| 427 | |||
| 428 | /** |
||
| 429 | * @retrieve session data |
||
| 430 | * @access public |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | public function id() |
||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * @param \EE_Cart $cart |
||
| 441 | * @return bool |
||
| 442 | */ |
||
| 443 | public function set_cart(EE_Cart $cart) |
||
| 449 | |||
| 450 | |||
| 451 | /** |
||
| 452 | * reset_cart |
||
| 453 | */ |
||
| 454 | public function reset_cart() |
||
| 460 | |||
| 461 | |||
| 462 | /** |
||
| 463 | * @return \EE_Cart |
||
| 464 | */ |
||
| 465 | public function cart() |
||
| 471 | |||
| 472 | |||
| 473 | /** |
||
| 474 | * @param \EE_Checkout $checkout |
||
| 475 | * @return bool |
||
| 476 | */ |
||
| 477 | public function set_checkout(EE_Checkout $checkout) |
||
| 483 | |||
| 484 | |||
| 485 | /** |
||
| 486 | * reset_checkout |
||
| 487 | */ |
||
| 488 | public function reset_checkout() |
||
| 494 | |||
| 495 | |||
| 496 | /** |
||
| 497 | * @return \EE_Checkout |
||
| 498 | */ |
||
| 499 | public function checkout() |
||
| 505 | |||
| 506 | |||
| 507 | /** |
||
| 508 | * @param \EE_Transaction $transaction |
||
| 509 | * @return bool |
||
| 510 | * @throws EE_Error |
||
| 511 | */ |
||
| 512 | public function set_transaction(EE_Transaction $transaction) |
||
| 520 | |||
| 521 | |||
| 522 | /** |
||
| 523 | * reset_transaction |
||
| 524 | */ |
||
| 525 | public function reset_transaction() |
||
| 531 | |||
| 532 | |||
| 533 | /** |
||
| 534 | * @return \EE_Transaction |
||
| 535 | */ |
||
| 536 | public function transaction() |
||
| 543 | |||
| 544 | |||
| 545 | /** |
||
| 546 | * retrieve session data |
||
| 547 | * |
||
| 548 | * @param null $key |
||
| 549 | * @param bool $reset_cache |
||
| 550 | * @return array |
||
| 551 | */ |
||
| 552 | public function get_session_data($key = null, $reset_cache = false) |
||
| 564 | |||
| 565 | |||
| 566 | /** |
||
| 567 | * Returns TRUE on success, FALSE on fail |
||
| 568 | * |
||
| 569 | * @param array $data |
||
| 570 | * @return bool |
||
| 571 | */ |
||
| 572 | public function set_session_data($data) |
||
| 573 | { |
||
| 574 | // nothing ??? bad data ??? go home! |
||
| 575 | if (empty($data) || ! is_array($data)) { |
||
| 576 | EE_Error::add_error( |
||
| 577 | esc_html__( |
||
| 578 | 'No session data or invalid session data was provided.', |
||
| 579 | 'event_espresso' |
||
| 580 | ), |
||
| 581 | __FILE__, |
||
| 582 | __FUNCTION__, |
||
| 583 | __LINE__ |
||
| 584 | ); |
||
| 585 | return false; |
||
| 586 | } |
||
| 587 | foreach ($data as $key => $value) { |
||
| 588 | if (isset($this->_default_session_vars[ $key ])) { |
||
| 589 | EE_Error::add_error( |
||
| 590 | sprintf( |
||
| 591 | esc_html__( |
||
| 592 | 'Sorry! %s is a default session datum and can not be reset.', |
||
| 593 | 'event_espresso' |
||
| 594 | ), |
||
| 595 | $key |
||
| 596 | ), |
||
| 597 | __FILE__, |
||
| 598 | __FUNCTION__, |
||
| 599 | __LINE__ |
||
| 600 | ); |
||
| 601 | return false; |
||
| 602 | } |
||
| 603 | $this->_session_data[ $key ] = $value; |
||
| 604 | $this->setSaveState(); |
||
| 605 | } |
||
| 606 | return true; |
||
| 607 | } |
||
| 608 | |||
| 609 | |||
| 610 | /** |
||
| 611 | * @initiate session |
||
| 612 | * @access private |
||
| 613 | * @return TRUE on success, FALSE on fail |
||
| 614 | * @throws EE_Error |
||
| 615 | * @throws InvalidArgumentException |
||
| 616 | * @throws InvalidDataTypeException |
||
| 617 | * @throws InvalidInterfaceException |
||
| 618 | * @throws InvalidSessionDataException |
||
| 619 | * @throws RuntimeException |
||
| 620 | * @throws ReflectionException |
||
| 621 | */ |
||
| 622 | private function _espresso_session() |
||
| 667 | |||
| 668 | |||
| 669 | /** |
||
| 670 | * _get_session_data |
||
| 671 | * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup |
||
| 672 | * databases |
||
| 673 | * |
||
| 674 | * @return array |
||
| 675 | * @throws EE_Error |
||
| 676 | * @throws InvalidArgumentException |
||
| 677 | * @throws InvalidSessionDataException |
||
| 678 | * @throws InvalidDataTypeException |
||
| 679 | * @throws InvalidInterfaceException |
||
| 680 | * @throws RuntimeException |
||
| 681 | */ |
||
| 682 | protected function _retrieve_session_data() |
||
| 776 | |||
| 777 | |||
| 778 | /** |
||
| 779 | * _generate_session_id |
||
| 780 | * Retrieves the PHP session id either directly from the PHP session, |
||
| 781 | * or from the $_REQUEST array if it was passed in from an AJAX request. |
||
| 782 | * The session id is then salted and hashed (mmm sounds tasty) |
||
| 783 | * so that it can be safely used as a $_REQUEST param |
||
| 784 | * |
||
| 785 | * @return string |
||
| 786 | */ |
||
| 787 | protected function _generate_session_id() |
||
| 797 | |||
| 798 | |||
| 799 | /** |
||
| 800 | * _get_sid_salt |
||
| 801 | * |
||
| 802 | * @return string |
||
| 803 | */ |
||
| 804 | protected function _get_sid_salt() |
||
| 822 | |||
| 823 | |||
| 824 | /** |
||
| 825 | * _set_init_access_and_expiration |
||
| 826 | * |
||
| 827 | * @return void |
||
| 828 | */ |
||
| 829 | protected function _set_init_access_and_expiration() |
||
| 838 | |||
| 839 | |||
| 840 | /** |
||
| 841 | * @update session data prior to saving to the db |
||
| 842 | * @access public |
||
| 843 | * @param bool $new_session |
||
| 844 | * @return TRUE on success, FALSE on fail |
||
| 845 | * @throws EE_Error |
||
| 846 | * @throws InvalidArgumentException |
||
| 847 | * @throws InvalidDataTypeException |
||
| 848 | * @throws InvalidInterfaceException |
||
| 849 | * @throws ReflectionException |
||
| 850 | */ |
||
| 851 | public function update($new_session = false) |
||
| 919 | |||
| 920 | |||
| 921 | /** |
||
| 922 | * @create session data array |
||
| 923 | * @access public |
||
| 924 | * @return bool |
||
| 925 | * @throws EE_Error |
||
| 926 | * @throws InvalidArgumentException |
||
| 927 | * @throws InvalidDataTypeException |
||
| 928 | * @throws InvalidInterfaceException |
||
| 929 | * @throws ReflectionException |
||
| 930 | */ |
||
| 931 | private function _create_espresso_session() |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Detects if there is anything worth saving in the session (eg the cart is a good one, notices are pretty good |
||
| 940 | * too). This is used when determining if we want to save the session or not. |
||
| 941 | * @since 4.9.67.p |
||
| 942 | * @return bool |
||
| 943 | */ |
||
| 944 | private function sessionHasStuffWorthSaving() |
||
| 959 | |||
| 960 | |||
| 961 | /** |
||
| 962 | * _save_session_to_db |
||
| 963 | * |
||
| 964 | * @param bool $clear_session |
||
| 965 | * @return string |
||
| 966 | * @throws EE_Error |
||
| 967 | * @throws InvalidArgumentException |
||
| 968 | * @throws InvalidDataTypeException |
||
| 969 | * @throws InvalidInterfaceException |
||
| 970 | * @throws ReflectionException |
||
| 971 | */ |
||
| 972 | private function _save_session_to_db($clear_session = false) |
||
| 1015 | |||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * @get the full page request the visitor is accessing |
||
| 1019 | * @access public |
||
| 1020 | * @return string |
||
| 1021 | */ |
||
| 1022 | public function _get_page_visit() |
||
| 1051 | |||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * @the current wp user id |
||
| 1055 | * @access public |
||
| 1056 | * @return int |
||
| 1057 | */ |
||
| 1058 | public function _wp_user_id() |
||
| 1064 | |||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * Clear EE_Session data |
||
| 1068 | * |
||
| 1069 | * @access public |
||
| 1070 | * @param string $class |
||
| 1071 | * @param string $function |
||
| 1072 | * @return void |
||
| 1073 | * @throws EE_Error |
||
| 1074 | * @throws InvalidArgumentException |
||
| 1075 | * @throws InvalidDataTypeException |
||
| 1076 | * @throws InvalidInterfaceException |
||
| 1077 | * @throws ReflectionException |
||
| 1078 | */ |
||
| 1079 | public function clear_session($class = '', $function = '') |
||
| 1097 | |||
| 1098 | |||
| 1099 | /** |
||
| 1100 | * resets all non-default session vars. Returns TRUE on success, FALSE on fail |
||
| 1101 | * |
||
| 1102 | * @param array|mixed $data_to_reset |
||
| 1103 | * @param bool $show_all_notices |
||
| 1104 | * @return bool |
||
| 1105 | */ |
||
| 1106 | public function reset_data($data_to_reset = array(), $show_all_notices = false) |
||
| 1183 | |||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * wp_loaded |
||
| 1187 | * |
||
| 1188 | * @access public |
||
| 1189 | * @throws EE_Error |
||
| 1190 | * @throws InvalidDataTypeException |
||
| 1191 | * @throws InvalidInterfaceException |
||
| 1192 | * @throws InvalidArgumentException |
||
| 1193 | * @throws ReflectionException |
||
| 1194 | */ |
||
| 1195 | public function wp_loaded() |
||
| 1201 | |||
| 1202 | |||
| 1203 | /** |
||
| 1204 | * Used to reset the entire object (for tests). |
||
| 1205 | * |
||
| 1206 | * @since 4.3.0 |
||
| 1207 | * @throws EE_Error |
||
| 1208 | * @throws InvalidDataTypeException |
||
| 1209 | * @throws InvalidInterfaceException |
||
| 1210 | * @throws InvalidArgumentException |
||
| 1211 | * @throws ReflectionException |
||
| 1212 | */ |
||
| 1213 | public function reset_instance() |
||
| 1218 | |||
| 1219 | |||
| 1220 | public function configure_garbage_collection_filters() |
||
| 1243 | |||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
||
| 1247 | * @param $data1 |
||
| 1248 | * @return string |
||
| 1249 | */ |
||
| 1250 | private function find_serialize_error($data1) |
||
| 1306 | |||
| 1307 | |||
| 1308 | /** |
||
| 1309 | * Saves an array of settings used for configuring aspects of session behaviour |
||
| 1310 | * |
||
| 1311 | * @param array $updated_settings |
||
| 1312 | */ |
||
| 1313 | private function updateSessionSettings(array $updated_settings = array()) |
||
| 1319 | |||
| 1320 | |||
| 1321 | /** |
||
| 1322 | * garbage_collection |
||
| 1323 | */ |
||
| 1324 | public function garbageCollection() |
||
| 1368 | } |
||
| 1369 |