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) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Get the session data. |
||
| 136 | * |
||
| 137 | * @param string|null $column |
||
| 138 | * |
||
| 139 | * @return mixed |
||
| 140 | */ |
||
| 141 | 18 | private function getSessionData($column = null) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Check if user changed. |
||
| 150 | * |
||
| 151 | * @param array $data |
||
| 152 | * |
||
| 153 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
| 154 | */ |
||
| 155 | 12 | private function checkIfUserChanged(array $data) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Create session for guest. |
||
| 176 | * |
||
| 177 | * @param array $data |
||
| 178 | * |
||
| 179 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
| 180 | */ |
||
| 181 | 12 | private function createSessionForGuest(array $data) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Regenerate system session. |
||
| 196 | * |
||
| 197 | * @param array|null $data |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | private function regenerateSystemSession($data = null) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Reset the session uuid. |
||
| 215 | * |
||
| 216 | * @param array|null $data |
||
| 217 | * |
||
| 218 | * @return array|null |
||
| 219 | */ |
||
| 220 | private function resetSessionUuid($data = null) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Put the session data. |
||
| 236 | * |
||
| 237 | * @param array $data |
||
| 238 | */ |
||
| 239 | 18 | private function putSessionData(array $data) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Check the session uuid. |
||
| 248 | */ |
||
| 249 | 18 | private function checkSessionUuid() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Get the system session id. |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | 18 | private function getSystemSessionId() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | 18 | private function createSessionIfIsUnknown() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Check if the session is known. |
||
| 290 | * |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | 18 | private function sessionIsKnown() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Find a session by its uuid. |
||
| 309 | * |
||
| 310 | * @param string $uuid |
||
| 311 | * |
||
| 312 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
| 313 | */ |
||
| 314 | private function findByUuid($uuid) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Generate session data. |
||
| 321 | * |
||
| 322 | * @param array $sessionInfo |
||
| 323 | */ |
||
| 324 | 18 | private function generateSession(array $sessionInfo) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Check if the session data is reliable. |
||
| 336 | * |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | 18 | private function checkSessionDataIsReliable() |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Ensure that the session data is complete. |
||
| 357 | */ |
||
| 358 | private function ensureSessionDataIsComplete() |
||
| 379 | } |
||
| 380 |
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.