Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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(Application $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 $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 | * @return string |
||
| 250 | */ |
||
| 251 | protected function getLaravelVersion() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | protected function getSqlQueries() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param $queryArray |
||
| 290 | * @return mixed |
||
| 291 | */ |
||
| 292 | protected function mergeBindings($queryArray) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Return current route name |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | protected function getRouteName() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Return current url |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | protected function getUrl() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @return array|null |
||
| 369 | */ |
||
| 370 | View Code Duplication | protected function getQueryStringArray() |
|
| 400 | |||
| 401 | /** |
||
| 402 | * @return array|null |
||
| 403 | */ |
||
| 404 | View Code Duplication | protected function getPostDataArray() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * @param $key |
||
| 437 | * @param $value |
||
| 438 | * @return mixed|string |
||
| 439 | */ |
||
| 440 | protected function parseRequestFieldValue($key, $value) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Return request method |
||
| 469 | * |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | protected function getRequestMethod() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Return server ip address |
||
| 484 | * |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | protected function getServerIp() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Return client ip |
||
| 499 | * |
||
| 500 | * @return string |
||
| 501 | */ |
||
| 502 | protected function getClientIp() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Return client user agent string |
||
| 514 | * |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | protected function getClientUserAgent() |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Return current enviroment |
||
| 529 | * |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | protected function getEnvironment() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Retrive parameter from current session |
||
| 539 | * |
||
| 540 | * @param string $key |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | protected function getFromSession($key) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Return group id |
||
| 555 | * |
||
| 556 | * @param array $log |
||
| 557 | * @return string |
||
| 558 | */ |
||
| 559 | protected function getGroupId(array $log) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Return current active user id |
||
| 584 | * |
||
| 585 | * @return int |
||
| 586 | */ |
||
| 587 | protected function getUserId() |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @return string |
||
| 628 | */ |
||
| 629 | protected function getArtisanCommandName() |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @return bool |
||
| 639 | */ |
||
| 640 | protected function getRunningInConsole() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @return float |
||
| 647 | */ |
||
| 648 | protected function getLoggerVersion() |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Return process identifier token |
||
| 655 | * |
||
| 656 | * @return string |
||
| 657 | */ |
||
| 658 | protected function getProcessIdentifier() |
||
| 662 | |||
| 663 | } |
||
| 664 |
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.