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; |
||
| 11 | class FieldProvider |
||
| 12 | { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * The registered field providers. |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $providers = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Default field |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $defaultProviders = [ |
||
| 27 | 'getSessionId', |
||
| 28 | 'getRouteName', |
||
| 29 | 'getUrl', |
||
| 30 | 'getRequestMethod', |
||
| 31 | 'getServerIp', |
||
| 32 | 'getClientIp', |
||
| 33 | 'getClientUserAgent', |
||
| 34 | 'getEnvironment', |
||
| 35 | 'getFromSession', |
||
| 36 | 'getProcessIdentifier', |
||
| 37 | 'getUserId', |
||
| 38 | 'getGroupId', |
||
| 39 | 'getLaravelVersion', |
||
| 40 | 'getSqlQueries', |
||
| 41 | 'getArtisanCommandName', |
||
| 42 | 'getRunningInConsole', |
||
| 43 | 'getLoggerVersion', |
||
| 44 | 'getPostDataArray', |
||
| 45 | 'getQueryStringArray', |
||
| 46 | ]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Session store |
||
| 50 | * |
||
| 51 | * @var \Illuminate\Session\Store |
||
| 52 | */ |
||
| 53 | protected $session; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Router |
||
| 57 | * |
||
| 58 | * @var Router |
||
| 59 | */ |
||
| 60 | protected $router; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Server variable |
||
| 64 | * |
||
| 65 | * @var Request |
||
| 66 | */ |
||
| 67 | protected $request; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Token provider |
||
| 71 | * |
||
| 72 | * @var UniqueProcessIdentifier |
||
| 73 | */ |
||
| 74 | protected $tokenProvider; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Current environment |
||
| 78 | * |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $environment; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var DataCollector |
||
| 85 | */ |
||
| 86 | protected $dataCollector; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var Application |
||
| 90 | */ |
||
| 91 | protected $app; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return void |
||
|
|
|||
| 95 | */ |
||
| 96 | public function __construct() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param Application $app |
||
| 106 | */ |
||
| 107 | public function setApp(Application $app) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set session store |
||
| 114 | * |
||
| 115 | * @param type $service |
||
| 116 | */ |
||
| 117 | public function setSessionStore(SessionStore $service) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Set router |
||
| 124 | * |
||
| 125 | * @param Router $router |
||
| 126 | */ |
||
| 127 | public function setRouter(Router $router) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Set request |
||
| 134 | * |
||
| 135 | * @param Request $request |
||
| 136 | */ |
||
| 137 | public function setRequest(Request $request) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Set current environment |
||
| 144 | * |
||
| 145 | * @param string $environment |
||
| 146 | */ |
||
| 147 | public function setEnvironment($environment) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param DataCollector $dataCollector |
||
| 154 | */ |
||
| 155 | public function setDataCollector(DataCollector $dataCollector) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Register a custom HTML macro. |
||
| 162 | * |
||
| 163 | * @param string $name |
||
| 164 | * @param mixed $macro |
||
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | public function extend($name, $provider) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Set token provider |
||
| 174 | * |
||
| 175 | * @param UniqueProcessIdentifier $tokenProvider |
||
| 176 | */ |
||
| 177 | public function setTokenProvider(TokenProvider $tokenProvider) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Return resolved field-value array |
||
| 184 | * |
||
| 185 | * @param array $callbacks |
||
| 186 | * @param array $log |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | public function resolveValues(array $callbacks, array $log) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Handle class calls |
||
| 213 | * |
||
| 214 | * @param string $name |
||
| 215 | * @param mixed $params |
||
| 216 | * @return mixed |
||
| 217 | * |
||
| 218 | * @throws \BadMethodCallException |
||
| 219 | */ |
||
| 220 | public function __call($name, $params) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Return hashed version of session id |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | protected function getSessionId() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | protected function getLaravelVersion() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | protected function getSqlQueries() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param $queryArray |
||
| 292 | * @return mixed |
||
| 293 | */ |
||
| 294 | protected function mergeBindings($queryArray) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Return current route name |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | protected function getRouteName() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Return current url |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | protected function getUrl() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @return array|null |
||
| 371 | */ |
||
| 372 | protected function getQueryStringArray() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @return array|null |
||
| 405 | */ |
||
| 406 | protected function getPostDataArray() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param $key |
||
| 461 | * @param $value |
||
| 462 | * @return mixed|string |
||
| 463 | */ |
||
| 464 | protected function parseRequestFieldValue($key, $value) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Return request method |
||
| 493 | * |
||
| 494 | * @return string |
||
| 495 | */ |
||
| 496 | protected function getRequestMethod() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Return server ip address |
||
| 508 | * |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | protected function getServerIp() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Return client ip |
||
| 523 | * |
||
| 524 | * @return string |
||
| 525 | */ |
||
| 526 | protected function getClientIp() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Return client user agent string |
||
| 538 | * |
||
| 539 | * @return string |
||
| 540 | */ |
||
| 541 | protected function getClientUserAgent() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Return current enviroment |
||
| 553 | * |
||
| 554 | * @return string |
||
| 555 | */ |
||
| 556 | protected function getEnvironment() |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Retrive parameter from current session |
||
| 563 | * |
||
| 564 | * @param string $key |
||
| 565 | * @return string |
||
| 566 | */ |
||
| 567 | protected function getFromSession($key) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Return group id |
||
| 579 | * |
||
| 580 | * @param array $log |
||
| 581 | * @return string |
||
| 582 | */ |
||
| 583 | protected function getGroupId(array $log) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Return current active user id |
||
| 608 | * |
||
| 609 | * @return int |
||
| 610 | */ |
||
| 611 | protected function getUserId() |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @return string |
||
| 652 | */ |
||
| 653 | protected function getArtisanCommandName() |
||
| 660 | |||
| 661 | /** |
||
| 662 | * @return bool |
||
| 663 | */ |
||
| 664 | protected function getRunningInConsole() |
||
| 668 | |||
| 669 | /** |
||
| 670 | * @return float |
||
| 671 | */ |
||
| 672 | protected function getLoggerVersion() |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Return process identifier token |
||
| 679 | * |
||
| 680 | * @return string |
||
| 681 | */ |
||
| 682 | protected function getProcessIdentifier() |
||
| 686 | |||
| 687 | } |
||
| 688 |
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.