Complex classes like FieldProvider 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 FieldProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Understand\UnderstandLaravel5; |
||
| 9 | class FieldProvider |
||
| 10 | { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * The registered field providers. |
||
| 14 | * |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | protected $providers = []; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Default field |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $defaultProviders = [ |
||
| 25 | 'getSessionId', |
||
| 26 | 'getRouteName', |
||
| 27 | 'getUrl', |
||
| 28 | 'getRequestMethod', |
||
| 29 | 'getServerIp', |
||
| 30 | 'getClientIp', |
||
| 31 | 'getClientUserAgent', |
||
| 32 | 'getEnvironment', |
||
| 33 | 'getFromSession', |
||
| 34 | 'getProcessIdentifier', |
||
| 35 | 'getUserId', |
||
| 36 | 'getGroupId', |
||
| 37 | 'getLaravelVersion', |
||
| 38 | 'getSqlQueries', |
||
| 39 | 'getArtisanCommandName', |
||
| 40 | 'getRunningInConsole', |
||
| 41 | 'getLoggerVersion', |
||
| 42 | 'getPostDataArray', |
||
| 43 | 'getQueryStringArray', |
||
| 44 | ]; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Session store |
||
| 48 | * |
||
| 49 | * @var \Illuminate\Session\Store |
||
| 50 | */ |
||
| 51 | protected $session; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Router |
||
| 55 | * |
||
| 56 | * @var Router |
||
| 57 | */ |
||
| 58 | protected $router; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Server variable |
||
| 62 | * |
||
| 63 | * @var Request |
||
| 64 | */ |
||
| 65 | protected $request; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Token provider |
||
| 69 | * |
||
| 70 | * @var UniqueProcessIdentifier |
||
| 71 | */ |
||
| 72 | protected $tokenProvider; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Current environment |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $environment; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var DataCollector |
||
| 83 | */ |
||
| 84 | protected $dataCollector; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var Application |
||
| 88 | */ |
||
| 89 | protected $app; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return void |
||
|
|
|||
| 93 | */ |
||
| 94 | public function __construct() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param Application $app |
||
| 104 | */ |
||
| 105 | public function setApp($app) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Set session store |
||
| 112 | * |
||
| 113 | * @param type $service |
||
| 114 | */ |
||
| 115 | public function setSessionStore(SessionStore $service) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Set router |
||
| 122 | * |
||
| 123 | * @param Router $router |
||
| 124 | */ |
||
| 125 | public function setRouter($router) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Set request |
||
| 132 | * |
||
| 133 | * @param Request $request |
||
| 134 | */ |
||
| 135 | public function setRequest(Request $request) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set current environment |
||
| 142 | * |
||
| 143 | * @param string $environment |
||
| 144 | */ |
||
| 145 | public function setEnvironment($environment) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param DataCollector $dataCollector |
||
| 152 | */ |
||
| 153 | public function setDataCollector(DataCollector $dataCollector) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Register a custom HTML macro. |
||
| 160 | * |
||
| 161 | * @param string $name |
||
| 162 | * @param mixed $macro |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | public function extend($name, $provider) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set token provider |
||
| 172 | * |
||
| 173 | * @param UniqueProcessIdentifier $tokenProvider |
||
| 174 | */ |
||
| 175 | public function setTokenProvider(TokenProvider $tokenProvider) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Return resolved field-value array |
||
| 182 | * |
||
| 183 | * @param array $callbacks |
||
| 184 | * @param array $log |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | public function resolveValues(array $callbacks, array $log) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Handle class calls |
||
| 211 | * |
||
| 212 | * @param string $name |
||
| 213 | * @param mixed $params |
||
| 214 | * @return mixed |
||
| 215 | * |
||
| 216 | * @throws \BadMethodCallException |
||
| 217 | */ |
||
| 218 | public function __call($name, $params) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Return hashed version of session id |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | protected function getSessionId() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Detect if the application is Laravel or Lumen |
||
| 250 | * |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | protected function isLaravel() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get Laravel/Lumen version |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | protected function getLaravelVersion() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | protected function getSqlQueries() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param $queryArray |
||
| 316 | * @return mixed |
||
| 317 | */ |
||
| 318 | protected function mergeBindings($queryArray) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Return current route name |
||
| 358 | * |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | protected function getRouteName() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Return current url |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | protected function getUrl() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return array|null |
||
| 395 | */ |
||
| 396 | protected function getQueryStringArray() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return array|null |
||
| 429 | */ |
||
| 430 | protected function getPostDataArray() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param $key |
||
| 485 | * @param $value |
||
| 486 | * @return mixed|string |
||
| 487 | */ |
||
| 488 | protected function parseRequestFieldValue($key, $value) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Return request method |
||
| 517 | * |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | protected function getRequestMethod() |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Return server ip address |
||
| 532 | * |
||
| 533 | * @return string |
||
| 534 | */ |
||
| 535 | protected function getServerIp() |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Return client ip |
||
| 547 | * |
||
| 548 | * @return string |
||
| 549 | */ |
||
| 550 | protected function getClientIp() |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Return client user agent string |
||
| 562 | * |
||
| 563 | * @return string |
||
| 564 | */ |
||
| 565 | protected function getClientUserAgent() |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Return current enviroment |
||
| 577 | * |
||
| 578 | * @return string |
||
| 579 | */ |
||
| 580 | protected function getEnvironment() |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Retrive parameter from current session |
||
| 587 | * |
||
| 588 | * @param string $key |
||
| 589 | * @return string |
||
| 590 | */ |
||
| 591 | protected function getFromSession($key) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Return group id |
||
| 603 | * |
||
| 604 | * @param array $log |
||
| 605 | * @return string |
||
| 606 | */ |
||
| 607 | protected function getGroupId(array $log) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Return current active user id |
||
| 632 | * |
||
| 633 | * @return int |
||
| 634 | */ |
||
| 635 | protected function getUserId() |
||
| 673 | |||
| 674 | /** |
||
| 675 | * @return string |
||
| 676 | */ |
||
| 677 | protected function getArtisanCommandName() |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @return bool |
||
| 687 | */ |
||
| 688 | protected function getRunningInConsole() |
||
| 692 | |||
| 693 | /** |
||
| 694 | * @return float |
||
| 695 | */ |
||
| 696 | protected function getLoggerVersion() |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Return process identifier token |
||
| 703 | * |
||
| 704 | * @return string |
||
| 705 | */ |
||
| 706 | protected function getProcessIdentifier() |
||
| 710 | |||
| 711 | } |
||
| 712 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.