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 |
||
| 74 | class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Countable |
||
| 75 | { |
||
| 76 | /** |
||
| 77 | * @var string the name of the session variable that stores the flash message data. |
||
| 78 | */ |
||
| 79 | public $flashParam = '__flash'; |
||
| 80 | /** |
||
| 81 | * @var \SessionHandlerInterface|array an object implementing the SessionHandlerInterface or a configuration array. If set, will be used to provide persistency instead of build-in methods. |
||
| 82 | */ |
||
| 83 | public $handler; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var array parameter-value pairs to override default session cookie parameters that are used for session_set_cookie_params() function |
||
| 87 | * Array may have the following possible keys: 'lifetime', 'path', 'domain', 'secure', 'httponly' |
||
| 88 | * @see http://www.php.net/manual/en/function.session-set-cookie-params.php |
||
| 89 | */ |
||
| 90 | private $_cookieParams = ['httponly' => true]; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var $frozenSessionData array|null is used for saving session between recreations due to session parameters update. |
||
| 94 | */ |
||
| 95 | private $frozenSessionData; |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * Initializes the application component. |
||
| 100 | * This method is required by IApplicationComponent and is invoked by application. |
||
| 101 | */ |
||
| 102 | 92 | public function init() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Returns a value indicating whether to use custom session storage. |
||
| 114 | * This method should be overridden to return true by child classes that implement custom session storage. |
||
| 115 | * To implement custom session storage, override these methods: [[openSession()]], [[closeSession()]], |
||
| 116 | * [[readSession()]], [[writeSession()]], [[destroySession()]] and [[gcSession()]]. |
||
| 117 | * @return bool whether to use custom storage. |
||
| 118 | */ |
||
| 119 | 50 | public function getUseCustomStorage() |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Starts the session. |
||
| 126 | */ |
||
| 127 | 52 | public function open() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Registers session handler. |
||
| 151 | * @throws \yii\base\InvalidConfigException |
||
| 152 | */ |
||
| 153 | 51 | protected function registerSessionHandler() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Ends the current session and store session data. |
||
| 188 | */ |
||
| 189 | 75 | public function close() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Frees all session variables and destroys all data registered to a session. |
||
| 198 | * |
||
| 199 | * This method has no effect when session is not [[getIsActive()|active]]. |
||
| 200 | * Make sure to call [[open()]] before calling it. |
||
| 201 | * @see open() |
||
| 202 | * @see isActive |
||
| 203 | */ |
||
| 204 | 1 | public function destroy() |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @return bool whether the session has started |
||
| 219 | */ |
||
| 220 | 92 | public function getIsActive() |
|
| 224 | |||
| 225 | private $_hasSessionId; |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Returns a value indicating whether the current request has sent the session ID. |
||
| 229 | * The default implementation will check cookie and $_GET using the session name. |
||
| 230 | * If you send session ID via other ways, you may need to override this method |
||
| 231 | * or call [[setHasSessionId()]] to explicitly set whether the session ID is sent. |
||
| 232 | * @return bool whether the current request has sent the session ID. |
||
| 233 | */ |
||
| 234 | 22 | public function getHasSessionId() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Sets the value indicating whether the current request has sent the session ID. |
||
| 253 | * This method is provided so that you can override the default way of determining |
||
| 254 | * whether the session ID is sent. |
||
| 255 | * @param bool $value whether the current request has sent the session ID. |
||
| 256 | */ |
||
| 257 | public function setHasSessionId($value) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Gets the session ID. |
||
| 264 | * This is a wrapper for [PHP session_id()](http://php.net/manual/en/function.session-id.php). |
||
| 265 | * @return string the current session ID |
||
| 266 | */ |
||
| 267 | 1 | public function getId() |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Sets the session ID. |
||
| 274 | * This is a wrapper for [PHP session_id()](http://php.net/manual/en/function.session-id.php). |
||
| 275 | * @param string $value the session ID for the current session |
||
| 276 | */ |
||
| 277 | 1 | public function setId($value) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Updates the current session ID with a newly generated one. |
||
| 284 | * |
||
| 285 | * Please refer to <http://php.net/session_regenerate_id> for more details. |
||
| 286 | * |
||
| 287 | * This method has no effect when session is not [[getIsActive()|active]]. |
||
| 288 | * Make sure to call [[open()]] before calling it. |
||
| 289 | * |
||
| 290 | * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
||
| 291 | * @see open() |
||
| 292 | * @see isActive |
||
| 293 | */ |
||
| 294 | public function regenerateID($deleteOldSession = false) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Gets the name of the current session. |
||
| 309 | * This is a wrapper for [PHP session_name()](http://php.net/manual/en/function.session-name.php). |
||
| 310 | * @return string the current session name |
||
| 311 | */ |
||
| 312 | 22 | public function getName() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Sets the name for the current session. |
||
| 319 | * This is a wrapper for [PHP session_name()](http://php.net/manual/en/function.session-name.php). |
||
| 320 | * @param string $value the session name for the current session, must be an alphanumeric string. |
||
| 321 | * It defaults to "PHPSESSID". |
||
| 322 | */ |
||
| 323 | public function setName($value) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Gets the current session save path. |
||
| 330 | * This is a wrapper for [PHP session_save_path()](http://php.net/manual/en/function.session-save-path.php). |
||
| 331 | * @return string the current session save path, defaults to '/tmp'. |
||
| 332 | */ |
||
| 333 | public function getSavePath() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Sets the current session save path. |
||
| 340 | * This is a wrapper for [PHP session_save_path()](http://php.net/manual/en/function.session-save-path.php). |
||
| 341 | * @param string $value the current session save path. This can be either a directory name or a [path alias](guide:concept-aliases). |
||
| 342 | * @throws InvalidArgumentException if the path is not a valid directory |
||
| 343 | */ |
||
| 344 | public function setSavePath($value) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return array the session cookie parameters. |
||
| 356 | * @see http://php.net/manual/en/function.session-get-cookie-params.php |
||
| 357 | */ |
||
| 358 | 51 | public function getCookieParams() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Sets the session cookie parameters. |
||
| 365 | * The cookie parameters passed to this method will be merged with the result |
||
| 366 | * of `session_get_cookie_params()`. |
||
| 367 | * @param array $value cookie parameters, valid keys include: `lifetime`, `path`, `domain`, `secure` and `httponly`. |
||
| 368 | * @throws InvalidArgumentException if the parameters are incomplete. |
||
| 369 | * @see http://us2.php.net/manual/en/function.session-set-cookie-params.php |
||
| 370 | */ |
||
| 371 | public function setCookieParams(array $value) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Sets the session cookie parameters. |
||
| 378 | * This method is called by [[open()]] when it is about to open the session. |
||
| 379 | * @throws InvalidArgumentException if the parameters are incomplete. |
||
| 380 | * @see http://us2.php.net/manual/en/function.session-set-cookie-params.php |
||
| 381 | */ |
||
| 382 | 51 | private function setCookieParamsInternal() |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Returns the value indicating whether cookies should be used to store session IDs. |
||
| 394 | * @return bool|null the value indicating whether cookies should be used to store session IDs. |
||
| 395 | * @see setUseCookies() |
||
| 396 | */ |
||
| 397 | 1 | public function getUseCookies() |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Sets the value indicating whether cookies should be used to store session IDs. |
||
| 410 | * |
||
| 411 | * Three states are possible: |
||
| 412 | * |
||
| 413 | * - true: cookies and only cookies will be used to store session IDs. |
||
| 414 | * - false: cookies will not be used to store session IDs. |
||
| 415 | * - null: if possible, cookies will be used to store session IDs; if not, other mechanisms will be used (e.g. GET parameter) |
||
| 416 | * |
||
| 417 | * @param bool|null $value the value indicating whether cookies should be used to store session IDs. |
||
| 418 | */ |
||
| 419 | 4 | public function setUseCookies($value) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * @return float the probability (percentage) that the GC (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance. |
||
| 437 | */ |
||
| 438 | 1 | public function getGCProbability() |
|
| 442 | |||
| 443 | /** |
||
| 444 | * @param float $value the probability (percentage) that the GC (garbage collection) process is started on every session initialization. |
||
| 445 | * @throws InvalidArgumentException if the value is not between 0 and 100. |
||
| 446 | */ |
||
| 447 | 1 | public function setGCProbability($value) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * @return bool whether transparent sid support is enabled or not, defaults to false. |
||
| 462 | */ |
||
| 463 | 1 | public function getUseTransparentSessionID() |
|
| 467 | |||
| 468 | /** |
||
| 469 | * @param bool $value whether transparent sid support is enabled or not. |
||
| 470 | */ |
||
| 471 | 1 | public function setUseTransparentSessionID($value) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * @return int the number of seconds after which data will be seen as 'garbage' and cleaned up. |
||
| 480 | * The default value is 1440 seconds (or the value of "session.gc_maxlifetime" set in php.ini). |
||
| 481 | */ |
||
| 482 | 21 | public function getTimeout() |
|
| 486 | |||
| 487 | /** |
||
| 488 | * @param int $value the number of seconds after which data will be seen as 'garbage' and cleaned up |
||
| 489 | */ |
||
| 490 | 4 | public function setTimeout($value) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Session open handler. |
||
| 499 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
| 500 | * @internal Do not call this method directly. |
||
| 501 | * @param string $savePath session save path |
||
| 502 | * @param string $sessionName session name |
||
| 503 | * @return bool whether session is opened successfully |
||
| 504 | */ |
||
| 505 | 3 | public function openSession($savePath, $sessionName) |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Session close handler. |
||
| 512 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
| 513 | * @internal Do not call this method directly. |
||
| 514 | * @return bool whether session is closed successfully |
||
| 515 | */ |
||
| 516 | 3 | public function closeSession() |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Session read handler. |
||
| 523 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
| 524 | * @internal Do not call this method directly. |
||
| 525 | * @param string $id session ID |
||
| 526 | * @return string the session data |
||
| 527 | */ |
||
| 528 | public function readSession($id) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Session write handler. |
||
| 535 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
| 536 | * @internal Do not call this method directly. |
||
| 537 | * @param string $id session ID |
||
| 538 | * @param string $data session data |
||
| 539 | * @return bool whether session write is successful |
||
| 540 | */ |
||
| 541 | public function writeSession($id, $data) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Session destroy handler. |
||
| 548 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
| 549 | * @internal Do not call this method directly. |
||
| 550 | * @param string $id session ID |
||
| 551 | * @return bool whether session is destroyed successfully |
||
| 552 | */ |
||
| 553 | public function destroySession($id) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Session GC (garbage collection) handler. |
||
| 560 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
| 561 | * @internal Do not call this method directly. |
||
| 562 | * @param int $maxLifetime the number of seconds after which data will be seen as 'garbage' and cleaned up. |
||
| 563 | * @return bool whether session is GCed successfully |
||
| 564 | */ |
||
| 565 | 1 | public function gcSession($maxLifetime) |
|
| 569 | |||
| 570 | /** |
||
| 571 | * Returns an iterator for traversing the session variables. |
||
| 572 | * This method is required by the interface [[\IteratorAggregate]]. |
||
| 573 | * @return SessionIterator an iterator for traversing the session variables. |
||
| 574 | */ |
||
| 575 | public function getIterator() |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Returns the number of items in the session. |
||
| 583 | * @return int the number of session variables |
||
| 584 | */ |
||
| 585 | public function getCount() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Returns the number of items in the session. |
||
| 593 | * This method is required by [[\Countable]] interface. |
||
| 594 | * @return int number of items in the session. |
||
| 595 | */ |
||
| 596 | public function count() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Returns the session variable value with the session variable name. |
||
| 603 | * If the session variable does not exist, the `$defaultValue` will be returned. |
||
| 604 | * @param string $key the session variable name |
||
| 605 | * @param mixed $defaultValue the default value to be returned when the session variable does not exist. |
||
| 606 | * @return mixed the session variable value, or $defaultValue if the session variable does not exist. |
||
| 607 | */ |
||
| 608 | 52 | public function get($key, $defaultValue = null) |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Adds a session variable. |
||
| 616 | * If the specified name already exists, the old value will be overwritten. |
||
| 617 | * @param string $key session variable name |
||
| 618 | * @param mixed $value session variable value |
||
| 619 | */ |
||
| 620 | 37 | public function set($key, $value) |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Removes a session variable. |
||
| 628 | * @param string $key the name of the session variable to be removed |
||
| 629 | * @return mixed the removed value, null if no such session variable. |
||
| 630 | */ |
||
| 631 | 33 | public function remove($key) |
|
| 643 | |||
| 644 | /** |
||
| 645 | * Removes all session variables. |
||
| 646 | */ |
||
| 647 | 14 | public function removeAll() |
|
| 654 | |||
| 655 | /** |
||
| 656 | * @param mixed $key session variable name |
||
| 657 | * @return bool whether there is the named session variable |
||
| 658 | */ |
||
| 659 | public function has($key) |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Updates the counters for flash messages and removes outdated flash messages. |
||
| 667 | * This method should only be called once in [[init()]]. |
||
| 668 | */ |
||
| 669 | 52 | protected function updateFlashCounters() |
|
| 686 | |||
| 687 | /** |
||
| 688 | * Returns a flash message. |
||
| 689 | * @param string $key the key identifying the flash message |
||
| 690 | * @param mixed $defaultValue value to be returned if the flash message does not exist. |
||
| 691 | * @param bool $delete whether to delete this flash message right after this method is called. |
||
| 692 | * If false, the flash message will be automatically deleted in the next request. |
||
| 693 | * @return mixed the flash message or an array of messages if addFlash was used |
||
| 694 | * @see setFlash() |
||
| 695 | * @see addFlash() |
||
| 696 | * @see hasFlash() |
||
| 697 | * @see getAllFlashes() |
||
| 698 | * @see removeFlash() |
||
| 699 | */ |
||
| 700 | public function getFlash($key, $defaultValue = null, $delete = false) |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Returns all flash messages. |
||
| 721 | * |
||
| 722 | * You may use this method to display all the flash messages in a view file: |
||
| 723 | * |
||
| 724 | * ```php |
||
| 725 | * <?php |
||
| 726 | * foreach (Yii::$app->session->getAllFlashes() as $key => $message) { |
||
| 727 | * echo '<div class="alert alert-' . $key . '">' . $message . '</div>'; |
||
| 728 | * } ?> |
||
| 729 | * ``` |
||
| 730 | * |
||
| 731 | * With the above code you can use the [bootstrap alert][] classes such as `success`, `info`, `danger` |
||
| 732 | * as the flash message key to influence the color of the div. |
||
| 733 | * |
||
| 734 | * Note that if you use [[addFlash()]], `$message` will be an array, and you will have to adjust the above code. |
||
| 735 | * |
||
| 736 | * [bootstrap alert]: http://getbootstrap.com/components/#alerts |
||
| 737 | * |
||
| 738 | * @param bool $delete whether to delete the flash messages right after this method is called. |
||
| 739 | * If false, the flash messages will be automatically deleted in the next request. |
||
| 740 | * @return array flash messages (key => message or key => [message1, message2]). |
||
| 741 | * @see setFlash() |
||
| 742 | * @see addFlash() |
||
| 743 | * @see getFlash() |
||
| 744 | * @see hasFlash() |
||
| 745 | * @see removeFlash() |
||
| 746 | */ |
||
| 747 | public function getAllFlashes($delete = false) |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Sets a flash message. |
||
| 772 | * A flash message will be automatically deleted after it is accessed in a request and the deletion will happen |
||
| 773 | * in the next request. |
||
| 774 | * If there is already an existing flash message with the same key, it will be overwritten by the new one. |
||
| 775 | * @param string $key the key identifying the flash message. Note that flash messages |
||
| 776 | * and normal session variables share the same name space. If you have a normal |
||
| 777 | * session variable using the same name, its value will be overwritten by this method. |
||
| 778 | * @param mixed $value flash message |
||
| 779 | * @param bool $removeAfterAccess whether the flash message should be automatically removed only if |
||
| 780 | * it is accessed. If false, the flash message will be automatically removed after the next request, |
||
| 781 | * regardless if it is accessed or not. If true (default value), the flash message will remain until after |
||
| 782 | * it is accessed. |
||
| 783 | * @see getFlash() |
||
| 784 | * @see addFlash() |
||
| 785 | * @see removeFlash() |
||
| 786 | */ |
||
| 787 | public function setFlash($key, $value = true, $removeAfterAccess = true) |
||
| 794 | |||
| 795 | /** |
||
| 796 | * Adds a flash message. |
||
| 797 | * If there are existing flash messages with the same key, the new one will be appended to the existing message array. |
||
| 798 | * @param string $key the key identifying the flash message. |
||
| 799 | * @param mixed $value flash message |
||
| 800 | * @param bool $removeAfterAccess whether the flash message should be automatically removed only if |
||
| 801 | * it is accessed. If false, the flash message will be automatically removed after the next request, |
||
| 802 | * regardless if it is accessed or not. If true (default value), the flash message will remain until after |
||
| 803 | * it is accessed. |
||
| 804 | * @see getFlash() |
||
| 805 | * @see setFlash() |
||
| 806 | * @see removeFlash() |
||
| 807 | */ |
||
| 808 | public function addFlash($key, $value = true, $removeAfterAccess = true) |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Removes a flash message. |
||
| 826 | * @param string $key the key identifying the flash message. Note that flash messages |
||
| 827 | * and normal session variables share the same name space. If you have a normal |
||
| 828 | * session variable using the same name, it will be removed by this method. |
||
| 829 | * @return mixed the removed flash message. Null if the flash message does not exist. |
||
| 830 | * @see getFlash() |
||
| 831 | * @see setFlash() |
||
| 832 | * @see addFlash() |
||
| 833 | * @see removeAllFlashes() |
||
| 834 | */ |
||
| 835 | public function removeFlash($key) |
||
| 844 | |||
| 845 | /** |
||
| 846 | * Removes all flash messages. |
||
| 847 | * Note that flash messages and normal session variables share the same name space. |
||
| 848 | * If you have a normal session variable using the same name, it will be removed |
||
| 849 | * by this method. |
||
| 850 | * @see getFlash() |
||
| 851 | * @see setFlash() |
||
| 852 | * @see addFlash() |
||
| 853 | * @see removeFlash() |
||
| 854 | */ |
||
| 855 | public function removeAllFlashes() |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Returns a value indicating whether there are flash messages associated with the specified key. |
||
| 866 | * @param string $key key identifying the flash message type |
||
| 867 | * @return bool whether any flash messages exist under specified key |
||
| 868 | */ |
||
| 869 | public function hasFlash($key) |
||
| 873 | |||
| 874 | /** |
||
| 875 | * This method is required by the interface [[\ArrayAccess]]. |
||
| 876 | * @param mixed $offset the offset to check on |
||
| 877 | * @return bool |
||
| 878 | */ |
||
| 879 | public function offsetExists($offset) |
||
| 885 | |||
| 886 | /** |
||
| 887 | * This method is required by the interface [[\ArrayAccess]]. |
||
| 888 | * @param int $offset the offset to retrieve element. |
||
| 889 | * @return mixed the element at the offset, null if no element is found at the offset |
||
| 890 | */ |
||
| 891 | public function offsetGet($offset) |
||
| 897 | |||
| 898 | /** |
||
| 899 | * This method is required by the interface [[\ArrayAccess]]. |
||
| 900 | * @param int $offset the offset to set element |
||
| 901 | * @param mixed $item the element value |
||
| 902 | */ |
||
| 903 | public function offsetSet($offset, $item) |
||
| 908 | |||
| 909 | /** |
||
| 910 | * This method is required by the interface [[\ArrayAccess]]. |
||
| 911 | * @param mixed $offset the offset to unset element |
||
| 912 | */ |
||
| 913 | public function offsetUnset($offset) |
||
| 918 | |||
| 919 | /** |
||
| 920 | * If session is started it's not possible to edit session ini settings. In PHP7.2+ it throws exception. |
||
| 921 | * This function saves session data to temporary variable and stop session. |
||
| 922 | * @since 2.0.14 |
||
| 923 | */ |
||
| 924 | 8 | protected function freeze() |
|
| 934 | |||
| 935 | /** |
||
| 936 | * Starts session and restores data from temporary variable |
||
| 937 | * @since 2.0.14 |
||
| 938 | */ |
||
| 939 | 8 | protected function unfreeze() |
|
| 957 | |||
| 958 | /** |
||
| 959 | * Set cache limiter |
||
| 960 | * |
||
| 961 | * @param string $cacheLimiter |
||
| 962 | * @since 2.0.14 |
||
| 963 | */ |
||
| 964 | 1 | public function setCacheLimiter($cacheLimiter) |
|
| 970 | |||
| 971 | /** |
||
| 972 | * Returns current cache limiter |
||
| 973 | * |
||
| 974 | * @return string current cache limiter |
||
| 975 | * @since 2.0.14 |
||
| 976 | */ |
||
| 977 | public function getCacheLimiter() |
||
| 981 | } |
||
| 982 |
If you suppress an error, we recommend checking for the error condition explicitly: