Complex classes like TrackingManager 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 TrackingManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Arcanedev\LaravelTracker; |
||
| 14 | class TrackingManager implements TrackingManagerContract |
||
| 15 | { |
||
| 16 | /* ------------------------------------------------------------------------------------------------ |
||
| 17 | | Properties |
||
| 18 | | ------------------------------------------------------------------------------------------------ |
||
| 19 | */ |
||
| 20 | /** |
||
| 21 | * The application instance. |
||
| 22 | * |
||
| 23 | * @var \Illuminate\Contracts\Foundation\Application |
||
| 24 | */ |
||
| 25 | private $app; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The request instance. |
||
| 29 | * |
||
| 30 | * @var \Illuminate\Http\Request |
||
| 31 | */ |
||
| 32 | private $request; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Tracking enabled status. |
||
| 36 | * |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | protected $enabled = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The current session data. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $sessionData = [ |
||
| 47 | 'user_id' => null, |
||
| 48 | 'device_id' => null, |
||
| 49 | 'agent_id' => null, |
||
| 50 | 'geoip_id' => null, |
||
| 51 | 'referrer_id' => null, |
||
| 52 | 'cookie_id' => null, |
||
| 53 | 'language_id' => null, |
||
| 54 | 'client_ip' => '', |
||
| 55 | 'is_robot' => false, |
||
| 56 | 'user_agent' => '', |
||
| 57 | ]; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The current session activity data. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $sessionActivityData = [ |
||
| 65 | 'session_id' => null, |
||
| 66 | 'path_id' => null, |
||
| 67 | 'query_id' => null, |
||
| 68 | 'referrer_id' => null, |
||
| 69 | 'route_path_id' => null, |
||
| 70 | 'error_id' => null, |
||
| 71 | 'method' => '', |
||
| 72 | 'is_ajax' => false, |
||
| 73 | 'is_secure' => false, |
||
| 74 | 'is_json' => false, |
||
| 75 | 'wants_json' => false, |
||
| 76 | ]; |
||
| 77 | |||
| 78 | /* ------------------------------------------------------------------------------------------------ |
||
| 79 | | Constructor |
||
| 80 | | ------------------------------------------------------------------------------------------------ |
||
| 81 | */ |
||
| 82 | /** |
||
| 83 | * TrackingManager constructor. |
||
| 84 | * |
||
| 85 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 86 | */ |
||
| 87 | 222 | public function __construct(Application $app) |
|
| 92 | |||
| 93 | /* ------------------------------------------------------------------------------------------------ |
||
| 94 | | Getters & Setters |
||
| 95 | | ------------------------------------------------------------------------------------------------ |
||
| 96 | */ |
||
| 97 | /** |
||
| 98 | * Get the config repository. |
||
| 99 | * |
||
| 100 | * @return \Illuminate\Contracts\Config\Repository |
||
| 101 | */ |
||
| 102 | 222 | private function config() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Get the tracker config. |
||
| 109 | * |
||
| 110 | * @param string $key |
||
| 111 | * @param mixed|null $default |
||
| 112 | * |
||
| 113 | * @return mixed |
||
| 114 | */ |
||
| 115 | 222 | private function getConfig($key, $default = null) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Set the request. |
||
| 122 | * |
||
| 123 | * @param \Illuminate\Http\Request $request |
||
| 124 | * |
||
| 125 | * @return self |
||
| 126 | */ |
||
| 127 | 12 | private function setRequest(Request $request) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Merge session data. |
||
| 144 | * |
||
| 145 | * @param array $data |
||
| 146 | * |
||
| 147 | * @return self |
||
| 148 | */ |
||
| 149 | 12 | private function mergeSessionData(array $data) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Merge session activity data. |
||
| 158 | * |
||
| 159 | * @param array $data |
||
| 160 | * |
||
| 161 | * @return self |
||
| 162 | */ |
||
| 163 | 16 | private function mergeSessionActivityData(array $data) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Get the cookie tracker. |
||
| 172 | * |
||
| 173 | * @return \Arcanedev\LaravelTracker\Contracts\Trackers\CookieTracker |
||
| 174 | */ |
||
| 175 | private function getCookieTracker() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get the device tracker. |
||
| 182 | * |
||
| 183 | * @return \Arcanedev\LaravelTracker\Contracts\Trackers\DeviceTracker |
||
| 184 | */ |
||
| 185 | 12 | private function getDeviceTracker() |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Get the error tracker. |
||
| 192 | * |
||
| 193 | * @return \Arcanedev\LaravelTracker\Contracts\Trackers\ErrorTracker |
||
| 194 | */ |
||
| 195 | 4 | private function getErrorTracker() |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Get the geoip tracker. |
||
| 202 | * |
||
| 203 | * @return \Arcanedev\LaravelTracker\Contracts\Trackers\GeoIpTracker |
||
| 204 | */ |
||
| 205 | 12 | private function getGeoIpTracker() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Get the language tracker. |
||
| 212 | * |
||
| 213 | * @return \Arcanedev\LaravelTracker\Contracts\Trackers\LanguageTracker |
||
| 214 | */ |
||
| 215 | 12 | private function getLanguageTracker() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Get the path tracker. |
||
| 222 | * |
||
| 223 | * @return \Arcanedev\LaravelTracker\Contracts\Trackers\PathTracker |
||
| 224 | */ |
||
| 225 | 12 | private function getPathTracker() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Get the query tracker. |
||
| 232 | * |
||
| 233 | * @return \Arcanedev\LaravelTracker\Contracts\Trackers\QueryTracker |
||
| 234 | */ |
||
| 235 | 12 | private function getQueryTracker() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Get the referer tracker. |
||
| 242 | * |
||
| 243 | * @return \Arcanedev\LaravelTracker\Contracts\Trackers\RefererTracker |
||
| 244 | */ |
||
| 245 | 12 | private function getRefererTracker() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Get the session tracker. |
||
| 252 | * |
||
| 253 | * @return \Arcanedev\LaravelTracker\Contracts\Trackers\SessionTracker |
||
| 254 | */ |
||
| 255 | 12 | private function getSessionTracker() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Get the session activity tracker. |
||
| 262 | * |
||
| 263 | * @return \Arcanedev\LaravelTracker\Contracts\Trackers\SessionActivityTracker |
||
| 264 | */ |
||
| 265 | 12 | private function getSessionActivityTracker() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Get the route tracker. |
||
| 272 | * |
||
| 273 | * @return \Arcanedev\LaravelTracker\Contracts\Trackers\RouteTracker |
||
| 274 | */ |
||
| 275 | 12 | private function getRouteTracker() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Get the user agent tracker. |
||
| 282 | * |
||
| 283 | * @return \Arcanedev\LaravelTracker\Contracts\Trackers\UserAgentTracker |
||
| 284 | */ |
||
| 285 | 12 | private function getUserAgentTracker() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Get the user tracker. |
||
| 292 | * |
||
| 293 | * @return \Arcanedev\LaravelTracker\Contracts\Trackers\UserTracker |
||
| 294 | */ |
||
| 295 | 12 | private function getUserTracker() |
|
| 299 | |||
| 300 | /* ------------------------------------------------------------------------------------------------ |
||
| 301 | | Main Functions |
||
| 302 | | ------------------------------------------------------------------------------------------------ |
||
| 303 | */ |
||
| 304 | /** |
||
| 305 | * Start the tracking. |
||
| 306 | * |
||
| 307 | * @param \Illuminate\Http\Request $request |
||
| 308 | */ |
||
| 309 | 12 | public function trackRequest(Request $request) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Track the matched route. |
||
| 327 | * |
||
| 328 | * @param \Illuminate\Routing\Route $route |
||
| 329 | * @param \Illuminate\Http\Request $request |
||
| 330 | */ |
||
| 331 | 12 | public function trackMatchedRoute(Route $route, Request $request) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Track the exception. |
||
| 348 | * |
||
| 349 | * @param \Exception $exception |
||
| 350 | */ |
||
| 351 | 4 | public function trackException(\Exception $exception) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Enable the tracking. |
||
| 362 | */ |
||
| 363 | 6 | public function enable() |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Disable the tracking. |
||
| 370 | */ |
||
| 371 | 6 | public function disable() |
|
| 375 | |||
| 376 | /* ------------------------------------------------------------------------------------------------ |
||
| 377 | | Check Functions |
||
| 378 | | ------------------------------------------------------------------------------------------------ |
||
| 379 | */ |
||
| 380 | /** |
||
| 381 | * Check if the tracker is enabled. |
||
| 382 | * |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | 34 | public function isEnabled() |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Track the trackable if enabled. |
||
| 392 | * |
||
| 393 | * @param string $key |
||
| 394 | * @param \Closure $callback |
||
| 395 | * @param mixed|null $default |
||
| 396 | * |
||
| 397 | * @return mixed |
||
| 398 | */ |
||
| 399 | 16 | private function trackIfEnabled($key, \Closure $callback, $default = null) |
|
| 405 | |||
| 406 | /* ------------------------------------------------------------------------------------------------ |
||
| 407 | | Other Functions |
||
| 408 | | ------------------------------------------------------------------------------------------------ |
||
| 409 | */ |
||
| 410 | /** |
||
| 411 | * Get the stored session id. |
||
| 412 | * |
||
| 413 | * @return int |
||
| 414 | */ |
||
| 415 | 12 | private function getSessionId() |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Track the path. |
||
| 438 | * |
||
| 439 | * @return int|null |
||
| 440 | */ |
||
| 441 | 12 | private function getPathId() |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Track the query. |
||
| 452 | * |
||
| 453 | * @return int|null |
||
| 454 | */ |
||
| 455 | 12 | private function getQueryId() |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Get the user id. |
||
| 466 | * |
||
| 467 | * @return int|null |
||
| 468 | */ |
||
| 469 | 12 | private function getUserId() |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Get the tracked device id. |
||
| 478 | * |
||
| 479 | * @return int|null |
||
| 480 | */ |
||
| 481 | 12 | private function getDeviceId() |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Get the tracked ip address ip. |
||
| 490 | * |
||
| 491 | * @return int|null |
||
| 492 | */ |
||
| 493 | 12 | private function getGeoIpId() |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Get the tracked user agent id. |
||
| 504 | * |
||
| 505 | * @return int|null |
||
| 506 | */ |
||
| 507 | 12 | private function getAgentId() |
|
| 513 | |||
| 514 | /** |
||
| 515 | * Get the tracked referer id. |
||
| 516 | * |
||
| 517 | * @return int|null |
||
| 518 | */ |
||
| 519 | 12 | private function getRefererId() |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Get the tracked cookie id. |
||
| 531 | * |
||
| 532 | * @return int|null |
||
| 533 | */ |
||
| 534 | 12 | private function getCookieId() |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Get the tracked language id. |
||
| 545 | * |
||
| 546 | * @return int|null |
||
| 547 | */ |
||
| 548 | private function getLanguageId() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Check if the visitor is a robot. |
||
| 557 | * |
||
| 558 | * @return bool |
||
| 559 | */ |
||
| 560 | 12 | protected function isRobot() |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Get the tracker instance. |
||
| 570 | * |
||
| 571 | * @param string $abstract |
||
| 572 | * |
||
| 573 | * @return mixed |
||
| 574 | */ |
||
| 575 | 16 | private function make($abstract) |
|
| 579 | } |
||
| 580 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.