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 | ]; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Session store |
||
| 46 | * |
||
| 47 | * @var \Illuminate\Session\Store |
||
| 48 | */ |
||
| 49 | protected $session; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Router |
||
| 53 | * |
||
| 54 | * @var Router |
||
| 55 | */ |
||
| 56 | protected $router; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Server variable |
||
| 60 | * |
||
| 61 | * @var Request |
||
| 62 | */ |
||
| 63 | protected $request; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Token provider |
||
| 67 | * |
||
| 68 | * @var UniqueProcessIdentifier |
||
| 69 | */ |
||
| 70 | protected $tokenProvider; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Current environment |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $environment; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var DataCollector |
||
| 81 | */ |
||
| 82 | protected $dataCollector; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var Application |
||
| 86 | */ |
||
| 87 | protected $app; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return void |
||
|
|
|||
| 91 | */ |
||
| 92 | public function __construct() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param Application $app |
||
| 102 | */ |
||
| 103 | public function setApp(Application $app) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Set session store |
||
| 110 | * |
||
| 111 | * @param type $service |
||
| 112 | */ |
||
| 113 | public function setSessionStore(SessionStore $service) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Set router |
||
| 120 | * |
||
| 121 | * @param Router $router |
||
| 122 | */ |
||
| 123 | public function setRouter(Router $router) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Set request |
||
| 130 | * |
||
| 131 | * @param Request $request |
||
| 132 | */ |
||
| 133 | public function setRequest(Request $request) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Set current environment |
||
| 140 | * |
||
| 141 | * @param string $environment |
||
| 142 | */ |
||
| 143 | public function setEnvironment($environment) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param DataCollector $dataCollector |
||
| 150 | */ |
||
| 151 | public function setDataCollector(DataCollector $dataCollector) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Register a custom HTML macro. |
||
| 158 | * |
||
| 159 | * @param string $name |
||
| 160 | * @param mixed $macro |
||
| 161 | * @return void |
||
| 162 | */ |
||
| 163 | public function extend($name, $provider) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Set token provider |
||
| 170 | * |
||
| 171 | * @param UniqueProcessIdentifier $tokenProvider |
||
| 172 | */ |
||
| 173 | public function setTokenProvider(TokenProvider $tokenProvider) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Return resolved field-value array |
||
| 180 | * |
||
| 181 | * @param array $callbacks |
||
| 182 | * @param array $log |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | public function resolveValues(array $callbacks, array $log) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Handle class calls |
||
| 209 | * |
||
| 210 | * @param string $name |
||
| 211 | * @param mixed $params |
||
| 212 | * @return mixed |
||
| 213 | * |
||
| 214 | * @throws \BadMethodCallException |
||
| 215 | */ |
||
| 216 | public function __call($name, $params) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Return hashed version of session id |
||
| 228 | * |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | protected function getSessionId() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | protected function getLaravelVersion() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | protected function getSqlQueries() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Return current route name |
||
| 267 | * |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | protected function getRouteName() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Return current url |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | protected function getUrl() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Return request method |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | protected function getRequestMethod() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Return server ip address |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | protected function getServerIp() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Return client ip |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | protected function getClientIp() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Return client user agent string |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | protected function getClientUserAgent() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Return current enviroment |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | protected function getEnvironment() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Retrive parameter from current session |
||
| 381 | * |
||
| 382 | * @param string $key |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | protected function getFromSession($key) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Return group id |
||
| 397 | * |
||
| 398 | * @param array $log |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | protected function getGroupId(array $log) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Return current active user id |
||
| 415 | * |
||
| 416 | * @return int |
||
| 417 | */ |
||
| 418 | protected function getUserId() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | protected function getArtisanCommandName() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @return bool |
||
| 464 | */ |
||
| 465 | protected function getRunningInConsole() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @return float |
||
| 472 | */ |
||
| 473 | protected function getLoggerVersion() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Return process identifier token |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | protected function getProcessIdentifier() |
||
| 487 | |||
| 488 | } |
||
| 489 |
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.