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 | 639 | public function getId() |
|
| 140 | { |
||
| 141 | 639 | return session_id(); |
|
| 142 | } |
||
| 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) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns true if session ID is set |
||
| 164 | * |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | 639 | public function cookieExists() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Does a session started and is it currently active? |
||
| 174 | * |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | 639 | public function sessionExists() |
|
| 178 | { |
||
| 179 | 639 | $sid = defined('SID') ? constant('SID') : false; |
|
| 180 | 639 | if ($sid !== false && $this->getId()) { |
|
| 181 | 639 | return true; |
|
| 182 | } |
||
| 183 | 1 | if (headers_sent()) { |
|
| 184 | return true; |
||
| 185 | } |
||
| 186 | 1 | return false; |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Start session |
||
| 191 | * |
||
| 192 | * if No session currently exists, attempt to start it. Calls |
||
| 193 | * {@link isValid()} once session_start() is called, and raises an |
||
| 194 | * exception if validation fails. |
||
| 195 | * |
||
| 196 | * @return void |
||
| 197 | * @throws SessionException |
||
| 198 | */ |
||
| 199 | 639 | public function start() |
|
| 200 | { |
||
| 201 | 639 | if ($this->sessionExists()) { |
|
| 202 | 639 | return; |
|
| 203 | } |
||
| 204 | |||
| 205 | 1 | $this->initAdapter(); |
|
| 206 | |||
| 207 | 1 | session_start(); |
|
| 208 | 1 | } |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Destroy/end a session |
||
| 212 | * |
||
| 213 | * @return void |
||
| 214 | */ |
||
| 215 | public function destroy() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set session save handler object |
||
| 232 | * |
||
| 233 | * @param \SessionHandlerInterface $saveHandler |
||
| 234 | * @return Session |
||
| 235 | */ |
||
| 236 | public function setAdapter($saveHandler) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get SaveHandler Object |
||
| 244 | * |
||
| 245 | * @return \SessionHandlerInterface |
||
| 246 | */ |
||
| 247 | public function getAdapter() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Register Save Handler with ext/session |
||
| 254 | * |
||
| 255 | * Since ext/session is coupled to this particular session manager |
||
| 256 | * register the save handler with ext/session. |
||
| 257 | * |
||
| 258 | * @return bool |
||
| 259 | * @throws ComponentException |
||
| 260 | */ |
||
| 261 | 1 | protected function initAdapter() |
|
| 262 | { |
||
| 263 | 1 | if (is_null($this->adapter) || $this->adapter === 'files') { |
|
| 264 | // try to apply settings |
||
| 265 | 1 | if ($settings = $this->getOption('settings', 'files')) { |
|
| 266 | 1 | $this->setSavePath($settings['save_path']); |
|
| 267 | } |
||
| 268 | 1 | return true; |
|
| 269 | } elseif (is_string($this->adapter)) { |
||
| 270 | $adapterClass = '\\Bluz\\Session\\Adapter\\'.ucfirst($this->adapter); |
||
| 271 | if (!class_exists($adapterClass) || !is_subclass_of($adapterClass, '\SessionHandlerInterface')) { |
||
| 272 | throw new ComponentException("Class for session adapter `{$this->adapter}` not found"); |
||
| 273 | } |
||
| 274 | $settings = $this->getOption('settings', $this->adapter) ?: []; |
||
| 275 | |||
| 276 | $this->adapter = new $adapterClass($settings); |
||
| 277 | } |
||
| 278 | |||
| 279 | return session_set_save_handler($this->adapter); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Set the session cookie lifetime |
||
| 284 | * |
||
| 285 | * If a session already exists, destroys it (without sending an expiration |
||
| 286 | * cookie), regenerates the session ID, and restarts the session. |
||
| 287 | * |
||
| 288 | * @param integer $ttl TTL in seconds |
||
| 289 | * @return void |
||
| 290 | */ |
||
| 291 | public function setSessionCookieLifetime($ttl) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Expire the session cookie |
||
| 304 | * |
||
| 305 | * Sends a session cookie with no value, and with an expiry in the past. |
||
| 306 | * |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | public function expireSessionCookie() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Set session save path |
||
| 327 | * |
||
| 328 | * @param string $savePath |
||
| 329 | * @return Session |
||
| 330 | * @throws ComponentException |
||
| 331 | */ |
||
| 332 | 1 | protected function setSavePath($savePath) |
|
| 333 | { |
||
| 334 | 1 | if (!is_dir($savePath) |
|
| 335 | 1 | || !is_writable($savePath) |
|
| 336 | ) { |
||
| 337 | throw new ComponentException('Session path is not writable'); |
||
| 338 | } |
||
| 339 | 1 | session_save_path($savePath); |
|
| 340 | 1 | return $this; |
|
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Set key/value pair |
||
| 345 | * |
||
| 346 | * @param string $key |
||
| 347 | * @param mixed $value |
||
| 348 | * @return void |
||
| 349 | */ |
||
| 350 | 7 | public function set($key, $value) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Get value by key |
||
| 362 | * |
||
| 363 | * @param string $key |
||
| 364 | * @return mixed |
||
| 365 | */ |
||
| 366 | 640 | public function get($key) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Isset |
||
| 377 | * |
||
| 378 | * @param string $key |
||
| 379 | * @return bool |
||
| 380 | */ |
||
| 381 | 640 | public function contains($key) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Unset |
||
| 393 | * |
||
| 394 | * @param string $key |
||
| 395 | * @return void |
||
| 396 | */ |
||
| 397 | 639 | public function delete($key) |
|
| 403 | } |
||
| 404 |