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 |
||
| 16 | { |
||
| 17 | /* ------------------------------------------------------------------------------------------------ |
||
| 18 | | Properties |
||
| 19 | | ------------------------------------------------------------------------------------------------ |
||
| 20 | */ |
||
| 21 | /** @var PhpSession */ |
||
| 22 | private $session; |
||
| 23 | |||
| 24 | private $sessionInfo = []; |
||
| 25 | |||
| 26 | /* ------------------------------------------------------------------------------------------------ |
||
| 27 | | Constructor |
||
| 28 | | ------------------------------------------------------------------------------------------------ |
||
| 29 | */ |
||
| 30 | public function __construct() |
||
| 34 | |||
| 35 | /* ------------------------------------------------------------------------------------------------ |
||
| 36 | | Getters & Setters |
||
| 37 | | ------------------------------------------------------------------------------------------------ |
||
| 38 | */ |
||
| 39 | /** |
||
| 40 | * Get the session key. |
||
| 41 | * |
||
| 42 | * @return string |
||
| 43 | */ |
||
| 44 | private function getSessionKey() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Set the session data. |
||
| 51 | * |
||
| 52 | * @param array $data |
||
| 53 | */ |
||
| 54 | private function setSessionData(array $data) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get the session id. |
||
| 65 | * |
||
| 66 | * @return int |
||
| 67 | */ |
||
| 68 | private function getSessionId() |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Set the session id. |
||
| 75 | * |
||
| 76 | * @param mixed $id |
||
| 77 | */ |
||
| 78 | private function setSessionId($id) |
||
| 84 | |||
| 85 | /* ------------------------------------------------------------------------------------------------ |
||
| 86 | | Main Functions |
||
| 87 | | ------------------------------------------------------------------------------------------------ |
||
| 88 | */ |
||
| 89 | /** |
||
| 90 | * Track the session. |
||
| 91 | * |
||
| 92 | * @param array $data |
||
| 93 | * |
||
| 94 | * @return int |
||
| 95 | */ |
||
| 96 | public function track(array $data) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Check the session data. |
||
| 105 | * |
||
| 106 | * @param array $newData |
||
| 107 | * @param array $currentData |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | public function checkData(array $newData, array $currentData) |
||
| 117 | |||
| 118 | /* ------------------------------------------------------------------------------------------------ |
||
| 119 | | Other Functions |
||
| 120 | | ------------------------------------------------------------------------------------------------ |
||
| 121 | */ |
||
| 122 | /** |
||
| 123 | * Update the session data. |
||
| 124 | * |
||
| 125 | * @param array $data |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | private function updateData(array $data) |
||
| 138 | |||
| 139 | private function getSessionData($column = null) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Check if user changed. |
||
| 148 | * |
||
| 149 | * @param array $data |
||
| 150 | * @param \Arcanedev\LaravelTracker\Models\Session $model |
||
| 151 | * |
||
| 152 | * @return \Arcanedev\LaravelTracker\Models\Session Session |
||
| 153 | */ |
||
| 154 | private function checkIfUserChanged(array $data, $model) |
||
| 167 | |||
| 168 | private function regenerateSystemSession($data = null) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Reset the session uuid. |
||
| 182 | * |
||
| 183 | * @param array|null $data |
||
| 184 | * |
||
| 185 | * @return array|null |
||
| 186 | */ |
||
| 187 | private function resetSessionUuid($data = null) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Put the session data. |
||
| 203 | * |
||
| 204 | * @param mixed $data |
||
| 205 | */ |
||
| 206 | private function putSessionData($data) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Check the session uuid. |
||
| 213 | */ |
||
| 214 | private function checkSessionUuid() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get the system session id. |
||
| 222 | * |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | private function getSystemSessionId() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | private function createSessionIfIsUnknown() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Check if the session is known. |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | private function sessionIsKnown() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Find a session by its uuid. |
||
| 274 | * |
||
| 275 | * @param string $uuid |
||
| 276 | * |
||
| 277 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
| 278 | */ |
||
| 279 | private function findByUuid($uuid) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Store the session information. |
||
| 286 | */ |
||
| 287 | private function storeSession() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Generate session data. |
||
| 294 | * |
||
| 295 | * @param array $sessionInfo |
||
| 296 | */ |
||
| 297 | private function generateSession($sessionInfo) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Check if the session data is reliable. |
||
| 310 | * |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | private function checkSessionDataIsReliable() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Ensure that the session data is complete. |
||
| 331 | */ |
||
| 332 | private function ensureSessionDataIsComplete() |
||
| 352 | } |
||
| 353 |
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.