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() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Generates cryptographically secure session identifier. |
||
| 361 | * |
||
| 362 | * @param int $length |
||
| 363 | * |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | private function getNewSessionId($length = Configuration::SESSION_ID_LENGTH) |
||
| 374 | } |
||
| 375 |