Complex classes like Native 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 Native, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Native implements Manager |
||
| 25 | { |
||
| 26 | use NativeSessionTrait; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var Configuration |
||
| 30 | */ |
||
| 31 | protected $configuration; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Session handler. |
||
| 35 | * |
||
| 36 | * @var Handler |
||
| 37 | */ |
||
| 38 | protected $sessionHandler; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $sessionId; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | protected $sessionStarted = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Session has been destroyed. |
||
| 52 | * |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $sessionDestroyed = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Session manager constructor. |
||
| 59 | * |
||
| 60 | * @param Configuration $configuration |
||
| 61 | * @param Handler|null $sessionHandler |
||
| 62 | * |
||
| 63 | * @throws \RuntimeException |
||
| 64 | */ |
||
| 65 | public function __construct(Configuration $configuration, Handler $sessionHandler = null) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | public function getConfiguration() : Configuration |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | public function getSessionId() : string |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | * |
||
| 102 | * @throws \RuntimeException |
||
| 103 | */ |
||
| 104 | public function setSessionId(string $sessionId) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | * |
||
| 116 | * @throws \RuntimeException |
||
| 117 | * |
||
| 118 | * @return array|null |
||
| 119 | */ |
||
| 120 | public function sessionStart() : array |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Configure session settings. |
||
| 152 | */ |
||
| 153 | protected function configureSession() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Initialize session. |
||
| 170 | * |
||
| 171 | * @throws \RuntimeException |
||
| 172 | */ |
||
| 173 | final protected function initializeSession() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Retrieve session saved data. |
||
| 196 | * |
||
| 197 | * @return array |
||
| 198 | * |
||
| 199 | * @SuppressWarnings(PHPMD.Superglobals) |
||
| 200 | */ |
||
| 201 | final protected function loadSessionData() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritdoc} |
||
| 218 | * |
||
| 219 | * @throws \RuntimeException |
||
| 220 | * |
||
| 221 | * @SuppressWarnings(PMD.Superglobals) |
||
| 222 | */ |
||
| 223 | public function sessionRegenerateId() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritdoc} |
||
| 248 | * |
||
| 249 | * @throws \RuntimeException |
||
| 250 | * |
||
| 251 | * @SuppressWarnings(PMD.Superglobals) |
||
| 252 | */ |
||
| 253 | public function sessionEnd(array $data = []) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | * |
||
| 283 | * @throws \RuntimeException |
||
| 284 | * |
||
| 285 | * @SuppressWarnings(PMD.Superglobals) |
||
| 286 | */ |
||
| 287 | public function sessionDestroy() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * {@inheritdoc} |
||
| 309 | */ |
||
| 310 | public function isSessionStarted() : bool |
||
| 314 | |||
| 315 | /** |
||
| 316 | * {@inheritdoc} |
||
| 317 | */ |
||
| 318 | public function isSessionDestroyed() : bool |
||
| 322 | |||
| 323 | /** |
||
| 324 | * {@inheritdoc} |
||
| 325 | */ |
||
| 326 | public function shouldRegenerateId() : bool |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Verify session ini settings. |
||
| 333 | * |
||
| 334 | * @throws \RuntimeException |
||
| 335 | */ |
||
| 336 | final protected function verifyIniSettings() |
||
| 337 | { |
||
| 338 | if ($this->hasBoolIniSetting('use_trans_sid') !== false) { |
||
| 339 | throw new \RuntimeException('"session.use_trans_sid" ini setting must be set to false'); |
||
| 340 | } |
||
| 341 | |||
| 342 | if ($this->hasBoolIniSetting('use_cookies') !== true) { |
||
| 343 | throw new \RuntimeException('"session.use_cookies" ini setting must be set to true'); |
||
| 344 | } |
||
| 345 | |||
| 346 | if ($this->hasBoolIniSetting('use_only_cookies') !== true) { |
||
| 347 | throw new \RuntimeException('"session.use_only_cookies" ini setting must be set to true'); |
||
| 348 | } |
||
| 349 | |||
| 350 | if ($this->hasBoolIniSetting('use_strict_mode') !== false) { |
||
| 351 | throw new \RuntimeException('"session.use_strict_mode" ini setting must be set to false'); |
||
| 352 | } |
||
| 353 | |||
| 354 | if ($this->getStringIniSetting('cache_limiter') !== '') { |
||
| 355 | throw new \RuntimeException('"session.cache_limiter" ini setting must be set to empty string'); |
||
| 356 | } |
||
| 357 | } |
||
| 375 |