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 |
||
| 24 | class Session implements EmitterAwareInterface |
||
| 25 | { |
||
| 26 | use EmitterTrait; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Session manager. |
||
| 30 | * |
||
| 31 | * @var Manager |
||
| 32 | */ |
||
| 33 | protected $sessionManager; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Session initial data. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $originalData; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Session data. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $data; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Session constructor. |
||
| 51 | * |
||
| 52 | * @param Manager $sessionManager |
||
| 53 | * @param array $initialData |
||
| 54 | * |
||
| 55 | * @throws \InvalidArgumentException |
||
| 56 | */ |
||
| 57 | public function __construct(Manager $sessionManager, array $initialData = []) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get session manager. |
||
| 73 | * |
||
| 74 | * @return Manager |
||
| 75 | */ |
||
| 76 | public function getManager() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Start session. |
||
| 83 | * |
||
| 84 | * @throws \RuntimeException |
||
| 85 | */ |
||
| 86 | public function start() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Regenerate session identifier keeping parameters. |
||
| 107 | * |
||
| 108 | * @throws \RuntimeException |
||
| 109 | */ |
||
| 110 | public function regenerateId() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Revert session to its original data. |
||
| 125 | */ |
||
| 126 | public function reset() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Close session keeping original session data. |
||
| 141 | */ |
||
| 142 | public function abort() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Close session. |
||
| 157 | */ |
||
| 158 | public function close() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Destroy session. |
||
| 173 | * |
||
| 174 | * @throws \RuntimeException |
||
| 175 | */ |
||
| 176 | public function destroy() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Has session been started. |
||
| 193 | * |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | public function isActive() : bool |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Has session been destroyed. |
||
| 203 | * |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | public function isDestroyed() : bool |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get session identifier. |
||
| 213 | * |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public function getId() : string |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Set session identifier. |
||
| 223 | * |
||
| 224 | * @param string $sessionId |
||
| 225 | * |
||
| 226 | * @throws \RuntimeException |
||
| 227 | */ |
||
| 228 | public function setId(string $sessionId) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Session parameter existence. |
||
| 239 | * |
||
| 240 | * @param string $key |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | public function has(string $key) : bool |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Retrieve session parameter. |
||
| 251 | * |
||
| 252 | * @param string $key |
||
| 253 | * @param mixed|null $default |
||
| 254 | * |
||
| 255 | * @return mixed |
||
| 256 | */ |
||
| 257 | public function get(string $key, $default = null) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Set session parameter. |
||
| 264 | * |
||
| 265 | * @param string $key |
||
| 266 | * @param mixed $value |
||
| 267 | * |
||
| 268 | * @throws \InvalidArgumentException |
||
| 269 | * |
||
| 270 | * @return self |
||
| 271 | */ |
||
| 272 | public function set(string $key, $value) : self |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Verify only scalar values allowed. |
||
| 283 | * |
||
| 284 | * @param string|int|float|bool|array $value |
||
| 285 | * |
||
| 286 | * @throws \InvalidArgumentException |
||
| 287 | */ |
||
| 288 | final protected function verifyScalarValue($value) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Remove session parameter. |
||
| 303 | * |
||
| 304 | * @param string $key |
||
| 305 | * |
||
| 306 | * @return self |
||
| 307 | */ |
||
| 308 | public function remove(string $key) : self |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Remove all session parameters. |
||
| 319 | * |
||
| 320 | * @return self |
||
| 321 | */ |
||
| 322 | public function clear() : self |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Manage session timeout. |
||
| 336 | * |
||
| 337 | * @throws \RuntimeException |
||
| 338 | */ |
||
| 339 | protected function manageTimeout() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get cookie header content. |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function getCookieString() : string |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get session cookie parameters. |
||
| 379 | * |
||
| 380 | * @param int $expireTime |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | protected function getCookieParameters(int $expireTime) : string |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get session configuration. |
||
| 417 | * |
||
| 418 | * @return Configuration |
||
| 419 | */ |
||
| 420 | protected function getConfiguration() : Configuration |
||
| 424 | } |
||
| 425 |