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; |
||
| 16 | class SessionTracker implements SessionTrackerContract |
||
| 17 | { |
||
| 18 | /* ------------------------------------------------------------------------------------------------ |
||
| 19 | | Properties |
||
| 20 | | ------------------------------------------------------------------------------------------------ |
||
| 21 | */ |
||
| 22 | /** @var \Illuminate\Session\Store */ |
||
| 23 | private $session; |
||
| 24 | |||
| 25 | /** @var array */ |
||
| 26 | private $sessionInfo = []; |
||
| 27 | |||
| 28 | /* ------------------------------------------------------------------------------------------------ |
||
| 29 | | Constructor |
||
| 30 | | ------------------------------------------------------------------------------------------------ |
||
| 31 | */ |
||
| 32 | /** |
||
| 33 | * SessionTracker constructor. |
||
| 34 | * |
||
| 35 | * @param \Illuminate\Session\Store $session |
||
| 36 | */ |
||
| 37 | 24 | public function __construct(SessionStore $session) |
|
| 41 | |||
| 42 | /* ------------------------------------------------------------------------------------------------ |
||
| 43 | | Getters & Setters |
||
| 44 | | ------------------------------------------------------------------------------------------------ |
||
| 45 | */ |
||
| 46 | /** |
||
| 47 | * Get the session key. |
||
| 48 | * |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | 6 | private function getSessionKey() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Set the session data. |
||
| 58 | * |
||
| 59 | * @param array $data |
||
| 60 | */ |
||
| 61 | 6 | private function setSessionData(array $data) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Get the session id. |
||
| 72 | * |
||
| 73 | * @return int |
||
| 74 | */ |
||
| 75 | 2 | private function getSessionId() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Set the session id. |
||
| 82 | * |
||
| 83 | * @param mixed $id |
||
| 84 | */ |
||
| 85 | 2 | private function setSessionId($id) |
|
| 90 | |||
| 91 | /* ------------------------------------------------------------------------------------------------ |
||
| 92 | | Main Functions |
||
| 93 | | ------------------------------------------------------------------------------------------------ |
||
| 94 | */ |
||
| 95 | /** |
||
| 96 | * Track the session. |
||
| 97 | * |
||
| 98 | * @param array $data |
||
| 99 | * |
||
| 100 | * @return int |
||
| 101 | */ |
||
| 102 | 6 | public function track(array $data) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Check the session data. |
||
| 111 | * |
||
| 112 | * @param array $newData |
||
| 113 | * @param array $currentData |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | 6 | public function checkData(array $newData, array $currentData) |
|
| 123 | |||
| 124 | /* ------------------------------------------------------------------------------------------------ |
||
| 125 | | Other Functions |
||
| 126 | | ------------------------------------------------------------------------------------------------ |
||
| 127 | */ |
||
| 128 | /** |
||
| 129 | * Update the session data. |
||
| 130 | * |
||
| 131 | * @param array $data |
||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | private function updateData(array $data) |
||
| 144 | |||
| 145 | 6 | private function getSessionData($column = null) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Check if user changed. |
||
| 154 | * |
||
| 155 | * @param array $data |
||
| 156 | * @param \Arcanedev\LaravelTracker\Models\Session $model |
||
| 157 | * |
||
| 158 | * @return \Arcanedev\LaravelTracker\Models\Session Session |
||
| 159 | */ |
||
| 160 | private function checkIfUserChanged(array $data, $model) |
||
| 173 | |||
| 174 | private function regenerateSystemSession($data = null) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Reset the session uuid. |
||
| 188 | * |
||
| 189 | * @param array|null $data |
||
| 190 | * |
||
| 191 | * @return array|null |
||
| 192 | */ |
||
| 193 | private function resetSessionUuid($data = null) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Put the session data. |
||
| 209 | * |
||
| 210 | * @param mixed $data |
||
| 211 | */ |
||
| 212 | 2 | private function putSessionData($data) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Check the session uuid. |
||
| 221 | */ |
||
| 222 | 6 | private function checkSessionUuid() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Get the system session id. |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | 6 | private function getSystemSessionId() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | 6 | private function createSessionIfIsUnknown() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Check if the session is known. |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | 6 | private function sessionIsKnown() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Find a session by its uuid. |
||
| 286 | * |
||
| 287 | * @param string $uuid |
||
| 288 | * |
||
| 289 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
| 290 | */ |
||
| 291 | private function findByUuid($uuid) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Store the session information. |
||
| 298 | */ |
||
| 299 | 2 | private function storeSession() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Generate session data. |
||
| 306 | * |
||
| 307 | * @param array $sessionInfo |
||
| 308 | */ |
||
| 309 | 6 | private function generateSession($sessionInfo) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Check if the session data is reliable. |
||
| 322 | * |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | 6 | private function checkSessionDataIsReliable() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Ensure that the session data is complete. |
||
| 343 | */ |
||
| 344 | private function ensureSessionDataIsComplete() |
||
| 364 | } |
||
| 365 |
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.