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 |
||
| 50 | class Request |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * Array of supported HTTP methods. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | * |
||
| 57 | * @since 2.0 |
||
| 58 | */ |
||
| 59 | private $HTTPMethods = array('HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'TRACE'); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The HTTP method of this request (must be in HTTPMethods array). |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | * |
||
| 66 | * @since 2.0 |
||
| 67 | */ |
||
| 68 | private $method; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * An associative array of HTTP headers on this request. |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | * |
||
| 75 | * @since 2.0 |
||
| 76 | */ |
||
| 77 | private $headers; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * An associative array of HTTP cookies on this request. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | * |
||
| 84 | * @since 2.0 |
||
| 85 | */ |
||
| 86 | private $cookies; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The HTTP params (form data and query string) on this request. |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | * |
||
| 93 | * @since 2.0 |
||
| 94 | */ |
||
| 95 | private $params; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * An associative 3D array of uploaded files. |
||
| 99 | * |
||
| 100 | * @var array |
||
| 101 | * |
||
| 102 | * @since 2.0 |
||
| 103 | */ |
||
| 104 | private $files; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The request body if one was provided. |
||
| 108 | * |
||
| 109 | * @var string |
||
| 110 | * |
||
| 111 | * @since 2.0 |
||
| 112 | */ |
||
| 113 | private $body; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * The host header provided on the request. |
||
| 117 | * |
||
| 118 | * @var string |
||
| 119 | * |
||
| 120 | * @since 2.0 |
||
| 121 | */ |
||
| 122 | private $host; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * The IP of the client making the request. |
||
| 126 | * |
||
| 127 | * @var string |
||
| 128 | * |
||
| 129 | * @since 2.0 |
||
| 130 | */ |
||
| 131 | private $IP; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * The URI requested. |
||
| 135 | * |
||
| 136 | * @var string |
||
| 137 | * |
||
| 138 | * @since 2.0 |
||
| 139 | */ |
||
| 140 | private $URI; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * The query string provided on the request (if any). |
||
| 144 | * |
||
| 145 | * @var string |
||
| 146 | * |
||
| 147 | * @since 2.0 |
||
| 148 | */ |
||
| 149 | private $queryString; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Builds up the request based on available PHP super globals, in addition to |
||
| 153 | * any overrides provided (useful for testing). |
||
| 154 | * |
||
| 155 | * @param array $overrides Hash array of PHP super globals to override |
||
| 156 | * |
||
| 157 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 158 | * |
||
| 159 | * @since 2.0 |
||
| 160 | */ |
||
| 161 | public function __construct($overrides = array()) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Tries to get the requested param from the $_SERVER super global, otherwise returns an |
||
| 260 | * empty string. |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | * |
||
| 264 | * @since 3.0 |
||
| 265 | */ |
||
| 266 | private function getGlobalServerValue($param) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get the HTTP method of this request. |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | * |
||
| 282 | * @since 2.0 |
||
| 283 | */ |
||
| 284 | public function getMethod() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Set the HTTP method of this request. |
||
| 291 | * |
||
| 292 | * @param string $method |
||
| 293 | * |
||
| 294 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 295 | * |
||
| 296 | * @since 2.0 |
||
| 297 | */ |
||
| 298 | public function setMethod($method) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Return all headers on this request. |
||
| 309 | * |
||
| 310 | * @return array |
||
| 311 | * |
||
| 312 | * @since 2.0 |
||
| 313 | */ |
||
| 314 | public function getHeaders() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Get the header matching the key provided. |
||
| 321 | * |
||
| 322 | * @param string $key The key to search for |
||
| 323 | * @param mixed $default If key is not found, return this instead |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | * |
||
| 327 | * @since 2.0 |
||
| 328 | */ |
||
| 329 | public function getHeader($key, $default = null) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Tries to get the current HTTP request headers from super globals. |
||
| 340 | * |
||
| 341 | * @return array |
||
| 342 | * |
||
| 343 | * @since 2.0 |
||
| 344 | */ |
||
| 345 | private function getGlobalHeaders() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Return all cookies on this request. |
||
| 369 | * |
||
| 370 | * @return array |
||
| 371 | * |
||
| 372 | * @since 2.0 |
||
| 373 | */ |
||
| 374 | public function getCookies() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get the cookie matching the key provided. |
||
| 381 | * |
||
| 382 | * @param string $key The key to search for |
||
| 383 | * @param mixed $default If key is not found, return this instead |
||
| 384 | * |
||
| 385 | * @return mixed |
||
| 386 | * |
||
| 387 | * @since 2.0 |
||
| 388 | */ |
||
| 389 | public function getCookie($key, $default = null) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Return all params on this request. |
||
| 400 | * |
||
| 401 | * @return array |
||
| 402 | * |
||
| 403 | * @since 2.0 |
||
| 404 | */ |
||
| 405 | public function getParams() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Get the param matching the key provided. |
||
| 412 | * |
||
| 413 | * @param string $key The key to search for |
||
| 414 | * @param mixed $default If key is not found, return this instead |
||
| 415 | * |
||
| 416 | * @return string |
||
| 417 | * |
||
| 418 | * @since 2.0 |
||
| 419 | */ |
||
| 420 | public function getParam($key, $default = null) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Append the hash array provided to the params for this request. |
||
| 431 | * |
||
| 432 | * @param array A hash array of values to add to the request params |
||
| 433 | * |
||
| 434 | * @since 2.0 |
||
| 435 | */ |
||
| 436 | public function addParams($params) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Set the params array. |
||
| 445 | * |
||
| 446 | * @param array A hash array of values to set as the request params |
||
| 447 | * |
||
| 448 | * @since 2.0 |
||
| 449 | */ |
||
| 450 | public function setParams($params) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Return all files on this request. |
||
| 459 | * |
||
| 460 | * @return array |
||
| 461 | * |
||
| 462 | * @since 2.0 |
||
| 463 | */ |
||
| 464 | public function getFiles() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get the file matching the key provided. |
||
| 471 | * |
||
| 472 | * @param string $key The key to search for |
||
| 473 | * @param mixed $default If key is not found, return this instead |
||
| 474 | * |
||
| 475 | * @return mixed |
||
| 476 | * |
||
| 477 | * @since 2.0 |
||
| 478 | */ |
||
| 479 | public function getFile($key, $default = null) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Get the request body if one was provided. |
||
| 490 | * |
||
| 491 | * @return string |
||
| 492 | * |
||
| 493 | * @since 2.0 |
||
| 494 | */ |
||
| 495 | public function getBody() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Attempts to get the raw body of the current request from super globals. |
||
| 502 | * |
||
| 503 | * @return string |
||
| 504 | * |
||
| 505 | * @since 2.0 |
||
| 506 | */ |
||
| 507 | private function getGlobalBody() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Get the Accept header of the request. |
||
| 518 | * |
||
| 519 | * @return string |
||
| 520 | * |
||
| 521 | * @since 2.0 |
||
| 522 | */ |
||
| 523 | public function getAccept() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Get the Content-Type header of the request. |
||
| 530 | * |
||
| 531 | * @return string |
||
| 532 | * |
||
| 533 | * @since 2.0 |
||
| 534 | */ |
||
| 535 | public function getContentType() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Get the Content-Length header of the request. |
||
| 542 | * |
||
| 543 | * @return string |
||
| 544 | * |
||
| 545 | * @since 2.0 |
||
| 546 | */ |
||
| 547 | public function getContentLength() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Get the host name of the client that sent the request. |
||
| 554 | * |
||
| 555 | * @return string |
||
| 556 | * |
||
| 557 | * @since 2.0 |
||
| 558 | */ |
||
| 559 | public function getHost() |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Get the URI that was requested. |
||
| 566 | * |
||
| 567 | * @return string |
||
| 568 | * |
||
| 569 | * @since 2.0 |
||
| 570 | */ |
||
| 571 | public function getURI() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Get the URL that was requested. |
||
| 578 | * |
||
| 579 | * @return string |
||
| 580 | * |
||
| 581 | * @since 2.0 |
||
| 582 | */ |
||
| 583 | public function getURL() |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Get the IP address of the client that sent the request. |
||
| 592 | * |
||
| 593 | * @return string |
||
| 594 | * |
||
| 595 | * @since 2.0 |
||
| 596 | */ |
||
| 597 | public function getIP() |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Get the Referrer header of the request. |
||
| 604 | * |
||
| 605 | * @return string |
||
| 606 | * |
||
| 607 | * @since 2.0 |
||
| 608 | */ |
||
| 609 | public function getReferrer() |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Get the User-Agent header of the request. |
||
| 616 | * |
||
| 617 | * @return string |
||
| 618 | * |
||
| 619 | * @since 2.0 |
||
| 620 | */ |
||
| 621 | public function getUserAgent() |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Get the query string provided on the request. |
||
| 628 | * |
||
| 629 | * @return string |
||
| 630 | * |
||
| 631 | * @since 2.0 |
||
| 632 | */ |
||
| 633 | public function getQueryString() |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Parses the route provided to extract matching params of the route from this request's URI. |
||
| 640 | * |
||
| 641 | * @param string $route The route with parameter names, e.g. /user/{username} |
||
| 642 | * @param array $defaultParams Optional hash array of default request param values to use if they are missing from URI |
||
| 643 | * |
||
| 644 | * @since 2.0 |
||
| 645 | */ |
||
| 646 | public function parseParamsFromRoute($route, $defaultParams = array()) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Checks to see if the request contains a secure/encrypted token. |
||
| 679 | * |
||
| 680 | * @return bool |
||
| 681 | * |
||
| 682 | * @since 2.0 |
||
| 683 | */ |
||
| 684 | public function isSecureURI() |
||
| 692 | } |
||
| 693 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: