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 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 Session, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | final class Session implements \Countable, \Iterator, \ArrayAccess { |
||
| 49 | /** @var null|string[] Encryption algorithm to use */ |
||
| 50 | private static $encryptionAlgorithm = null; |
||
| 51 | |||
| 52 | /** @var SessionBackend Session backend */ |
||
| 53 | private $backend; |
||
| 54 | |||
| 55 | /** @var int Session index */ |
||
| 56 | private $index; |
||
| 57 | |||
| 58 | /** @var LoggerInterface */ |
||
| 59 | private $logger; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param SessionBackend $backend |
||
| 63 | * @param int $index |
||
| 64 | * @param LoggerInterface $logger |
||
| 65 | */ |
||
| 66 | public function __construct( SessionBackend $backend, $index, LoggerInterface $logger ) { |
||
| 71 | |||
| 72 | public function __destruct() { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Returns the session ID |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getId() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Returns the SessionId object |
||
| 86 | * @private For internal use by WebRequest |
||
| 87 | * @return SessionId |
||
| 88 | */ |
||
| 89 | public function getSessionId() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Changes the session ID |
||
| 95 | * @return string New ID (might be the same as the old) |
||
| 96 | */ |
||
| 97 | public function resetId() { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Fetch the SessionProvider for this session |
||
| 103 | * @return SessionProviderInterface |
||
| 104 | */ |
||
| 105 | public function getProvider() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Indicate whether this session is persisted across requests |
||
| 111 | * |
||
| 112 | * For example, if cookies are set. |
||
| 113 | * |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | public function isPersistent() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Make this session persisted across requests |
||
| 122 | * |
||
| 123 | * If the session is already persistent, equivalent to calling |
||
| 124 | * $this->renew(). |
||
| 125 | */ |
||
| 126 | public function persist() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Make this session not be persisted across requests |
||
| 132 | */ |
||
| 133 | public function unpersist() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Indicate whether the user should be remembered independently of the |
||
| 139 | * session ID. |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function shouldRememberUser() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Set whether the user should be remembered independently of the session |
||
| 148 | * ID. |
||
| 149 | * @param bool $remember |
||
| 150 | */ |
||
| 151 | public function setRememberUser( $remember ) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns the request associated with this session |
||
| 157 | * @return WebRequest |
||
| 158 | */ |
||
| 159 | public function getRequest() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns the authenticated user for this session |
||
| 165 | * @return User |
||
| 166 | */ |
||
| 167 | public function getUser() { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Fetch the rights allowed the user when this session is active. |
||
| 173 | * @return null|string[] Allowed user rights, or null to allow all. |
||
| 174 | */ |
||
| 175 | public function getAllowedUserRights() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Indicate whether the session user info can be changed |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function canSetUser() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Set a new user for this session |
||
| 189 | * @note This should only be called when the user has been authenticated |
||
| 190 | * @param User $user User to set on the session. |
||
| 191 | * This may become a "UserValue" in the future, or User may be refactored |
||
| 192 | * into such. |
||
| 193 | */ |
||
| 194 | public function setUser( $user ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get a suggested username for the login form |
||
| 200 | * @return string|null |
||
| 201 | */ |
||
| 202 | public function suggestLoginUsername() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Whether HTTPS should be forced |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public function shouldForceHTTPS() { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Set whether HTTPS should be forced |
||
| 216 | * @param bool $force |
||
| 217 | */ |
||
| 218 | public function setForceHTTPS( $force ) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Fetch the "logged out" timestamp |
||
| 224 | * @return int |
||
| 225 | */ |
||
| 226 | public function getLoggedOutTimestamp() { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set the "logged out" timestamp |
||
| 232 | * @param int $ts |
||
| 233 | */ |
||
| 234 | public function setLoggedOutTimestamp( $ts ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Fetch provider metadata |
||
| 240 | * @protected For use by SessionProvider subclasses only |
||
| 241 | * @return mixed |
||
| 242 | */ |
||
| 243 | public function getProviderMetadata() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Delete all session data and clear the user (if possible) |
||
| 249 | */ |
||
| 250 | public function clear() { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Renew the session |
||
| 264 | * |
||
| 265 | * Resets the TTL in the backend store if the session is near expiring, and |
||
| 266 | * re-persists the session to any active WebRequests if persistent. |
||
| 267 | */ |
||
| 268 | public function renew() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Fetch a copy of this session attached to an alternative WebRequest |
||
| 274 | * |
||
| 275 | * Actions on the copy will affect this session too, and vice versa. |
||
| 276 | * |
||
| 277 | * @param WebRequest $request Any existing session associated with this |
||
| 278 | * WebRequest object will be overwritten. |
||
| 279 | * @return Session |
||
| 280 | */ |
||
| 281 | public function sessionWithRequest( WebRequest $request ) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Fetch a value from the session |
||
| 288 | * @param string|int $key |
||
| 289 | * @param mixed $default Returned if $this->exists( $key ) would be false |
||
| 290 | * @return mixed |
||
| 291 | */ |
||
| 292 | public function get( $key, $default = null ) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Test if a value exists in the session |
||
| 299 | * @note Unlike isset(), null values are considered to exist. |
||
| 300 | * @param string|int $key |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | public function exists( $key ) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Set a value in the session |
||
| 310 | * @param string|int $key |
||
| 311 | * @param mixed $value |
||
| 312 | */ |
||
| 313 | public function set( $key, $value ) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Remove a value from the session |
||
| 323 | * @param string|int $key |
||
| 324 | */ |
||
| 325 | public function remove( $key ) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Fetch a CSRF token from the session |
||
| 335 | * |
||
| 336 | * Note that this does not persist the session, which you'll probably want |
||
| 337 | * to do if you want the token to actually be useful. |
||
| 338 | * |
||
| 339 | * @param string|string[] $salt Token salt |
||
| 340 | * @param string $key Token key |
||
| 341 | * @return Token |
||
| 342 | */ |
||
| 343 | public function getToken( $salt = '', $key = 'default' ) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Remove a CSRF token from the session |
||
| 365 | * |
||
| 366 | * The next call to self::getToken() with $key will generate a new secret. |
||
| 367 | * |
||
| 368 | * @param string $key Token key |
||
| 369 | */ |
||
| 370 | public function resetToken( $key = 'default' ) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Remove all CSRF tokens from the session |
||
| 380 | */ |
||
| 381 | public function resetAllTokens() { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Fetch the secret keys for self::setSecret() and self::getSecret(). |
||
| 387 | * @return string[] Encryption key, HMAC key |
||
| 388 | */ |
||
| 389 | private function getSecretKeys() { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Decide what type of encryption to use, based on system capabilities. |
||
| 413 | * @return array |
||
| 414 | */ |
||
| 415 | private static function getEncryptionAlgorithm() { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Set a value in the session, encrypted |
||
| 464 | * |
||
| 465 | * This relies on the secrecy of $wgSecretKey (by default), or $wgSessionSecret. |
||
| 466 | * |
||
| 467 | * @param string|int $key |
||
| 468 | * @param mixed $value |
||
| 469 | */ |
||
| 470 | public function setSecret( $key, $value ) { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Fetch a value from the session that was set with self::setSecret() |
||
| 519 | * @param string|int $key |
||
| 520 | * @param mixed $default Returned if $this->exists( $key ) would be false or decryption fails |
||
| 521 | * @return mixed |
||
| 522 | */ |
||
| 523 | public function getSecret( $key, $default = null ) { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Delay automatic saving while multiple updates are being made |
||
| 595 | * |
||
| 596 | * Calls to save() or clear() will not be delayed. |
||
| 597 | * |
||
| 598 | * @return \ScopedCallback When this goes out of scope, a save will be triggered |
||
| 599 | */ |
||
| 600 | public function delaySave() { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Save the session |
||
| 606 | */ |
||
| 607 | public function save() { |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @name Interface methods |
||
| 613 | * @{ |
||
| 614 | */ |
||
| 615 | |||
| 616 | public function count() { |
||
| 620 | |||
| 621 | public function current() { |
||
| 625 | |||
| 626 | public function key() { |
||
| 630 | |||
| 631 | public function next() { |
||
| 635 | |||
| 636 | public function rewind() { |
||
| 640 | |||
| 641 | public function valid() { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @note Despite the name, this seems to be intended to implement isset() |
||
| 648 | * rather than array_key_exists(). So do that. |
||
| 649 | */ |
||
| 650 | public function offsetExists( $offset ) { |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @note This supports indirect modifications but can't mark the session |
||
| 657 | * dirty when those happen. SessionBackend::save() checks the hash of the |
||
| 658 | * data to detect such changes. |
||
| 659 | * @note Accessing a nonexistent key via this mechanism causes that key to |
||
| 660 | * be created with a null value, and does not raise a PHP warning. |
||
| 661 | */ |
||
| 662 | public function &offsetGet( $offset ) { |
||
| 670 | |||
| 671 | public function offsetSet( $offset, $value ) { |
||
| 674 | |||
| 675 | public function offsetUnset( $offset ) { |
||
| 678 | |||
| 679 | /**@}*/ |
||
| 680 | |||
| 681 | } |
||
| 682 |