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 | * @throws SessionException |
||
| 50 | * @return Session |
||
| 51 | */ |
||
| 52 | public function setName($name) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get session name |
||
| 73 | * |
||
| 74 | * Proxies to {@link session_name()}. |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public function getName() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Set Namespace |
||
| 92 | * |
||
| 93 | * @param string $namespace |
||
| 94 | * @return Session |
||
| 95 | */ |
||
| 96 | public function setNamespace($namespace) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get Namespace |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 6 | public function getNamespace() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Set session ID |
||
| 114 | * |
||
| 115 | * Can safely be called in the middle of a session. |
||
| 116 | * |
||
| 117 | * @param string $id |
||
| 118 | * @return Session |
||
| 119 | * @throws SessionException |
||
| 120 | */ |
||
| 121 | public function setId($id) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get session ID |
||
| 134 | * |
||
| 135 | * Proxies to {@link session_id()} |
||
| 136 | * |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function getId() |
||
| 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 | * @return bool |
||
| 152 | */ |
||
| 153 | public function regenerateId($deleteOldSession = true) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Returns true if session ID is set |
||
| 163 | * |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | 638 | public function cookieExists() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Does a session started and is it currently active? |
||
| 173 | * |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | 638 | public function sessionExists() |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Start session |
||
| 183 | * |
||
| 184 | * if No session currently exists, attempt to start it. Calls |
||
| 185 | * {@link isValid()} once session_start() is called, and raises an |
||
| 186 | * exception if validation fails. |
||
| 187 | * |
||
| 188 | * @return void |
||
| 189 | * @throws SessionException |
||
| 190 | */ |
||
| 191 | 638 | public function start() |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Destroy/end a session |
||
| 204 | * |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | public function destroy() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Set session save handler object |
||
| 224 | * |
||
| 225 | * @param \SessionHandlerInterface $saveHandler |
||
| 226 | * @return Session |
||
| 227 | */ |
||
| 228 | public function setAdapter($saveHandler) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get SaveHandler Object |
||
| 236 | * |
||
| 237 | * @return \SessionHandlerInterface |
||
| 238 | */ |
||
| 239 | public function getAdapter() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Register Save Handler with ext/session |
||
| 246 | * |
||
| 247 | * Since ext/session is coupled to this particular session manager |
||
| 248 | * register the save handler with ext/session. |
||
| 249 | * |
||
| 250 | * @return bool |
||
| 251 | * @throws ComponentException |
||
| 252 | */ |
||
| 253 | 1 | protected function initAdapter() |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Set the session cookie lifetime |
||
| 276 | * |
||
| 277 | * If a session already exists, destroys it (without sending an expiration |
||
| 278 | * cookie), regenerates the session ID, and restarts the session. |
||
| 279 | * |
||
| 280 | * @param integer $ttl TTL in seconds |
||
| 281 | * @return void |
||
| 282 | */ |
||
| 283 | public function setSessionCookieLifetime($ttl) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Expire the session cookie |
||
| 296 | * |
||
| 297 | * Sends a session cookie with no value, and with an expiry in the past. |
||
| 298 | * |
||
| 299 | * @return void |
||
| 300 | */ |
||
| 301 | public function expireSessionCookie() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Set session save path |
||
| 319 | * |
||
| 320 | * @param string $savePath |
||
| 321 | * @return Session |
||
| 322 | * @throws ComponentException |
||
| 323 | */ |
||
| 324 | 1 | protected function setSavePath($savePath) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Set key/value pair |
||
| 337 | * |
||
| 338 | * @param string $key |
||
| 339 | * @param mixed $value |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | 7 | public function set($key, $value) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Get value by key |
||
| 354 | * |
||
| 355 | * @param string $key |
||
| 356 | * @return mixed |
||
| 357 | */ |
||
| 358 | 639 | public function get($key) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Isset |
||
| 368 | * |
||
| 369 | * @param string $key |
||
| 370 | * @return bool |
||
| 371 | */ |
||
| 372 | 639 | public function contains($key) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Unset |
||
| 384 | * |
||
| 385 | * @param string $key |
||
| 386 | * @return void |
||
| 387 | */ |
||
| 388 | 638 | public function delete($key) |
|
| 394 | } |
||
| 395 |