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 manager constructor. |
||
| 52 | * |
||
| 53 | * @param Configuration $configuration |
||
| 54 | * @param Handler|null $sessionHandler |
||
| 55 | * |
||
| 56 | * @throws \RuntimeException |
||
| 57 | */ |
||
| 58 | public function __construct(Configuration $configuration, Handler $sessionHandler = null) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | public function getConfiguration() : Configuration |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | public function getSessionId() : string |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | * |
||
| 95 | * @throws \RuntimeException |
||
| 96 | */ |
||
| 97 | public function setSessionId(string $sessionId) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | * |
||
| 111 | * @throws \RuntimeException |
||
| 112 | * |
||
| 113 | * @return array|null |
||
| 114 | */ |
||
| 115 | public function sessionStart() : array |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Configure session settings. |
||
| 143 | */ |
||
| 144 | protected function configureSession() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Initialize session. |
||
| 161 | * |
||
| 162 | * @throws \RuntimeException |
||
| 163 | */ |
||
| 164 | final protected function initializeSession() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Retrieve session saved data. |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | * |
||
| 190 | * @SuppressWarnings(PHPMD.Superglobals) |
||
| 191 | */ |
||
| 192 | final protected function loadSessionData() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * {@inheritdoc} |
||
| 209 | * |
||
| 210 | * @throws \RuntimeException |
||
| 211 | * |
||
| 212 | * @SuppressWarnings(PMD.Superglobals) |
||
| 213 | */ |
||
| 214 | public function sessionRegenerateId() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | * |
||
| 240 | * @throws \RuntimeException |
||
| 241 | * |
||
| 242 | * @SuppressWarnings(PMD.Superglobals) |
||
| 243 | */ |
||
| 244 | public function sessionEnd(array $data = []) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * {@inheritdoc} |
||
| 273 | * |
||
| 274 | * @throws \RuntimeException |
||
| 275 | * |
||
| 276 | * @SuppressWarnings(PMD.Superglobals) |
||
| 277 | */ |
||
| 278 | public function sessionDestroy() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * {@inheritdoc} |
||
| 299 | */ |
||
| 300 | public function isSessionStarted() : bool |
||
| 304 | |||
| 305 | /** |
||
| 306 | * {@inheritdoc} |
||
| 307 | */ |
||
| 308 | public function shouldRegenerateId() : bool |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Verify session ini settings. |
||
| 315 | * |
||
| 316 | * @throws \RuntimeException |
||
| 317 | */ |
||
| 318 | final protected function verifyIniSettings() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Generates cryptographically secure session identifier. |
||
| 343 | * |
||
| 344 | * @param int $length |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | private function getNewSessionId($length = Configuration::SESSION_ID_LENGTH) |
||
| 356 | } |
||
| 357 |