Total Complexity | 56 |
Total Lines | 262 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Http 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 Http, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class Http |
||
33 | { |
||
34 | /** |
||
35 | * Return's the protocol that the request was made with. |
||
36 | * |
||
37 | * @return string |
||
38 | */ |
||
39 | public function protocol() |
||
40 | { |
||
41 | if ($this->server('HTTPS') == 'on' || |
||
42 | $this->server('HTTPS') == 1 || |
||
43 | $this->server('SERVER_PORT') == 443 || |
||
44 | (config('security.allow-x-headers', false) && $this->server('HTTP_X_FORWARDED_PROTO') == 'https') || |
||
45 | (config('security.allow-x-headers', false) && $this->server('HTTP_X_FORWARDED_PORT') == 443)) |
||
46 | { |
||
47 | return 'https'; |
||
48 | } |
||
49 | |||
50 | return 'http'; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Fetch an item from the COOKIE array. |
||
55 | * |
||
56 | * @param string $index The index key |
||
57 | * @param mixed $default The default value |
||
58 | * |
||
59 | * @return string|array |
||
60 | */ |
||
61 | public function cookie($index = null, $default = null) |
||
62 | { |
||
63 | return (func_num_args() === 0) ? $_COOKIE : Arr::get($_COOKIE, strtoupper($index), $default); |
||
|
|||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Fetch an item from the FILE array. |
||
68 | * |
||
69 | * @param string $index The index key |
||
70 | * @param mixed $default The default value |
||
71 | * |
||
72 | * @return string|array |
||
73 | */ |
||
74 | public function file($index = null, $default = null) |
||
75 | { |
||
76 | return (func_num_args() === 0) ? $_FILES : Arr::get($_FILES, strtoupper($index), $default); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Fetch an item from the SERVER array. |
||
81 | * |
||
82 | * @param string $index The index key |
||
83 | * @param mixed $default The default value |
||
84 | * |
||
85 | * @return string|array |
||
86 | */ |
||
87 | public function server($index = null, $default = null) |
||
88 | { |
||
89 | return (func_num_args() === 0) ? $_SERVER : Arr::get($_SERVER, strtoupper($index), $default); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Gets the URI Protocol based setting, will attempt to detect the path |
||
94 | * portion of the current URI. |
||
95 | * |
||
96 | * @param string $protocol |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | public function detectPath(string $protocol = '') |
||
101 | { |
||
102 | if (empty($protocol)) { |
||
103 | $protocol = 'REQUEST_URI'; |
||
104 | } |
||
105 | |||
106 | switch($protocol) { |
||
107 | case 'REQUEST_URI': |
||
108 | $path = $this->parseRequestUri(); |
||
109 | break; |
||
110 | case 'QUERY_STRING': |
||
111 | $path = $this->parseQueryString(); |
||
112 | break; |
||
113 | case 'PATH_INFO': |
||
114 | default: |
||
115 | $path = $this->server($protocol) ?? $this->parseRequestUri(); |
||
116 | break; |
||
117 | } |
||
118 | |||
119 | return $path; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Filters a value from the start of a string in this case the passed URI string. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | protected function parseRequestUri() |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Will parse QUERY_STRING and automatically detect the URI from it. |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | protected function parseQueryString() |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Filters the uri string remove any malicious characters and inappropriate slashes. |
||
193 | * |
||
194 | * @param string $uri |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | protected function filterDecode($uri) |
||
199 | { |
||
200 | // Remove all characters except letters, |
||
201 | // digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=. |
||
202 | $uri = filter_var(rawurldecode($uri), FILTER_SANITIZE_URL); |
||
203 | |||
204 | // Return argument if not empty or return a single slash |
||
205 | return trim($uri, '/') ?: '/'; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Parse the base URL. |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | public function parseBaseUrl() |
||
214 | { |
||
215 | $filename = basename($this->server('SCRIPT_FILENAME')); |
||
216 | |||
217 | if ($filename === basename($this->server('SCRIPT_NAME'))) { |
||
218 | $baseUrl = $this->server('SCRIPT_NAME'); |
||
219 | } elseif ($filename === basename($this->server('PHP_SELF'))) { |
||
220 | $baseUrl = $this->server('PHP_SELF'); |
||
221 | } elseif ($filename === basename($this->server('ORIG_SCRIPT_NAME'))) { |
||
222 | $baseUrl = $this->server('ORIG_SCRIPT_NAME'); |
||
223 | } else { |
||
224 | $path = $this->server('PHP_SELF', ''); |
||
225 | $file = $this->server('SCRIPT_FILENAME', ''); |
||
226 | $segs = explode('/', trim($file, '/')); |
||
227 | $segs = array_reverse($segs); |
||
228 | $index = 0; |
||
229 | $last = count($segs); |
||
230 | $baseUrl = ''; |
||
231 | |||
232 | do { |
||
233 | $seg = $segs[$index]; |
||
234 | $baseUrl = '/'.$seg.$baseUrl; |
||
235 | ++$index; |
||
236 | } while ($last > $index && (false !== $pos = strpos($path, $baseUrl)) && 0 != $pos); |
||
237 | } |
||
238 | |||
239 | // Does the baseUrl have anything in common with the request_uri? |
||
240 | $requestUri = $this->parseRequestUri(); |
||
241 | |||
242 | if ('' !== $requestUri && '/' !== $requestUri[0]) { |
||
243 | $requestUri = '/'.$requestUri; |
||
244 | } |
||
245 | |||
246 | $baseUrl = dirname($baseUrl); |
||
247 | |||
248 | if (empty($baseUrl) || false !== strpos(rawurldecode($requestUri), $baseUrl)) { |
||
249 | // no match whatsoever; set it blank |
||
250 | return ''; |
||
251 | } |
||
252 | |||
253 | // If using mod_rewrite or ISAPI_Rewrite strip the script filename |
||
254 | // out of baseUrl. $pos !== 0 makes sure it is not matching a value |
||
255 | // from PATH_INFO or QUERY_STRING |
||
256 | if (strlen($requestUri) >= strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && 0 !== $pos) { |
||
257 | $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl)); |
||
258 | } |
||
259 | |||
260 | return rtrim($baseUrl, '/'.DIRECTORY_SEPARATOR); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Parse the path info. |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | public function parsePathInfo() |
||
294 | } |
||
295 | } |