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 & Setters |
||
| 26 | | ------------------------------------------------------------------------------------------------ |
||
| 27 | */ |
||
| 28 | /** |
||
| 29 | * Get the session key. |
||
| 30 | * |
||
| 31 | * @return string |
||
| 32 | */ |
||
| 33 | 12 | private function getSessionKey() |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Set the session data. |
||
| 40 | * |
||
| 41 | * @param array $data |
||
| 42 | */ |
||
| 43 | 12 | private function setSessionData(array $data) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Get the session id. |
||
| 54 | * |
||
| 55 | * @return int |
||
| 56 | */ |
||
| 57 | 12 | private function getSessionId() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Set the session id. |
||
| 64 | * |
||
| 65 | * @param mixed $id |
||
| 66 | */ |
||
| 67 | 12 | private function setSessionId($id) |
|
| 71 | |||
| 72 | /* ------------------------------------------------------------------------------------------------ |
||
| 73 | | Main Functions |
||
| 74 | | ------------------------------------------------------------------------------------------------ |
||
| 75 | */ |
||
| 76 | /** |
||
| 77 | * Track the session. |
||
| 78 | * |
||
| 79 | * @param array $data |
||
| 80 | * |
||
| 81 | * @return int |
||
| 82 | */ |
||
| 83 | 12 | public function track(array $data) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Check the session data. |
||
| 92 | * |
||
| 93 | * @param array $currentData |
||
| 94 | * @param array $newData |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | 6 | public function checkData(array $currentData, array $newData) |
|
| 104 | |||
| 105 | /* ------------------------------------------------------------------------------------------------ |
||
| 106 | | Other Functions |
||
| 107 | | ------------------------------------------------------------------------------------------------ |
||
| 108 | */ |
||
| 109 | /** |
||
| 110 | * Update the session data. |
||
| 111 | * |
||
| 112 | * @param array $data |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | 6 | private function updateData(array $data) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Get the session data. |
||
| 126 | * |
||
| 127 | * @param string|null $column |
||
| 128 | * |
||
| 129 | * @return mixed |
||
| 130 | */ |
||
| 131 | 12 | private function getSessionData($column = null) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Check if user changed. |
||
| 140 | * |
||
| 141 | * @param array $data |
||
| 142 | * |
||
| 143 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
| 144 | */ |
||
| 145 | 6 | private function checkIfUserChanged(array $data) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Create session for guest. |
||
| 166 | * |
||
| 167 | * @param array $data |
||
| 168 | * |
||
| 169 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
| 170 | */ |
||
| 171 | 6 | private function createSessionForGuest(array $data) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Regenerate system session. |
||
| 185 | * |
||
| 186 | * @param array|null $data |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | private function regenerateSystemSession($data = null) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Reset the session uuid. |
||
| 204 | * |
||
| 205 | * @param array|null $data |
||
| 206 | * |
||
| 207 | * @return array|null |
||
| 208 | */ |
||
| 209 | private function resetSessionUuid($data = null) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Put the session data. |
||
| 225 | * |
||
| 226 | * @param array $data |
||
| 227 | */ |
||
| 228 | 12 | private function putSessionData(array $data) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Check the session uuid. |
||
| 237 | */ |
||
| 238 | 12 | private function checkSessionUuid() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Get the system session id. |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | 12 | private function getSystemSessionId() |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | 12 | private function createSessionIfIsUnknown() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Check if the session is known. |
||
| 278 | * |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | 12 | private function sessionIsKnown() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Find a session by its uuid. |
||
| 297 | * |
||
| 298 | * @param string $uuid |
||
| 299 | * |
||
| 300 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
| 301 | */ |
||
| 302 | private function findByUuid($uuid) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Generate session data. |
||
| 309 | * |
||
| 310 | * @param array $sessionInfo |
||
| 311 | */ |
||
| 312 | 12 | private function generateSession(array $sessionInfo) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Check if the session data is reliable. |
||
| 324 | * |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | 12 | private function checkSessionDataIsReliable() |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Ensure that the session data is complete. |
||
| 345 | */ |
||
| 346 | private function ensureSessionDataIsComplete() |
||
| 367 | } |
||
| 368 |
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.