Complex classes like THttpRequest 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 THttpRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 77 | class THttpRequest extends \Prado\TApplicationComponent implements \IteratorAggregate, \ArrayAccess, \Countable, \Prado\IModule |
||
| 78 | { |
||
| 79 | const CGIFIX__PATH_INFO = 1; |
||
| 80 | const CGIFIX__SCRIPT_NAME = 2; |
||
| 81 | /** |
||
| 82 | * @var TUrlManager the URL manager module |
||
| 83 | */ |
||
| 84 | private $_urlManager=null; |
||
| 85 | /** |
||
| 86 | * @var string the ID of the URL manager module |
||
| 87 | */ |
||
| 88 | private $_urlManagerID=''; |
||
| 89 | /** |
||
| 90 | * @var string Separator used to separate GET variable name and value when URL format is Path. |
||
| 91 | */ |
||
| 92 | private $_separator=','; |
||
| 93 | /** |
||
| 94 | * @var string requested service ID |
||
| 95 | */ |
||
| 96 | private $_serviceID=null; |
||
| 97 | /** |
||
| 98 | * @var string requested service parameter |
||
| 99 | */ |
||
| 100 | private $_serviceParam=null; |
||
| 101 | /** |
||
| 102 | * @var THttpCookieCollection cookies sent from user |
||
| 103 | */ |
||
| 104 | private $_cookies=null; |
||
| 105 | /** |
||
| 106 | * @var string requested URI (URL w/o host info) |
||
| 107 | */ |
||
| 108 | private $_requestUri; |
||
| 109 | /** |
||
| 110 | * @var string path info of URL |
||
| 111 | */ |
||
| 112 | private $_pathInfo; |
||
| 113 | /** |
||
| 114 | * @var boolean whether the session ID should be kept in cookie only |
||
| 115 | */ |
||
| 116 | private $_cookieOnly=null; |
||
| 117 | private $_urlFormat=THttpRequestUrlFormat::Get; |
||
| 118 | private $_services; |
||
|
|
|||
| 119 | private $_requestResolved=false; |
||
| 120 | private $_enableCookieValidation=false; |
||
| 121 | private $_cgiFix=0; |
||
| 122 | /** |
||
| 123 | * @var boolean whether to cache the TUrlManager class (useful with a lot of TUrlMappings) |
||
| 124 | */ |
||
| 125 | private $_enableCache=false; |
||
| 126 | /** |
||
| 127 | * @var string request URL |
||
| 128 | */ |
||
| 129 | private $_url=null; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var string module id |
||
| 133 | */ |
||
| 134 | private $_id; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var array contains all request variables |
||
| 138 | */ |
||
| 139 | private $_items=array(); |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return string id of this module |
||
| 143 | */ |
||
| 144 | public function getID() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string id of this module |
||
| 151 | */ |
||
| 152 | public function setID($value) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Initializes the module. |
||
| 159 | * This method is required by IModule and is invoked by application. |
||
| 160 | * @param TXmlElement module configuration |
||
| 161 | */ |
||
| 162 | public function init($config) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Strips slashes from input data. |
||
| 211 | * This method is applied when magic quotes is enabled. |
||
| 212 | * @param mixed input data to be processed |
||
| 213 | * @return mixed processed data |
||
| 214 | */ |
||
| 215 | public function stripSlashes(&$data) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return TUri the request URL |
||
| 222 | */ |
||
| 223 | public function getUrl() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Set true to cache the UrlManager instance. Consider to enable this cache |
||
| 246 | * when the application defines a lot of TUrlMappingPatterns |
||
| 247 | * @param boolean true to cache urlmanager instance. |
||
| 248 | */ |
||
| 249 | public function setEnableCache($value) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return boolean true if urlmanager instance should be cached, false otherwise. |
||
| 256 | */ |
||
| 257 | public function getEnableCache() |
||
| 261 | |||
| 262 | protected function getCacheKey() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Saves the current UrlManager instance to cache. |
||
| 269 | * @return boolean true if UrlManager instance was cached, false otherwise. |
||
| 270 | */ |
||
| 271 | protected function cacheUrlManager($manager) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Loads UrlManager instance from cache. |
||
| 293 | * @return TUrlManager intance if load was successful, null otherwise. |
||
| 294 | */ |
||
| 295 | protected function loadCachedUrlManager() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return string the ID of the URL manager module |
||
| 312 | */ |
||
| 313 | public function getUrlManager() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Sets the URL manager module. |
||
| 320 | * By default, {@link TUrlManager} is used for managing URLs. |
||
| 321 | * You may specify a different module for URL managing tasks |
||
| 322 | * by loading it as an application module and setting this property |
||
| 323 | * with the module ID. |
||
| 324 | * @param string the ID of the URL manager module |
||
| 325 | */ |
||
| 326 | public function setUrlManager($value) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @return TUrlManager the URL manager module |
||
| 333 | */ |
||
| 334 | public function getUrlManagerModule() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @return THttpRequestUrlFormat the format of URLs. Defaults to THttpRequestUrlFormat::Get. |
||
| 361 | */ |
||
| 362 | public function getUrlFormat() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Sets the format of URLs constructed and interpretted by the request module. |
||
| 369 | * A Get URL format is like index.php?name1=value1&name2=value2 |
||
| 370 | * while a Path URL format is like index.php/name1,value1/name2,value. |
||
| 371 | * Changing the UrlFormat will affect {@link constructUrl} and how GET variables |
||
| 372 | * are parsed. |
||
| 373 | * @param THttpRequestUrlFormat the format of URLs. |
||
| 374 | */ |
||
| 375 | public function setUrlFormat($value) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return string separator used to separate GET variable name and value when URL format is Path. Defaults to comma ','. |
||
| 382 | */ |
||
| 383 | public function getUrlParamSeparator() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param string separator used to separate GET variable name and value when URL format is Path. |
||
| 390 | * @throws TInvalidDataValueException if the separator is not a single character |
||
| 391 | */ |
||
| 392 | public function setUrlParamSeparator($value) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return string request type, can be GET, POST, HEAD, or PUT |
||
| 402 | */ |
||
| 403 | public function getRequestType() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param boolean $mimetypeOnly whether to return only the mimetype (default: true) |
||
| 410 | * @return string content type (e.g. 'application/json' or 'text/html; encoding=gzip') or null if not specified |
||
| 411 | */ |
||
| 412 | public function getContentType($mimetypeOnly = true) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @return boolean if the request is sent via secure channel (https) |
||
| 425 | */ |
||
| 426 | public function getIsSecureConnection() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @return string part of the request URL after script name and before question mark. |
||
| 433 | */ |
||
| 434 | public function getPathInfo() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @return string part of that request URL after the question mark |
||
| 441 | */ |
||
| 442 | public function getQueryString() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @return string the requested http procolol. Blank string if not defined. |
||
| 449 | */ |
||
| 450 | public function getHttpProtocolVersion() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param integer|null Either {@link CASE_UPPER} or {@link CASE_LOWER} or as is null (default) |
||
| 457 | * @return array |
||
| 458 | */ |
||
| 459 | public function getHeaders($case=null) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @return string part of that request URL after the host info (including pathinfo and query string) |
||
| 483 | */ |
||
| 484 | public function getRequestUri() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param boolean|null whether to use HTTPS instead of HTTP even if the current request is sent via HTTP or vice versa |
||
| 491 | * null - keep current schema |
||
| 492 | * true - force https |
||
| 493 | * false - force http |
||
| 494 | * @return string schema and hostname of the requested URL |
||
| 495 | */ |
||
| 496 | public function getBaseUrl($forceSecureConnection=null) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @return string entry script URL (w/o host part) |
||
| 507 | */ |
||
| 508 | public function getApplicationUrl() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param boolean|null whether to use HTTPS instead of HTTP even if the current request is sent via HTTP or vice versa |
||
| 518 | * null - keep current schema |
||
| 519 | * true - force https |
||
| 520 | * false - force http |
||
| 521 | * @return string entry script URL (w/ host part) |
||
| 522 | */ |
||
| 523 | public function getAbsoluteApplicationUrl($forceSecureConnection=null) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @return string application entry script file path (processed w/ realpath()) |
||
| 530 | */ |
||
| 531 | public function getApplicationFilePath() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * @return string server name |
||
| 538 | */ |
||
| 539 | public function getServerName() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @return integer server port number |
||
| 546 | */ |
||
| 547 | public function getServerPort() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @return string URL referrer, null if not present |
||
| 554 | */ |
||
| 555 | public function getUrlReferrer() |
||
| 559 | |||
| 560 | /** |
||
| 561 | * @return string server software |
||
| 562 | * @since 3.3.3 |
||
| 563 | */ |
||
| 564 | public function getServerSoftware() |
||
| 568 | |||
| 569 | /** |
||
| 570 | * @return array user browser capabilities |
||
| 571 | * @see get_browser |
||
| 572 | */ |
||
| 573 | public function getBrowser() |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @return string user agent |
||
| 587 | */ |
||
| 588 | public function getUserAgent() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @return string user IP address |
||
| 595 | */ |
||
| 596 | public function getUserHostAddress() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @return string user host name, null if cannot be determined |
||
| 603 | */ |
||
| 604 | public function getUserHost() |
||
| 608 | |||
| 609 | /** |
||
| 610 | * @return string user browser accept types |
||
| 611 | */ |
||
| 612 | public function getAcceptTypes() |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Returns a list of user preferred languages. |
||
| 620 | * The languages are returned as an array. Each array element |
||
| 621 | * represents a single language preference. The languages are ordered |
||
| 622 | * according to user preferences. The first language is the most preferred. |
||
| 623 | * @return array list of user preferred languages. |
||
| 624 | */ |
||
| 625 | public function getUserLanguages() |
||
| 629 | |||
| 630 | /** |
||
| 631 | * @return boolean whether cookies should be validated. Defaults to false. |
||
| 632 | */ |
||
| 633 | public function getEnableCookieValidation() |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @param boolean whether cookies should be validated. |
||
| 640 | */ |
||
| 641 | public function setEnableCookieValidation($value) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @return integer whether to use ORIG_PATH_INFO and/or ORIG_SCRIPT_NAME. Defaults to 0. |
||
| 648 | * @see THttpRequest::CGIFIX__PATH_INFO, THttpRequest::CGIFIX__SCRIPT_NAME |
||
| 649 | */ |
||
| 650 | public function getCgiFix() |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Enable this, if you're using PHP via CGI with php.ini setting "cgi.fix_pathinfo=1" |
||
| 657 | * and have trouble with friendly URL feature. Enable this only if you really know what you are doing! |
||
| 658 | * @param integer enable bitwise to use ORIG_PATH_INFO and/or ORIG_SCRIPT_NAME. |
||
| 659 | * @see THttpRequest::CGIFIX__PATH_INFO, THttpRequest::CGIFIX__SCRIPT_NAME |
||
| 660 | */ |
||
| 661 | public function setCgiFix($value) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * @return THttpCookieCollection list of cookies to be sent |
||
| 668 | */ |
||
| 669 | public function getCookies() |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @return array list of uploaded files. |
||
| 694 | */ |
||
| 695 | public function getUploadedFiles() |
||
| 699 | |||
| 700 | /** |
||
| 701 | * @return array list of server variables. |
||
| 702 | */ |
||
| 703 | public function getServerVariables() |
||
| 707 | |||
| 708 | /** |
||
| 709 | * @return array list of environment variables. |
||
| 710 | */ |
||
| 711 | public function getEnvironmentVariables() |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Constructs a URL that can be recognized by PRADO. |
||
| 718 | * The actual construction work is done by the URL manager module. |
||
| 719 | * This method may append session information to the generated URL if needed. |
||
| 720 | * You may provide your own URL manager module by setting {@link setUrlManager UrlManager} |
||
| 721 | * to provide your own URL scheme. |
||
| 722 | * |
||
| 723 | * Note, the constructed URL does not contain the protocol and hostname part. |
||
| 724 | * You may obtain an absolute URL by prepending the constructed URL with {@link getBaseUrl BaseUrl}. |
||
| 725 | * @param string service ID |
||
| 726 | * @param string service parameter |
||
| 727 | * @param array GET parameters, null if not needed |
||
| 728 | * @param boolean whether to encode the ampersand in URL, defaults to true. |
||
| 729 | * @param boolean whether to encode the GET parameters (their names and values), defaults to false. |
||
| 730 | * @return string URL |
||
| 731 | * @see TUrlManager::constructUrl |
||
| 732 | */ |
||
| 733 | public function constructUrl($serviceID,$serviceParam,$getItems=null,$encodeAmpersand=true,$encodeGetItems=true) |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Parses the request URL and returns an array of input parameters (excluding GET variables). |
||
| 746 | * You may override this method to support customized URL format. |
||
| 747 | * @return array list of input parameters, indexed by parameter names |
||
| 748 | * @see TUrlManager::parseUrl |
||
| 749 | */ |
||
| 750 | protected function parseUrl() |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Resolves the requested service. |
||
| 757 | * This method implements a URL-based service resolution. |
||
| 758 | * A URL in the format of /index.php?sp=serviceID.serviceParameter |
||
| 759 | * will be resolved with the serviceID and the serviceParameter. |
||
| 760 | * You may override this method to provide your own way of service resolution. |
||
| 761 | * @param array list of valid service IDs |
||
| 762 | * @return string the currently requested service ID, null if no service ID is found |
||
| 763 | * @see constructUrl |
||
| 764 | */ |
||
| 765 | public function resolveRequest($serviceIDs) |
||
| 784 | |||
| 785 | /** |
||
| 786 | * @return boolean true if request is already resolved, false otherwise. |
||
| 787 | */ |
||
| 788 | public function getRequestResolved() |
||
| 792 | |||
| 793 | /** |
||
| 794 | * @return string requested service ID |
||
| 795 | */ |
||
| 796 | public function getServiceID() |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Sets the requested service ID. |
||
| 803 | * @param string requested service ID |
||
| 804 | */ |
||
| 805 | public function setServiceID($value) |
||
| 809 | |||
| 810 | /** |
||
| 811 | * @return string requested service parameter |
||
| 812 | */ |
||
| 813 | public function getServiceParameter() |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Sets the requested service parameter. |
||
| 820 | * @param string requested service parameter |
||
| 821 | */ |
||
| 822 | public function setServiceParameter($value) |
||
| 826 | |||
| 827 | //------ The following methods enable THttpRequest to be TMap-like ----- |
||
| 828 | |||
| 829 | /** |
||
| 830 | * Returns an iterator for traversing the items in the list. |
||
| 831 | * This method is required by the interface \IteratorAggregate. |
||
| 832 | * @return Iterator an iterator for traversing the items in the list. |
||
| 833 | */ |
||
| 834 | public function getIterator() |
||
| 838 | |||
| 839 | /** |
||
| 840 | * @return integer the number of items in the request |
||
| 841 | */ |
||
| 842 | public function getCount() |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Returns the number of items in the request. |
||
| 849 | * This method is required by \Countable interface. |
||
| 850 | * @return integer number of items in the request. |
||
| 851 | */ |
||
| 852 | public function count() |
||
| 856 | |||
| 857 | /** |
||
| 858 | * @return array the key list |
||
| 859 | */ |
||
| 860 | public function getKeys() |
||
| 864 | |||
| 865 | /** |
||
| 866 | * Returns the item with the specified key. |
||
| 867 | * This method is exactly the same as {@link offsetGet}. |
||
| 868 | * @param mixed the key |
||
| 869 | * @return mixed the element at the offset, null if no element is found at the offset |
||
| 870 | */ |
||
| 871 | public function itemAt($key) |
||
| 875 | |||
| 876 | /** |
||
| 877 | * Adds an item into the request. |
||
| 878 | * Note, if the specified key already exists, the old value will be overwritten. |
||
| 879 | * @param mixed key |
||
| 880 | * @param mixed value |
||
| 881 | */ |
||
| 882 | public function add($key,$value) |
||
| 886 | |||
| 887 | /** |
||
| 888 | * Removes an item from the request by its key. |
||
| 889 | * @param mixed the key of the item to be removed |
||
| 890 | * @return mixed the removed value, null if no such key exists. |
||
| 891 | * @throws TInvalidOperationException if the item cannot be removed |
||
| 892 | */ |
||
| 893 | public function remove($key) |
||
| 904 | |||
| 905 | /** |
||
| 906 | * Removes all items in the request. |
||
| 907 | */ |
||
| 908 | public function clear() |
||
| 913 | |||
| 914 | /** |
||
| 915 | * @param mixed the key |
||
| 916 | * @return boolean whether the request contains an item with the specified key |
||
| 917 | */ |
||
| 918 | public function contains($key) |
||
| 922 | |||
| 923 | /** |
||
| 924 | * @return array the list of items in array |
||
| 925 | */ |
||
| 926 | public function toArray() |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Returns whether there is an element at the specified offset. |
||
| 933 | * This method is required by the interface \ArrayAccess. |
||
| 934 | * @param mixed the offset to check on |
||
| 935 | * @return boolean |
||
| 936 | */ |
||
| 937 | public function offsetExists($offset) |
||
| 941 | |||
| 942 | /** |
||
| 943 | * Returns the element at the specified offset. |
||
| 944 | * This method is required by the interface \ArrayAccess. |
||
| 945 | * @param integer the offset to retrieve element. |
||
| 946 | * @return mixed the element at the offset, null if no element is found at the offset |
||
| 947 | */ |
||
| 948 | public function offsetGet($offset) |
||
| 952 | |||
| 953 | /** |
||
| 954 | * Sets the element at the specified offset. |
||
| 955 | * This method is required by the interface \ArrayAccess. |
||
| 956 | * @param integer the offset to set element |
||
| 957 | * @param mixed the element value |
||
| 958 | */ |
||
| 959 | public function offsetSet($offset,$item) |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Unsets the element at the specified offset. |
||
| 966 | * This method is required by the interface \ArrayAccess. |
||
| 967 | * @param mixed the offset to unset element |
||
| 968 | */ |
||
| 969 | public function offsetUnset($offset) |
||
| 973 | } |
This check marks private properties in classes that are never used. Those properties can be removed.