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 | 36 | public function __construct(SessionStore $session) |
|
| 38 | { |
||
| 39 | 36 | $this->session = $session; |
|
| 40 | 36 | } |
|
| 41 | |||
| 42 | /* ------------------------------------------------------------------------------------------------ |
||
| 43 | | Getters & Setters |
||
| 44 | | ------------------------------------------------------------------------------------------------ |
||
| 45 | */ |
||
| 46 | /** |
||
| 47 | * Get the session key. |
||
| 48 | * |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | 12 | private function getSessionKey() |
|
| 52 | { |
||
| 53 | 12 | return config('laravel-tracker.session.name', 'session_name_here'); |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Set the session data. |
||
| 58 | * |
||
| 59 | * @param array $data |
||
| 60 | */ |
||
| 61 | 12 | private function setSessionData(array $data) |
|
| 62 | { |
||
| 63 | 12 | $this->generateSession($data); |
|
| 64 | |||
| 65 | 12 | if ($this->createSessionIfIsUnknown()) { |
|
| 66 | $this->ensureSessionDataIsComplete(); |
||
| 67 | } |
||
| 68 | 12 | } |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Get the session id. |
||
| 72 | * |
||
| 73 | * @return int |
||
| 74 | */ |
||
| 75 | 12 | private function getSessionId() |
|
| 76 | { |
||
| 77 | 12 | return $this->sessionInfo['id']; |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set the session id. |
||
| 82 | * |
||
| 83 | * @param mixed $id |
||
| 84 | */ |
||
| 85 | 12 | private function setSessionId($id) |
|
| 86 | { |
||
| 87 | 12 | $this->sessionInfo['id'] = $id; |
|
| 88 | 12 | } |
|
| 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) |
|
| 102 | { |
||
| 103 | 12 | $this->setSessionData($data); |
|
| 104 | |||
| 105 | 12 | return $this->getSessionId(); |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Check the session data. |
||
| 110 | * |
||
| 111 | * @param array $newData |
||
| 112 | * @param array $currentData |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | 6 | public function checkData(array $newData, array $currentData) |
|
| 117 | { |
||
| 118 | 6 | return ($newData && $currentData && $newData !== $currentData) |
|
| 119 | 3 | ? $this->updateData($newData) |
|
| 120 | 6 | : $newData; |
|
| 121 | } |
||
| 122 | |||
| 123 | /* ------------------------------------------------------------------------------------------------ |
||
| 124 | | Other Functions |
||
| 125 | | ------------------------------------------------------------------------------------------------ |
||
| 126 | */ |
||
| 127 | /** |
||
| 128 | * Update the session data. |
||
| 129 | * |
||
| 130 | * @param array $data |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | private function updateData(array $data) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get the session data. |
||
| 146 | * |
||
| 147 | * @param string|null $column |
||
| 148 | * |
||
| 149 | * @return mixed |
||
| 150 | */ |
||
| 151 | 12 | private function getSessionData($column = null) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Check if user changed. |
||
| 160 | * |
||
| 161 | * @param array $data |
||
| 162 | * @param \Arcanedev\LaravelTracker\Models\Session $model |
||
| 163 | * |
||
| 164 | * @return \Arcanedev\LaravelTracker\Models\Session Session |
||
| 165 | */ |
||
| 166 | private function checkIfUserChanged(array $data, $model) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Regenerate system session. |
||
| 182 | * |
||
| 183 | * @param array|null $data |
||
| 184 | * |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | private function regenerateSystemSession($data = null) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Reset the session uuid. |
||
| 201 | * |
||
| 202 | * @param array|null $data |
||
| 203 | * |
||
| 204 | * @return array|null |
||
| 205 | */ |
||
| 206 | private function resetSessionUuid($data = null) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Put the session data. |
||
| 222 | * |
||
| 223 | * @param array $data |
||
| 224 | */ |
||
| 225 | 12 | private function putSessionData(array $data) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Check the session uuid. |
||
| 234 | */ |
||
| 235 | 12 | private function checkSessionUuid() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Get the system session id. |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | 12 | private function getSystemSessionId() |
|
| 252 | |||
| 253 | /** |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | 12 | private function createSessionIfIsUnknown() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Check if the session is known. |
||
| 277 | * |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | 12 | private function sessionIsKnown() |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Find a session by its uuid. |
||
| 296 | * |
||
| 297 | * @param string $uuid |
||
| 298 | * |
||
| 299 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
| 300 | */ |
||
| 301 | private function findByUuid($uuid) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Store the session information. |
||
| 308 | */ |
||
| 309 | 12 | private function storeSession() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Generate session data. |
||
| 316 | * |
||
| 317 | * @param array $sessionInfo |
||
| 318 | */ |
||
| 319 | 12 | private function generateSession(array $sessionInfo) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Check if the session data is reliable. |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | 12 | private function checkSessionDataIsReliable() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Ensure that the session data is complete. |
||
| 352 | */ |
||
| 353 | private function ensureSessionDataIsComplete() |
||
| 373 | } |
||
| 374 |
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.