Complex classes like SessionTracker 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 SessionTracker, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Arcanedev\LaravelTracker\Trackers; |
||
15 | class SessionTracker extends AbstractTracker implements SessionTrackerContract |
||
16 | { |
||
17 | /* ------------------------------------------------------------------------------------------------ |
||
18 | | Properties |
||
19 | | ------------------------------------------------------------------------------------------------ |
||
20 | */ |
||
21 | /** @var array */ |
||
22 | private $sessionInfo = []; |
||
23 | |||
24 | /* ------------------------------------------------------------------------------------------------ |
||
25 | | Getters and Setters |
||
26 | | ------------------------------------------------------------------------------------------------ |
||
27 | */ |
||
28 | /** |
||
29 | * Get the model. |
||
30 | * |
||
31 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
32 | */ |
||
33 | 18 | protected function getModel() |
|
37 | |||
38 | /** |
||
39 | * Get the session key. |
||
40 | * |
||
41 | * @return string |
||
42 | */ |
||
43 | 18 | private function getSessionKey() |
|
47 | |||
48 | /** |
||
49 | * Set the session data. |
||
50 | * |
||
51 | * @param array $data |
||
52 | */ |
||
53 | 18 | private function setSessionData(array $data) |
|
61 | |||
62 | /** |
||
63 | * Get the session id. |
||
64 | * |
||
65 | * @return int |
||
66 | */ |
||
67 | 18 | private function getSessionId() |
|
71 | |||
72 | /** |
||
73 | * Set the session id. |
||
74 | * |
||
75 | * @param mixed $id |
||
76 | */ |
||
77 | 18 | private function setSessionId($id) |
|
81 | |||
82 | /* ------------------------------------------------------------------------------------------------ |
||
83 | | Main Functions |
||
84 | | ------------------------------------------------------------------------------------------------ |
||
85 | */ |
||
86 | /** |
||
87 | * Track the session. |
||
88 | * |
||
89 | * @param array $data |
||
90 | * |
||
91 | * @return int |
||
92 | */ |
||
93 | 18 | public function track(array $data) |
|
99 | |||
100 | /** |
||
101 | * Check the session data. |
||
102 | * |
||
103 | * @param array $currentData |
||
104 | * @param array $newData |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | 12 | public function checkData(array $currentData, array $newData) |
|
114 | |||
115 | /* ------------------------------------------------------------------------------------------------ |
||
116 | | Other Functions |
||
117 | | ------------------------------------------------------------------------------------------------ |
||
118 | */ |
||
119 | /** |
||
120 | * Update the session data. |
||
121 | * |
||
122 | * @param array $data |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | 12 | private function updateData(array $data) |
|
132 | |||
133 | /** |
||
134 | * Get the session data. |
||
135 | * |
||
136 | * @param string|null $column |
||
137 | * |
||
138 | * @return mixed |
||
139 | */ |
||
140 | 18 | private function getSessionData($column = null) |
|
146 | |||
147 | /** |
||
148 | * Check if user changed. |
||
149 | * |
||
150 | * @param array $data |
||
151 | */ |
||
152 | 12 | private function checkIfUserChanged(array $data) |
|
167 | |||
168 | /** |
||
169 | * Regenerate system session. |
||
170 | * |
||
171 | * @param array|null $data |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | private function regenerateSystemSession($data = null) |
||
186 | |||
187 | /** |
||
188 | * Reset the session uuid. |
||
189 | * |
||
190 | * @param array|null $data |
||
191 | * |
||
192 | * @return array|null |
||
193 | */ |
||
194 | private function resetSessionUuid($data = null) |
||
207 | |||
208 | /** |
||
209 | * Put the session data. |
||
210 | * |
||
211 | * @param array $data |
||
212 | */ |
||
213 | 18 | private function putSessionData(array $data) |
|
219 | |||
220 | /** |
||
221 | * Check the session uuid. |
||
222 | */ |
||
223 | 18 | private function checkSessionUuid() |
|
228 | |||
229 | /** |
||
230 | * Get the system session id. |
||
231 | * |
||
232 | * @return string |
||
233 | */ |
||
234 | 18 | private function getSystemSessionId() |
|
238 | |||
239 | /** |
||
240 | * @return bool |
||
241 | */ |
||
242 | 18 | private function createSessionIfIsUnknown() |
|
261 | |||
262 | /** |
||
263 | * Check if the session is known. |
||
264 | * |
||
265 | * @return bool |
||
266 | */ |
||
267 | 18 | private function sessionIsKnown() |
|
280 | |||
281 | /** |
||
282 | * Find a session by its uuid. |
||
283 | * |
||
284 | * @param string $uuid |
||
285 | * |
||
286 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
287 | */ |
||
288 | private function findByUuid($uuid) |
||
292 | |||
293 | /** |
||
294 | * Generate session data. |
||
295 | * |
||
296 | * @param array $sessionInfo |
||
297 | */ |
||
298 | 18 | private function generateSession(array $sessionInfo) |
|
307 | |||
308 | /** |
||
309 | * Check if the session data is reliable. |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | 18 | private function checkSessionDataIsReliable() |
|
328 | |||
329 | /** |
||
330 | * Ensure that the session data is complete. |
||
331 | */ |
||
332 | private function ensureSessionDataIsComplete() |
||
353 | } |
||
354 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.