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 | 2 | /** |
|
| 71 | * Session expiring time |
||
| 72 | 2 | * |
|
| 73 | 2 | * @since 2.2.0 |
|
| 74 | * @access private |
||
| 75 | * |
||
| 76 | 2 | * @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 | 2 | private $session_expiration = false; |
|
| 89 | 2 | ||
| 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 |
||
| 193 | 11 | ||
| 194 | 11 | /** |
|
| 195 | * Get session data |
||
| 196 | 11 | * |
|
| 197 | * @since 2.2.0 |
||
| 198 | * @access public |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public function get_session_data() { |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * Get session by session id |
||
| 209 | * |
||
| 210 | * @since 2.2.0 |
||
| 211 | * @access public |
||
| 212 | * |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | public function get_session_cookie() { |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * Check if session exist for specific session id |
||
| 243 | * |
||
| 244 | * @since 2.2.0 |
||
| 245 | * @access public |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | public function has_session() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Set cookie name |
||
| 255 | * |
||
| 256 | * @since 2.2.0 |
||
| 257 | 4 | * @access private |
|
| 258 | * |
||
| 259 | 4 | * @return string Cookie name. |
|
| 260 | */ |
||
| 261 | private function set_cookie_name() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get Session |
||
| 268 | * |
||
| 269 | * Retrieve session variable for a given session key. |
||
| 270 | * |
||
| 271 | * @since 1.0 |
||
| 272 | * @access public |
||
| 273 | * |
||
| 274 | * @param string $key Session key. |
||
| 275 | * @param mixed $default default value. |
||
| 276 | * |
||
| 277 | * @return string|array Session variable. |
||
| 278 | */ |
||
| 279 | public function get( $key, $default = false ) { |
||
| 284 | 4 | ||
| 285 | /** |
||
| 286 | * Set Session |
||
| 287 | * |
||
| 288 | 4 | * @since 1.0 |
|
| 289 | * @access public |
||
| 290 | 4 | * |
|
| 291 | 4 | * @param string $key Session key. |
|
| 292 | 4 | * @param mixed $value Session variable. |
|
| 293 | * |
||
| 294 | 4 | * @return string Session variable. |
|
| 295 | */ |
||
| 296 | public function set( $key, $value ) { |
||
| 304 | |||
| 305 | 2 | /** |
|
| 306 | * Set Session Cookies |
||
| 307 | 2 | * |
|
| 308 | * Cookies are used to increase the session lifetime using the give setting. This is helpful for when a user closes |
||
| 309 | 2 | * their browser after making a donation and comes back to the site. |
|
| 310 | 2 | * |
|
| 311 | 2 | * @since 1.4 |
|
| 312 | 2 | * @access public |
|
| 313 | 2 | * |
|
| 314 | 2 | * @param bool $set Flag to check if set cookie or not. |
|
| 315 | 2 | */ |
|
| 316 | public function set_session_cookies( $set ) { |
||
| 329 | 2 | ||
| 330 | /** |
||
| 331 | 2 | * Set Cookie Expiration |
|
| 332 | * |
||
| 333 | * Force the cookie expiration time if set, default to 24 hours. |
||
| 334 | * |
||
| 335 | * @since 1.0 |
||
| 336 | * @access public |
||
| 337 | * |
||
| 338 | * @return int |
||
| 339 | */ |
||
| 340 | public function set_expiration_time() { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get Session Expiration |
||
| 349 | * |
||
| 350 | * Looks at the session cookies and returns the expiration date for this session if applicable |
||
| 351 | * |
||
| 352 | * @since 2.2.0 |
||
| 353 | * @access public |
||
| 354 | * |
||
| 355 | * @return string Formatted expiration date string. |
||
| 356 | */ |
||
| 357 | public function get_session_expiration() { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Maybe Start Session |
||
| 363 | * |
||
| 364 | * Starts a new session if one hasn't started yet. |
||
| 365 | * |
||
| 366 | * @since 2.2.0 |
||
| 367 | * @access public |
||
| 368 | * |
||
| 369 | * @return void |
||
| 370 | */ |
||
| 371 | public function maybe_start_session() { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Generate a unique donor ID. |
||
| 383 | * |
||
| 384 | * Uses Portable PHP password hashing framework to generate a unique cryptographically strong ID. |
||
| 385 | * |
||
| 386 | * @since 2.2.0 |
||
| 387 | * @access public |
||
| 388 | */ |
||
| 389 | public function generate_donor_id() { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Save donor session data |
||
| 398 | * |
||
| 399 | * @since 2.2.0 |
||
| 400 | * @access public |
||
| 401 | */ |
||
| 402 | public function save_data() { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Destroy all session data. |
||
| 427 | * |
||
| 428 | * @since 2.2.0 |
||
| 429 | * @access public |
||
| 430 | */ |
||
| 431 | public function destroy_session() { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Delete nonce cookie if generating fresh form html. |
||
| 445 | * |
||
| 446 | * @since 2.2.0 |
||
| 447 | * @access public |
||
| 448 | * |
||
| 449 | * @return bool |
||
| 450 | */ |
||
| 451 | public function is_delete_nonce_cookie(){ |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Get cookie names |
||
| 463 | * |
||
| 464 | * @since 2.2.0 |
||
| 465 | * @access public |
||
| 466 | * |
||
| 467 | * @param string $type Nonce type. |
||
| 468 | * |
||
| 469 | * @return string Cookie name |
||
| 470 | */ |
||
| 471 | public function get_cookie_name( $type = '' ) { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * When a user is logged out, ensure they have a unique nonce by using the donor/session ID. |
||
| 489 | * Note: for internal logic only. |
||
| 490 | * |
||
| 491 | * @since 2.2.0 |
||
| 492 | * @access public |
||
| 493 | * |
||
| 494 | * @param int $uid User ID. |
||
| 495 | * |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | public function __nonce_user_logged_out( $uid ) { |
||
| 501 | |||
| 502 | |||
| 503 | /** |
||
| 504 | * Cleanup session data from the database and clear caches. |
||
| 505 | * Note: for internal logic only. |
||
| 506 | * |
||
| 507 | * @since 2.2.0 |
||
| 508 | * @access public |
||
| 509 | */ |
||
| 510 | public function __cleanup_sessions() { // @codingStandardsIgnoreLine |
||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * Get Session ID |
||
| 517 | * |
||
| 518 | * Retrieve session ID. |
||
| 519 | * |
||
| 520 | * @since 1.0 |
||
| 521 | * @deprecated 2.2.0 |
||
| 522 | * @access public |
||
| 523 | * |
||
| 524 | * @return string Session ID. |
||
| 525 | */ |
||
| 526 | public function get_id() { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Set Cookie Variant Time |
||
| 532 | * |
||
| 533 | * Force the cookie expiration variant time to custom expiration option, less and hour. defaults to 23 hours |
||
| 534 | * (set_expiration_variant_time used in WP_Session). |
||
| 535 | * |
||
| 536 | * @since 1.0 |
||
| 537 | * @deprecated 2.2.0 |
||
| 538 | * @access public |
||
| 539 | * |
||
| 540 | * @return int |
||
| 541 | */ |
||
| 542 | public function set_expiration_variant_time() { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Starts a new session if one has not started yet. |
||
| 549 | * |
||
| 550 | * Checks to see if the server supports PHP sessions or if the GIVE_USE_PHP_SESSIONS constant is defined. |
||
| 551 | * |
||
| 552 | * @since 1.0 |
||
| 553 | * @access public |
||
| 554 | * @deprecated 2.2.0 |
||
| 555 | * |
||
| 556 | * @return bool $ret True if we are using PHP sessions, false otherwise. |
||
| 557 | */ |
||
| 558 | public function use_php_sessions() { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Should Start Session |
||
| 568 | * |
||
| 569 | * Determines if we should start sessions. |
||
| 570 | * |
||
| 571 | * @since 1.4 |
||
| 572 | * @access public |
||
| 573 | * @deprecated 2.2.0 |
||
| 574 | * |
||
| 575 | * @return bool |
||
| 576 | */ |
||
| 577 | public function should_start_session() { |
||
| 612 | } |
||
| 613 |