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 |
||
26 | class Session implements EmitterAwareInterface |
||
27 | { |
||
28 | use EmitterTrait; |
||
29 | |||
30 | /** |
||
31 | * Session manager. |
||
32 | * |
||
33 | * @var Manager |
||
34 | */ |
||
35 | protected $sessionManager; |
||
36 | |||
37 | /** |
||
38 | * Session initial data. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $originalData; |
||
43 | |||
44 | /** |
||
45 | * Session data. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $data; |
||
50 | |||
51 | /** |
||
52 | * Session constructor. |
||
53 | * |
||
54 | * @param Manager $sessionManager |
||
55 | * @param array $initialData |
||
56 | * |
||
57 | * @throws \InvalidArgumentException |
||
58 | */ |
||
59 | public function __construct(Manager $sessionManager, array $initialData = []) |
||
72 | |||
73 | /** |
||
74 | * Get session manager. |
||
75 | * |
||
76 | * @return Manager |
||
77 | */ |
||
78 | public function getManager() : Manager |
||
82 | |||
83 | /** |
||
84 | * Start session. |
||
85 | * |
||
86 | * @throws \RuntimeException |
||
87 | */ |
||
88 | public function start() |
||
106 | |||
107 | /** |
||
108 | * Regenerate session identifier keeping parameters. |
||
109 | * |
||
110 | * @throws \RuntimeException |
||
111 | */ |
||
112 | public function regenerateId() |
||
124 | |||
125 | /** |
||
126 | * Revert session to its original data. |
||
127 | */ |
||
128 | public function reset() |
||
140 | |||
141 | /** |
||
142 | * Close session keeping original session data. |
||
143 | */ |
||
144 | public function abort() |
||
156 | |||
157 | /** |
||
158 | * Close session. |
||
159 | */ |
||
160 | public function close() |
||
172 | |||
173 | /** |
||
174 | * Destroy session. |
||
175 | * |
||
176 | * @throws \RuntimeException |
||
177 | */ |
||
178 | public function destroy() |
||
192 | |||
193 | /** |
||
194 | * Has session been started. |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | public function isActive() : bool |
||
202 | |||
203 | /** |
||
204 | * Has session been destroyed. |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | public function isDestroyed() : bool |
||
212 | |||
213 | /** |
||
214 | * Get session identifier. |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | public function getId() : string |
||
222 | |||
223 | /** |
||
224 | * Load session identifier from request. |
||
225 | * |
||
226 | * @param ServerRequestInterface $request |
||
227 | * |
||
228 | * @throws \RuntimeException |
||
229 | */ |
||
230 | public function loadIdFromRequest(ServerRequestInterface $request) |
||
239 | |||
240 | /** |
||
241 | * Set session identifier. |
||
242 | * |
||
243 | * @param string $sessionId |
||
244 | * |
||
245 | * @throws \RuntimeException |
||
246 | */ |
||
247 | public function setId(string $sessionId) |
||
255 | |||
256 | /** |
||
257 | * Session parameter existence. |
||
258 | * |
||
259 | * @param string $key |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function has(string $key) : bool |
||
267 | |||
268 | /** |
||
269 | * Retrieve session parameter. |
||
270 | * |
||
271 | * @param string $key |
||
272 | * @param mixed|null $default |
||
273 | * |
||
274 | * @return mixed |
||
275 | */ |
||
276 | public function get(string $key, $default = null) |
||
280 | |||
281 | /** |
||
282 | * Set session parameter. |
||
283 | * |
||
284 | * @param string $key |
||
285 | * @param mixed $value |
||
286 | * |
||
287 | * @throws \InvalidArgumentException |
||
288 | * |
||
289 | * @return self |
||
290 | */ |
||
291 | public function set(string $key, $value) : self |
||
299 | |||
300 | /** |
||
301 | * Verify only scalar values allowed. |
||
302 | * |
||
303 | * @param string|int|float|bool|array $value |
||
304 | * |
||
305 | * @throws \InvalidArgumentException |
||
306 | */ |
||
307 | final protected function verifyScalarValue($value) |
||
319 | |||
320 | /** |
||
321 | * Remove session parameter. |
||
322 | * |
||
323 | * @param string $key |
||
324 | * |
||
325 | * @return self |
||
326 | */ |
||
327 | public function remove(string $key) : self |
||
335 | |||
336 | /** |
||
337 | * Remove all session parameters. |
||
338 | * |
||
339 | * @return self |
||
340 | */ |
||
341 | public function clear() : self |
||
352 | |||
353 | /** |
||
354 | * Manage session timeout. |
||
355 | * |
||
356 | * @throws \RuntimeException |
||
357 | */ |
||
358 | protected function manageTimeout() |
||
373 | |||
374 | /** |
||
375 | * Return response with added session cookie. |
||
376 | * |
||
377 | * @param ResponseInterface $response |
||
378 | * |
||
379 | * @return ResponseInterface |
||
380 | */ |
||
381 | public function withSessionCookie(ResponseInterface $response) : ResponseInterface |
||
391 | |||
392 | /** |
||
393 | * Get session cookie content. |
||
394 | * |
||
395 | * @return string |
||
396 | */ |
||
397 | public function getSessionCookieString() : string |
||
409 | |||
410 | /** |
||
411 | * Get session expired cookie. |
||
412 | * |
||
413 | * @return string |
||
414 | */ |
||
415 | protected function getExpiredCookieString() : string |
||
426 | |||
427 | /** |
||
428 | * Get normal session cookie. |
||
429 | * |
||
430 | * @return string |
||
431 | */ |
||
432 | protected function getInitiatedCookieString() : string |
||
449 | |||
450 | /** |
||
451 | * Get session cookie parameters. |
||
452 | * |
||
453 | * @param int $expireTime |
||
454 | * @param int $lifetime |
||
455 | * |
||
456 | * @return string |
||
457 | */ |
||
458 | protected function getCookieParameters(int $expireTime, int $lifetime) : string |
||
487 | |||
488 | /** |
||
489 | * Get normalized cookie domain. |
||
490 | * |
||
491 | * @return string |
||
492 | */ |
||
493 | protected function getCookieDomain() : string |
||
509 | |||
510 | /** |
||
511 | * Get session configuration. |
||
512 | * |
||
513 | * @return Configuration |
||
514 | */ |
||
515 | protected function getConfiguration() : Configuration |
||
519 | } |
||
520 |