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 |
||
| 9 | class Session implements \ArrayAccess |
||
| 10 | { |
||
| 11 | use EmitterAwareTrait; |
||
| 12 | |||
| 13 | const DEFAULT_NAMESPACE = 'default'; |
||
| 14 | const METADATA_NAMESPACE = '__metadata__'; |
||
| 15 | |||
| 16 | /** @var Id\Handler */ |
||
| 17 | private $idHandler; |
||
| 18 | |||
| 19 | /** @var int Number of requests after which id is regeneratd */ |
||
| 20 | private $idRequestsLimit = null; |
||
| 21 | |||
| 22 | /** @var int Time after id is regenerated */ |
||
| 23 | private $idTtl = 1440; |
||
| 24 | |||
| 25 | /** @var bool */ |
||
| 26 | private $idRegenerated; |
||
| 27 | |||
| 28 | /** @var int */ |
||
| 29 | private $regenerationTrace; |
||
| 30 | |||
| 31 | /** @var StoreInterface */ |
||
| 32 | private $store; |
||
| 33 | |||
| 34 | /** @var bool|mixed Session values */ |
||
| 35 | private $values = array(); |
||
| 36 | |||
| 37 | /** @var int Specifies the number of seconds after which session will be automatically expired */ |
||
| 38 | private $ttl = 1440; |
||
| 39 | |||
| 40 | /** @var int First trace (timestamp), time when session was created */ |
||
| 41 | private $firstTrace; |
||
| 42 | |||
| 43 | /** @var int Last trace (Unix timestamp) */ |
||
| 44 | private $lastTrace; |
||
| 45 | |||
| 46 | /** @var int */ |
||
| 47 | private $requestsCount; |
||
| 48 | |||
| 49 | /** @var FingerprintGeneratorInterface[] */ |
||
| 50 | private $fingerprintGenerators = array(); |
||
| 51 | |||
| 52 | /** @var string */ |
||
| 53 | private $fingerprint = ''; |
||
| 54 | |||
| 55 | /** @var bool Is session opened? */ |
||
| 56 | private $opened = false; |
||
| 57 | |||
| 58 | /** @var SessionFlash */ |
||
| 59 | private $flash; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param StoreInterface $store |
||
| 63 | */ |
||
| 64 | public function __construct(StoreInterface $store) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Creates new session |
||
| 74 | * |
||
| 75 | * It should be called only once at the beginning. If called for existing |
||
| 76 | * session it ovewrites it (clears all values etc). |
||
| 77 | * It can be replaced with {@link self::open()} (called with "true" argument) |
||
| 78 | * |
||
| 79 | * @return bool Session opened? |
||
| 80 | */ |
||
| 81 | public function create() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Opens the session (for a given request) |
||
| 102 | * |
||
| 103 | * If session hasn't been created earlier with {@link self::create()} method then: |
||
| 104 | * - if argument is set to true, session will be created implicitly (behaves |
||
| 105 | * like PHP's native session_start()), |
||
| 106 | * - otherwise, session won't be created and apprporiate listeners will be notified. |
||
| 107 | * |
||
| 108 | * If called earlier, then second (and next ones) call does nothing |
||
| 109 | * |
||
| 110 | * @param bool $createNewIfNotExists Create new session if not exists earlier? |
||
| 111 | * @return bool Session opened? |
||
| 112 | */ |
||
| 113 | public function open($createNewIfNotExists = false) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Is session opened? |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function isOpened() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Alias of {@link self::isOpened()}. |
||
| 149 | * |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | public function isOpen() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Is session expired? |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function isExpired() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Close the session. |
||
| 169 | */ |
||
| 170 | public function close() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Destroy the session. |
||
| 187 | */ |
||
| 188 | public function destroy() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get session identifier |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function getId() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Regenerates session id. |
||
| 207 | * |
||
| 208 | * Destroys current session in store and generates new id, which will be saved |
||
| 209 | * at the end of script execution (together with values). |
||
| 210 | * |
||
| 211 | * Id is regenerated at the most once per script execution (even if called a few times). |
||
| 212 | * |
||
| 213 | * Mitigates Session Fixation - use it whenever the user's privilege level changes. |
||
| 214 | */ |
||
| 215 | public function regenerateId() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param Id\Handler $idHandler |
||
| 232 | */ |
||
| 233 | public function setIdHandler(Id\Handler $idHandler) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return Id\Handler |
||
| 240 | */ |
||
| 241 | public function getIdHandler() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param int $limit |
||
| 252 | */ |
||
| 253 | public function setIdRequestsLimit($limit) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param int $ttl |
||
| 260 | */ |
||
| 261 | public function setIdTtl($ttl) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Determine if session id should be regenerated? (based on request_counter or regenerationTrace) |
||
| 268 | */ |
||
| 269 | protected function shouldRegenerateId() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return StoreInterface |
||
| 284 | */ |
||
| 285 | protected function getStore() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param FingerprintGeneratorInterface $fingerprintGenerator |
||
| 292 | */ |
||
| 293 | public function addFingerprintGenerator(FingerprintGeneratorInterface $fingerprintGenerator) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | protected function generateFingerprint() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function getFingerprint() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Gets first trace timestamp. |
||
| 322 | * |
||
| 323 | * @return int |
||
| 324 | */ |
||
| 325 | public function getFirstTrace() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Updates last trace timestamp. |
||
| 332 | */ |
||
| 333 | protected function updateLastTrace() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Gets last trace timestamp. |
||
| 340 | * |
||
| 341 | * @return int |
||
| 342 | */ |
||
| 343 | public function getLastTrace() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Gets last (id) regeneration timestamp. |
||
| 350 | * |
||
| 351 | * @return int |
||
| 352 | */ |
||
| 353 | public function getRegenerationTrace() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * It must be called before {@link self::open()}. |
||
| 360 | * |
||
| 361 | * @param int $ttl |
||
| 362 | * @throws Exception |
||
| 363 | */ |
||
| 364 | public function setTtl($ttl) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @return int |
||
| 379 | */ |
||
| 380 | public function getTtl() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @return int |
||
| 387 | */ |
||
| 388 | public function getRequestsCount() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Sets session value in given or default namespace |
||
| 395 | * |
||
| 396 | * @param string $name |
||
| 397 | * @param mixed $value |
||
| 398 | * @param string $namespace |
||
| 399 | */ |
||
| 400 | public function setValue($name, $value, $namespace = self::DEFAULT_NAMESPACE) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Gets session value from given or default namespace |
||
| 407 | * |
||
| 408 | * @param string $name |
||
| 409 | * @param string $namespace |
||
| 410 | * @return mixed |
||
| 411 | */ |
||
| 412 | public function getValue($name, $namespace = self::DEFAULT_NAMESPACE) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Gets and unsets value (flash value) for given or default namespace |
||
| 419 | * |
||
| 420 | * @param string $name |
||
| 421 | * @param string $namespace |
||
| 422 | * @return mixed |
||
| 423 | */ |
||
| 424 | public function getUnsetValue($name, $namespace = self::DEFAULT_NAMESPACE) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get all values for given or default namespace |
||
| 434 | * |
||
| 435 | * @param string $namespace |
||
| 436 | * @return array |
||
| 437 | */ |
||
| 438 | public function getValues($namespace = self::DEFAULT_NAMESPACE) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @param string $name |
||
| 445 | * @param string $namespace |
||
| 446 | * @return bool |
||
| 447 | */ |
||
| 448 | public function issetValue($name, $namespace = self::DEFAULT_NAMESPACE) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param string $name |
||
| 455 | * @param string $namespace |
||
| 456 | */ |
||
| 457 | public function unsetValue($name, $namespace = self::DEFAULT_NAMESPACE) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param string $namespace |
||
| 466 | */ |
||
| 467 | public function unsetValues($namespace = self::DEFAULT_NAMESPACE) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param mixed $offset |
||
| 476 | * @param mixed $value |
||
| 477 | */ |
||
| 478 | public function offsetSet($offset, $value) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param mixed $offset |
||
| 485 | * @return mixed |
||
| 486 | */ |
||
| 487 | public function offsetGet($offset) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param mixed $offset |
||
| 494 | * @return bool |
||
| 495 | */ |
||
| 496 | public function offsetExists($offset) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param mixed $offset |
||
| 503 | */ |
||
| 504 | public function offsetUnset($offset) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Loads session data from defined store. |
||
| 511 | * |
||
| 512 | * @return bool |
||
| 513 | */ |
||
| 514 | protected function load() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Saves session data into defined store. |
||
| 539 | * |
||
| 540 | * @return bool |
||
| 541 | */ |
||
| 542 | protected function save() |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Put a key / value pair or array of key / value pairs in the session. |
||
| 561 | * |
||
| 562 | * @param string|array $key |
||
| 563 | * @param mixed $value |
||
| 564 | * @return void |
||
| 565 | */ |
||
| 566 | public function put($key, $value = null) |
||
| 576 | |||
| 577 | |||
| 578 | /** |
||
| 579 | * Push a value onto a session array. |
||
| 580 | * |
||
| 581 | * @param string $key |
||
| 582 | * @param mixed $value |
||
| 583 | * @return void |
||
| 584 | */ |
||
| 585 | public function push($key, $value) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Remove one or many items from the session. |
||
| 594 | * |
||
| 595 | * @param string|array $keys |
||
| 596 | * @return void |
||
| 597 | */ |
||
| 598 | public function forget($keys) |
||
| 607 | |||
| 608 | |||
| 609 | /** |
||
| 610 | * Call the flash session handler. |
||
| 611 | * |
||
| 612 | * @return SessionFlash |
||
| 613 | */ |
||
| 614 | public function flash() |
||
| 622 | } |
||
| 623 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.