Complex classes like Response 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 Response, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Response extends AbstractMessage implements \Serializable |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var array Array of reason phrases and their corresponding status codes |
||
| 21 | */ |
||
| 22 | private static $statusTexts = array( |
||
| 23 | 100 => 'Continue', |
||
| 24 | 101 => 'Switching Protocols', |
||
| 25 | 102 => 'Processing', |
||
| 26 | 200 => 'OK', |
||
| 27 | 201 => 'Created', |
||
| 28 | 202 => 'Accepted', |
||
| 29 | 203 => 'Non-Authoritative Information', |
||
| 30 | 204 => 'No Content', |
||
| 31 | 205 => 'Reset Content', |
||
| 32 | 206 => 'Partial Content', |
||
| 33 | 207 => 'Multi-Status', |
||
| 34 | 208 => 'Already Reported', |
||
| 35 | 226 => 'IM Used', |
||
| 36 | 300 => 'Multiple Choices', |
||
| 37 | 301 => 'Moved Permanently', |
||
| 38 | 302 => 'Found', |
||
| 39 | 303 => 'See Other', |
||
| 40 | 304 => 'Not Modified', |
||
| 41 | 305 => 'Use Proxy', |
||
| 42 | 307 => 'Temporary Redirect', |
||
| 43 | 308 => 'Permanent Redirect', |
||
| 44 | 400 => 'Bad Request', |
||
| 45 | 401 => 'Unauthorized', |
||
| 46 | 402 => 'Payment Required', |
||
| 47 | 403 => 'Forbidden', |
||
| 48 | 404 => 'Not Found', |
||
| 49 | 405 => 'Method Not Allowed', |
||
| 50 | 406 => 'Not Acceptable', |
||
| 51 | 407 => 'Proxy Authentication Required', |
||
| 52 | 408 => 'Request Timeout', |
||
| 53 | 409 => 'Conflict', |
||
| 54 | 410 => 'Gone', |
||
| 55 | 411 => 'Length Required', |
||
| 56 | 412 => 'Precondition Failed', |
||
| 57 | 413 => 'Request Entity Too Large', |
||
| 58 | 414 => 'Request-URI Too Long', |
||
| 59 | 415 => 'Unsupported Media Type', |
||
| 60 | 416 => 'Requested Range Not Satisfiable', |
||
| 61 | 417 => 'Expectation Failed', |
||
| 62 | 422 => 'Unprocessable Entity', |
||
| 63 | 423 => 'Locked', |
||
| 64 | 424 => 'Failed Dependency', |
||
| 65 | 425 => 'Reserved for WebDAV advanced collections expired proposal', |
||
| 66 | 426 => 'Upgrade required', |
||
| 67 | 428 => 'Precondition Required', |
||
| 68 | 429 => 'Too Many Requests', |
||
| 69 | 431 => 'Request Header Fields Too Large', |
||
| 70 | 500 => 'Internal Server Error', |
||
| 71 | 501 => 'Not Implemented', |
||
| 72 | 502 => 'Bad Gateway', |
||
| 73 | 503 => 'Service Unavailable', |
||
| 74 | 504 => 'Gateway Timeout', |
||
| 75 | 505 => 'HTTP Version Not Supported', |
||
| 76 | 506 => 'Variant Also Negotiates (Experimental)', |
||
| 77 | 507 => 'Insufficient Storage', |
||
| 78 | 508 => 'Loop Detected', |
||
| 79 | 510 => 'Not Extended', |
||
| 80 | 511 => 'Network Authentication Required', |
||
| 81 | ); |
||
| 82 | |||
| 83 | /** @var EntityBodyInterface The response body */ |
||
| 84 | protected $body; |
||
| 85 | |||
| 86 | /** @var string The reason phrase of the response (human readable code) */ |
||
| 87 | protected $reasonPhrase; |
||
| 88 | |||
| 89 | /** @var string The status code of the response */ |
||
| 90 | protected $statusCode; |
||
| 91 | |||
| 92 | /** @var array Information about the request */ |
||
| 93 | protected $info = array(); |
||
| 94 | |||
| 95 | /** @var string The effective URL that returned this response */ |
||
| 96 | protected $effectiveUrl; |
||
| 97 | |||
| 98 | /** @var array Cacheable response codes (see RFC 2616:13.4) */ |
||
| 99 | protected static $cacheResponseCodes = array(200, 203, 206, 300, 301, 410); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Create a new Response based on a raw response message |
||
| 103 | * |
||
| 104 | * @param string $message Response message |
||
| 105 | * |
||
| 106 | * @return self|bool Returns false on error |
||
| 107 | */ |
||
| 108 | public static function fromMessage($message) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Construct the response |
||
| 131 | * |
||
| 132 | * @param string $statusCode The response status code (e.g. 200, 404, etc) |
||
| 133 | * @param ToArrayInterface|array $headers The response headers |
||
| 134 | * @param string|resource|EntityBodyInterface $body The body of the response |
||
| 135 | * |
||
| 136 | * @throws BadResponseException if an invalid response code is given |
||
| 137 | */ |
||
| 138 | public function __construct($statusCode, $headers = null, $body = null) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function __toString() |
||
| 162 | |||
| 163 | public function serialize() |
||
| 171 | |||
| 172 | public function unserialize($serialize) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the response entity body |
||
| 180 | * |
||
| 181 | * @param bool $asString Set to TRUE to return a string of the body rather than a full body object |
||
| 182 | * |
||
| 183 | * @return EntityBodyInterface|string |
||
| 184 | */ |
||
| 185 | public function getBody($asString = false) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Set the response entity body |
||
| 192 | * |
||
| 193 | * @param EntityBodyInterface|string $body Body to set |
||
| 194 | * |
||
| 195 | * @return self |
||
| 196 | */ |
||
| 197 | public function setBody($body) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Set the protocol and protocol version of the response |
||
| 206 | * |
||
| 207 | * @param string $protocol Response protocol |
||
| 208 | * @param string $version Protocol version |
||
| 209 | * |
||
| 210 | * @return self |
||
| 211 | */ |
||
| 212 | public function setProtocol($protocol, $version) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get the protocol used for the response (e.g. HTTP) |
||
| 222 | * |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function getProtocol() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get the HTTP protocol version |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function getProtocolVersion() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get a cURL transfer information |
||
| 242 | * |
||
| 243 | * @param string $key A single statistic to check |
||
| 244 | * |
||
| 245 | * @return array|string|null Returns all stats if no key is set, a single stat if a key is set, or null if a key |
||
| 246 | * is set and not found |
||
| 247 | * @link http://www.php.net/manual/en/function.curl-getinfo.php |
||
| 248 | */ |
||
| 249 | public function getInfo($key = null) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set the transfer information |
||
| 262 | * |
||
| 263 | * @param array $info Array of cURL transfer stats |
||
| 264 | * |
||
| 265 | * @return self |
||
| 266 | */ |
||
| 267 | public function setInfo(array $info) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Set the response status |
||
| 276 | * |
||
| 277 | * @param int $statusCode Response status code to set |
||
| 278 | * @param string $reasonPhrase Response reason phrase |
||
| 279 | * |
||
| 280 | * @return self |
||
| 281 | * @throws BadResponseException when an invalid response code is received |
||
| 282 | */ |
||
| 283 | public function setStatus($statusCode, $reasonPhrase = '') |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get the response status code |
||
| 298 | * |
||
| 299 | * @return integer |
||
| 300 | */ |
||
| 301 | public function getStatusCode() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get the entire response as a string |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function getMessage() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get the the raw message headers as a string |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function getRawHeaders() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get the response reason phrase- a human readable version of the numeric |
||
| 342 | * status code |
||
| 343 | * |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | public function getReasonPhrase() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Get the Accept-Ranges HTTP header |
||
| 353 | * |
||
| 354 | * @return string Returns what partial content range types this server supports. |
||
| 355 | */ |
||
| 356 | public function getAcceptRanges() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Calculate the age of the response |
||
| 363 | * |
||
| 364 | * @return integer |
||
| 365 | */ |
||
| 366 | public function calculateAge() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get the Age HTTP header |
||
| 379 | * |
||
| 380 | * @return integer|null Returns the age the object has been in a proxy cache in seconds. |
||
| 381 | */ |
||
| 382 | public function getAge() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get the Allow HTTP header |
||
| 389 | * |
||
| 390 | * @return string|null Returns valid actions for a specified resource. To be used for a 405 Method not allowed. |
||
| 391 | */ |
||
| 392 | public function getAllow() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Check if an HTTP method is allowed by checking the Allow response header |
||
| 399 | * |
||
| 400 | * @param string $method Method to check |
||
| 401 | * |
||
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | public function isMethodAllowed($method) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Get the Cache-Control HTTP header |
||
| 420 | * |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | public function getCacheControl() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Get the Connection HTTP header |
||
| 430 | * |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | public function getConnection() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Get the Content-Encoding HTTP header |
||
| 440 | * |
||
| 441 | * @return string|null |
||
| 442 | */ |
||
| 443 | public function getContentEncoding() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get the Content-Language HTTP header |
||
| 450 | * |
||
| 451 | * @return string|null Returns the language the content is in. |
||
| 452 | */ |
||
| 453 | public function getContentLanguage() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Get the Content-Length HTTP header |
||
| 460 | * |
||
| 461 | * @return integer Returns the length of the response body in bytes |
||
| 462 | */ |
||
| 463 | public function getContentLength() |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Get the Content-Location HTTP header |
||
| 470 | * |
||
| 471 | * @return string|null Returns an alternate location for the returned data (e.g /index.htm) |
||
| 472 | */ |
||
| 473 | public function getContentLocation() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get the Content-Disposition HTTP header |
||
| 480 | * |
||
| 481 | * @return string|null Returns the Content-Disposition header |
||
| 482 | */ |
||
| 483 | public function getContentDisposition() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Get the Content-MD5 HTTP header |
||
| 490 | * |
||
| 491 | * @return string|null Returns a Base64-encoded binary MD5 sum of the content of the response. |
||
| 492 | */ |
||
| 493 | public function getContentMd5() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Get the Content-Range HTTP header |
||
| 500 | * |
||
| 501 | * @return string Returns where in a full body message this partial message belongs (e.g. bytes 21010-47021/47022). |
||
| 502 | */ |
||
| 503 | public function getContentRange() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Get the Content-Type HTTP header |
||
| 510 | * |
||
| 511 | * @return string Returns the mime type of this content. |
||
| 512 | */ |
||
| 513 | public function getContentType() |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Checks if the Content-Type is of a certain type. This is useful if the |
||
| 520 | * Content-Type header contains charset information and you need to know if |
||
| 521 | * the Content-Type matches a particular type. |
||
| 522 | * |
||
| 523 | * @param string $type Content type to check against |
||
| 524 | * |
||
| 525 | * @return bool |
||
| 526 | */ |
||
| 527 | public function isContentType($type) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Get the Date HTTP header |
||
| 534 | * |
||
| 535 | * @return string|null Returns the date and time that the message was sent. |
||
| 536 | */ |
||
| 537 | public function getDate() |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Get the ETag HTTP header |
||
| 544 | * |
||
| 545 | * @return string|null Returns an identifier for a specific version of a resource, often a Message digest. |
||
| 546 | */ |
||
| 547 | public function getEtag() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Get the Expires HTTP header |
||
| 554 | * |
||
| 555 | * @return string|null Returns the date/time after which the response is considered stale. |
||
| 556 | */ |
||
| 557 | public function getExpires() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Get the Last-Modified HTTP header |
||
| 564 | * |
||
| 565 | * @return string|null Returns the last modified date for the requested object, in RFC 2822 format |
||
| 566 | * (e.g. Tue, 15 Nov 1994 12:45:26 GMT) |
||
| 567 | */ |
||
| 568 | public function getLastModified() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Get the Location HTTP header |
||
| 575 | * |
||
| 576 | * @return string|null Used in redirection, or when a new resource has been created. |
||
| 577 | */ |
||
| 578 | public function getLocation() |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Get the Pragma HTTP header |
||
| 585 | * |
||
| 586 | * @return Header|null Returns the implementation-specific headers that may have various effects anywhere along |
||
| 587 | * the request-response chain. |
||
| 588 | */ |
||
| 589 | public function getPragma() |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Get the Proxy-Authenticate HTTP header |
||
| 596 | * |
||
| 597 | * @return string|null Authentication to access the proxy (e.g. Basic) |
||
| 598 | */ |
||
| 599 | public function getProxyAuthenticate() |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Get the Retry-After HTTP header |
||
| 606 | * |
||
| 607 | * @return int|null If an entity is temporarily unavailable, this instructs the client to try again after a |
||
| 608 | * specified period of time. |
||
| 609 | */ |
||
| 610 | public function getRetryAfter() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Get the Server HTTP header |
||
| 617 | * |
||
| 618 | * @return string|null A name for the server |
||
| 619 | */ |
||
| 620 | public function getServer() |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Get the Set-Cookie HTTP header |
||
| 627 | * |
||
| 628 | * @return string|null An HTTP cookie. |
||
| 629 | */ |
||
| 630 | public function getSetCookie() |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Get the Trailer HTTP header |
||
| 637 | * |
||
| 638 | * @return string|null The Trailer general field value indicates that the given set of header fields is present in |
||
| 639 | * the trailer of a message encoded with chunked transfer-coding. |
||
| 640 | */ |
||
| 641 | public function getTrailer() |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Get the Transfer-Encoding HTTP header |
||
| 648 | * |
||
| 649 | * @return string|null The form of encoding used to safely transfer the entity to the user |
||
| 650 | */ |
||
| 651 | public function getTransferEncoding() |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Get the Vary HTTP header |
||
| 658 | * |
||
| 659 | * @return string|null Tells downstream proxies how to match future request headers to decide whether the cached |
||
| 660 | * response can be used rather than requesting a fresh one from the origin server. |
||
| 661 | */ |
||
| 662 | public function getVary() |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Get the Via HTTP header |
||
| 669 | * |
||
| 670 | * @return string|null Informs the client of proxies through which the response was sent. |
||
| 671 | */ |
||
| 672 | public function getVia() |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Get the Warning HTTP header |
||
| 679 | * |
||
| 680 | * @return string|null A general warning about possible problems with the entity body |
||
| 681 | */ |
||
| 682 | public function getWarning() |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Get the WWW-Authenticate HTTP header |
||
| 689 | * |
||
| 690 | * @return string|null Indicates the authentication scheme that should be used to access the requested entity |
||
| 691 | */ |
||
| 692 | public function getWwwAuthenticate() |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Checks if HTTP Status code is a Client Error (4xx) |
||
| 699 | * |
||
| 700 | * @return bool |
||
| 701 | */ |
||
| 702 | public function isClientError() |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Checks if HTTP Status code is Server OR Client Error (4xx or 5xx) |
||
| 709 | * |
||
| 710 | * @return boolean |
||
| 711 | */ |
||
| 712 | public function isError() |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Checks if HTTP Status code is Information (1xx) |
||
| 719 | * |
||
| 720 | * @return bool |
||
| 721 | */ |
||
| 722 | public function isInformational() |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Checks if HTTP Status code is a Redirect (3xx) |
||
| 729 | * |
||
| 730 | * @return bool |
||
| 731 | */ |
||
| 732 | public function isRedirect() |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Checks if HTTP Status code is Server Error (5xx) |
||
| 739 | * |
||
| 740 | * @return bool |
||
| 741 | */ |
||
| 742 | public function isServerError() |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Checks if HTTP Status code is Successful (2xx | 304) |
||
| 749 | * |
||
| 750 | * @return bool |
||
| 751 | */ |
||
| 752 | public function isSuccessful() |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Check if the response can be cached based on the response headers |
||
| 759 | * |
||
| 760 | * @return bool Returns TRUE if the response can be cached or false if not |
||
| 761 | */ |
||
| 762 | public function canCache() |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Gets the number of seconds from the current time in which this response is still considered fresh |
||
| 786 | * |
||
| 787 | * @return int|null Returns the number of seconds |
||
| 788 | */ |
||
| 789 | public function getMaxAge() |
||
| 807 | |||
| 808 | /** |
||
| 809 | * Check if the response is considered fresh. |
||
| 810 | * |
||
| 811 | * A response is considered fresh when its age is less than or equal to the freshness lifetime (maximum age) of the |
||
| 812 | * response. |
||
| 813 | * |
||
| 814 | * @return bool|null |
||
| 815 | */ |
||
| 816 | public function isFresh() |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Check if the response can be validated against the origin server using a conditional GET request. |
||
| 825 | * |
||
| 826 | * @return bool |
||
| 827 | */ |
||
| 828 | public function canValidate() |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Get the freshness of the response by returning the difference of the maximum lifetime of the response and the |
||
| 835 | * age of the response (max-age - age). |
||
| 836 | * |
||
| 837 | * Freshness values less than 0 mean that the response is no longer fresh and is ABS(freshness) seconds expired. |
||
| 838 | * Freshness values of greater than zero is the number of seconds until the response is no longer fresh. A NULL |
||
| 839 | * result means that no freshness information is available. |
||
| 840 | * |
||
| 841 | * @return int |
||
| 842 | */ |
||
| 843 | public function getFreshness() |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Parse the JSON response body and return an array |
||
| 853 | * |
||
| 854 | * @return array|string|int|bool|float |
||
| 855 | * @throws RuntimeException if the response body is not in JSON format |
||
| 856 | */ |
||
| 857 | public function json() |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Parse the XML response body and return a \SimpleXMLElement. |
||
| 869 | * |
||
| 870 | * In order to prevent XXE attacks, this method disables loading external |
||
| 871 | * entities. If you rely on external entities, then you must parse the |
||
| 872 | * XML response manually by accessing the response body directly. |
||
| 873 | * |
||
| 874 | * @return \SimpleXMLElement |
||
| 875 | * @throws RuntimeException if the response body is not in XML format |
||
| 876 | * @link http://websec.io/2012/08/27/Preventing-XXE-in-PHP.html |
||
| 877 | */ |
||
| 878 | public function xml() |
||
| 904 | |||
| 905 | /** |
||
| 906 | * Get the redirect count of this response |
||
| 907 | * |
||
| 908 | * @return int |
||
| 909 | */ |
||
| 910 | public function getRedirectCount() |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Set the effective URL that resulted in this response (e.g. the last redirect URL) |
||
| 917 | * |
||
| 918 | * @param string $url The effective URL |
||
| 919 | * |
||
| 920 | * @return self |
||
| 921 | */ |
||
| 922 | public function setEffectiveUrl($url) |
||
| 928 | |||
| 929 | /** |
||
| 930 | * Get the effective URL that resulted in this response (e.g. the last redirect URL) |
||
| 931 | * |
||
| 932 | * @return string |
||
| 933 | */ |
||
| 934 | public function getEffectiveUrl() |
||
| 938 | |||
| 939 | /** |
||
| 940 | * @deprecated |
||
| 941 | * @codeCoverageIgnore |
||
| 942 | */ |
||
| 943 | public function getPreviousResponse() |
||
| 948 | |||
| 949 | /** |
||
| 950 | * @deprecated |
||
| 951 | * @codeCoverageIgnore |
||
| 952 | */ |
||
| 953 | public function setRequest($request) |
||
| 958 | |||
| 959 | /** |
||
| 960 | * @deprecated |
||
| 961 | * @codeCoverageIgnore |
||
| 962 | */ |
||
| 963 | public function getRequest() |
||
| 968 | } |
||
| 969 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.