Complex classes like VisitorTracker 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 VisitorTracker, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Arcanedev\LaravelTracker\Trackers; |
||
| 15 | class VisitorTracker extends AbstractTracker implements VisitorTrackerContract |
||
| 16 | { |
||
| 17 | /* ----------------------------------------------------------------- |
||
| 18 | | Properties |
||
| 19 | | ----------------------------------------------------------------- |
||
| 20 | */ |
||
| 21 | |||
| 22 | /** @var array */ |
||
| 23 | private $visitorInfo = []; |
||
| 24 | |||
| 25 | /* ----------------------------------------------------------------- |
||
| 26 | | Getters and Setters |
||
| 27 | | ----------------------------------------------------------------- |
||
| 28 | */ |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Get the model. |
||
| 32 | * |
||
| 33 | * @return \Arcanedev\LaravelTracker\Models\Visitor |
||
| 34 | */ |
||
| 35 | 9 | protected function getModel() |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Get the session key. |
||
| 42 | * |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | 9 | private function getSessionKey() |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Set the visitor data. |
||
| 52 | * |
||
| 53 | * @param array $data |
||
| 54 | */ |
||
| 55 | 9 | private function setVisitorData(array $data) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Get the visitor id. |
||
| 66 | * |
||
| 67 | * @return int |
||
| 68 | */ |
||
| 69 | 9 | private function getVisitorId() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Set the visitor id. |
||
| 76 | * |
||
| 77 | * @param mixed $id |
||
| 78 | */ |
||
| 79 | 9 | private function setVisitorId($id) |
|
| 83 | |||
| 84 | /* ----------------------------------------------------------------- |
||
| 85 | | Main Methods |
||
| 86 | | ----------------------------------------------------------------- |
||
| 87 | */ |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Track the visitor. |
||
| 91 | * |
||
| 92 | * @param array $data |
||
| 93 | * |
||
| 94 | * @return int |
||
| 95 | */ |
||
| 96 | 9 | public function track(array $data) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Check the visitor data. |
||
| 105 | * |
||
| 106 | * @param array $currentData |
||
| 107 | * @param array $newData |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | 6 | public function checkData(array $currentData, array $newData) |
|
| 117 | |||
| 118 | /* ----------------------------------------------------------------- |
||
| 119 | | Other Methods |
||
| 120 | | ----------------------------------------------------------------- |
||
| 121 | */ |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Update the visitor data. |
||
| 125 | * |
||
| 126 | * @param array $data |
||
| 127 | * |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | 6 | private function updateData(array $data) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Get the visitor data. |
||
| 139 | * |
||
| 140 | * @param string|null $column |
||
| 141 | * |
||
| 142 | * @return mixed |
||
| 143 | */ |
||
| 144 | 9 | private function getVisitorData($column = null) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Check if user changed. |
||
| 153 | * |
||
| 154 | * @param array $data |
||
| 155 | */ |
||
| 156 | 6 | private function checkIfUserChanged(array $data) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Regenerate visitor data for the system. |
||
| 174 | * |
||
| 175 | * @param array|null $data |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | private function regenerateSystemVisitor($data = null) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Reset the visitor uuid. |
||
| 193 | * |
||
| 194 | * @param array|null $data |
||
| 195 | * |
||
| 196 | * @return array|null |
||
| 197 | */ |
||
| 198 | private function resetVisitorUuid($data = null) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Put the session data. |
||
| 214 | * |
||
| 215 | * @param array $data |
||
| 216 | */ |
||
| 217 | 9 | private function putSessionData(array $data) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Check the visitor uuid. |
||
| 226 | */ |
||
| 227 | 9 | private function checkVisitorUuid() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Get the visitor id from the system. |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | 9 | private function getVisitorIdFromSystem() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Create a new visitor if is unknown. |
||
| 245 | * |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | 9 | private function createVisitorIfIsUnknown() |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Check if the visitor is known. |
||
| 274 | * |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | 9 | private function isVisitorKnown() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Find a visitor by its uuid. |
||
| 293 | * |
||
| 294 | * @param string $uuid |
||
| 295 | * |
||
| 296 | * @return \Arcanedev\LaravelTracker\Models\Visitor |
||
| 297 | */ |
||
| 298 | private function findByUuid($uuid) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Generate visitor data. |
||
| 305 | * |
||
| 306 | * @param array $visitorInfo |
||
| 307 | */ |
||
| 308 | 9 | private function generateVisitor(array $visitorInfo) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Check if the visitor data is reliable. |
||
| 320 | * |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | 9 | private function checkVisitorDataIsReliable() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Check the data is unreliable. |
||
| 336 | * |
||
| 337 | * @param array|null $data |
||
| 338 | * @param string $key |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | 9 | private function checkDataIsUnreliable($data, $key) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Ensure that the visitor data is complete. |
||
| 349 | */ |
||
| 350 | private function ensureVisitorDataIsComplete() |
||
| 371 | } |
||
| 372 |
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.