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 | 18 | public function __construct(SessionStore $session) |
|
| 41 | |||
| 42 | /* ------------------------------------------------------------------------------------------------ |
||
| 43 | | Getters & Setters |
||
| 44 | | ------------------------------------------------------------------------------------------------ |
||
| 45 | */ |
||
| 46 | /** |
||
| 47 | * Get the session key. |
||
| 48 | * |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | 12 | private function getSessionKey() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Set the session data. |
||
| 58 | * |
||
| 59 | * @param array $data |
||
| 60 | */ |
||
| 61 | 12 | private function setSessionData(array $data) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Get the session id. |
||
| 72 | * |
||
| 73 | * @return int |
||
| 74 | */ |
||
| 75 | 12 | private function getSessionId() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Set the session id. |
||
| 82 | * |
||
| 83 | * @param mixed $id |
||
| 84 | */ |
||
| 85 | 12 | private function setSessionId($id) |
|
| 89 | |||
| 90 | /* ------------------------------------------------------------------------------------------------ |
||
| 91 | | Main Functions |
||
| 92 | | ------------------------------------------------------------------------------------------------ |
||
| 93 | */ |
||
| 94 | /** |
||
| 95 | * Track the session. |
||
| 96 | * |
||
| 97 | * @param array $data |
||
| 98 | * |
||
| 99 | * @return int |
||
| 100 | */ |
||
| 101 | 12 | public function track(array $data) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Check the session data. |
||
| 110 | * |
||
| 111 | * @param array $currentData |
||
| 112 | * @param array $newData |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | 6 | public function checkData(array $currentData, array $newData) |
|
| 122 | |||
| 123 | /* ------------------------------------------------------------------------------------------------ |
||
| 124 | | Other Functions |
||
| 125 | | ------------------------------------------------------------------------------------------------ |
||
| 126 | */ |
||
| 127 | /** |
||
| 128 | * Update the session data. |
||
| 129 | * |
||
| 130 | * @param array $data |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | 6 | private function updateData(array $data) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Get the session data. |
||
| 144 | * |
||
| 145 | * @param string|null $column |
||
| 146 | * |
||
| 147 | * @return mixed |
||
| 148 | */ |
||
| 149 | 12 | private function getSessionData($column = null) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Check if user changed. |
||
| 158 | * |
||
| 159 | * @param array $data |
||
| 160 | * |
||
| 161 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
| 162 | */ |
||
| 163 | 6 | private function checkIfUserChanged(array $data) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Create session for guest. |
||
| 184 | * |
||
| 185 | * @param array $data |
||
| 186 | * |
||
| 187 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
| 188 | */ |
||
| 189 | 6 | private function createSessionForGuest(array $data) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Regenerate system session. |
||
| 203 | * |
||
| 204 | * @param array|null $data |
||
| 205 | * |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | private function regenerateSystemSession($data = null) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Reset the session uuid. |
||
| 222 | * |
||
| 223 | * @param array|null $data |
||
| 224 | * |
||
| 225 | * @return array|null |
||
| 226 | */ |
||
| 227 | private function resetSessionUuid($data = null) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Put the session data. |
||
| 243 | * |
||
| 244 | * @param array $data |
||
| 245 | */ |
||
| 246 | 12 | private function putSessionData(array $data) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Check the session uuid. |
||
| 255 | */ |
||
| 256 | 12 | private function checkSessionUuid() |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Get the system session id. |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | 12 | private function getSystemSessionId() |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | 12 | private function createSessionIfIsUnknown() |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Check if the session is known. |
||
| 296 | * |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | 12 | private function sessionIsKnown() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Find a session by its uuid. |
||
| 315 | * |
||
| 316 | * @param string $uuid |
||
| 317 | * |
||
| 318 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
| 319 | */ |
||
| 320 | private function findByUuid($uuid) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Generate session data. |
||
| 327 | * |
||
| 328 | * @param array $sessionInfo |
||
| 329 | */ |
||
| 330 | 12 | private function generateSession(array $sessionInfo) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Check if the session data is reliable. |
||
| 342 | * |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | 12 | private function checkSessionDataIsReliable() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Ensure that the session data is complete. |
||
| 363 | */ |
||
| 364 | private function ensureSessionDataIsComplete() |
||
| 385 | } |
||
| 386 |
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.