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 | * @see Cookie |
||
| 87 | * @todo This class is currently really basic and could do with a more well-thought-out implementation. |
||
| 88 | * |
||
| 89 | * @package framework |
||
| 90 | * @subpackage control |
||
| 91 | */ |
||
| 92 | |||
| 93 | class Session { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var $timeout Set session timeout in seconds. |
||
| 97 | * @config |
||
| 98 | */ |
||
| 99 | private static $timeout = 0; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @config |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | private static $session_ips = array(); |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @config |
||
| 109 | * @var string |
||
| 110 | */ |
||
| 111 | private static $cookie_domain; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @config |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | private static $cookie_path; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @config |
||
| 121 | * @var string |
||
| 122 | */ |
||
| 123 | private static $session_store_path; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @config |
||
| 127 | * @var boolean |
||
| 128 | */ |
||
| 129 | private static $cookie_secure = false; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Session data |
||
| 133 | */ |
||
| 134 | protected $data = array(); |
||
| 135 | |||
| 136 | protected $changedData = array(); |
||
| 137 | |||
| 138 | protected function userAgent() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Start PHP session, then create a new Session object with the given start data. |
||
| 148 | * |
||
| 149 | * @param $data array|Session Can be an array of data (such as $_SESSION) or another Session object to clone. |
||
| 150 | */ |
||
| 151 | public function __construct($data) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Cookie domain, for example 'www.php.net'. |
||
| 168 | * |
||
| 169 | * To make cookies visible on all subdomains then the domain |
||
| 170 | * must be prefixed with a dot like '.php.net'. |
||
| 171 | * |
||
| 172 | * @deprecated 4.0 Use the "Session.cookie_domain" config setting instead |
||
| 173 | * |
||
| 174 | * @param string $domain The domain to set |
||
| 175 | */ |
||
| 176 | public static function set_cookie_domain($domain) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get the cookie domain. |
||
| 183 | * |
||
| 184 | * @deprecated 4.0 Use the "Session.cookie_domain" config setting instead |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public static function get_cookie_domain() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Path to set on the domain where the session cookie will work. |
||
| 195 | * Use a single slash ('/') for all paths on the domain. |
||
| 196 | * |
||
| 197 | * @deprecated 4.0 Use the "Session.cookie_path" config setting instead |
||
| 198 | * |
||
| 199 | * @param string $path The path to set |
||
| 200 | */ |
||
| 201 | public static function set_cookie_path($path) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get the path on the domain where the session cookie will work. |
||
| 208 | * |
||
| 209 | * @deprecated 4.0 Use the "Session.cookie_path" config setting instead |
||
| 210 | * |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | public static function get_cookie_path() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Secure cookie, tells the browser to only send it over SSL. |
||
| 224 | * |
||
| 225 | * @deprecated 4.0 Use the "Session.cookie_secure" config setting instead |
||
| 226 | * |
||
| 227 | * @param boolean $secure |
||
| 228 | */ |
||
| 229 | public static function set_cookie_secure($secure) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get if the cookie is secure |
||
| 236 | * |
||
| 237 | * @deprecated 4.0 Use the "Session.cookie_secure" config setting instead |
||
| 238 | * |
||
| 239 | * @return boolean |
||
| 240 | */ |
||
| 241 | public static function get_cookie_secure() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Set the session store path |
||
| 248 | * |
||
| 249 | * @deprecated 4.0 Use the "Session.session_store_path" config setting instead |
||
| 250 | * |
||
| 251 | * @param string $path Filesystem path to the session store |
||
| 252 | */ |
||
| 253 | public static function set_session_store_path($path) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get the session store path |
||
| 260 | * @return string |
||
| 261 | * @deprecated since version 4.0 |
||
| 262 | */ |
||
| 263 | public static function get_session_store_path() { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Provide an <code>array</code> of rules specifing timeouts for IPv4 address ranges or |
||
| 270 | * individual IPv4 addresses. The key is an IP address or range and the value is the time |
||
| 271 | * until the session expires in seconds. For example: |
||
| 272 | * |
||
| 273 | * Session::set_timeout_ips(array( |
||
| 274 | * '127.0.0.1' => 36000 |
||
| 275 | * )); |
||
| 276 | * |
||
| 277 | * Any user connecting from 127.0.0.1 (localhost) will have their session expired after 10 hours. |
||
| 278 | * |
||
| 279 | * Session::set_timeout is used to set the timeout value for any users whose address is not in the given IP range. |
||
| 280 | * |
||
| 281 | * @deprecated 4.0 Use the "Session.timeout_ips" config setting instead |
||
| 282 | * |
||
| 283 | * @param array $session_ips Array of IPv4 rules. |
||
|
|
|||
| 284 | */ |
||
| 285 | public static function set_timeout_ips($ips) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Add a value to a specific key in the session array |
||
| 292 | */ |
||
| 293 | public static function add_to_array($name, $val) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Set a key/value pair in the session |
||
| 299 | * |
||
| 300 | * @param string $name Key |
||
| 301 | * @param string $val Value |
||
| 302 | */ |
||
| 303 | public static function set($name, $val) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Return a specific value by session key |
||
| 309 | * |
||
| 310 | * @param string $name Key to lookup |
||
| 311 | */ |
||
| 312 | public static function get($name) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Return all the values in session |
||
| 318 | * |
||
| 319 | * @return Array |
||
| 320 | */ |
||
| 321 | public static function get_all() { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Clear a given session key, value pair. |
||
| 327 | * |
||
| 328 | * @param string $name Key to lookup |
||
| 329 | */ |
||
| 330 | public static function clear($name) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Clear all the values |
||
| 336 | * |
||
| 337 | * @return void |
||
| 338 | */ |
||
| 339 | public static function clear_all() { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Save all the values in our session to $_SESSION |
||
| 346 | */ |
||
| 347 | public static function save() { |
||
| 350 | |||
| 351 | protected static $default_session = null; |
||
| 352 | |||
| 353 | protected static function current_session() { |
||
| 364 | |||
| 365 | public function inst_start($sid = null) { |
||
| 400 | |||
| 401 | public function inst_destroy($removeCookie = true) { |
||
| 419 | |||
| 420 | public function inst_set($name, $val) { |
||
| 451 | |||
| 452 | public function inst_addToArray($name, $val) { |
||
| 467 | |||
| 468 | public function inst_get($name) { |
||
| 492 | |||
| 493 | public function inst_clear($name) { |
||
| 516 | |||
| 517 | public function inst_clearAll() { |
||
| 524 | |||
| 525 | public function inst_getAll() { |
||
| 528 | |||
| 529 | public function inst_finalize() { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Save data to session |
||
| 535 | * Only save the changes, so that anyone manipulating $_SESSION directly doesn't get burned. |
||
| 536 | */ |
||
| 537 | public function inst_save() { |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Recursively apply the changes represented in $data to $dest. |
||
| 551 | * Used to update $_SESSION |
||
| 552 | */ |
||
| 553 | protected function recursivelyApply($data, &$dest) { |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Return the changed data, for debugging purposes. |
||
| 566 | * @return array |
||
| 567 | */ |
||
| 568 | public function inst_changedData() { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Sets the appropriate form message in session, with type. This will be shown once, |
||
| 574 | * for the form specified. |
||
| 575 | * |
||
| 576 | * @param string $formname the form name you wish to use ( usually $form->FormName() ) |
||
| 577 | * @param string $message the message you wish to add to it |
||
| 578 | * @param string $type the type of message |
||
| 579 | */ |
||
| 580 | public static function setFormMessage($formname, $message, $type) { |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Is there a session ID in the request? |
||
| 587 | * @return bool |
||
| 588 | */ |
||
| 589 | public static function request_contains_session_id() { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Initialize session. |
||
| 597 | * |
||
| 598 | * @param string $sid Start the session with a specific ID |
||
| 599 | */ |
||
| 600 | public static function start($sid = null) { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Destroy the active session. |
||
| 606 | * |
||
| 607 | * @param bool $removeCookie If set to TRUE, removes the user's cookie, FALSE does not remove |
||
| 608 | */ |
||
| 609 | public static function destroy($removeCookie = true) { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Set the timeout of a Session value |
||
| 615 | * |
||
| 616 | * @deprecated 4.0 Use the "Session.timeout" config setting instead |
||
| 617 | * |
||
| 618 | * @param int $timeout Time until a session expires in seconds. Defaults to expire when browser is closed. |
||
| 619 | */ |
||
| 620 | public static function set_timeout($timeout) { |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @deprecated 4.0 Use the "Session.timeout" config setting instead |
||
| 633 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.