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 |
||
18 | class SessionWare |
||
19 | { |
||
20 | const SESSION_LIFETIME_FLASH = 300; // 5 minutes |
||
21 | const SESSION_LIFETIME_SHORT = 600; // 10 minutes |
||
22 | const SESSION_LIFETIME_NORMAL = 900; // 15 minutes |
||
23 | const SESSION_LIFETIME_DEFAULT = 1440; // 24 minutes |
||
24 | const SESSION_LIFETIME_EXTENDED = 3600; // 1 hour |
||
25 | const SESSION_LIFETIME_INFINITE = PHP_INT_MAX; // Around 1145 years (x86_64) |
||
26 | |||
27 | const TIMEOUT_CONTROL_KEY = '__SESSIONWARE_TIMEOUT_TIMESTAMP__'; |
||
28 | |||
29 | /** |
||
30 | * Default session settings. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected static $defaultSettings = [ |
||
35 | 'name' => null, |
||
36 | 'savePath' => null, |
||
37 | 'lifetime' => self::SESSION_LIFETIME_DEFAULT, |
||
38 | 'timeoutKey' => self::TIMEOUT_CONTROL_KEY, |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $settings; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $initialSessionParams; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $sessionName; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $sessionLifetime; |
||
60 | |||
61 | /** |
||
62 | * Middleware constructor. |
||
63 | * |
||
64 | * @param array $settings |
||
65 | * @param array $initialSessionParams |
||
66 | */ |
||
67 | public function __construct(array $settings = [], array $initialSessionParams = []) |
||
77 | |||
78 | /** |
||
79 | * Retrieve default session parameters. |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | protected function getSessionParams() |
||
97 | |||
98 | /** |
||
99 | * Execute the middleware. |
||
100 | * |
||
101 | * @param ServerRequestInterface $request |
||
102 | * @param ResponseInterface $response |
||
103 | * @param callable $next |
||
104 | * |
||
105 | * @throws \InvalidArgumentException |
||
106 | * @throws \RuntimeException |
||
107 | * |
||
108 | * @return ResponseInterface |
||
109 | */ |
||
110 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
118 | |||
119 | /** |
||
120 | * Configure session settings. |
||
121 | * |
||
122 | * @param array $requestCookies |
||
123 | * |
||
124 | * @throws \InvalidArgumentException |
||
125 | * @throws \RuntimeException |
||
126 | */ |
||
127 | protected function startSession(array $requestCookies = []) |
||
163 | |||
164 | /** |
||
165 | * Configure session name. |
||
166 | */ |
||
167 | protected function configureSessionName() |
||
173 | |||
174 | /** |
||
175 | * Configure session identifier. |
||
176 | * |
||
177 | * @param array $requestCookies |
||
178 | */ |
||
179 | protected function configureSessionId(array $requestCookies = []) |
||
191 | |||
192 | /** |
||
193 | * Configure session save path. |
||
194 | * |
||
195 | * @throws \RuntimeException |
||
196 | */ |
||
197 | protected function configureSessionSavePath() |
||
215 | |||
216 | /** |
||
217 | * Return session save path to be used. |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | protected function getSessionSavePath() |
||
239 | |||
240 | /** |
||
241 | * Configure session timeout. |
||
242 | * |
||
243 | * @throws \InvalidArgumentException |
||
244 | */ |
||
245 | protected function configureSessionTimeout() |
||
258 | |||
259 | /** |
||
260 | * Populate session with initial parameters if they don't exist. |
||
261 | */ |
||
262 | protected function populateSession() |
||
270 | |||
271 | /** |
||
272 | * Manage session timeout. |
||
273 | * |
||
274 | * @throws \InvalidArgumentException |
||
275 | */ |
||
276 | protected function manageSessionTimeout() |
||
291 | |||
292 | /** |
||
293 | * Add session cookie Set-Cookie header to response. |
||
294 | * |
||
295 | * @param ResponseInterface $response |
||
296 | * |
||
297 | * @throws \InvalidArgumentException |
||
298 | * |
||
299 | * @return ResponseInterface |
||
300 | */ |
||
301 | protected function respondWithSessionCookie(ResponseInterface $response) |
||
337 | |||
338 | /** |
||
339 | * Retrieve session timeout control key. |
||
340 | * |
||
341 | * @throws \InvalidArgumentException |
||
342 | * |
||
343 | * @return string |
||
344 | */ |
||
345 | protected function getSessionTimeoutControlKey() |
||
356 | |||
357 | /** |
||
358 | * Regenerate session id with cryptographically secure session identifier |
||
359 | */ |
||
360 | final public static function regenerateSessionId() |
||
364 | |||
365 | /** |
||
366 | * Generates cryptographically secure session identifier. |
||
367 | * |
||
368 | * @param int $length |
||
369 | * |
||
370 | * @return string |
||
371 | */ |
||
372 | final protected static function generateSessionId($length = 80) |
||
380 | } |
||
381 |
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: