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 |
||
| 22 | class Native implements Manager |
||
| 23 | { |
||
| 24 | use SessionIniSettingsTrait; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var Configuration |
||
| 28 | */ |
||
| 29 | protected $configuration; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $sessionId; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | protected $sessionStarted = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Session manager constructor. |
||
| 43 | * |
||
| 44 | * @param Configuration $configuration |
||
| 45 | * @param Handler $sessionHandler |
||
|
|
|||
| 46 | * |
||
| 47 | * @throws \RuntimeException |
||
| 48 | */ |
||
| 49 | public function __construct(Configuration $configuration, Handler $sessionHandler = null) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | public function getConfiguration() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | public function getSessionId() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | * |
||
| 93 | * @throws \RuntimeException |
||
| 94 | */ |
||
| 95 | public function setSessionId($sessionId) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | * |
||
| 109 | * @throws \RuntimeException |
||
| 110 | */ |
||
| 111 | public function sessionStart() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Initialize session. |
||
| 138 | * |
||
| 139 | * @throws \RuntimeException |
||
| 140 | */ |
||
| 141 | final protected function sessionInitialize() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | * |
||
| 163 | * @throws \RuntimeException |
||
| 164 | * |
||
| 165 | * @SuppressWarnings(PHPMD.Superglobals) |
||
| 166 | */ |
||
| 167 | public function loadSessionData() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritdoc} |
||
| 192 | * |
||
| 193 | * @throws \RuntimeException |
||
| 194 | * |
||
| 195 | * @SuppressWarnings(PMD.Superglobals) |
||
| 196 | */ |
||
| 197 | public function sessionEnd(array $data = []) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | * |
||
| 227 | * @throws \RuntimeException |
||
| 228 | * |
||
| 229 | * @SuppressWarnings(PMD.Superglobals) |
||
| 230 | */ |
||
| 231 | public function sessionReset() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | */ |
||
| 257 | public function isSessionStarted() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | */ |
||
| 265 | public function shouldRegenerate() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Check if running on CLI. |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | protected function isCli() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Verify session ini settings. |
||
| 282 | * |
||
| 283 | * @throws \RuntimeException |
||
| 284 | * |
||
| 285 | * @codeCoverageIgnore |
||
| 286 | */ |
||
| 287 | final protected function verifyIniSettings() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Configure session data serializer. |
||
| 312 | */ |
||
| 313 | protected function configureSessionSerializer() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Configure session timeout. |
||
| 327 | */ |
||
| 328 | protected function configureSessionGarbageCollector() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Configure session save handler. |
||
| 335 | * |
||
| 336 | * @param Handler $sessionHandler |
||
| 337 | */ |
||
| 338 | protected function configureSessionSaveHandler(Handler $sessionHandler = null) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Generates cryptographically secure session identifier. |
||
| 352 | * |
||
| 353 | * @param int $length |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | final protected function getNewSessionId($length = Configuration::SESSION_ID_LENGTH) |
||
| 365 | } |
||
| 366 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.