Complex classes like THttpSession 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 THttpSession, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
76 | class THttpSession extends \Prado\TApplicationComponent implements \IteratorAggregate, \ArrayAccess, \Countable, \Prado\IModule |
||
77 | { |
||
78 | /** |
||
79 | * @var bool whether this module has been initialized |
||
80 | */ |
||
81 | private $_initialized = false; |
||
82 | /** |
||
83 | * @var bool whether the session has started |
||
84 | */ |
||
85 | private $_started = false; |
||
86 | /** |
||
87 | * @var bool whether the session should be started when the module is initialized |
||
88 | */ |
||
89 | private $_autoStart = false; |
||
90 | /** |
||
91 | * @var THttpCookie cookie to be used to store session ID and other data |
||
92 | */ |
||
93 | private $_cookie; |
||
94 | /** |
||
95 | * @var string module id |
||
96 | */ |
||
97 | private $_id; |
||
98 | /** |
||
99 | * @var bool |
||
100 | */ |
||
101 | private $_customStorage = false; |
||
102 | |||
103 | /** |
||
104 | * @return string id of this module |
||
105 | */ |
||
106 | public function getID() |
||
110 | |||
111 | /** |
||
112 | * @param string $value id of this module |
||
113 | */ |
||
114 | public function setID($value) |
||
118 | |||
119 | /** |
||
120 | * Initializes the module. |
||
121 | * This method is required by IModule. |
||
122 | * If AutoStart is true, the session will be started. |
||
123 | * @param TXmlElement $config module configuration |
||
124 | */ |
||
125 | 10 | public function init($config) |
|
126 | { |
||
127 | 10 | if ($this->_autoStart) { |
|
128 | $this->open(); |
||
129 | } |
||
130 | 10 | $this->_initialized = true; |
|
131 | 10 | $this->getApplication()->setSession($this); |
|
132 | 10 | register_shutdown_function([$this, "close"]); |
|
133 | 10 | } |
|
134 | |||
135 | /** |
||
136 | * Starts the session if it has not started yet. |
||
137 | */ |
||
138 | public function open() |
||
153 | |||
154 | /** |
||
155 | * Ends the current session and store session data. |
||
156 | */ |
||
157 | public function close() |
||
164 | |||
165 | /** |
||
166 | * Destroys all data registered to a session. |
||
167 | */ |
||
168 | 1 | public function destroy() |
|
169 | { |
||
170 | 1 | if ($this->_started) { |
|
171 | session_destroy(); |
||
172 | $this->_started = false; |
||
173 | } |
||
174 | 1 | } |
|
175 | |||
176 | /** |
||
177 | * Update the current session id with a newly generated one |
||
178 | * |
||
179 | * @param bool $deleteOld Whether to delete the old associated session or not. |
||
180 | * @return string old session id |
||
181 | * @link http://php.net/manual/en/function.session-regenerate-id.php |
||
182 | */ |
||
183 | public function regenerate($deleteOld = false) |
||
189 | |||
190 | /** |
||
191 | * @return bool whether the session has started |
||
192 | */ |
||
193 | 1 | public function getIsStarted() |
|
194 | { |
||
195 | 1 | return $this->_started; |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * @return string the current session ID |
||
200 | */ |
||
201 | public function getSessionID() |
||
205 | |||
206 | /** |
||
207 | * @param string $value the session ID for the current session |
||
208 | * @throws TInvalidOperationException if session is started already |
||
209 | */ |
||
210 | public function setSessionID($value) |
||
218 | |||
219 | /** |
||
220 | * @return string the current session name |
||
221 | */ |
||
222 | public function getSessionName() |
||
226 | |||
227 | /** |
||
228 | * @param string $value the session name for the current session, must be an alphanumeric string, defaults to PHPSESSID |
||
229 | * @throws TInvalidOperationException if session is started already |
||
230 | */ |
||
231 | public function setSessionName($value) |
||
241 | |||
242 | /** |
||
243 | * @return string the current session save path, defaults to '/tmp'. |
||
244 | */ |
||
245 | public function getSavePath() |
||
249 | |||
250 | /** |
||
251 | * @param string $value the current session save path |
||
252 | * @throws TInvalidOperationException if session is started already |
||
253 | */ |
||
254 | public function setSavePath($value) |
||
266 | |||
267 | /** |
||
268 | * @return bool whether to use user-specified handlers to store session data. Defaults to false. |
||
269 | */ |
||
270 | public function getUseCustomStorage() |
||
274 | |||
275 | /** |
||
276 | * @param bool $value whether to use user-specified handlers to store session data. |
||
277 | * If true, make sure the methods {@link _open}, {@link _close}, {@link _read}, |
||
278 | * {@link _write}, {@link _destroy}, and {@link _gc} are overridden in child |
||
279 | * class, because they will be used as the callback handlers. |
||
280 | */ |
||
281 | 10 | public function setUseCustomStorage($value) |
|
282 | { |
||
283 | 10 | $this->_customStorage = TPropertyValue::ensureBoolean($value); |
|
284 | 10 | } |
|
285 | |||
286 | /** |
||
287 | * @return THttpCookie cookie that will be used to store session ID |
||
288 | */ |
||
289 | public function getCookie() |
||
296 | |||
297 | /** |
||
298 | * @return THttpSessionCookieMode how to use cookie to store session ID. Defaults to THttpSessionCookieMode::Allow. |
||
299 | */ |
||
300 | 3 | public function getCookieMode() |
|
310 | |||
311 | /** |
||
312 | * @param THttpSessionCookieMode $value how to use cookie to store session ID |
||
313 | * @throws TInvalidOperationException if session is started already |
||
314 | */ |
||
315 | 3 | public function setCookieMode($value) |
|
334 | |||
335 | /** |
||
336 | * @return bool whether the session should be automatically started when the session module is initialized, defaults to false. |
||
337 | */ |
||
338 | public function getAutoStart() |
||
342 | |||
343 | /** |
||
344 | * @param bool $value whether the session should be automatically started when the session module is initialized, defaults to false. |
||
345 | * @throws TInvalidOperationException if session is started already |
||
346 | */ |
||
347 | public function setAutoStart($value) |
||
355 | |||
356 | /** |
||
357 | * @return int the probability (percentage) that the gc (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance. |
||
358 | */ |
||
359 | public function getGCProbability() |
||
363 | |||
364 | /** |
||
365 | * @param int $value the probability (percentage) that the gc (garbage collection) process is started on every session initialization. |
||
366 | * @throws TInvalidOperationException if session is started already |
||
367 | * @throws TInvalidDataValueException if the value is beyond [0,100]. |
||
368 | */ |
||
369 | public function setGCProbability($value) |
||
383 | |||
384 | /** |
||
385 | * @return bool whether transparent sid support is enabled or not, defaults to false. |
||
386 | */ |
||
387 | public function getUseTransparentSessionID() |
||
391 | |||
392 | /** |
||
393 | * Ensure that {@link setCookieMode CookieMode} is not set to "None" before enabling |
||
394 | * the use of transparent session ids. Refer to the main documentation of the class |
||
395 | * THttpSession class for a configuration example. |
||
396 | * |
||
397 | * @param bool $value whether transparent sid support is enabled or not. |
||
398 | */ |
||
399 | public function setUseTransparentSessionID($value) |
||
411 | |||
412 | /** |
||
413 | * @return int the number of seconds after which data will be seen as 'garbage' and cleaned up, defaults to 1440 seconds. |
||
414 | */ |
||
415 | public function getTimeout() |
||
419 | |||
420 | /** |
||
421 | * @param int $value the number of seconds after which data will be seen as 'garbage' and cleaned up |
||
422 | * @throws TInvalidOperationException if session is started already |
||
423 | */ |
||
424 | public function setTimeout($value) |
||
432 | |||
433 | /** |
||
434 | * Session open handler. |
||
435 | * This method should be overridden if {@link setUseCustomStorage UseCustomStorage} is set true. |
||
436 | * @param string $savePath session save path |
||
437 | * @param string $sessionName session name |
||
438 | * @return bool whether session is opened successfully |
||
439 | */ |
||
440 | public function _open($savePath, $sessionName) |
||
444 | |||
445 | /** |
||
446 | * Session close handler. |
||
447 | * This method should be overridden if {@link setUseCustomStorage UseCustomStorage} is set true. |
||
448 | * @return bool whether session is closed successfully |
||
449 | */ |
||
450 | public function _close() |
||
454 | |||
455 | /** |
||
456 | * Session read handler. |
||
457 | * This method should be overridden if {@link setUseCustomStorage UseCustomStorage} is set true. |
||
458 | * @param string $id session ID |
||
459 | * @return string the session data |
||
460 | */ |
||
461 | public function _read($id) |
||
465 | |||
466 | /** |
||
467 | * Session write handler. |
||
468 | * This method should be overridden if {@link setUseCustomStorage UseCustomStorage} is set true. |
||
469 | * @param string $id session ID |
||
470 | * @param string $data session data |
||
471 | * @return bool whether session write is successful |
||
472 | */ |
||
473 | public function _write($id, $data) |
||
477 | |||
478 | /** |
||
479 | * Session destroy handler. |
||
480 | * This method should be overridden if {@link setUseCustomStorage UseCustomStorage} is set true. |
||
481 | * @param string $id session ID |
||
482 | * @return bool whether session is destroyed successfully |
||
483 | */ |
||
484 | public function _destroy($id) |
||
488 | |||
489 | /** |
||
490 | * Session GC (garbage collection) handler. |
||
491 | * This method should be overridden if {@link setUseCustomStorage UseCustomStorage} is set true. |
||
492 | * @param int $maxLifetime the number of seconds after which data will be seen as 'garbage' and cleaned up. |
||
493 | * @return bool whether session is GCed successfully |
||
494 | */ |
||
495 | public function _gc($maxLifetime) |
||
499 | |||
500 | //------ The following methods enable THttpSession to be TMap-like ----- |
||
501 | |||
502 | /** |
||
503 | * Returns an iterator for traversing the session variables. |
||
504 | * This method is required by the interface \IteratorAggregate. |
||
505 | * @return TSessionIterator an iterator for traversing the session variables. |
||
506 | */ |
||
507 | public function getIterator() |
||
511 | |||
512 | /** |
||
513 | * @return int the number of session variables |
||
514 | */ |
||
515 | public function getCount() |
||
519 | |||
520 | /** |
||
521 | * Returns the number of items in the session. |
||
522 | * This method is required by \Countable interface. |
||
523 | * @return int number of items in the session. |
||
524 | */ |
||
525 | public function count() |
||
529 | |||
530 | /** |
||
531 | * @return array the list of session variable names |
||
532 | */ |
||
533 | public function getKeys() |
||
537 | |||
538 | /** |
||
539 | * Returns the session variable value with the session variable name. |
||
540 | * This method is exactly the same as {@link offsetGet}. |
||
541 | * @param mixed $key the session variable name |
||
542 | * @return mixed the session variable value, null if no such variable exists |
||
543 | */ |
||
544 | public function itemAt($key) |
||
548 | |||
549 | /** |
||
550 | * Adds a session variable. |
||
551 | * Note, if the specified name already exists, the old value will be removed first. |
||
552 | * @param mixed $key session variable name |
||
553 | * @param mixed $value session variable value |
||
554 | */ |
||
555 | 1 | public function add($key, $value) |
|
556 | { |
||
557 | 1 | $_SESSION[$key] = $value; |
|
558 | 1 | } |
|
559 | |||
560 | /** |
||
561 | * Removes a session variable. |
||
562 | * @param mixed $key the name of the session variable to be removed |
||
563 | * @return mixed the removed value, null if no such session variable. |
||
564 | */ |
||
565 | 1 | public function remove($key) |
|
566 | { |
||
567 | 1 | if (isset($_SESSION[$key])) { |
|
568 | 1 | $value = $_SESSION[$key]; |
|
569 | 1 | unset($_SESSION[$key]); |
|
570 | 1 | return $value; |
|
571 | } else { |
||
572 | return null; |
||
573 | } |
||
574 | } |
||
575 | |||
576 | /** |
||
577 | * Removes all session variables |
||
578 | */ |
||
579 | public function clear() |
||
585 | |||
586 | /** |
||
587 | * @param mixed $key session variable name |
||
588 | * @return bool whether there is the named session variable |
||
589 | */ |
||
590 | public function contains($key) |
||
594 | |||
595 | /** |
||
596 | * @return array the list of all session variables in array |
||
597 | */ |
||
598 | public function toArray() |
||
602 | |||
603 | /** |
||
604 | * This method is required by the interface \ArrayAccess. |
||
605 | * @param mixed $offset the offset to check on |
||
606 | * @return bool |
||
607 | */ |
||
608 | public function offsetExists($offset) |
||
612 | |||
613 | /** |
||
614 | * This method is required by the interface \ArrayAccess. |
||
615 | * @param int $offset the offset to retrieve element. |
||
616 | * @return mixed the element at the offset, null if no element is found at the offset |
||
617 | */ |
||
618 | 4 | public function offsetGet($offset) |
|
619 | { |
||
620 | 4 | return $_SESSION[$offset] ?? null; |
|
621 | } |
||
622 | |||
623 | /** |
||
624 | * This method is required by the interface \ArrayAccess. |
||
625 | * @param int $offset the offset to set element |
||
626 | * @param mixed $item the element value |
||
627 | */ |
||
628 | 2 | public function offsetSet($offset, $item) |
|
629 | { |
||
630 | 2 | $_SESSION[$offset] = $item; |
|
631 | 2 | } |
|
632 | |||
633 | /** |
||
634 | * This method is required by the interface \ArrayAccess. |
||
635 | * @param mixed $offset the offset to unset element |
||
636 | */ |
||
637 | public function offsetUnset($offset) |
||
641 | } |
||
642 |