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 | protected function getQueryStringArray() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @return array|null |
||
| 403 | */ |
||
| 404 | protected function getPostDataArray() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param $key |
||
| 444 | * @param $value |
||
| 445 | * @return mixed|string |
||
| 446 | */ |
||
| 447 | protected function parseRequestFieldValue($key, $value) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Return request method |
||
| 476 | * |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | protected function getRequestMethod() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Return server ip address |
||
| 491 | * |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | protected function getServerIp() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Return client ip |
||
| 506 | * |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | protected function getClientIp() |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Return client user agent string |
||
| 521 | * |
||
| 522 | * @return string |
||
| 523 | */ |
||
| 524 | protected function getClientUserAgent() |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Return current enviroment |
||
| 536 | * |
||
| 537 | * @return string |
||
| 538 | */ |
||
| 539 | protected function getEnvironment() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Retrive parameter from current session |
||
| 546 | * |
||
| 547 | * @param string $key |
||
| 548 | * @return string |
||
| 549 | */ |
||
| 550 | protected function getFromSession($key) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Return group id |
||
| 562 | * |
||
| 563 | * @param array $log |
||
| 564 | * @return string |
||
| 565 | */ |
||
| 566 | protected function getGroupId(array $log) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Return current active user id |
||
| 591 | * |
||
| 592 | * @return int |
||
| 593 | */ |
||
| 594 | protected function getUserId() |
||
| 632 | |||
| 633 | /** |
||
| 634 | * @return string |
||
| 635 | */ |
||
| 636 | protected function getArtisanCommandName() |
||
| 643 | |||
| 644 | /** |
||
| 645 | * @return bool |
||
| 646 | */ |
||
| 647 | protected function getRunningInConsole() |
||
| 651 | |||
| 652 | /** |
||
| 653 | * @return float |
||
| 654 | */ |
||
| 655 | protected function getLoggerVersion() |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Return process identifier token |
||
| 662 | * |
||
| 663 | * @return string |
||
| 664 | */ |
||
| 665 | protected function getProcessIdentifier() |
||
| 669 | |||
| 670 | } |
||
| 671 |
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.