Complex classes like SessionWare 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 SessionWare, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class SessionWare implements EmitterAwareInterface |
||
| 22 | { |
||
| 23 | use EmitterTrait; |
||
| 24 | |||
| 25 | const SESSION_LIFETIME_FLASH = 300; // 5 minutes |
||
| 26 | const SESSION_LIFETIME_SHORT = 600; // 10 minutes |
||
| 27 | const SESSION_LIFETIME_NORMAL = 900; // 15 minutes |
||
| 28 | const SESSION_LIFETIME_DEFAULT = 1440; // 24 minutes |
||
| 29 | const SESSION_LIFETIME_EXTENDED = 3600; // 1 hour |
||
| 30 | const SESSION_LIFETIME_INFINITE = PHP_INT_MAX; // Around 1145 years (x86_64) |
||
| 31 | |||
| 32 | const SESSION_TIMEOUT_KEY_DEFAULT = '__SESSIONWARE_TIMEOUT_TIMESTAMP__'; |
||
| 33 | |||
| 34 | const SESSION_ID_LENGTH = 80; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $settings; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $initialSessionParams; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $sessionName; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | protected $sessionLifetime; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $sessionTimeoutKey; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Middleware constructor. |
||
| 63 | * |
||
| 64 | * @param array $settings |
||
| 65 | * @param array $initialSessionParams |
||
| 66 | */ |
||
| 67 | public function __construct(array $settings = [], array $initialSessionParams = []) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Execute the middleware. |
||
| 76 | * |
||
| 77 | * @param ServerRequestInterface $request |
||
| 78 | * @param ResponseInterface $response |
||
| 79 | * @param callable $next |
||
| 80 | * |
||
| 81 | * @throws \InvalidArgumentException |
||
| 82 | * @throws \RuntimeException |
||
| 83 | * |
||
| 84 | * @return ResponseInterface |
||
| 85 | */ |
||
| 86 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Configure session settings. |
||
| 97 | * |
||
| 98 | * @param ServerRequestInterface $request |
||
| 99 | * |
||
| 100 | * @throws \InvalidArgumentException |
||
| 101 | * @throws \RuntimeException |
||
| 102 | */ |
||
| 103 | protected function startSession(ServerRequestInterface $request) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Verify session ini settings. |
||
| 151 | * |
||
| 152 | * @throws \RuntimeException |
||
| 153 | * |
||
| 154 | * @codeCoverageIgnore |
||
| 155 | */ |
||
| 156 | final protected function verifySessionSettings() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Retrieve default session settings. |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | protected function getSessionSettings() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Configure session name. |
||
| 204 | * |
||
| 205 | * @param array $settings |
||
| 206 | * |
||
| 207 | * @throws \InvalidArgumentException |
||
| 208 | */ |
||
| 209 | protected function configureSessionName(array $settings) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Configure session cookies parameters. |
||
| 222 | * |
||
| 223 | * @param array $settings |
||
| 224 | */ |
||
| 225 | protected function configureSessionCookies(array $settings) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Configure session save path if using default PHP session save handler. |
||
| 235 | * |
||
| 236 | * @param array $settings |
||
| 237 | * |
||
| 238 | * @throws \RuntimeException |
||
| 239 | */ |
||
| 240 | protected function configureSessionSavePath(array $settings) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Configure session timeout. |
||
| 271 | * |
||
| 272 | * @param array $settings |
||
| 273 | * |
||
| 274 | * @throws \InvalidArgumentException |
||
| 275 | */ |
||
| 276 | protected function configureSessionTimeout(array $settings) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Configure session serialize handler. |
||
| 301 | */ |
||
| 302 | protected function configureSessionSerializer() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Configure session identifier. |
||
| 314 | * |
||
| 315 | * @param ServerRequestInterface $request |
||
| 316 | */ |
||
| 317 | protected function configureSessionId(ServerRequestInterface $request) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Manage session timeout. |
||
| 328 | * |
||
| 329 | * @throws \InvalidArgumentException |
||
| 330 | */ |
||
| 331 | protected function manageSessionTimeout() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Close previous session and create a new empty one. |
||
| 346 | */ |
||
| 347 | protected function recreateSession() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Populate session with initial parameters if they don't exist. |
||
| 360 | * |
||
| 361 | * @param array $initialSessionParams |
||
| 362 | */ |
||
| 363 | protected function populateSession(array $initialSessionParams) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Add session cookie Set-Cookie header to response. |
||
| 374 | * |
||
| 375 | * @param ResponseInterface $response |
||
| 376 | * |
||
| 377 | * @throws \InvalidArgumentException |
||
| 378 | * |
||
| 379 | * @return ResponseInterface |
||
| 380 | */ |
||
| 381 | protected function respondWithSessionCookie(ResponseInterface $response) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Generates cryptographically secure session identifier. |
||
| 426 | * |
||
| 427 | * @param int $length |
||
| 428 | * |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | final public static function generateSessionId($length = self::SESSION_ID_LENGTH) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Retrieve session ini setting. |
||
| 442 | * |
||
| 443 | * @param string $setting |
||
| 444 | * @param mixed|null $default |
||
| 445 | * |
||
| 446 | * @return mixed |
||
| 447 | */ |
||
| 448 | private function getSessionSetting($setting, $default = null) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Set session ini setting. |
||
| 463 | * |
||
| 464 | * @param string $setting |
||
| 465 | * @param mixed $value |
||
| 466 | */ |
||
| 467 | private function setSessionSetting($setting, $value) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Normalize session setting name to start with 'session.'. |
||
| 474 | * |
||
| 475 | * @param string $setting |
||
| 476 | * |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | private function normalizeSessionSettingName($setting) |
||
| 483 | } |
||
| 484 |
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: