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 = []) |
||
278 | |||
279 | /** |
||
280 | * {@inheritdoc} |
||
281 | * |
||
282 | * @throws \RuntimeException |
||
283 | * |
||
284 | * @SuppressWarnings(PMD.Superglobals) |
||
285 | */ |
||
286 | public function sessionDestroy() |
||
305 | |||
306 | /** |
||
307 | * {@inheritdoc} |
||
308 | */ |
||
309 | public function isSessionStarted() : bool |
||
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | */ |
||
317 | public function isSessionDestroyed() : bool |
||
321 | |||
322 | /** |
||
323 | * {@inheritdoc} |
||
324 | */ |
||
325 | public function shouldRegenerateId() : bool |
||
329 | |||
330 | /** |
||
331 | * Verify session ini settings. |
||
332 | * |
||
333 | * @throws \RuntimeException |
||
334 | */ |
||
335 | final protected function verifyIniSettings() |
||
357 | |||
358 | /** |
||
359 | * Ini settings configuration helper. |
||
360 | */ |
||
361 | final public function configureIniSettings() |
||
369 | |||
370 | /** |
||
371 | * Generates cryptographically secure session identifier. |
||
372 | * |
||
373 | * @param int $length |
||
374 | * |
||
375 | * @return string |
||
376 | */ |
||
377 | private function getNewSessionId($length = Configuration::SESSION_ID_LENGTH) |
||
385 | } |
||
386 |