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 |
||
73 | class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Countable |
||
74 | { |
||
75 | /** |
||
76 | * @var string the name of the session variable that stores the flash message data. |
||
77 | */ |
||
78 | public $flashParam = '__flash'; |
||
79 | /** |
||
80 | * @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. |
||
81 | */ |
||
82 | public $handler; |
||
83 | |||
84 | /** |
||
85 | * @var array parameter-value pairs to override default session cookie parameters that are used for session_set_cookie_params() function |
||
86 | * Array may have the following possible keys: 'lifetime', 'path', 'domain', 'secure', 'httponly' |
||
87 | * @see http://www.php.net/manual/en/function.session-set-cookie-params.php |
||
88 | */ |
||
89 | private $_cookieParams = ['httponly' => true]; |
||
90 | |||
91 | |||
92 | /** |
||
93 | * Initializes the application component. |
||
94 | * This method is required by IApplicationComponent and is invoked by application. |
||
95 | */ |
||
96 | 32 | public function init() |
|
105 | |||
106 | /** |
||
107 | * Returns a value indicating whether to use custom session storage. |
||
108 | * This method should be overridden to return true by child classes that implement custom session storage. |
||
109 | * To implement custom session storage, override these methods: [[openSession()]], [[closeSession()]], |
||
110 | * [[readSession()]], [[writeSession()]], [[destroySession()]] and [[gcSession()]]. |
||
111 | * @return boolean whether to use custom storage. |
||
112 | */ |
||
113 | 19 | public function getUseCustomStorage() |
|
117 | |||
118 | /** |
||
119 | * Starts the session. |
||
120 | */ |
||
121 | 19 | public function open() |
|
142 | |||
143 | /** |
||
144 | * Registers session handler. |
||
145 | * @throws \yii\base\InvalidConfigException |
||
146 | */ |
||
147 | 19 | protected function registerSessionHandler() |
|
179 | |||
180 | /** |
||
181 | * Ends the current session and store session data. |
||
182 | */ |
||
183 | 27 | public function close() |
|
189 | |||
190 | /** |
||
191 | * Frees all session variables and destroys all data registered to a session. |
||
192 | */ |
||
193 | 1 | public function destroy() |
|
205 | |||
206 | /** |
||
207 | * @return boolean whether the session has started |
||
208 | */ |
||
209 | 32 | public function getIsActive() |
|
213 | |||
214 | private $_hasSessionId; |
||
215 | |||
216 | /** |
||
217 | * Returns a value indicating whether the current request has sent the session ID. |
||
218 | * The default implementation will check cookie and $_GET using the session name. |
||
219 | * If you send session ID via other ways, you may need to override this method |
||
220 | * or call [[setHasSessionId()]] to explicitly set whether the session ID is sent. |
||
221 | * @return boolean whether the current request has sent the session ID. |
||
222 | */ |
||
223 | 9 | public function getHasSessionId() |
|
239 | |||
240 | /** |
||
241 | * Sets the value indicating whether the current request has sent the session ID. |
||
242 | * This method is provided so that you can override the default way of determining |
||
243 | * whether the session ID is sent. |
||
244 | * @param boolean $value whether the current request has sent the session ID. |
||
245 | */ |
||
246 | public function setHasSessionId($value) |
||
250 | |||
251 | /** |
||
252 | * Gets the session ID. |
||
253 | * This is a wrapper for [PHP session_id()](http://php.net/manual/en/function.session-id.php). |
||
254 | * @return string the current session ID |
||
255 | */ |
||
256 | public function getId() |
||
260 | |||
261 | /** |
||
262 | * Sets the session ID. |
||
263 | * This is a wrapper for [PHP session_id()](http://php.net/manual/en/function.session-id.php). |
||
264 | * @param string $value the session ID for the current session |
||
265 | */ |
||
266 | 1 | public function setId($value) |
|
270 | |||
271 | /** |
||
272 | * Updates the current session ID with a newly generated one . |
||
273 | * Please refer to <http://php.net/session_regenerate_id> for more details. |
||
274 | * @param boolean $deleteOldSession Whether to delete the old associated session file or not. |
||
275 | */ |
||
276 | 13 | public function regenerateID($deleteOldSession = false) |
|
288 | |||
289 | /** |
||
290 | * Gets the name of the current session. |
||
291 | * This is a wrapper for [PHP session_name()](http://php.net/manual/en/function.session-name.php). |
||
292 | * @return string the current session name |
||
293 | */ |
||
294 | 9 | public function getName() |
|
298 | |||
299 | /** |
||
300 | * Sets the name for the current session. |
||
301 | * This is a wrapper for [PHP session_name()](http://php.net/manual/en/function.session-name.php). |
||
302 | * @param string $value the session name for the current session, must be an alphanumeric string. |
||
303 | * It defaults to "PHPSESSID". |
||
304 | */ |
||
305 | public function setName($value) |
||
309 | |||
310 | /** |
||
311 | * Gets the current session save path. |
||
312 | * This is a wrapper for [PHP session_save_path()](http://php.net/manual/en/function.session-save-path.php). |
||
313 | * @return string the current session save path, defaults to '/tmp'. |
||
314 | */ |
||
315 | public function getSavePath() |
||
319 | |||
320 | /** |
||
321 | * Sets the current session save path. |
||
322 | * This is a wrapper for [PHP session_save_path()](http://php.net/manual/en/function.session-save-path.php). |
||
323 | * @param string $value the current session save path. This can be either a directory name or a path alias. |
||
324 | * @throws InvalidParamException if the path is not a valid directory |
||
325 | */ |
||
326 | public function setSavePath($value) |
||
335 | |||
336 | /** |
||
337 | * @return array the session cookie parameters. |
||
338 | * @see http://php.net/manual/en/function.session-get-cookie-params.php |
||
339 | */ |
||
340 | 19 | public function getCookieParams() |
|
344 | |||
345 | /** |
||
346 | * Sets the session cookie parameters. |
||
347 | * The cookie parameters passed to this method will be merged with the result |
||
348 | * of `session_get_cookie_params()`. |
||
349 | * @param array $value cookie parameters, valid keys include: `lifetime`, `path`, `domain`, `secure` and `httponly`. |
||
350 | * @throws InvalidParamException if the parameters are incomplete. |
||
351 | * @see http://us2.php.net/manual/en/function.session-set-cookie-params.php |
||
352 | */ |
||
353 | public function setCookieParams(array $value) |
||
357 | |||
358 | /** |
||
359 | * Sets the session cookie parameters. |
||
360 | * This method is called by [[open()]] when it is about to open the session. |
||
361 | * @throws InvalidParamException if the parameters are incomplete. |
||
362 | * @see http://us2.php.net/manual/en/function.session-set-cookie-params.php |
||
363 | */ |
||
364 | 19 | private function setCookieParamsInternal() |
|
373 | |||
374 | /** |
||
375 | * Returns the value indicating whether cookies should be used to store session IDs. |
||
376 | * @return boolean|null the value indicating whether cookies should be used to store session IDs. |
||
377 | * @see setUseCookies() |
||
378 | */ |
||
379 | public function getUseCookies() |
||
389 | |||
390 | /** |
||
391 | * Sets the value indicating whether cookies should be used to store session IDs. |
||
392 | * Three states are possible: |
||
393 | * |
||
394 | * - true: cookies and only cookies will be used to store session IDs. |
||
395 | * - false: cookies will not be used to store session IDs. |
||
396 | * - null: if possible, cookies will be used to store session IDs; if not, other mechanisms will be used (e.g. GET parameter) |
||
397 | * |
||
398 | * @param boolean|null $value the value indicating whether cookies should be used to store session IDs. |
||
399 | */ |
||
400 | public function setUseCookies($value) |
||
413 | |||
414 | /** |
||
415 | * @return float the probability (percentage) that the GC (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance. |
||
416 | */ |
||
417 | public function getGCProbability() |
||
421 | |||
422 | /** |
||
423 | * @param float $value the probability (percentage) that the GC (garbage collection) process is started on every session initialization. |
||
424 | * @throws InvalidParamException if the value is not between 0 and 100. |
||
425 | */ |
||
426 | public function setGCProbability($value) |
||
436 | |||
437 | /** |
||
438 | * @return boolean whether transparent sid support is enabled or not, defaults to false. |
||
439 | */ |
||
440 | public function getUseTransparentSessionID() |
||
444 | |||
445 | /** |
||
446 | * @param boolean $value whether transparent sid support is enabled or not. |
||
447 | */ |
||
448 | public function setUseTransparentSessionID($value) |
||
452 | |||
453 | /** |
||
454 | * @return integer the number of seconds after which data will be seen as 'garbage' and cleaned up. |
||
455 | * The default value is 1440 seconds (or the value of "session.gc_maxlifetime" set in php.ini). |
||
456 | */ |
||
457 | 4 | public function getTimeout() |
|
461 | |||
462 | /** |
||
463 | * @param integer $value the number of seconds after which data will be seen as 'garbage' and cleaned up |
||
464 | */ |
||
465 | public function setTimeout($value) |
||
469 | |||
470 | /** |
||
471 | * Session open handler. |
||
472 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
473 | * Do not call this method directly. |
||
474 | * @param string $savePath session save path |
||
475 | * @param string $sessionName session name |
||
476 | * @return boolean whether session is opened successfully |
||
477 | */ |
||
478 | public function openSession($savePath, $sessionName) |
||
482 | |||
483 | /** |
||
484 | * Session close handler. |
||
485 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
486 | * Do not call this method directly. |
||
487 | * @return boolean whether session is closed successfully |
||
488 | */ |
||
489 | public function closeSession() |
||
493 | |||
494 | /** |
||
495 | * Session read handler. |
||
496 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
497 | * Do not call this method directly. |
||
498 | * @param string $id session ID |
||
499 | * @return string the session data |
||
500 | */ |
||
501 | public function readSession($id) |
||
505 | |||
506 | /** |
||
507 | * Session write handler. |
||
508 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
509 | * Do not call this method directly. |
||
510 | * @param string $id session ID |
||
511 | * @param string $data session data |
||
512 | * @return boolean whether session write is successful |
||
513 | */ |
||
514 | public function writeSession($id, $data) |
||
518 | |||
519 | /** |
||
520 | * Session destroy handler. |
||
521 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
522 | * Do not call this method directly. |
||
523 | * @param string $id session ID |
||
524 | * @return boolean whether session is destroyed successfully |
||
525 | */ |
||
526 | public function destroySession($id) |
||
530 | |||
531 | /** |
||
532 | * Session GC (garbage collection) handler. |
||
533 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
534 | * Do not call this method directly. |
||
535 | * @param integer $maxLifetime the number of seconds after which data will be seen as 'garbage' and cleaned up. |
||
536 | * @return boolean whether session is GCed successfully |
||
537 | */ |
||
538 | public function gcSession($maxLifetime) |
||
542 | |||
543 | /** |
||
544 | * Returns an iterator for traversing the session variables. |
||
545 | * This method is required by the interface [[\IteratorAggregate]]. |
||
546 | * @return SessionIterator an iterator for traversing the session variables. |
||
547 | */ |
||
548 | public function getIterator() |
||
553 | |||
554 | /** |
||
555 | * Returns the number of items in the session. |
||
556 | * @return integer the number of session variables |
||
557 | */ |
||
558 | public function getCount() |
||
563 | |||
564 | /** |
||
565 | * Returns the number of items in the session. |
||
566 | * This method is required by [[\Countable]] interface. |
||
567 | * @return integer number of items in the session. |
||
568 | */ |
||
569 | public function count() |
||
573 | |||
574 | /** |
||
575 | * Returns the session variable value with the session variable name. |
||
576 | * If the session variable does not exist, the `$defaultValue` will be returned. |
||
577 | * @param string $key the session variable name |
||
578 | * @param mixed $defaultValue the default value to be returned when the session variable does not exist. |
||
579 | * @return mixed the session variable value, or $defaultValue if the session variable does not exist. |
||
580 | */ |
||
581 | 19 | public function get($key, $defaultValue = null) |
|
586 | |||
587 | /** |
||
588 | * Adds a session variable. |
||
589 | * If the specified name already exists, the old value will be overwritten. |
||
590 | * @param string $key session variable name |
||
591 | * @param mixed $value session variable value |
||
592 | */ |
||
593 | 16 | public function set($key, $value) |
|
598 | |||
599 | /** |
||
600 | * Removes a session variable. |
||
601 | * @param string $key the name of the session variable to be removed |
||
602 | * @return mixed the removed value, null if no such session variable. |
||
603 | */ |
||
604 | 13 | public function remove($key) |
|
616 | |||
617 | /** |
||
618 | * Removes all session variables |
||
619 | */ |
||
620 | 3 | public function removeAll() |
|
627 | |||
628 | /** |
||
629 | * @param mixed $key session variable name |
||
630 | * @return boolean whether there is the named session variable |
||
631 | */ |
||
632 | public function has($key) |
||
637 | |||
638 | /** |
||
639 | * Updates the counters for flash messages and removes outdated flash messages. |
||
640 | * This method should only be called once in [[init()]]. |
||
641 | */ |
||
642 | 19 | protected function updateFlashCounters() |
|
659 | |||
660 | /** |
||
661 | * Returns a flash message. |
||
662 | * @param string $key the key identifying the flash message |
||
663 | * @param mixed $defaultValue value to be returned if the flash message does not exist. |
||
664 | * @param boolean $delete whether to delete this flash message right after this method is called. |
||
665 | * If false, the flash message will be automatically deleted in the next request. |
||
666 | * @return mixed the flash message or an array of messages if addFlash was used |
||
667 | * @see setFlash() |
||
668 | * @see addFlash() |
||
669 | * @see hasFlash() |
||
670 | * @see getAllFlashes() |
||
671 | * @see removeFlash() |
||
672 | */ |
||
673 | public function getFlash($key, $defaultValue = null, $delete = false) |
||
691 | |||
692 | /** |
||
693 | * Returns all flash messages. |
||
694 | * |
||
695 | * You may use this method to display all the flash messages in a view file: |
||
696 | * |
||
697 | * ```php |
||
698 | * <?php |
||
699 | * foreach (Yii::$app->session->getAllFlashes() as $key => $message) { |
||
700 | * echo '<div class="alert alert-' . $key . '">' . $message . '</div>'; |
||
701 | * } ?> |
||
702 | * ``` |
||
703 | * |
||
704 | * With the above code you can use the [bootstrap alert][] classes such as `success`, `info`, `danger` |
||
705 | * as the flash message key to influence the color of the div. |
||
706 | * |
||
707 | * Note that if you use [[addFlash()]], `$message` will be an array, and you will have to adjust the above code. |
||
708 | * |
||
709 | * [bootstrap alert]: http://getbootstrap.com/components/#alerts |
||
710 | * |
||
711 | * @param boolean $delete whether to delete the flash messages right after this method is called. |
||
712 | * If false, the flash messages will be automatically deleted in the next request. |
||
713 | * @return array flash messages (key => message or key => [message1, message2]). |
||
714 | * @see setFlash() |
||
715 | * @see addFlash() |
||
716 | * @see getFlash() |
||
717 | * @see hasFlash() |
||
718 | * @see removeFlash() |
||
719 | */ |
||
720 | public function getAllFlashes($delete = false) |
||
742 | |||
743 | /** |
||
744 | * Sets a flash message. |
||
745 | * A flash message will be automatically deleted after it is accessed in a request and the deletion will happen |
||
746 | * in the next request. |
||
747 | * If there is already an existing flash message with the same key, it will be overwritten by the new one. |
||
748 | * @param string $key the key identifying the flash message. Note that flash messages |
||
749 | * and normal session variables share the same name space. If you have a normal |
||
750 | * session variable using the same name, its value will be overwritten by this method. |
||
751 | * @param mixed $value flash message |
||
752 | * @param boolean $removeAfterAccess whether the flash message should be automatically removed only if |
||
753 | * it is accessed. If false, the flash message will be automatically removed after the next request, |
||
754 | * regardless if it is accessed or not. If true (default value), the flash message will remain until after |
||
755 | * it is accessed. |
||
756 | * @see getFlash() |
||
757 | * @see addFlash() |
||
758 | * @see removeFlash() |
||
759 | */ |
||
760 | public function setFlash($key, $value = true, $removeAfterAccess = true) |
||
767 | |||
768 | /** |
||
769 | * Adds a flash message. |
||
770 | * If there are existing flash messages with the same key, the new one will be appended to the existing message array. |
||
771 | * @param string $key the key identifying the flash message. |
||
772 | * @param mixed $value flash message |
||
773 | * @param boolean $removeAfterAccess whether the flash message should be automatically removed only if |
||
774 | * it is accessed. If false, the flash message will be automatically removed after the next request, |
||
775 | * regardless if it is accessed or not. If true (default value), the flash message will remain until after |
||
776 | * it is accessed. |
||
777 | * @see getFlash() |
||
778 | * @see setFlash() |
||
779 | * @see removeFlash() |
||
780 | */ |
||
781 | public function addFlash($key, $value = true, $removeAfterAccess = true) |
||
796 | |||
797 | /** |
||
798 | * Removes a flash message. |
||
799 | * @param string $key the key identifying the flash message. Note that flash messages |
||
800 | * and normal session variables share the same name space. If you have a normal |
||
801 | * session variable using the same name, it will be removed by this method. |
||
802 | * @return mixed the removed flash message. Null if the flash message does not exist. |
||
803 | * @see getFlash() |
||
804 | * @see setFlash() |
||
805 | * @see addFlash() |
||
806 | * @see removeAllFlashes() |
||
807 | */ |
||
808 | public function removeFlash($key) |
||
817 | |||
818 | /** |
||
819 | * Removes all flash messages. |
||
820 | * Note that flash messages and normal session variables share the same name space. |
||
821 | * If you have a normal session variable using the same name, it will be removed |
||
822 | * by this method. |
||
823 | * @see getFlash() |
||
824 | * @see setFlash() |
||
825 | * @see addFlash() |
||
826 | * @see removeFlash() |
||
827 | */ |
||
828 | public function removeAllFlashes() |
||
836 | |||
837 | /** |
||
838 | * Returns a value indicating whether there are flash messages associated with the specified key. |
||
839 | * @param string $key key identifying the flash message type |
||
840 | * @return boolean whether any flash messages exist under specified key |
||
841 | */ |
||
842 | public function hasFlash($key) |
||
846 | |||
847 | /** |
||
848 | * This method is required by the interface [[\ArrayAccess]]. |
||
849 | * @param mixed $offset the offset to check on |
||
850 | * @return boolean |
||
851 | */ |
||
852 | public function offsetExists($offset) |
||
858 | |||
859 | /** |
||
860 | * This method is required by the interface [[\ArrayAccess]]. |
||
861 | * @param integer $offset the offset to retrieve element. |
||
862 | * @return mixed the element at the offset, null if no element is found at the offset |
||
863 | */ |
||
864 | public function offsetGet($offset) |
||
870 | |||
871 | /** |
||
872 | * This method is required by the interface [[\ArrayAccess]]. |
||
873 | * @param integer $offset the offset to set element |
||
874 | * @param mixed $item the element value |
||
875 | */ |
||
876 | public function offsetSet($offset, $item) |
||
881 | |||
882 | /** |
||
883 | * This method is required by the interface [[\ArrayAccess]]. |
||
884 | * @param mixed $offset the offset to unset element |
||
885 | */ |
||
886 | public function offsetUnset($offset) |
||
891 | } |
||
892 |
If you suppress an error, we recommend checking for the error condition explicitly: