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 | /** @var array */ |
||
22 | private $visitorInfo = []; |
||
23 | |||
24 | /* ------------------------------------------------------------------------------------------------ |
||
25 | | Getters and Setters |
||
26 | | ------------------------------------------------------------------------------------------------ |
||
27 | */ |
||
28 | /** |
||
29 | * Get the model. |
||
30 | * |
||
31 | * @return \Arcanedev\LaravelTracker\Models\Visitor |
||
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 visitor data. |
||
50 | * |
||
51 | * @param array $data |
||
52 | */ |
||
53 | 18 | private function setVisitorData(array $data) |
|
54 | { |
||
55 | 18 | $this->generateVisitor($data); |
|
56 | |||
57 | 18 | if ($this->createVisitorIfIsUnknown()) { |
|
58 | $this->ensureVisitorDataIsComplete(); |
||
59 | } |
||
60 | 18 | } |
|
61 | |||
62 | /** |
||
63 | * Get the visitor id. |
||
64 | * |
||
65 | * @return int |
||
66 | */ |
||
67 | 18 | private function getVisitorId() |
|
68 | { |
||
69 | 18 | return $this->visitorInfo['id']; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Set the visitor id. |
||
74 | * |
||
75 | * @param mixed $id |
||
76 | */ |
||
77 | 18 | private function setVisitorId($id) |
|
78 | { |
||
79 | 18 | $this->visitorInfo['id'] = $id; |
|
80 | 18 | } |
|
81 | |||
82 | /* ------------------------------------------------------------------------------------------------ |
||
83 | | Main Functions |
||
84 | | ------------------------------------------------------------------------------------------------ |
||
85 | */ |
||
86 | /** |
||
87 | * Track the visitor. |
||
88 | * |
||
89 | * @param array $data |
||
90 | * |
||
91 | * @return int |
||
92 | */ |
||
93 | 18 | public function track(array $data) |
|
99 | |||
100 | /** |
||
101 | * Check the visitor 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 visitor data. |
||
121 | * |
||
122 | * @param array $data |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | 12 | private function updateData(array $data) |
|
132 | |||
133 | /** |
||
134 | * Get the visitor data. |
||
135 | * |
||
136 | * @param string|null $column |
||
137 | * |
||
138 | * @return mixed |
||
139 | */ |
||
140 | 18 | private function getVisitorData($column = null) |
|
141 | { |
||
142 | 18 | $data = $this->session()->get($this->getSessionKey()); |
|
143 | |||
144 | 18 | return is_null($column) ? $data : Arr::get($data, $column, null); |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * Check if user changed. |
||
149 | * |
||
150 | * @param array $data |
||
151 | */ |
||
152 | 12 | private function checkIfUserChanged(array $data) |
|
167 | |||
168 | /** |
||
169 | * Regenerate visitor data for the system. |
||
170 | * |
||
171 | * @param array|null $data |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | private function regenerateSystemVisitor($data = null) |
||
176 | { |
||
177 | $data = $data ?: $this->getVisitorData(); |
||
178 | |||
179 | if ($data) { |
||
180 | $this->resetVisitorUuid($data); |
||
181 | $this->createVisitorIfIsUnknown(); |
||
182 | } |
||
183 | |||
184 | return $this->visitorInfo; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Reset the visitor uuid. |
||
189 | * |
||
190 | * @param array|null $data |
||
191 | * |
||
192 | * @return array|null |
||
193 | */ |
||
194 | private function resetVisitorUuid($data = null) |
||
195 | { |
||
196 | $this->visitorInfo['uuid'] = null; |
||
197 | |||
198 | $data = $data ?: $this->visitorInfo; |
||
199 | |||
200 | unset($data['uuid']); |
||
201 | |||
202 | $this->putSessionData($data); |
||
203 | $this->checkVisitorUuid(); |
||
204 | |||
205 | return $data; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Put the session data. |
||
210 | * |
||
211 | * @param array $data |
||
212 | */ |
||
213 | 18 | private function putSessionData(array $data) |
|
219 | |||
220 | /** |
||
221 | * Check the visitor uuid. |
||
222 | */ |
||
223 | 18 | private function checkVisitorUuid() |
|
224 | { |
||
225 | 18 | if ( ! isset($this->visitorInfo['uuid']) || ! $this->visitorInfo['uuid']) |
|
228 | |||
229 | /** |
||
230 | * Get the visitor id from the system. |
||
231 | * |
||
232 | * @return string |
||
233 | */ |
||
234 | 18 | private function getVisitorIdFromSystem() |
|
238 | |||
239 | /** |
||
240 | * Create a new visitor if is unknown. |
||
241 | * |
||
242 | * @return bool |
||
243 | */ |
||
244 | 18 | private function createVisitorIfIsUnknown() |
|
265 | |||
266 | /** |
||
267 | * Check if the visitor is known. |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | 18 | private function isVisitorKnown() |
|
284 | |||
285 | /** |
||
286 | * Find a visitor by its uuid. |
||
287 | * |
||
288 | * @param string $uuid |
||
289 | * |
||
290 | * @return \Arcanedev\LaravelTracker\Models\Visitor |
||
291 | */ |
||
292 | private function findByUuid($uuid) |
||
296 | |||
297 | /** |
||
298 | * Generate visitor data. |
||
299 | * |
||
300 | * @param array $visitorInfo |
||
301 | */ |
||
302 | 18 | private function generateVisitor(array $visitorInfo) |
|
311 | |||
312 | /** |
||
313 | * Check if the visitor data is reliable. |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | 18 | private function checkVisitorDataIsReliable() |
|
327 | |||
328 | /** |
||
329 | * Check the data is unreliable. |
||
330 | * |
||
331 | * @param array|null $data |
||
332 | * @param string $key |
||
333 | * |
||
334 | * @return bool |
||
335 | */ |
||
336 | 18 | private function checkDataIsUnreliable($data, $key) |
|
340 | |||
341 | /** |
||
342 | * Ensure that the visitor data is complete. |
||
343 | */ |
||
344 | private function ensureVisitorDataIsComplete() |
||
365 | } |
||
366 |
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.