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 |
||
| 86 | class Session { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var $timeout Set session timeout in seconds. |
||
| 90 | * @config |
||
| 91 | */ |
||
| 92 | private static $timeout = 0; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @config |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | private static $session_ips = array(); |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @config |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | private static $cookie_domain; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @config |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | private static $cookie_path; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @config |
||
| 114 | * @var string |
||
| 115 | */ |
||
| 116 | private static $session_store_path; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @config |
||
| 120 | * @var boolean |
||
| 121 | */ |
||
| 122 | private static $cookie_secure = false; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Session data |
||
| 126 | */ |
||
| 127 | protected $data = array(); |
||
| 128 | |||
| 129 | protected $changedData = array(); |
||
| 130 | |||
| 131 | protected function userAgent() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Start PHP session, then create a new Session object with the given start data. |
||
| 141 | * |
||
| 142 | * @param $data array|Session Can be an array of data (such as $_SESSION) or another Session object to clone. |
||
| 143 | */ |
||
| 144 | public function __construct($data) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Cookie domain, for example 'www.php.net'. |
||
| 153 | * |
||
| 154 | * To make cookies visible on all subdomains then the domain |
||
| 155 | * must be prefixed with a dot like '.php.net'. |
||
| 156 | * |
||
| 157 | * @deprecated 4.0 Use the "Session.cookie_domain" config setting instead |
||
| 158 | * |
||
| 159 | * @param string $domain The domain to set |
||
| 160 | */ |
||
| 161 | public static function set_cookie_domain($domain) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Get the cookie domain. |
||
| 168 | * |
||
| 169 | * @deprecated 4.0 Use the "Session.cookie_domain" config setting instead |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public static function get_cookie_domain() { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Path to set on the domain where the session cookie will work. |
||
| 180 | * Use a single slash ('/') for all paths on the domain. |
||
| 181 | * |
||
| 182 | * @deprecated 4.0 Use the "Session.cookie_path" config setting instead |
||
| 183 | * |
||
| 184 | * @param string $path The path to set |
||
| 185 | */ |
||
| 186 | public static function set_cookie_path($path) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the path on the domain where the session cookie will work. |
||
| 193 | * |
||
| 194 | * @deprecated 4.0 Use the "Session.cookie_path" config setting instead |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public static function get_cookie_path() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Secure cookie, tells the browser to only send it over SSL. |
||
| 209 | * |
||
| 210 | * @deprecated 4.0 Use the "Session.cookie_secure" config setting instead |
||
| 211 | * |
||
| 212 | * @param boolean $secure |
||
| 213 | */ |
||
| 214 | public static function set_cookie_secure($secure) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get if the cookie is secure |
||
| 221 | * |
||
| 222 | * @deprecated 4.0 Use the "Session.cookie_secure" config setting instead |
||
| 223 | * |
||
| 224 | * @return boolean |
||
| 225 | */ |
||
| 226 | public static function get_cookie_secure() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Set the session store path |
||
| 233 | * |
||
| 234 | * @deprecated 4.0 Use the "Session.session_store_path" config setting instead |
||
| 235 | * |
||
| 236 | * @param string $path Filesystem path to the session store |
||
| 237 | */ |
||
| 238 | public static function set_session_store_path($path) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get the session store path |
||
| 245 | * @return string |
||
| 246 | * @deprecated since version 4.0 |
||
| 247 | */ |
||
| 248 | public static function get_session_store_path() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Provide an <code>array</code> of rules specifing timeouts for IPv4 address ranges or |
||
| 255 | * individual IPv4 addresses. The key is an IP address or range and the value is the time |
||
| 256 | * until the session expires in seconds. For example: |
||
| 257 | * |
||
| 258 | * Session::set_timeout_ips(array( |
||
| 259 | * '127.0.0.1' => 36000 |
||
| 260 | * )); |
||
| 261 | * |
||
| 262 | * Any user connecting from 127.0.0.1 (localhost) will have their session expired after 10 hours. |
||
| 263 | * |
||
| 264 | * Session::set_timeout is used to set the timeout value for any users whose address is not in the given IP range. |
||
| 265 | * |
||
| 266 | * @deprecated 4.0 Use the "Session.timeout_ips" config setting instead |
||
| 267 | * |
||
| 268 | * @param array $session_ips Array of IPv4 rules. |
||
| 269 | */ |
||
| 270 | public static function set_timeout_ips($ips) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Add a value to a specific key in the session array |
||
| 277 | */ |
||
| 278 | public static function add_to_array($name, $val) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Set a key/value pair in the session |
||
| 284 | * |
||
| 285 | * @param string $name Key |
||
| 286 | * @param string $val Value |
||
| 287 | */ |
||
| 288 | public static function set($name, $val) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Return a specific value by session key |
||
| 294 | * |
||
| 295 | * @param string $name Key to lookup |
||
| 296 | */ |
||
| 297 | public static function get($name) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Return all the values in session |
||
| 303 | * |
||
| 304 | * @return Array |
||
| 305 | */ |
||
| 306 | public static function get_all() { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Clear a given session key, value pair. |
||
| 312 | * |
||
| 313 | * @param string $name Key to lookup |
||
| 314 | */ |
||
| 315 | public static function clear($name) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Clear all the values |
||
| 321 | * |
||
| 322 | * @return void |
||
| 323 | */ |
||
| 324 | public static function clear_all() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Save all the values in our session to $_SESSION |
||
| 331 | */ |
||
| 332 | public static function save() { |
||
| 335 | |||
| 336 | protected static $default_session = null; |
||
| 337 | |||
| 338 | protected static function current_session() { |
||
| 349 | |||
| 350 | public function inst_start($sid = null) { |
||
| 397 | |||
| 398 | public function inst_destroy($removeCookie = true) { |
||
| 416 | |||
| 417 | public function inst_set($name, $val) { |
||
| 448 | |||
| 449 | public function inst_addToArray($name, $val) { |
||
| 464 | |||
| 465 | public function inst_get($name) { |
||
| 489 | |||
| 490 | public function inst_clear($name) { |
||
| 513 | |||
| 514 | public function inst_clearAll() { |
||
| 521 | |||
| 522 | public function inst_getAll() { |
||
| 525 | |||
| 526 | public function inst_finalize() { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Save data to session |
||
| 532 | * Only save the changes, so that anyone manipulating $_SESSION directly doesn't get burned. |
||
| 533 | */ |
||
| 534 | public function inst_save() { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Recursively apply the changes represented in $data to $dest. |
||
| 548 | * Used to update $_SESSION |
||
| 549 | */ |
||
| 550 | protected function recursivelyApply($data, &$dest) { |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Return the changed data, for debugging purposes. |
||
| 563 | * @return array |
||
| 564 | */ |
||
| 565 | public function inst_changedData() { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Sets the appropriate form message in session, with type. This will be shown once, |
||
| 571 | * for the form specified. |
||
| 572 | * |
||
| 573 | * @param string $formname the form name you wish to use ( usually $form->FormName() ) |
||
| 574 | * @param string $message the message you wish to add to it |
||
| 575 | * @param string $type the type of message |
||
| 576 | */ |
||
| 577 | public static function setFormMessage($formname, $message, $type){ |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Is there a session ID in the request? |
||
| 584 | * @return bool |
||
| 585 | */ |
||
| 586 | public static function request_contains_session_id() { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Initialize session. |
||
| 594 | * |
||
| 595 | * @param string $sid Start the session with a specific ID |
||
| 596 | */ |
||
| 597 | public static function start($sid = null) { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Destroy the active session. |
||
| 603 | * |
||
| 604 | * @param bool $removeCookie If set to TRUE, removes the user's cookie, FALSE does not remove |
||
| 605 | */ |
||
| 606 | public static function destroy($removeCookie = true) { |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Set the timeout of a Session value |
||
| 612 | * |
||
| 613 | * @deprecated 4.0 Use the "Session.timeout" config setting instead |
||
| 614 | * |
||
| 615 | * @param int $timeout Time until a session expires in seconds. Defaults to expire when browser is closed. |
||
| 616 | */ |
||
| 617 | public static function set_timeout($timeout) { |
||
| 621 | |||
| 622 | /** |
||
| 623 | * @deprecated 4.0 Use the "Session.timeout" config setting instead |
||
| 624 | */ |
||
| 625 | public static function get_timeout() { |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Validate the user agent against the current data, resetting the |
||
| 632 | * current session if a mismatch is detected. |
||
| 633 | * |
||
| 634 | * @deprecated 3.0..4.0 Removed in 4.0 |
||
| 635 | * @return bool If user agent has been set against this session, returns |
||
| 636 | * the valid state of this session as either true or false. If the agent |
||
| 637 | * isn't set it is assumed valid and returns true. |
||
| 638 | */ |
||
| 639 | private function expireIfInvalid() { |
||
| 656 | } |
||
| 657 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: