| Total Complexity | 42 | 
| Total Lines | 208 | 
| Duplicated Lines | 0 % | 
| Changes | 7 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like HttpResources 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 HttpResources, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 30 | trait HttpResources  | 
            ||
| 31 | { | 
            ||
| 32 | /**  | 
            ||
| 33 | * Filters a value from the start of a string in this case the passed URI string.  | 
            ||
| 34 | *  | 
            ||
| 35 | * @return string  | 
            ||
| 36 | */  | 
            ||
| 37 | protected function parseRequestUri(): string  | 
            ||
| 38 | 	{ | 
            ||
| 39 | $requestUri = '';  | 
            ||
| 40 | |||
| 41 | 		if ('1' == $this->server->get('IIS_WasUrlRewritten') && '' != $this->server->get('UNENCODED_URL')) { | 
            ||
| 42 | // IIS7 with URL Rewrite: make sure we get the unencoded URL (double slash problem)  | 
            ||
| 43 | 			$requestUri = $this->server->get('UNENCODED_URL'); | 
            ||
| 44 | 			$this->server->remove('UNENCODED_URL'); | 
            ||
| 45 | 			$this->server->remove('IIS_WasUrlRewritten'); | 
            ||
| 46 | 		} elseif ($this->server->has('REQUEST_URI')) { | 
            ||
| 47 | 			$requestUri = $this->server->get('REQUEST_URI'); | 
            ||
| 48 | |||
| 49 | 			if ('' !== $requestUri && '/' === $requestUri[0]) { | 
            ||
| 50 | // To only use path and query remove the fragment.  | 
            ||
| 51 | 				if (false !== $pos = strpos($requestUri, '#')) { | 
            ||
| 52 | $requestUri = substr($requestUri, 0, $pos);  | 
            ||
| 53 | }  | 
            ||
| 54 | 			} else { | 
            ||
| 55 | // HTTP proxy reqs setup request URI with scheme and host [and port] + the URL path,  | 
            ||
| 56 | // only use URL path.  | 
            ||
| 57 | $uriComponents = parse_url($requestUri);  | 
            ||
| 58 | |||
| 59 | 				if (isset($uriComponents['path'])) { | 
            ||
| 60 | $requestUri = $uriComponents['path'];  | 
            ||
| 61 | }  | 
            ||
| 62 | |||
| 63 | 				if (isset($uriComponents['query'])) { | 
            ||
| 64 | $requestUri .= '?'.$uriComponents['query'];  | 
            ||
| 65 | }  | 
            ||
| 66 | }  | 
            ||
| 67 | 		} elseif ($this->server->has('ORIG_PATH_INFO')) { | 
            ||
| 68 | // IIS 5.0, PHP as CGI  | 
            ||
| 69 | 			$requestUri = $this->server->get('ORIG_PATH_INFO'); | 
            ||
| 70 | |||
| 71 | 			if ('' != $this->server->get('QUERY_STRING')) { | 
            ||
| 72 | 				$requestUri .= '?'.$this->server->get('QUERY_STRING'); | 
            ||
| 73 | }  | 
            ||
| 74 | |||
| 75 | 			$this->server->remove('ORIG_PATH_INFO'); | 
            ||
| 76 | }  | 
            ||
| 77 | |||
| 78 | // normalize the request URI to ease creating sub-requests from this request  | 
            ||
| 79 | 		$this->server->set('REQUEST_URI', $requestUri); | 
            ||
| 80 | |||
| 81 | return $this->filterDecode($requestUri);  | 
            ||
| 82 | }  | 
            ||
| 83 | |||
| 84 | /**  | 
            ||
| 85 | * Will parse QUERY_STRING and automatically detect the URI from it.  | 
            ||
| 86 | *  | 
            ||
| 87 | * @return string  | 
            ||
| 88 | */  | 
            ||
| 89 | protected function parseQueryString(): string  | 
            ||
| 90 | 	{ | 
            ||
| 91 | 		$uri = $_SERVER['QUERY_STRING'] ?? @getenv('QUERY_STRING'); | 
            ||
| 92 | |||
| 93 | 		if (trim($uri, '/') === '') { | 
            ||
| 94 | return '';  | 
            ||
| 95 | 		} elseif (0 === strncmp($uri, '/', 1)) { | 
            ||
| 96 | 			$uri    				 = explode('?', $uri, 2); | 
            ||
| 97 | $_SERVER['QUERY_STRING'] = $uri[1] ?? '';  | 
            ||
| 98 | $uri = $uri[0] ?? '';  | 
            ||
| 99 | }  | 
            ||
| 100 | |||
| 101 | parse_str($_SERVER['QUERY_STRING'], $_GET);  | 
            ||
| 102 | |||
| 103 | return $this->filterDecode($uri);  | 
            ||
| 104 | }  | 
            ||
| 105 | |||
| 106 | /**  | 
            ||
| 107 | * Parse the base URL.  | 
            ||
| 108 | *  | 
            ||
| 109 | * @return string  | 
            ||
| 110 | */  | 
            ||
| 111 | public function parseBaseUrl(): string  | 
            ||
| 164 | }  | 
            ||
| 165 | |||
| 166 | /**  | 
            ||
| 167 | * Returns the prefix as encoded in the string when the string starts with  | 
            ||
| 168 | * the given prefix, null otherwise.  | 
            ||
| 169 | *  | 
            ||
| 170 | * return string|null  | 
            ||
| 171 | */  | 
            ||
| 172 | private function getUrlencoded(string $string, string $prefix): ?string  | 
            ||
| 185 | }  | 
            ||
| 186 | |||
| 187 | |||
| 188 | /**  | 
            ||
| 189 | * Parse the path info.  | 
            ||
| 190 | *  | 
            ||
| 191 | * @return string  | 
            ||
| 192 | */  | 
            ||
| 193 | public function parsePathInfo(): string  | 
            ||
| 194 | 	{ | 
            ||
| 195 | 		if (null === ($requestUri = $this->getRequestUri())) { | 
            ||
| 196 | return '/';  | 
            ||
| 197 | }  | 
            ||
| 198 | |||
| 199 | // Remove the query string from REQUEST_URI  | 
            ||
| 200 | 		if (false !== $pos = strpos($requestUri, '?')) { | 
            ||
| 201 | $requestUri = substr($requestUri, 0, $pos);  | 
            ||
| 202 | }  | 
            ||
| 203 | |||
| 204 | 		if ('' !== $requestUri && '/' !== $requestUri[0]) { | 
            ||
| 205 | $requestUri = '/'.$requestUri;  | 
            ||
| 206 | }  | 
            ||
| 207 | |||
| 208 | 		if (null === ($baseUrl = $this->getBaseUrl())) { | 
            ||
| 209 | return $requestUri;  | 
            ||
| 210 | }  | 
            ||
| 211 | |||
| 212 | $pathInfo = substr($requestUri, \strlen($baseUrl));  | 
            ||
| 213 | |||
| 214 | 		if (false === $pathInfo || '' === $pathInfo) { | 
            ||
| 215 | // If substr() returns false then PATH_INFO is set to an empty string  | 
            ||
| 216 | return '/';  | 
            ||
| 217 | }  | 
            ||
| 218 | |||
| 219 | return $this->filterDecode($pathInfo);  | 
            ||
| 220 | }  | 
            ||
| 221 | |||
| 222 | /**  | 
            ||
| 223 | * Filters the uri string remove any malicious characters and inappropriate slashes.  | 
            ||
| 224 | *  | 
            ||
| 225 | * @param string $uri  | 
            ||
| 226 | *  | 
            ||
| 227 | * @return string  | 
            ||
| 228 | */  | 
            ||
| 229 | protected function filterDecode($uri): string  | 
            ||
| 238 | }  | 
            ||
| 239 | }  |