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_KEY = 'session'; |
||
| 33 | const TIMEOUT_CONTROL_KEY = '__SESSIONWARE_TIMEOUT_TIMESTAMP__'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $settings; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $initialSessionParams; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $sessionName; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | protected $sessionLifetime; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $sessionTimeoutKey; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Middleware constructor. |
||
| 62 | * |
||
| 63 | * @param array $settings |
||
| 64 | * @param array $initialSessionParams |
||
| 65 | */ |
||
| 66 | public function __construct(array $settings = [], array $initialSessionParams = []) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Execute the middleware. |
||
| 75 | * |
||
| 76 | * @param ServerRequestInterface $request |
||
| 77 | * @param ResponseInterface $response |
||
| 78 | * @param callable $next |
||
| 79 | * |
||
| 80 | * @throws \InvalidArgumentException |
||
| 81 | * @throws \RuntimeException |
||
| 82 | * |
||
| 83 | * @return ResponseInterface |
||
| 84 | */ |
||
| 85 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Configure session settings. |
||
| 96 | * |
||
| 97 | * @param ServerRequestInterface $request |
||
| 98 | * |
||
| 99 | * @throws \InvalidArgumentException |
||
| 100 | * @throws \RuntimeException |
||
| 101 | */ |
||
| 102 | protected function startSession(ServerRequestInterface $request) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Retrieve default session parameters. |
||
| 142 | * |
||
| 143 | * @param array $customSettings |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | protected function getSessionSettings(array $customSettings) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Configure session name. |
||
| 170 | * |
||
| 171 | * @throws \InvalidArgumentException |
||
| 172 | * |
||
| 173 | * @param array $settings |
||
| 174 | */ |
||
| 175 | protected function configureSessionName(array $settings) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Configure session cookies parameters. |
||
| 188 | * |
||
| 189 | * @param array $settings |
||
| 190 | */ |
||
| 191 | protected function configureSessionCookies(array $settings) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Configure session save path if using default PHP session save handler. |
||
| 201 | * |
||
| 202 | * @param array $settings |
||
| 203 | * |
||
| 204 | * @throws \RuntimeException |
||
| 205 | */ |
||
| 206 | protected function configureSessionSavePath(array $settings) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Configure session timeout. |
||
| 237 | * |
||
| 238 | * @param array $settings |
||
| 239 | * |
||
| 240 | * @throws \InvalidArgumentException |
||
| 241 | */ |
||
| 242 | protected function configureSessionTimeout(array $settings) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Configure session serialize handler. |
||
| 267 | */ |
||
| 268 | protected function configureSessionSerializer() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Prevent headers from being automatically sent to client on session start. |
||
| 280 | */ |
||
| 281 | protected function configureSessionHeaders() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Configure session identifier. |
||
| 292 | * |
||
| 293 | * @param ServerRequestInterface $request |
||
| 294 | */ |
||
| 295 | protected function configureSessionId(ServerRequestInterface $request) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Manage session timeout. |
||
| 313 | * |
||
| 314 | * @throws \InvalidArgumentException |
||
| 315 | */ |
||
| 316 | protected function manageSessionTimeout() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Populate session with initial parameters if they don't exist. |
||
| 337 | * |
||
| 338 | * @param array $initialSessionParams |
||
| 339 | */ |
||
| 340 | protected function populateSession(array $initialSessionParams) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Add session cookie Set-Cookie header to response. |
||
| 351 | * |
||
| 352 | * @param ResponseInterface $response |
||
| 353 | * |
||
| 354 | * @throws \InvalidArgumentException |
||
| 355 | * |
||
| 356 | * @return ResponseInterface |
||
| 357 | */ |
||
| 358 | protected function respondWithSessionCookie(ResponseInterface $response) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Generates cryptographically secure session identifier. |
||
| 403 | * |
||
| 404 | * @param int $length |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | final public static function generateSessionId($length = 80) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Retrieve session ini setting. |
||
| 419 | * |
||
| 420 | * @param string $setting |
||
| 421 | * @param null|mixed $default |
||
| 422 | * |
||
| 423 | * @return null|string |
||
| 424 | */ |
||
| 425 | private function getSessionSetting($setting, $default = null) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Set session ini setting. |
||
| 440 | * |
||
| 441 | * @param string $setting |
||
| 442 | * @param mixed $value |
||
| 443 | */ |
||
| 444 | private function setSessionSetting($setting, $value) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Normalize session setting name to start with 'session.'. |
||
| 451 | * |
||
| 452 | * @param string $setting |
||
| 453 | * |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | private function normalizeSessionSettingName($setting) |
||
| 460 | } |
||
| 461 |
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: