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 |
||
23 | class Session |
||
24 | { |
||
25 | use Options; |
||
26 | |||
27 | /** |
||
28 | * @var string value returned by session_name() |
||
29 | */ |
||
30 | protected $name; |
||
31 | |||
32 | /** |
||
33 | * @var string namespace |
||
34 | */ |
||
35 | protected $namespace = 'bluz'; |
||
36 | |||
37 | /** |
||
38 | * @var \SessionHandlerInterface Session save handler |
||
39 | */ |
||
40 | protected $adapter; |
||
41 | |||
42 | /** |
||
43 | * Attempt to set the session name |
||
44 | * |
||
45 | * If the session has already been started, or if the name provided fails |
||
46 | * validation, an exception will be raised. |
||
47 | * |
||
48 | * @param string $name |
||
49 | * |
||
50 | * @throws SessionException |
||
51 | * @return void |
||
52 | */ |
||
53 | public function setName($name) : void |
||
70 | |||
71 | /** |
||
72 | * Get session name |
||
73 | * |
||
74 | * Proxies to {@link session_name()}. |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | public function getName() : string |
||
89 | |||
90 | /** |
||
91 | * Set Namespace |
||
92 | * |
||
93 | * @param string $namespace |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | public function setNamespace(string $namespace) : void |
||
101 | |||
102 | /** |
||
103 | * Get Namespace |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | 8 | public function getNamespace() : string |
|
111 | |||
112 | /** |
||
113 | * Set session ID |
||
114 | * |
||
115 | * Can safely be called in the middle of a session. |
||
116 | * |
||
117 | * @param string $id |
||
118 | * |
||
119 | * @return void |
||
120 | * @throws SessionException |
||
121 | */ |
||
122 | public function setId($id) : void |
||
131 | |||
132 | /** |
||
133 | * Get session ID |
||
134 | * |
||
135 | * Proxies to {@link session_id()} |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getId() : string |
||
143 | |||
144 | /** |
||
145 | * Regenerate id |
||
146 | * |
||
147 | * Regenerate the session ID, using session save handler's |
||
148 | * native ID generation Can safely be called in the middle of a session. |
||
149 | * |
||
150 | * @param bool $deleteOldSession |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function regenerateId($deleteOldSession = true) : bool |
||
161 | |||
162 | /** |
||
163 | * Returns true if session ID is set |
||
164 | * |
||
165 | * @return bool |
||
166 | */ |
||
167 | 11 | public function cookieExists() : bool |
|
171 | |||
172 | /** |
||
173 | * Does a session started and is it currently active? |
||
174 | * |
||
175 | * @return bool |
||
176 | */ |
||
177 | 13 | public function sessionExists() : bool |
|
181 | |||
182 | /** |
||
183 | * Start session |
||
184 | * |
||
185 | * if No session currently exists, attempt to start it. Calls |
||
186 | * {@link isValid()} once session_start() is called, and raises an |
||
187 | * exception if validation fails. |
||
188 | * |
||
189 | * @return bool |
||
190 | * @throws ComponentException |
||
191 | */ |
||
192 | 13 | public function start() : bool |
|
204 | |||
205 | /** |
||
206 | * Destroy/end a session |
||
207 | * |
||
208 | * @return void |
||
209 | */ |
||
210 | public function destroy() : void |
||
224 | |||
225 | /** |
||
226 | * Set session save handler object |
||
227 | * |
||
228 | * @param \SessionHandlerInterface $saveHandler |
||
229 | * |
||
230 | * @return void |
||
231 | */ |
||
232 | 604 | public function setAdapter($saveHandler) : void |
|
236 | |||
237 | /** |
||
238 | * Get SaveHandler Object |
||
239 | * |
||
240 | * @return \SessionHandlerInterface |
||
241 | */ |
||
242 | public function getAdapter() : \SessionHandlerInterface |
||
246 | |||
247 | /** |
||
248 | * Register Save Handler with ext/session |
||
249 | * |
||
250 | * Since ext/session is coupled to this particular session manager |
||
251 | * register the save handler with ext/session. |
||
252 | * |
||
253 | * @return bool |
||
254 | * @throws ComponentException |
||
255 | */ |
||
256 | 1 | protected function initAdapter() : bool |
|
277 | |||
278 | /** |
||
279 | * Set the session cookie lifetime |
||
280 | * |
||
281 | * If a session already exists, destroys it (without sending an expiration |
||
282 | * cookie), regenerates the session ID, and restarts the session. |
||
283 | * |
||
284 | * @param integer $ttl TTL in seconds |
||
285 | * |
||
286 | * @return void |
||
287 | */ |
||
288 | public function setSessionCookieLifetime($ttl) : void |
||
298 | |||
299 | /** |
||
300 | * Expire the session cookie |
||
301 | * |
||
302 | * Sends a session cookie with no value, and with an expiry in the past. |
||
303 | * |
||
304 | * @return void |
||
305 | */ |
||
306 | public function expireSessionCookie() : void |
||
321 | |||
322 | /** |
||
323 | * Set session save path |
||
324 | * |
||
325 | * @param string $savePath |
||
326 | * |
||
327 | * @return void |
||
328 | * @throws ComponentException |
||
329 | */ |
||
330 | 1 | protected function setSavePath($savePath) : void |
|
339 | |||
340 | /** |
||
341 | * Set key/value pair |
||
342 | * |
||
343 | * @param string $key |
||
344 | * @param mixed $value |
||
345 | * |
||
346 | * @return void |
||
347 | * @throws ComponentException |
||
348 | */ |
||
349 | 9 | public function set($key, $value) : void |
|
358 | |||
359 | /** |
||
360 | * Get value by key |
||
361 | * |
||
362 | * @param string $key |
||
363 | * |
||
364 | * @return mixed |
||
365 | * @throws ComponentException |
||
366 | */ |
||
367 | 12 | public function get($key) |
|
374 | |||
375 | /** |
||
376 | * Isset |
||
377 | * |
||
378 | * @param string $key |
||
379 | * |
||
380 | * @return bool |
||
381 | * @throws ComponentException |
||
382 | */ |
||
383 | 12 | public function contains($key) : bool |
|
392 | |||
393 | /** |
||
394 | * Unset |
||
395 | * |
||
396 | * @param string $key |
||
397 | * |
||
398 | * @return void |
||
399 | * @throws ComponentException |
||
400 | */ |
||
401 | 1 | public function delete($key) |
|
407 | } |
||
408 |