Complex classes like Tracker 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 Tracker, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Arcanedev\LaravelTracker; |
||
20 | class Tracker implements TrackerContract |
||
21 | { |
||
22 | /* ------------------------------------------------------------------------------------------------ |
||
23 | | Properties |
||
24 | | ------------------------------------------------------------------------------------------------ |
||
25 | */ |
||
26 | /** |
||
27 | * The application container. |
||
28 | * |
||
29 | * @var \Illuminate\Contracts\Foundation\Application |
||
30 | */ |
||
31 | protected $app; |
||
32 | |||
33 | /** |
||
34 | * The request instance. |
||
35 | * |
||
36 | * @var \Illuminate\Http\Request |
||
37 | */ |
||
38 | private $request; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | protected $enabled = false; |
||
44 | |||
45 | /* ------------------------------------------------------------------------------------------------ |
||
46 | | Constructor |
||
47 | | ------------------------------------------------------------------------------------------------ |
||
48 | */ |
||
49 | /** |
||
50 | * Tracker constructor. |
||
51 | * |
||
52 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
53 | */ |
||
54 | 24 | public function __construct(Application $app) |
|
59 | |||
60 | /* ------------------------------------------------------------------------------------------------ |
||
61 | | Getters & Setters |
||
62 | | ------------------------------------------------------------------------------------------------ |
||
63 | */ |
||
64 | /** |
||
65 | * Get the config repository. |
||
66 | * |
||
67 | * @return \Illuminate\Contracts\Config\Repository |
||
68 | */ |
||
69 | 24 | private function config() |
|
73 | |||
74 | /** |
||
75 | * Get the tracker config. |
||
76 | * |
||
77 | * @param string $key |
||
78 | * @param mixed|null $default |
||
79 | * |
||
80 | * @return mixed |
||
81 | */ |
||
82 | 24 | private function getConfig($key, $default = null) |
|
86 | |||
87 | /** |
||
88 | * Set the request. |
||
89 | * |
||
90 | * @param \Illuminate\Http\Request $request |
||
91 | * |
||
92 | * @return self |
||
93 | */ |
||
94 | 6 | private function setRequest(Request $request) |
|
100 | |||
101 | /* ------------------------------------------------------------------------------------------------ |
||
102 | | Main Functions |
||
103 | | ------------------------------------------------------------------------------------------------ |
||
104 | */ |
||
105 | /** |
||
106 | * Start the tracking. |
||
107 | * |
||
108 | * @param \Illuminate\Http\Request $request |
||
109 | */ |
||
110 | 6 | public function track(Request $request) |
|
118 | |||
119 | /** |
||
120 | * Enable the tracker. |
||
121 | */ |
||
122 | 6 | public function enable() |
|
127 | |||
128 | /** |
||
129 | * Disable the tracker. |
||
130 | */ |
||
131 | 6 | public function disable() |
|
136 | |||
137 | /* ------------------------------------------------------------------------------------------------ |
||
138 | | Check Functions |
||
139 | | ------------------------------------------------------------------------------------------------ |
||
140 | */ |
||
141 | /** |
||
142 | * Check if the tracker is enabled. |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | 24 | public function isEnabled() |
|
150 | |||
151 | /* ------------------------------------------------------------------------------------------------ |
||
152 | | Other Functions |
||
153 | | ------------------------------------------------------------------------------------------------ |
||
154 | */ |
||
155 | /** |
||
156 | * Get the session log data. |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | 6 | protected function getSessionActivityData() |
|
174 | |||
175 | 6 | protected function getSessionId($updateLastActivity = false) |
|
179 | |||
180 | /** |
||
181 | * @return array |
||
182 | */ |
||
183 | protected function makeSessionData() |
||
201 | |||
202 | 6 | private function getPathId() |
|
206 | |||
207 | 6 | private function getQueryId() |
|
211 | |||
212 | /** |
||
213 | * Get the user id. |
||
214 | * |
||
215 | * @return int|null |
||
216 | */ |
||
217 | private function getUserId() |
||
223 | |||
224 | /** |
||
225 | * Get the device id. |
||
226 | * |
||
227 | * @return int|null |
||
228 | */ |
||
229 | private function getDeviceId() |
||
241 | |||
242 | /** |
||
243 | * Get the current device properties. |
||
244 | * |
||
245 | * @return array |
||
246 | */ |
||
247 | public function getCurrentDeviceProperties() |
||
258 | |||
259 | private function getGeoIpId() |
||
275 | |||
276 | private function getAgentId() |
||
287 | |||
288 | 6 | private function getRefererId() |
|
329 | |||
330 | /** |
||
331 | * Get the domain id. |
||
332 | * |
||
333 | * @param string $name |
||
334 | * |
||
335 | * @return int |
||
336 | */ |
||
337 | private function getDomainId($name) |
||
343 | |||
344 | private function storeSearchTerms($refererId, $searchTerms) |
||
354 | |||
355 | private function getCookieId() |
||
369 | |||
370 | private function getLanguageId() |
||
380 | |||
381 | /** |
||
382 | * @return bool |
||
383 | */ |
||
384 | protected function isRobot() |
||
391 | |||
392 | /** |
||
393 | * Get the user agent parser. |
||
394 | * |
||
395 | * @return \Arcanedev\LaravelTracker\Contracts\Parsers\UserAgentParser |
||
396 | */ |
||
397 | public function getUserAgentParser() |
||
401 | |||
402 | private function getCurrentUserAgent() |
||
406 | |||
407 | private function getCurrentUserAgentData() |
||
415 | } |
||
416 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.