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 Session |
||
| 52 | */ |
||
| 53 | public function setName($name) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Get session name |
||
| 74 | * |
||
| 75 | * Proxies to {@link session_name()}. |
||
| 76 | * |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | public function getName() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Set Namespace |
||
| 93 | * |
||
| 94 | * @param string $namespace |
||
| 95 | * |
||
| 96 | * @return Session |
||
| 97 | */ |
||
| 98 | public function setNamespace($namespace) |
||
| 99 | { |
||
| 100 | $this->namespace = $namespace; |
||
| 101 | return $this; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get Namespace |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | 6 | public function getNamespace() |
|
| 110 | { |
||
| 111 | 6 | return $this->namespace; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Set session ID |
||
| 116 | * |
||
| 117 | * Can safely be called in the middle of a session. |
||
| 118 | * |
||
| 119 | * @param string $id |
||
| 120 | * |
||
| 121 | * @return Session |
||
| 122 | * @throws SessionException |
||
| 123 | */ |
||
| 124 | public function setId($id) |
||
| 125 | { |
||
| 126 | if ($this->sessionExists()) { |
||
| 127 | throw new SessionException( |
||
| 128 | 'Session has already been started, to change the session ID call regenerateId()' |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | session_id($id); |
||
| 132 | return $this; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get session ID |
||
| 137 | * |
||
| 138 | * Proxies to {@link session_id()} |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public function getId() |
||
| 143 | { |
||
| 144 | return session_id(); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Regenerate id |
||
| 149 | * |
||
| 150 | * Regenerate the session ID, using session save handler's |
||
| 151 | * native ID generation Can safely be called in the middle of a session. |
||
| 152 | * |
||
| 153 | * @param bool $deleteOldSession |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | public function regenerateId($deleteOldSession = true) |
||
| 158 | { |
||
| 159 | if ($this->sessionExists()) { |
||
| 160 | if (session_id() !== '') { |
||
| 161 | return session_regenerate_id((bool)$deleteOldSession); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | return false; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns true if session ID is set |
||
| 169 | * |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | 11 | public function cookieExists() |
|
|
|
|||
| 173 | { |
||
| 174 | 11 | return isset($_COOKIE[session_name()]); |
|
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Does a session started and is it currently active? |
||
| 179 | * |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | 13 | public function sessionExists() |
|
| 183 | { |
||
| 184 | 13 | return session_status() === PHP_SESSION_ACTIVE; |
|
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Start session |
||
| 189 | * |
||
| 190 | * if No session currently exists, attempt to start it. Calls |
||
| 191 | * {@link isValid()} once session_start() is called, and raises an |
||
| 192 | * exception if validation fails. |
||
| 193 | * |
||
| 194 | * @return bool |
||
| 195 | * @throws \Bluz\Common\Exception\ComponentException |
||
| 196 | * @throws SessionException |
||
| 197 | */ |
||
| 198 | 13 | public function start() |
|
| 199 | { |
||
| 200 | 13 | if ($this->sessionExists()) { |
|
| 201 | 13 | return true; |
|
| 202 | } |
||
| 203 | |||
| 204 | 1 | if ($this->initAdapter()) { |
|
| 205 | 1 | return session_start(); |
|
| 206 | } |
||
| 207 | |||
| 208 | throw new ComponentException('Invalid adapter settings'); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Destroy/end a session |
||
| 213 | * |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | public function destroy() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Set session save handler object |
||
| 233 | * |
||
| 234 | * @param \SessionHandlerInterface $saveHandler |
||
| 235 | * |
||
| 236 | * @return Session |
||
| 237 | */ |
||
| 238 | 578 | public function setAdapter($saveHandler) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Get SaveHandler Object |
||
| 246 | * |
||
| 247 | * @return \SessionHandlerInterface |
||
| 248 | */ |
||
| 249 | public function getAdapter() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Register Save Handler with ext/session |
||
| 256 | * |
||
| 257 | * Since ext/session is coupled to this particular session manager |
||
| 258 | * register the save handler with ext/session. |
||
| 259 | * |
||
| 260 | * @return bool |
||
| 261 | * @throws ComponentException |
||
| 262 | */ |
||
| 263 | 1 | protected function initAdapter() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Set the session cookie lifetime |
||
| 287 | * |
||
| 288 | * If a session already exists, destroys it (without sending an expiration |
||
| 289 | * cookie), regenerates the session ID, and restarts the session. |
||
| 290 | * |
||
| 291 | * @param integer $ttl TTL in seconds |
||
| 292 | * |
||
| 293 | * @return void |
||
| 294 | */ |
||
| 295 | public function setSessionCookieLifetime($ttl) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Expire the session cookie |
||
| 308 | * |
||
| 309 | * Sends a session cookie with no value, and with an expiry in the past. |
||
| 310 | * |
||
| 311 | * @return void |
||
| 312 | */ |
||
| 313 | public function expireSessionCookie() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Set session save path |
||
| 331 | * |
||
| 332 | * @param string $savePath |
||
| 333 | * |
||
| 334 | * @return Session |
||
| 335 | * @throws ComponentException |
||
| 336 | */ |
||
| 337 | 1 | protected function setSavePath($savePath) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Set key/value pair |
||
| 350 | * |
||
| 351 | * @param string $key |
||
| 352 | * @param mixed $value |
||
| 353 | * |
||
| 354 | * @return void |
||
| 355 | */ |
||
| 356 | 7 | public function set($key, $value) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Get value by key |
||
| 368 | * |
||
| 369 | * @param string $key |
||
| 370 | * |
||
| 371 | * @return mixed |
||
| 372 | */ |
||
| 373 | 12 | public function get($key) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Isset |
||
| 383 | * |
||
| 384 | * @param string $key |
||
| 385 | * |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | 12 | public function contains($key) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Unset |
||
| 400 | * |
||
| 401 | * @param string $key |
||
| 402 | * |
||
| 403 | * @return void |
||
| 404 | */ |
||
| 405 | 1 | public function delete($key) |
|
| 411 | } |
||
| 412 |
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: