Complex classes like Net_URL2 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 Net_URL2, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 57 | class Net_URL2 |
||
| 58 | { |
||
| 59 | /** |
||
| 60 | * Do strict parsing in resolve() (see RFC 3986, section 5.2.2). Default |
||
| 61 | * is true. |
||
| 62 | */ |
||
| 63 | const OPTION_STRICT = 'strict'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Represent arrays in query using PHP's [] notation. Default is true. |
||
| 67 | */ |
||
| 68 | const OPTION_USE_BRACKETS = 'use_brackets'; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Drop zero-based integer sequences in query using PHP's [] notation. Default |
||
| 72 | * is true. |
||
| 73 | */ |
||
| 74 | const OPTION_DROP_SEQUENCE = 'drop_sequence'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * URL-encode query variable keys. Default is true. |
||
| 78 | */ |
||
| 79 | const OPTION_ENCODE_KEYS = 'encode_keys'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Query variable separators when parsing the query string. Every character |
||
| 83 | * is considered a separator. Default is "&". |
||
| 84 | */ |
||
| 85 | const OPTION_SEPARATOR_INPUT = 'input_separator'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Query variable separator used when generating the query string. Default |
||
| 89 | * is "&". |
||
| 90 | */ |
||
| 91 | const OPTION_SEPARATOR_OUTPUT = 'output_separator'; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Default options corresponds to how PHP handles $_GET. |
||
| 95 | */ |
||
| 96 | private $_options = array( |
||
| 97 | self::OPTION_STRICT => true, |
||
| 98 | self::OPTION_USE_BRACKETS => true, |
||
| 99 | self::OPTION_DROP_SEQUENCE => true, |
||
| 100 | self::OPTION_ENCODE_KEYS => true, |
||
| 101 | self::OPTION_SEPARATOR_INPUT => '&', |
||
| 102 | self::OPTION_SEPARATOR_OUTPUT => '&', |
||
| 103 | ); |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var string|bool |
||
| 107 | */ |
||
| 108 | private $_scheme = false; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var string|bool |
||
| 112 | */ |
||
| 113 | private $_userinfo = false; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var string|bool |
||
| 117 | */ |
||
| 118 | private $_host = false; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var string|bool |
||
| 122 | */ |
||
| 123 | private $_port = false; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var string |
||
| 127 | */ |
||
| 128 | private $_path = ''; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var string|bool |
||
| 132 | */ |
||
| 133 | private $_query = false; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var string|bool |
||
| 137 | */ |
||
| 138 | private $_fragment = false; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Constructor. |
||
| 142 | * |
||
| 143 | * @param string $url an absolute or relative URL |
||
| 144 | * @param array $options an array of OPTION_xxx constants |
||
| 145 | * |
||
| 146 | * @uses self::parseUrl() |
||
| 147 | */ |
||
| 148 | public function __construct($url, array $options = array()) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Magic Setter. |
||
| 161 | * |
||
| 162 | * This method will magically set the value of a private variable ($var) |
||
| 163 | * with the value passed as the args |
||
| 164 | * |
||
| 165 | * @param string $var The private variable to set. |
||
| 166 | * @param mixed $arg An argument of any type. |
||
| 167 | * |
||
| 168 | * @return void |
||
| 169 | */ |
||
| 170 | public function __set($var, $arg) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Magic Getter. |
||
| 180 | * |
||
| 181 | * This is the magic get method to retrieve the private variable |
||
| 182 | * that was set by either __set() or it's setter... |
||
| 183 | * |
||
| 184 | * @param string $var The property name to retrieve. |
||
| 185 | * |
||
| 186 | * @return mixed $this->$var Either a boolean false if the |
||
| 187 | * property is not set or the value |
||
| 188 | * of the private property. |
||
| 189 | */ |
||
| 190 | public function __get($var) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns the scheme, e.g. "http" or "urn", or false if there is no |
||
| 202 | * scheme specified, i.e. if this is a relative URL. |
||
| 203 | * |
||
| 204 | * @return string|bool |
||
| 205 | */ |
||
| 206 | public function getScheme() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Sets the scheme, e.g. "http" or "urn". Specify false if there is no |
||
| 213 | * scheme specified, i.e. if this is a relative URL. |
||
| 214 | * |
||
| 215 | * @param string|bool $scheme e.g. "http" or "urn", or false if there is no |
||
| 216 | * scheme specified, i.e. if this is a relative |
||
| 217 | * URL |
||
| 218 | * |
||
| 219 | * @return $this |
||
| 220 | * @see getScheme |
||
| 221 | */ |
||
| 222 | public function setScheme($scheme) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Returns the user part of the userinfo part (the part preceding the first |
||
| 230 | * ":"), or false if there is no userinfo part. |
||
| 231 | * |
||
| 232 | * @return string|bool |
||
| 233 | */ |
||
| 234 | public function getUser() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns the password part of the userinfo part (the part after the first |
||
| 243 | * ":"), or false if there is no userinfo part (i.e. the URL does not |
||
| 244 | * contain "@" in front of the hostname) or the userinfo part does not |
||
| 245 | * contain ":". |
||
| 246 | * |
||
| 247 | * @return string|bool |
||
| 248 | */ |
||
| 249 | public function getPassword() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns the userinfo part, or false if there is none, i.e. if the |
||
| 258 | * authority part does not contain "@". |
||
| 259 | * |
||
| 260 | * @return string|bool |
||
| 261 | */ |
||
| 262 | public function getUserinfo() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Sets the userinfo part. If two arguments are passed, they are combined |
||
| 269 | * in the userinfo part as username ":" password. |
||
| 270 | * |
||
| 271 | * @param string|bool $userinfo userinfo or username |
||
| 272 | * @param string|bool $password optional password, or false |
||
| 273 | * |
||
| 274 | * @return $this |
||
| 275 | */ |
||
| 276 | public function setUserinfo($userinfo, $password = false) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns the host part, or false if there is no authority part, e.g. |
||
| 292 | * relative URLs. |
||
| 293 | * |
||
| 294 | * @return string|bool a hostname, an IP address, or false |
||
| 295 | */ |
||
| 296 | public function getHost() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Sets the host part. Specify false if there is no authority part, e.g. |
||
| 303 | * relative URLs. |
||
| 304 | * |
||
| 305 | * @param string|bool $host a hostname, an IP address, or false |
||
| 306 | * |
||
| 307 | * @return $this |
||
| 308 | */ |
||
| 309 | public function setHost($host) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Returns the port number, or false if there is no port number specified, |
||
| 317 | * i.e. if the default port is to be used. |
||
| 318 | * |
||
| 319 | * @return string|bool |
||
| 320 | */ |
||
| 321 | public function getPort() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Sets the port number. Specify false if there is no port number specified, |
||
| 328 | * i.e. if the default port is to be used. |
||
| 329 | * |
||
| 330 | * @param string|bool $port a port number, or false |
||
| 331 | * |
||
| 332 | * @return $this |
||
| 333 | */ |
||
| 334 | public function setPort($port) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Returns the authority part, i.e. [ userinfo "@" ] host [ ":" port ], or |
||
| 342 | * false if there is no authority. |
||
| 343 | * |
||
| 344 | * @return string|bool |
||
| 345 | */ |
||
| 346 | public function getAuthority() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Sets the authority part, i.e. [ userinfo "@" ] host [ ":" port ]. Specify |
||
| 369 | * false if there is no authority. |
||
| 370 | * |
||
| 371 | * @param string|bool $authority a hostname or an IP address, possibly |
||
| 372 | * with userinfo prefixed and port number |
||
| 373 | * appended, e.g. "foo:[email protected]:81". |
||
| 374 | * |
||
| 375 | * @return $this |
||
| 376 | */ |
||
| 377 | public function setAuthority($authority) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Returns the path part (possibly an empty string). |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | public function getPath() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Sets the path part (possibly an empty string). |
||
| 416 | * |
||
| 417 | * @param string $path a path |
||
| 418 | * |
||
| 419 | * @return $this |
||
| 420 | */ |
||
| 421 | public function setPath($path) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Returns the query string (excluding the leading "?"), or false if "?" |
||
| 429 | * is not present in the URL. |
||
| 430 | * |
||
| 431 | * @return string|bool |
||
| 432 | * @see getQueryVariables |
||
| 433 | */ |
||
| 434 | public function getQuery() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Sets the query string (excluding the leading "?"). Specify false if "?" |
||
| 441 | * is not present in the URL. |
||
| 442 | * |
||
| 443 | * @param string|bool $query a query string, e.g. "foo=1&bar=2" |
||
| 444 | * |
||
| 445 | * @return $this |
||
| 446 | * @see setQueryVariables |
||
| 447 | */ |
||
| 448 | public function setQuery($query) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Returns the fragment name, or false if "#" is not present in the URL. |
||
| 456 | * |
||
| 457 | * @return string|bool |
||
| 458 | */ |
||
| 459 | public function getFragment() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Sets the fragment name. Specify false if "#" is not present in the URL. |
||
| 466 | * |
||
| 467 | * @param string|bool $fragment a fragment excluding the leading "#", or |
||
| 468 | * false |
||
| 469 | * |
||
| 470 | * @return $this |
||
| 471 | */ |
||
| 472 | public function setFragment($fragment) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Returns the query string like an array as the variables would appear in |
||
| 480 | * $_GET in a PHP script. If the URL does not contain a "?", an empty array |
||
| 481 | * is returned. |
||
| 482 | * |
||
| 483 | * @return array |
||
| 484 | */ |
||
| 485 | public function getQueryVariables() |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Parse a single query key=value pair into an existing php array |
||
| 521 | * |
||
| 522 | * @param string $key query-key |
||
| 523 | * @param string $value query-value |
||
| 524 | * @param array $array of existing query variables (if any) |
||
| 525 | * |
||
| 526 | * @return mixed |
||
| 527 | */ |
||
| 528 | private function _queryArrayByKey($key, $value, array $array = array()) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Parse a key-buffer to place value in array |
||
| 564 | * |
||
| 565 | * @param string $buffer to consume all keys from |
||
| 566 | * @param string $value to be set/add |
||
| 567 | * @param array $array to traverse and set/add value in |
||
| 568 | * |
||
| 569 | * @throws Exception |
||
| 570 | * @return array |
||
| 571 | */ |
||
| 572 | private function _queryArrayByBrackets($buffer, $value, array $array = null) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Query-key has brackets ("...[]") |
||
| 628 | * |
||
| 629 | * @param string $key query-key |
||
| 630 | * |
||
| 631 | * @return bool|int offset of opening bracket, false if no brackets |
||
| 632 | */ |
||
| 633 | private function _queryKeyBracketOffset($key) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Sets the query string to the specified variable in the query string. |
||
| 646 | * |
||
| 647 | * @param array $array (name => value) array |
||
| 648 | * |
||
| 649 | * @return $this |
||
| 650 | */ |
||
| 651 | public function setQueryVariables(array $array) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Sets the specified variable in the query string. |
||
| 666 | * |
||
| 667 | * @param string $name variable name |
||
| 668 | * @param mixed $value variable value |
||
| 669 | * |
||
| 670 | * @return $this |
||
| 671 | */ |
||
| 672 | public function setQueryVariable($name, $value) |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Removes the specified variable from the query string. |
||
| 682 | * |
||
| 683 | * @param string $name a query string variable, e.g. "foo" in "?foo=1" |
||
| 684 | * |
||
| 685 | * @return void |
||
| 686 | */ |
||
| 687 | public function unsetQueryVariable($name) |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Returns a string representation of this URL. |
||
| 696 | * |
||
| 697 | * @return string |
||
| 698 | */ |
||
| 699 | public function getURL() |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Put authority and path together, wrapping authority |
||
| 728 | * into proper separators/terminators. |
||
| 729 | * |
||
| 730 | * @param string|bool $authority authority |
||
| 731 | * @param string $path path |
||
| 732 | * |
||
| 733 | * @return string |
||
| 734 | */ |
||
| 735 | private function _buildAuthorityAndPath($authority, $path) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Returns a string representation of this URL. |
||
| 748 | * |
||
| 749 | * @return string |
||
| 750 | * @link https://php.net/language.oop5.magic#object.tostring |
||
| 751 | */ |
||
| 752 | public function __toString() |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Returns a normalized string representation of this URL. This is useful |
||
| 759 | * for comparison of URLs. |
||
| 760 | * |
||
| 761 | * @return string |
||
| 762 | */ |
||
| 763 | public function getNormalizedURL() |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Normalizes the URL |
||
| 772 | * |
||
| 773 | * See RFC 3986, Section 6. Normalization and Comparison |
||
| 774 | * |
||
| 775 | * @link https://tools.ietf.org/html/rfc3986#section-6 |
||
| 776 | * |
||
| 777 | * @return void |
||
| 778 | */ |
||
| 779 | public function normalize() |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Normalize case of %XX percentage-encodings (RFC 3986, section 6.2.2.1) |
||
| 832 | * Normalize percentage-encoded unreserved characters (section 6.2.2.2) |
||
| 833 | * |
||
| 834 | * @param string|array $mixed string or array of strings to normalize |
||
| 835 | * |
||
| 836 | * @return string|array |
||
| 837 | * @see normalize |
||
| 838 | * @see _normalizeCallback() |
||
| 839 | */ |
||
| 840 | private function _normalize($mixed) |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Callback for _normalize() of %XX percentage-encodings |
||
| 850 | * |
||
| 851 | * @param array $matches as by preg_replace_callback |
||
| 852 | * |
||
| 853 | * @return string |
||
| 854 | * @see normalize |
||
| 855 | * @see _normalize |
||
| 856 | * @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
||
| 857 | */ |
||
| 858 | private function _normalizeCallback($matches) |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Returns whether this instance represents an absolute URL. |
||
| 865 | * |
||
| 866 | * @return bool |
||
| 867 | */ |
||
| 868 | public function isAbsolute() |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Returns an Net_URL2 instance representing an absolute URL relative to |
||
| 875 | * this URL. |
||
| 876 | * |
||
| 877 | * @param Net_URL2|string $reference relative URL |
||
| 878 | * |
||
| 879 | * @throws Exception |
||
| 880 | * @return $this |
||
| 881 | */ |
||
| 882 | public function resolve($reference) |
||
| 948 | |||
| 949 | /** |
||
| 950 | * URL is fragment-only |
||
| 951 | * |
||
| 952 | * @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
||
| 953 | * @return bool |
||
| 954 | */ |
||
| 955 | private function _isFragmentOnly() |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Removes dots as described in RFC 3986, section 5.2.4, e.g. |
||
| 970 | * "/foo/../bar/baz" => "/bar/baz" |
||
| 971 | * |
||
| 972 | * @param string $path a path |
||
| 973 | * |
||
| 974 | * @return string a path |
||
| 975 | */ |
||
| 976 | public static function removeDotSegments($path) |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Percent-encodes all non-alphanumeric characters except these: _ . - ~ |
||
| 1029 | * Similar to PHP's rawurlencode(), except that it also encodes ~ in PHP |
||
| 1030 | * 5.2.x and earlier. |
||
| 1031 | * |
||
| 1032 | * @param string $string string to encode |
||
| 1033 | * |
||
| 1034 | * @return string |
||
| 1035 | */ |
||
| 1036 | public static function urlencode($string) |
||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * Returns a Net_URL2 instance representing the canonical URL of the |
||
| 1047 | * currently executing PHP script. |
||
| 1048 | * |
||
| 1049 | * @throws Exception |
||
| 1050 | * @return string |
||
| 1051 | */ |
||
| 1052 | public static function getCanonical() |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Returns the URL used to retrieve the current request. |
||
| 1074 | * |
||
| 1075 | * @return string |
||
| 1076 | */ |
||
| 1077 | public static function getRequestedURL() |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Returns a Net_URL2 instance representing the URL used to retrieve the |
||
| 1084 | * current request. |
||
| 1085 | * |
||
| 1086 | * @throws Exception |
||
| 1087 | * @return $this |
||
| 1088 | */ |
||
| 1089 | public static function getRequested() |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Returns the value of the specified option. |
||
| 1106 | * |
||
| 1107 | * @param string $optionName The name of the option to retrieve |
||
| 1108 | * |
||
| 1109 | * @return mixed |
||
| 1110 | */ |
||
| 1111 | public function getOption($optionName) |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * A simple version of http_build_query in userland. The encoded string is |
||
| 1119 | * percentage encoded according to RFC 3986. |
||
| 1120 | * |
||
| 1121 | * @param array $data An array, which has to be converted into |
||
| 1122 | * QUERY_STRING. Anything is possible. |
||
| 1123 | * @param string $separator Separator {@link self::OPTION_SEPARATOR_OUTPUT} |
||
| 1124 | * @param string $key For stacked values (arrays in an array). |
||
| 1125 | * |
||
| 1126 | * @return string |
||
| 1127 | */ |
||
| 1128 | protected function buildQuery(array $data, $separator, $key = null) |
||
| 1155 | |||
| 1156 | /** |
||
| 1157 | * This method uses a regex to parse the url into the designated parts. |
||
| 1158 | * |
||
| 1159 | * @param string $url URL |
||
| 1160 | * |
||
| 1161 | * @return void |
||
| 1162 | * @uses self::$_scheme, self::setAuthority(), self::$_path, self::$_query, |
||
| 1163 | * self::$_fragment |
||
| 1164 | * @see __construct |
||
| 1165 | */ |
||
| 1166 | protected function parseUrl($url) |
||
| 1186 | |||
| 1187 | /** |
||
| 1188 | * Encode characters that might have been forgotten to encode when passing |
||
| 1189 | * in an URL. Applied onto Userinfo, Path and Query. |
||
| 1190 | * |
||
| 1191 | * @param string $url URL |
||
| 1192 | * |
||
| 1193 | * @return string |
||
| 1194 | * @see parseUrl |
||
| 1195 | * @see setAuthority |
||
| 1196 | * @link https://pear.php.net/bugs/bug.php?id=20425 |
||
| 1197 | */ |
||
| 1198 | private function _encodeData($url) |
||
| 1205 | |||
| 1206 | /** |
||
| 1207 | * callback for encoding character data |
||
| 1208 | * |
||
| 1209 | * @param array $matches Matches |
||
| 1210 | * |
||
| 1211 | * @return string |
||
| 1212 | * @see _encodeData |
||
| 1213 | * @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
||
| 1214 | */ |
||
| 1215 | private function _encodeCallback(array $matches) |
||
| 1219 | } |
||
| 1220 |
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.