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) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Return current route name |
||
| 325 | * |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | protected function getRouteName() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Return current url |
||
| 340 | * |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | protected function getUrl() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return array|null |
||
| 362 | */ |
||
| 363 | View Code Duplication | protected function getQueryStringArray() |
|
| 393 | |||
| 394 | /** |
||
| 395 | * @return array|null |
||
| 396 | */ |
||
| 397 | View Code Duplication | protected function getPostDataArray() |
|
| 427 | |||
| 428 | /** |
||
| 429 | * @param $key |
||
| 430 | * @param $value |
||
| 431 | * @return mixed|string |
||
| 432 | */ |
||
| 433 | protected function parseRequestFieldValue($key, $value) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Return request method |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | protected function getRequestMethod() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Return server ip address |
||
| 477 | * |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | protected function getServerIp() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Return client ip |
||
| 492 | * |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | protected function getClientIp() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Return client user agent string |
||
| 507 | * |
||
| 508 | * @return string |
||
| 509 | */ |
||
| 510 | protected function getClientUserAgent() |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Return current enviroment |
||
| 522 | * |
||
| 523 | * @return string |
||
| 524 | */ |
||
| 525 | protected function getEnvironment() |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Retrive parameter from current session |
||
| 532 | * |
||
| 533 | * @param string $key |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | protected function getFromSession($key) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Return group id |
||
| 548 | * |
||
| 549 | * @param array $log |
||
| 550 | * @return string |
||
| 551 | */ |
||
| 552 | protected function getGroupId(array $log) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Return current active user id |
||
| 577 | * |
||
| 578 | * @return int |
||
| 579 | */ |
||
| 580 | protected function getUserId() |
||
| 618 | |||
| 619 | /** |
||
| 620 | * @return string |
||
| 621 | */ |
||
| 622 | protected function getArtisanCommandName() |
||
| 629 | |||
| 630 | /** |
||
| 631 | * @return bool |
||
| 632 | */ |
||
| 633 | protected function getRunningInConsole() |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @return float |
||
| 640 | */ |
||
| 641 | protected function getLoggerVersion() |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Return process identifier token |
||
| 648 | * |
||
| 649 | * @return string |
||
| 650 | */ |
||
| 651 | protected function getProcessIdentifier() |
||
| 655 | |||
| 656 | } |
||
| 657 |
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.