Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Request extends AbstractRequest |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @const string |
||
| 27 | */ |
||
| 28 | const HTTP_METHOD_GET = 'GET'; |
||
| 29 | const HTTP_METHOD_POST = 'POST'; |
||
| 30 | const HTTP_METHOD_PATCH = 'PATCH'; |
||
| 31 | const HTTP_METHOD_PUT = 'PUT'; |
||
| 32 | const HTTP_METHOD_DELETE = 'DELETE'; |
||
| 33 | const HTTP_METHOD_OPTIONS = 'OPTIONS'; |
||
| 34 | const HTTP_METHOD_HEAD = 'HEAD'; |
||
| 35 | const HTTP_METHOD_CONNECT = 'CONNECT'; |
||
| 36 | const HTTP_METHOD_TRACE = 'TRACE'; |
||
| 37 | const HTTP_METHOD_PURGE = 'PURGE'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * HTTP request method (GET is default). |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $method = Request::HTTP_METHOD_GET; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $charsets; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $encodings; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $languages; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $eTags; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Constructor. |
||
| 68 | * |
||
| 69 | * @param array $server |
||
| 70 | * @param array $query |
||
| 71 | * @param array $request |
||
| 72 | * @param array $cookies |
||
| 73 | * @param array $files |
||
| 74 | * @param SessionHandlerInterface|null $sessionHandler |
||
| 75 | * @param string|null $content |
||
| 76 | */ |
||
| 77 | public function __construct( |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Factory create Request. |
||
| 101 | * |
||
| 102 | * @param string $uri |
||
| 103 | * @param string $method |
||
| 104 | * @param array $parameters |
||
| 105 | * @param array $cookies |
||
| 106 | * @param array $server |
||
| 107 | * @param array $files |
||
| 108 | * @param string $content |
||
| 109 | * |
||
| 110 | * @return $this |
||
| 111 | */ |
||
| 112 | public static function create( |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Factory create Request from $_SERVER variables. |
||
| 177 | * |
||
| 178 | * @return Request |
||
| 179 | */ |
||
| 180 | public static function createFromServer() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Normalize query string. |
||
| 197 | * |
||
| 198 | * @param string $queryString |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public static function normalizeQueryString($queryString) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Pair value. |
||
| 217 | * |
||
| 218 | * @param string $queryString |
||
| 219 | * |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | private static function computePairValue($queryString) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Create fixed server request |
||
| 253 | * |
||
| 254 | * @param array $server |
||
| 255 | * |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | private static function fixCreateServerRequest(array &$server) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Fixing server component. |
||
| 282 | * |
||
| 283 | * @param array $server |
||
| 284 | * @param array $components |
||
| 285 | */ |
||
| 286 | private static function fixServerComponent(array &$server, array $components) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Fix schema. |
||
| 304 | * |
||
| 305 | * @param array $server |
||
| 306 | * @param string $schema |
||
| 307 | */ |
||
| 308 | private static function fixSchema(array &$server, $schema) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Fix server query string |
||
| 323 | * |
||
| 324 | * @param array $server |
||
| 325 | * @param array $query |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | private static function fixQueryString(array &$server, array $query, $queryString, $path) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get parameter value or return default value if not exist. |
||
| 354 | * |
||
| 355 | * Order of precedence: GET, POST |
||
| 356 | * |
||
| 357 | * @param string $key |
||
| 358 | * @param mixed $default |
||
| 359 | * |
||
| 360 | * @return mixed |
||
| 361 | */ |
||
| 362 | public function get($key, $default = null) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Check if parameter defined by key. |
||
| 377 | * |
||
| 378 | * @param string $key |
||
| 379 | * |
||
| 380 | * @return bool |
||
| 381 | */ |
||
| 382 | public function has($key) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Get all parameters. |
||
| 397 | * |
||
| 398 | * @return array |
||
| 399 | */ |
||
| 400 | public function all() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Get query string. |
||
| 407 | * |
||
| 408 | * @return string|null |
||
| 409 | */ |
||
| 410 | public function getQueryString() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Get list of charsets that accepted by client browser. |
||
| 419 | * |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | public function getCharsets() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get list of encodings that accepted by client browser. |
||
| 433 | * |
||
| 434 | * @return array |
||
| 435 | */ |
||
| 436 | public function getEncodings() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Get list of languages that accepted by client browser. |
||
| 447 | * |
||
| 448 | * @return array |
||
| 449 | */ |
||
| 450 | public function getLanguages() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Get ETags. |
||
| 461 | * |
||
| 462 | * @return array |
||
| 463 | */ |
||
| 464 | public function getETags() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Get client ip addresses. |
||
| 482 | * |
||
| 483 | * @return array |
||
| 484 | */ |
||
| 485 | public function getClientIps() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Get client ip address. |
||
| 508 | * |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | public function getClientIp() |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Check request is no cache. |
||
| 520 | * |
||
| 521 | * @return bool |
||
| 522 | */ |
||
| 523 | public function isNoCache() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Cast Request to string representation. |
||
| 540 | * |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | public function __toString() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Get accept header values by header name. |
||
| 558 | * |
||
| 559 | * @param string $headerName |
||
| 560 | * |
||
| 561 | * @return array |
||
| 562 | */ |
||
| 563 | private function getAcceptedHeaderValues($headerName) |
||
| 578 | } |
||
| 579 |