| Total Complexity | 59 |
| Total Lines | 323 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Url 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.
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 Url, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Url |
||
| 13 | { |
||
| 14 | private $_urlAsString = null; |
||
| 15 | private $_parts = array(); |
||
| 16 | private $_segments = array(); |
||
| 17 | const ABS_URL_REGEXP = '/^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/'; |
||
| 18 | const REL_URL_REGEXP = '/^(\/|\/([\w#!:.?+=&%@!\-\/]))?/'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * array of query-string parameters |
||
| 22 | * |
||
| 23 | * @var array(string, string) |
||
| 24 | */ |
||
| 25 | private $_queryOptions; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Creates new instance of Url |
||
| 29 | * |
||
| 30 | * @param string $url The url as string |
||
| 31 | * @param boolean $isAbsolute Whether the given url is absolute or not |
||
| 32 | * |
||
| 33 | * @throws UrlFormatException Exception if url is malformed |
||
| 34 | */ |
||
| 35 | public function __construct($url, $isAbsolute = true) |
||
| 36 | { |
||
| 37 | if ($isAbsolute) { |
||
| 38 | if (!preg_match(self::ABS_URL_REGEXP, $url)) { |
||
| 39 | throw new UrlFormatException(Messages::urlMalformedUrl($url)); |
||
| 40 | } |
||
| 41 | } else { |
||
| 42 | if (!preg_match(self::REL_URL_REGEXP, $url)) { //TODO: this matches EVERYTHING!!! what's the intent here? see #77 |
||
| 43 | throw new UrlFormatException(Messages::urlMalformedUrl($url)); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | $this->_parts = parse_url($url); |
||
| 48 | if ($this->_parts === false) { |
||
| 49 | throw new UrlFormatException(Messages::urlMalformedUrl($url)); |
||
| 50 | } |
||
| 51 | |||
| 52 | $path = urldecode($this->getPath()); |
||
| 53 | if ($path != null) { |
||
| 54 | $this->_segments = explode('/', trim($path, '/')); |
||
| 55 | foreach ($this->_segments as $segment) { |
||
| 56 | $segment = trim($segment); |
||
| 57 | if (empty($segment)) { |
||
| 58 | throw new UrlFormatException(Messages::urlMalformedUrl($url)); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | $this->_urlAsString = $url; |
||
| 64 | |||
| 65 | $this->_queryOptions = []; |
||
| 66 | if (!empty($this->_parts['query'])) { |
||
| 67 | parse_str($this->_parts['query'], $this->_queryOptions); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Gets the url represented by this instance as string |
||
| 73 | * |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | public function getUrlAsString() |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get the scheme part of the Url |
||
| 83 | * |
||
| 84 | * @return string|null Returns the scheme part of the url, |
||
| 85 | * if scheme is missing returns NULL |
||
| 86 | */ |
||
| 87 | public function getScheme() |
||
| 88 | { |
||
| 89 | return isset ($this->_parts['scheme']) ? $this->_parts['scheme'] : null; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get the host part of the Url |
||
| 94 | * |
||
| 95 | * @return string|null Returns the host part of the url, |
||
| 96 | * if host is missing returns NULL |
||
| 97 | */ |
||
| 98 | public function getHost() |
||
| 99 | { |
||
| 100 | return isset ($this->_parts['host']) ? $this->_parts['host'] : null; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get the port number present in the url |
||
| 105 | * |
||
| 106 | * @return int |
||
| 107 | */ |
||
| 108 | public function getPort() |
||
| 109 | { |
||
| 110 | $port = isset ($this->_parts['port']) ? $this->_parts['port'] : null; |
||
| 111 | if ($port != null) { |
||
| 112 | return $port; |
||
| 113 | } |
||
| 114 | |||
| 115 | $host = $this->getScheme(); |
||
| 116 | if ($host == 'https') { |
||
| 117 | $port = 443; |
||
| 118 | } else if ($host == 'http') { |
||
| 119 | $port = 80; |
||
| 120 | } |
||
| 121 | |||
| 122 | return $port; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * To get the path segment |
||
| 127 | * |
||
| 128 | * @return string Returns the host part of the url, |
||
| 129 | * if host is missing returns NULL |
||
| 130 | */ |
||
| 131 | public function getPath() |
||
| 132 | { |
||
| 133 | return isset ($this->_parts['path']) ? $this->_parts['path'] : null; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get the query part |
||
| 138 | * |
||
| 139 | * @return string|null Returns the query part of the url, |
||
| 140 | * if query is missing returns NULL |
||
| 141 | */ |
||
| 142 | public function getQuery() |
||
| 143 | { |
||
| 144 | return isset ($this->_parts['query']) ? $this->_parts['query'] : null; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get the fragment part |
||
| 149 | * |
||
| 150 | * @return string|null Returns the fragment part of the url, |
||
| 151 | * if fragment is missing returns NULL |
||
| 152 | */ |
||
| 153 | public function getFragment() |
||
| 154 | { |
||
| 155 | return isset ($this->_parts['fragment']) ? $this->_parts['fragment'] : null; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get the segments |
||
| 160 | * |
||
| 161 | * @return array Returns array of segments, |
||
| 162 | * if no segments then returns empty array. |
||
| 163 | */ |
||
| 164 | public function getSegments() |
||
| 165 | { |
||
| 166 | return $this->_segments; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Gets number of segments, if no segment then returns zero. |
||
| 171 | * |
||
| 172 | * @return int |
||
| 173 | */ |
||
| 174 | public function getSegmentCount() |
||
| 175 | { |
||
| 176 | return count($this->_segments); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Checks the url is absolute or not |
||
| 181 | * |
||
| 182 | * @return boolean Returns true if absolute url otherwise false |
||
| 183 | */ |
||
| 184 | public function isAbsolute() |
||
| 185 | { |
||
| 186 | return isset ($this->_parts['scheme']); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Checks the url is relative or not |
||
| 191 | * |
||
| 192 | * @return boolean |
||
| 193 | */ |
||
| 194 | public function isRelative() |
||
| 195 | { |
||
| 196 | return !$this->isAbsolute(); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Checks this url is base uri for the given url. |
||
| 201 | * |
||
| 202 | * @param Url $targetUri The url to inspect the base part. |
||
| 203 | * |
||
| 204 | * @return boolean |
||
| 205 | */ |
||
| 206 | public function isBaseOf(Url $targetUri) |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * This method verfies the client provided url query parameters and check whether |
||
| 233 | * any of the odata query option specified more than once or check any of the |
||
| 234 | * non-odata query parameter start will $ symbol or check any of the odata query |
||
| 235 | * option specified with out value. If any of the above check fails throws |
||
| 236 | * ODataException, else set _queryOptions member variable |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | * |
||
| 240 | * @throws ODataException |
||
| 241 | */ |
||
| 242 | public function validateQueryParameters() |
||
| 291 | } |
||
| 292 | } |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Gets the value for the specified item in the request query string |
||
| 298 | * Remark: This method assumes 'validateQueryParameters' has already been |
||
| 299 | * called. |
||
| 300 | * |
||
| 301 | * @param string $item The query item to get the value of. |
||
| 302 | * |
||
| 303 | * @return string|null The value for the specified item in the request |
||
| 304 | * query string NULL if the query option is absent. |
||
| 305 | */ |
||
| 306 | public function getQueryStringItem($item) |
||
| 307 | { |
||
| 308 | if (array_key_exists($item, $this->_queryOptions)) { |
||
| 309 | return $this->_queryOptions[$item]; |
||
| 310 | } |
||
| 311 | |||
| 312 | return null; |
||
| 313 | } |
||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * Verifies the given url option is a valid odata query option. |
||
| 318 | * |
||
| 319 | * @param string $optionName option to validate |
||
| 320 | * |
||
| 321 | * @return boolean True if the given option is a valid odata option False otherwise. |
||
| 322 | * |
||
| 323 | */ |
||
| 324 | private function _isODataQueryOption($optionName) |
||
| 335 | } |
||
| 336 | } |
||
| 337 |