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 Give_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 Give_Session, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Give_Session { |
||
|
|
|||
| 21 | /** |
||
| 22 | * Instance. |
||
| 23 | * |
||
| 24 | * @since 2.2.0 |
||
| 25 | * @access private |
||
| 26 | * @var Give_Session |
||
| 27 | */ |
||
| 28 | static private $instance; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Holds our session data |
||
| 32 | * |
||
| 33 | * @since 1.0 |
||
| 34 | * @access private |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $session = array(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Holds our session data |
||
| 42 | * |
||
| 43 | * @since 1.0 |
||
| 44 | * @access private |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $session_data_changed = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Cookie Name |
||
| 52 | * |
||
| 53 | * @since 1.0 |
||
| 54 | * @access private |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | private $cookie_name = ''; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Donor Unique ID |
||
| 62 | * |
||
| 63 | * @since 1.0 |
||
| 64 | * @access private |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $donor_id = ''; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Session expiring time |
||
| 72 | * |
||
| 73 | * @since 2.2.0 |
||
| 74 | * @access private |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | private $session_expiring = false; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Session expiration time |
||
| 82 | * |
||
| 83 | * @since 2.2.0 |
||
| 84 | * @access private |
||
| 85 | * |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | private $session_expiration = false; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Flag to check if donor has cookie or not |
||
| 92 | * |
||
| 93 | * @since 2.2.0 |
||
| 94 | * @access private |
||
| 95 | * |
||
| 96 | * @var bool |
||
| 97 | */ |
||
| 98 | private $has_cookie = false; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Expiration Time |
||
| 102 | * |
||
| 103 | * @since 1.0 |
||
| 104 | * @access private |
||
| 105 | * |
||
| 106 | * @var int |
||
| 107 | */ |
||
| 108 | private $exp_option = false; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Expiration Time |
||
| 112 | * |
||
| 113 | * @since 2.2.0 |
||
| 114 | * @access private |
||
| 115 | * |
||
| 116 | * @var string |
||
| 117 | */ |
||
| 118 | private $nonce_cookie_name = ''; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Singleton pattern. |
||
| 122 | * |
||
| 123 | * @since 2.2.0 |
||
| 124 | * @access private |
||
| 125 | */ |
||
| 126 | private function __construct() { |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Get instance. |
||
| 132 | * |
||
| 133 | * @since 2.2.0 |
||
| 134 | * @access public |
||
| 135 | * @return Give_Session |
||
| 136 | */ |
||
| 137 | View Code Duplication | public static function get_instance() { |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Setup |
||
| 148 | * |
||
| 149 | * @since 2.2.0 |
||
| 150 | * @access public |
||
| 151 | */ |
||
| 152 | private function __setup() { // @codingStandardsIgnoreLine |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get session data |
||
| 195 | * |
||
| 196 | * @since 2.2.0 |
||
| 197 | * @access public |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | public function get_session_data() { |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * Get session by session id |
||
| 208 | * |
||
| 209 | * @since 2.2.0 |
||
| 210 | * @access public |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | public function get_session_cookie() { |
||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * Load session cookie by ajax |
||
| 252 | * |
||
| 253 | * @since 2.2.6 |
||
| 254 | * @access private |
||
| 255 | * |
||
| 256 | * @return array|bool|string |
||
| 257 | */ |
||
| 258 | private function __handle_ajax_cookie(){ |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * Check if session exist for specific session id |
||
| 277 | * |
||
| 278 | * @since 2.2.0 |
||
| 279 | * @access public |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function has_session() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Set cookie name |
||
| 289 | * |
||
| 290 | * @since 2.2.0 |
||
| 291 | * @access private |
||
| 292 | * |
||
| 293 | * @return void |
||
| 294 | */ |
||
| 295 | private function set_cookie_name() { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Get Session |
||
| 319 | * |
||
| 320 | * Retrieve session variable for a given session key. |
||
| 321 | * |
||
| 322 | * @since 1.0 |
||
| 323 | * @access public |
||
| 324 | * |
||
| 325 | * @param string $key Session key. |
||
| 326 | * @param mixed $default default value. |
||
| 327 | * |
||
| 328 | * @return string|array Session variable. |
||
| 329 | */ |
||
| 330 | public function get( $key, $default = false ) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Set Session |
||
| 338 | * |
||
| 339 | * @since 1.0 |
||
| 340 | * @access public |
||
| 341 | * |
||
| 342 | * @param string $key Session key. |
||
| 343 | * @param mixed $value Session variable. |
||
| 344 | * |
||
| 345 | * @return string Session variable. |
||
| 346 | */ |
||
| 347 | public function set( $key, $value ) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Set Session Cookies |
||
| 358 | * |
||
| 359 | * Cookies are used to increase the session lifetime using the give setting. This is helpful for when a user closes |
||
| 360 | * their browser after making a donation and comes back to the site. |
||
| 361 | * |
||
| 362 | * @since 1.4 |
||
| 363 | * @access public |
||
| 364 | * |
||
| 365 | * @param bool $set Flag to check if set cookie or not. |
||
| 366 | */ |
||
| 367 | public function set_session_cookies( $set ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Set Cookie Expiration |
||
| 383 | * |
||
| 384 | * Force the cookie expiration time if set, default to 24 hours. |
||
| 385 | * |
||
| 386 | * @since 1.0 |
||
| 387 | * @access public |
||
| 388 | * |
||
| 389 | * @return int |
||
| 390 | */ |
||
| 391 | public function set_expiration_time() { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Get Session Expiration |
||
| 400 | * |
||
| 401 | * Looks at the session cookies and returns the expiration date for this session if applicable |
||
| 402 | * |
||
| 403 | * @since 2.2.0 |
||
| 404 | * @access public |
||
| 405 | * |
||
| 406 | * @return string|bool Formatted expiration date string. |
||
| 407 | */ |
||
| 408 | public function get_session_expiration() { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Maybe Start Session |
||
| 414 | * |
||
| 415 | * Starts a new session if one hasn't started yet. |
||
| 416 | * |
||
| 417 | * @since 2.2.0 |
||
| 418 | * @access public |
||
| 419 | * |
||
| 420 | * @return void |
||
| 421 | */ |
||
| 422 | public function maybe_start_session() { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Generate a unique donor ID. |
||
| 434 | * |
||
| 435 | * Uses Portable PHP password hashing framework to generate a unique cryptographically strong ID. |
||
| 436 | * |
||
| 437 | * @since 2.2.0 |
||
| 438 | * @access public |
||
| 439 | */ |
||
| 440 | public function generate_donor_id() { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Save donor session data |
||
| 449 | * |
||
| 450 | * @since 2.2.0 |
||
| 451 | * @access public |
||
| 452 | */ |
||
| 453 | public function save_data() { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Destroy all session data. |
||
| 478 | * |
||
| 479 | * @since 2.2.0 |
||
| 480 | * @access public |
||
| 481 | */ |
||
| 482 | public function destroy_session() { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Delete nonce cookie if generating fresh form html. |
||
| 496 | * |
||
| 497 | * @since 2.2.0 |
||
| 498 | * @access public |
||
| 499 | * |
||
| 500 | * @return bool |
||
| 501 | */ |
||
| 502 | public function is_delete_nonce_cookie(){ |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Get cookie names |
||
| 514 | * |
||
| 515 | * @since 2.2.0 |
||
| 516 | * @access public |
||
| 517 | * |
||
| 518 | * @param string $type Nonce type. |
||
| 519 | * |
||
| 520 | * @return string Cookie name |
||
| 521 | */ |
||
| 522 | public function get_cookie_name( $type = '' ) { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * When a user is logged out, ensure they have a unique nonce by using the donor/session ID. |
||
| 540 | * Note: for internal logic only. |
||
| 541 | * |
||
| 542 | * @since 2.2.0 |
||
| 543 | * @access public |
||
| 544 | * |
||
| 545 | * @param int $uid User ID. |
||
| 546 | * |
||
| 547 | * @return string |
||
| 548 | */ |
||
| 549 | public function __nonce_user_logged_out( $uid ) { |
||
| 552 | |||
| 553 | |||
| 554 | /** |
||
| 555 | * Cleanup session data from the database and clear caches. |
||
| 556 | * Note: for internal logic only. |
||
| 557 | * |
||
| 558 | * @since 2.2.0 |
||
| 559 | * @access public |
||
| 560 | */ |
||
| 561 | public function __cleanup_sessions() { // @codingStandardsIgnoreLine |
||
| 564 | |||
| 565 | |||
| 566 | /** |
||
| 567 | * Get Session ID |
||
| 568 | * |
||
| 569 | * Retrieve session ID. |
||
| 570 | * |
||
| 571 | * @since 1.0 |
||
| 572 | * @deprecated 2.2.0 |
||
| 573 | * @access public |
||
| 574 | * |
||
| 575 | * @return string Session ID. |
||
| 576 | */ |
||
| 577 | public function get_id() { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Set Cookie Variant Time |
||
| 583 | * |
||
| 584 | * Force the cookie expiration variant time to custom expiration option, less and hour. defaults to 23 hours |
||
| 585 | * (set_expiration_variant_time used in WP_Session). |
||
| 586 | * |
||
| 587 | * @since 1.0 |
||
| 588 | * @deprecated 2.2.0 |
||
| 589 | * @access public |
||
| 590 | * |
||
| 591 | * @return int |
||
| 592 | */ |
||
| 593 | public function set_expiration_variant_time() { |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Starts a new session if one has not started yet. |
||
| 600 | * |
||
| 601 | * Checks to see if the server supports PHP sessions or if the GIVE_USE_PHP_SESSIONS constant is defined. |
||
| 602 | * |
||
| 603 | * @since 1.0 |
||
| 604 | * @access public |
||
| 605 | * @deprecated 2.2.0 |
||
| 606 | * |
||
| 607 | * @return bool $ret True if we are using PHP sessions, false otherwise. |
||
| 608 | */ |
||
| 609 | public function use_php_sessions() { |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Should Start Session |
||
| 619 | * |
||
| 620 | * Determines if we should start sessions. |
||
| 621 | * |
||
| 622 | * @since 1.4 |
||
| 623 | * @access public |
||
| 624 | * @deprecated 2.2.0 |
||
| 625 | * |
||
| 626 | * @return bool |
||
| 627 | */ |
||
| 628 | public function should_start_session() { |
||
| 663 | } |
||
| 664 |