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 SessionBackend 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 SessionBackend, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | final class SessionBackend { |
||
| 50 | /** @var SessionId */ |
||
| 51 | private $id; |
||
| 52 | |||
| 53 | private $persist = false; |
||
| 54 | private $remember = false; |
||
| 55 | private $forceHTTPS = false; |
||
| 56 | |||
| 57 | /** @var array|null */ |
||
| 58 | private $data = null; |
||
| 59 | |||
| 60 | private $forcePersist = false; |
||
| 61 | private $metaDirty = false; |
||
| 62 | private $dataDirty = false; |
||
| 63 | |||
| 64 | /** @var string Used to detect subarray modifications */ |
||
| 65 | private $dataHash = null; |
||
| 66 | |||
| 67 | /** @var CachedBagOStuff */ |
||
| 68 | private $store; |
||
| 69 | |||
| 70 | /** @var LoggerInterface */ |
||
| 71 | private $logger; |
||
| 72 | |||
| 73 | /** @var int */ |
||
| 74 | private $lifetime; |
||
| 75 | |||
| 76 | /** @var User */ |
||
| 77 | private $user; |
||
| 78 | |||
| 79 | private $curIndex = 0; |
||
| 80 | |||
| 81 | /** @var WebRequest[] Session requests */ |
||
| 82 | private $requests = []; |
||
| 83 | |||
| 84 | /** @var SessionProvider provider */ |
||
| 85 | private $provider; |
||
| 86 | |||
| 87 | /** @var array|null provider-specified metadata */ |
||
| 88 | private $providerMetadata = null; |
||
| 89 | |||
| 90 | private $expires = 0; |
||
| 91 | private $loggedOut = 0; |
||
| 92 | private $delaySave = 0; |
||
| 93 | |||
| 94 | private $usePhpSessionHandling = true; |
||
| 95 | private $checkPHPSessionRecursionGuard = false; |
||
| 96 | |||
| 97 | private $shutdown = false; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param SessionId $id Session ID object |
||
| 101 | * @param SessionInfo $info Session info to populate from |
||
| 102 | * @param CachedBagOStuff $store Backend data store |
||
| 103 | * @param LoggerInterface $logger |
||
| 104 | * @param int $lifetime Session data lifetime in seconds |
||
| 105 | */ |
||
| 106 | public function __construct( |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return a new Session for this backend |
||
| 169 | * @param WebRequest $request |
||
| 170 | * @return Session |
||
| 171 | */ |
||
| 172 | public function getSession( WebRequest $request ) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Deregister a Session |
||
| 181 | * @private For use by \MediaWiki\Session\Session::__destruct() only |
||
| 182 | * @param int $index |
||
| 183 | */ |
||
| 184 | public function deregisterSession( $index ) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Shut down a session |
||
| 194 | * @private For use by \MediaWiki\Session\SessionManager::shutdown() only |
||
| 195 | */ |
||
| 196 | public function shutdown() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Returns the session ID. |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public function getId() { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Fetch the SessionId object |
||
| 211 | * @private For internal use by WebRequest |
||
| 212 | * @return SessionId |
||
| 213 | */ |
||
| 214 | public function getSessionId() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Changes the session ID |
||
| 220 | * @return string New ID (might be the same as the old) |
||
| 221 | */ |
||
| 222 | public function resetId() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Fetch the SessionProvider for this session |
||
| 258 | * @return SessionProviderInterface |
||
| 259 | */ |
||
| 260 | public function getProvider() { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Indicate whether this session is persisted across requests |
||
| 266 | * |
||
| 267 | * For example, if cookies are set. |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function isPersistent() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Make this session persisted across requests |
||
| 277 | * |
||
| 278 | * If the session is already persistent, equivalent to calling |
||
| 279 | * $this->renew(). |
||
| 280 | */ |
||
| 281 | public function persist() { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Make this session not persisted across requests |
||
| 299 | */ |
||
| 300 | public function unpersist() { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Indicate whether the user should be remembered independently of the |
||
| 328 | * session ID. |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | public function shouldRememberUser() { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Set whether the user should be remembered independently of the session |
||
| 337 | * ID. |
||
| 338 | * @param bool $remember |
||
| 339 | */ |
||
| 340 | View Code Duplication | public function setRememberUser( $remember ) { |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Returns the request associated with a Session |
||
| 355 | * @param int $index Session index |
||
| 356 | * @return WebRequest |
||
| 357 | */ |
||
| 358 | public function getRequest( $index ) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Returns the authenticated user for this session |
||
| 367 | * @return User |
||
| 368 | */ |
||
| 369 | public function getUser() { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Fetch the rights allowed the user when this session is active. |
||
| 375 | * @return null|string[] Allowed user rights, or null to allow all. |
||
| 376 | */ |
||
| 377 | public function getAllowedUserRights() { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Indicate whether the session user info can be changed |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | public function canSetUser() { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Set a new user for this session |
||
| 391 | * @note This should only be called when the user has been authenticated via a login process |
||
| 392 | * @param User $user User to set on the session. |
||
| 393 | * This may become a "UserValue" in the future, or User may be refactored |
||
| 394 | * into such. |
||
| 395 | */ |
||
| 396 | public function setUser( $user ) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Get a suggested username for the login form |
||
| 415 | * @param int $index Session index |
||
| 416 | * @return string|null |
||
| 417 | */ |
||
| 418 | public function suggestLoginUsername( $index ) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Whether HTTPS should be forced |
||
| 427 | * @return bool |
||
| 428 | */ |
||
| 429 | public function shouldForceHTTPS() { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Set whether HTTPS should be forced |
||
| 435 | * @param bool $force |
||
| 436 | */ |
||
| 437 | View Code Duplication | public function setForceHTTPS( $force ) { |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Fetch the "logged out" timestamp |
||
| 452 | * @return int |
||
| 453 | */ |
||
| 454 | public function getLoggedOutTimestamp() { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Set the "logged out" timestamp |
||
| 460 | * @param int $ts |
||
| 461 | */ |
||
| 462 | public function setLoggedOutTimestamp( $ts = null ) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Fetch provider metadata |
||
| 478 | * @protected For use by SessionProvider subclasses only |
||
| 479 | * @return array|null |
||
| 480 | */ |
||
| 481 | public function getProviderMetadata() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Set provider metadata |
||
| 487 | * @protected For use by SessionProvider subclasses only |
||
| 488 | * @param array|null $metadata |
||
| 489 | */ |
||
| 490 | public function setProviderMetadata( $metadata ) { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Fetch the session data array |
||
| 508 | * |
||
| 509 | * Note the caller is responsible for calling $this->dirty() if anything in |
||
| 510 | * the array is changed. |
||
| 511 | * |
||
| 512 | * @private For use by \MediaWiki\Session\Session only. |
||
| 513 | * @return array |
||
| 514 | */ |
||
| 515 | public function &getData() { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Add data to the session. |
||
| 521 | * |
||
| 522 | * Overwrites any existing data under the same keys. |
||
| 523 | * |
||
| 524 | * @param array $newData Key-value pairs to add to the session |
||
| 525 | */ |
||
| 526 | public function addData( array $newData ) { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Mark data as dirty |
||
| 544 | * @private For use by \MediaWiki\Session\Session only. |
||
| 545 | */ |
||
| 546 | public function dirty() { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Renew the session by resaving everything |
||
| 558 | * |
||
| 559 | * Resets the TTL in the backend store if the session is near expiring, and |
||
| 560 | * re-persists the session to any active WebRequests if persistent. |
||
| 561 | */ |
||
| 562 | public function renew() { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Delay automatic saving while multiple updates are being made |
||
| 586 | * |
||
| 587 | * Calls to save() will not be delayed. |
||
| 588 | * |
||
| 589 | * @return \Wikimedia\ScopedCallback When this goes out of scope, a save will be triggered |
||
| 590 | */ |
||
| 591 | public function delaySave() { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Save the session, unless delayed |
||
| 603 | * @see SessionBackend::save() |
||
| 604 | */ |
||
| 605 | private function autosave() { |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Save the session |
||
| 613 | * |
||
| 614 | * Update both the backend data and the associated WebRequest(s) to |
||
| 615 | * reflect the state of the the SessionBackend. This might include |
||
| 616 | * persisting or unpersisting the session. |
||
| 617 | * |
||
| 618 | * @param bool $closing Whether the session is being closed |
||
| 619 | */ |
||
| 620 | public function save( $closing = false ) { |
||
| 746 | |||
| 747 | /** |
||
| 748 | * For backwards compatibility, open the PHP session when the global |
||
| 749 | * session is persisted |
||
| 750 | */ |
||
| 751 | private function checkPHPSession() { |
||
| 771 | |||
| 772 | } |
||
| 773 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: