Complex classes like SwitchableSession 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 SwitchableSession, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | 1 | final class SwitchableSession extends Http\Session |
|
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var Http\Session |
||
| 41 | */ |
||
| 42 | private $systemSession; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var WebSocketsEntities\Clients\IClient|NULL |
||
| 46 | */ |
||
| 47 | private $client; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Has been session started? |
||
| 51 | * |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | private $started = FALSE; |
||
|
|
|||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $data = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $sections = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var bool |
||
| 68 | */ |
||
| 69 | private $attached = FALSE; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var \SessionHandlerInterface|NULL |
||
| 73 | */ |
||
| 74 | private $handler; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var NullHandler |
||
| 78 | */ |
||
| 79 | private $nullHandler; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var Serializers\ISessionSerializer |
||
| 83 | */ |
||
| 84 | private $serializer; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param Http\Session $session |
||
| 88 | * @param Serializers\ISessionSerializer $serializer |
||
| 89 | * @param \SessionHandlerInterface|NULL $handler |
||
| 90 | */ |
||
| 91 | public function __construct( |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param WebSocketsEntities\Clients\IClient $client |
||
| 104 | * @param WebSocketsHttp\IRequest $httpRequest |
||
| 105 | * |
||
| 106 | * @return void |
||
| 107 | * |
||
| 108 | * @throws Exceptions\InvalidArgumentException |
||
| 109 | * @throws Exceptions\LogicException |
||
| 110 | */ |
||
| 111 | public function attach(WebSocketsEntities\Clients\IClient $client, WebSocketsHttp\IRequest $httpRequest) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | public function detach() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function isAttached() : bool |
||
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | public function start() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritdoc} |
||
| 218 | */ |
||
| 219 | public function isStarted() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | */ |
||
| 231 | public function close() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * {@inheritdoc} |
||
| 245 | */ |
||
| 246 | public function destroy() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | */ |
||
| 265 | public function exists() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * {@inheritdoc} |
||
| 276 | */ |
||
| 277 | public function regenerateId() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritdoc} |
||
| 288 | */ |
||
| 289 | public function getId() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * {@inheritdoc} |
||
| 300 | */ |
||
| 301 | public function getSection($section, $class = 'Nette\Http\SessionSection') |
||
| 309 | |||
| 310 | /** |
||
| 311 | * {@inheritdoc} |
||
| 312 | */ |
||
| 313 | public function hasSection($section) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * {@inheritdoc} |
||
| 324 | */ |
||
| 325 | public function getIterator() : \ArrayIterator |
||
| 333 | |||
| 334 | /** |
||
| 335 | * {@inheritdoc} |
||
| 336 | */ |
||
| 337 | public function clean() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * {@inheritdoc} |
||
| 368 | */ |
||
| 369 | public function setName($name) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * {@inheritdoc} |
||
| 376 | */ |
||
| 377 | public function getName() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * {@inheritdoc} |
||
| 384 | */ |
||
| 385 | public function setOptions(array $options) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * {@inheritdoc} |
||
| 392 | */ |
||
| 393 | public function getOptions() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * {@inheritdoc} |
||
| 400 | */ |
||
| 401 | public function setExpiration($time) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * {@inheritdoc} |
||
| 408 | */ |
||
| 409 | public function setCookieParameters($path, $domain = NULL, $secure = NULL) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * {@inheritdoc} |
||
| 416 | */ |
||
| 417 | public function getCookieParameters() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * {@inheritdoc} |
||
| 424 | */ |
||
| 425 | public function setSavePath($path) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * {@inheritdoc} |
||
| 432 | */ |
||
| 433 | public function setHandler(\SessionHandlerInterface $handler) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param string $section |
||
| 440 | * |
||
| 441 | * @return array |
||
| 442 | */ |
||
| 443 | public function getData(string $section) : array |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return int |
||
| 454 | * |
||
| 455 | * @throws Exceptions\InvalidStateException |
||
| 456 | */ |
||
| 457 | private function getConnectionId() : int |
||
| 461 | } |
||
| 462 |