Total Complexity | 59 |
Total Lines | 292 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 1 | Features | 0 |
Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | final class Request extends \WebServCo\Framework\AbstractLibrary |
||
5 | { |
||
6 | /** |
||
7 | * Sanitized _SERVER data. |
||
8 | */ |
||
9 | public $server = []; |
||
10 | /** |
||
11 | * Request method. |
||
12 | */ |
||
13 | public $method; |
||
14 | /** |
||
15 | * Current script filename. Should most commonly be index.php |
||
16 | */ |
||
17 | public $filename; |
||
18 | /** |
||
19 | * Script path. |
||
20 | * For HTTP requests this will be public web server subdirectory |
||
21 | * the project is located in. |
||
22 | * For CLI request this will be the script path |
||
23 | * (full or relative, depending on how the script was called). |
||
24 | */ |
||
25 | public $path = ''; |
||
26 | /** |
||
27 | * Sanitized Framework customized target path. |
||
28 | */ |
||
29 | public $target = ''; |
||
30 | /** |
||
31 | * Sanitized request query. |
||
32 | */ |
||
33 | public $query = []; |
||
34 | |||
35 | public function __construct($config, $server, $post = []) |
||
36 | { |
||
37 | parent::__construct($config); |
||
38 | |||
39 | $this->init($server, $post); |
||
40 | } |
||
41 | |||
42 | private function init($server, $post = []) |
||
43 | { |
||
44 | $this->server = $this->sanitize($server); |
||
45 | $this->method = $this->getMethod(); |
||
46 | $this->filename = $this->getFilename(); |
||
47 | $this->path = $this->getPath(); |
||
48 | $this->process(); |
||
49 | |||
50 | switch ($this->method) { |
||
51 | case \WebServCo\Framework\Http::METHOD_GET: |
||
52 | case \WebServCo\Framework\Http::METHOD_HEAD: |
||
53 | break; |
||
54 | case \WebServCo\Framework\Http::METHOD_POST: |
||
55 | $this->processPost($post); |
||
56 | break; |
||
57 | } |
||
58 | if ($this->setting('clear_globals', true)) { |
||
59 | $this->clearGlobals(); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | private function clearGlobals() |
||
75 | } |
||
76 | |||
77 | private function processPost($post = []) |
||
84 | } |
||
85 | |||
86 | private function process() |
||
87 | { |
||
88 | $string = null; |
||
89 | switch (true) { |
||
90 | case isset($this->server['REQUEST_URI']): |
||
91 | $string = $this->server['REQUEST_URI']; |
||
92 | break; |
||
93 | case isset($this->server['PATH_INFO']): |
||
94 | $string = $this->server['PATH_INFO']; |
||
95 | break; |
||
96 | case isset($this->server['ORIG_PATH_INFO']): |
||
97 | $string = $this->server['ORIG_PATH_INFO']; |
||
98 | break; |
||
99 | case !empty($this->server['QUERY_STRING']): |
||
100 | $string = $this->server['ORIG_PATH_INFO']; |
||
101 | break; |
||
102 | default: |
||
103 | break; |
||
104 | } |
||
105 | if (empty($string)) { |
||
|
|||
106 | return false; //CLI |
||
107 | } |
||
108 | list ($target, $queryString) = $this->parse($string); |
||
109 | $this->target = $this->sanitize(urldecode($target)); |
||
110 | $this->query = $this->format($this->sanitize($queryString)); |
||
111 | } |
||
112 | |||
113 | private function getMethod() |
||
114 | { |
||
115 | return !empty($this->server['REQUEST_METHOD']) && |
||
116 | in_array( |
||
117 | $this->server['REQUEST_METHOD'], |
||
118 | \WebServCo\Framework\Http::getMethods() |
||
119 | ) ? |
||
120 | $this->server['REQUEST_METHOD'] : false; |
||
121 | } |
||
122 | |||
123 | private function getFilename() |
||
127 | } |
||
128 | |||
129 | private function getPath() |
||
136 | } |
||
137 | |||
138 | public function sanitize($data) |
||
139 | { |
||
140 | if (is_array($data)) { |
||
141 | array_walk_recursive($data, [$this, 'sanitizeString']); |
||
142 | return $data; |
||
143 | } |
||
144 | return $this->sanitizeString($data); |
||
145 | } |
||
146 | |||
147 | protected function sanitizeString($string) |
||
148 | { |
||
149 | // Strip tags, optionally strip or encode special characters. |
||
150 | $string = filter_var($string, FILTER_SANITIZE_STRING); |
||
151 | $unwanted = [ |
||
152 | "`", |
||
153 | //"'", |
||
154 | //'"', |
||
155 | "\b", |
||
156 | "\n", |
||
157 | "\r", |
||
158 | "\t", |
||
159 | //"?", |
||
160 | //"!", |
||
161 | //"~", |
||
162 | //"#", |
||
163 | //"^", |
||
164 | //"&", |
||
165 | //"*", |
||
166 | //"=", |
||
167 | //"[", |
||
168 | //"]", |
||
169 | //":", |
||
170 | //";", |
||
171 | //",", |
||
172 | //"|", |
||
173 | "\\", |
||
174 | //"{", |
||
175 | //"}", |
||
176 | //"(", |
||
177 | //")", |
||
178 | "\$" |
||
179 | ]; |
||
180 | $string = str_replace($unwanted, '', $string); |
||
181 | return $string; |
||
182 | } |
||
183 | |||
184 | private function parse($string) |
||
185 | { |
||
186 | $pathLen = strlen($this->path); |
||
187 | if (0 === strncasecmp($this->path, $string, $pathLen)) { |
||
188 | $string = substr($string, $pathLen); |
||
189 | } |
||
190 | $filenameLen = strlen($this->filename); |
||
191 | if (0 === strncasecmp($this->filename, $string, $filenameLen)) { |
||
192 | $string = substr($string, $filenameLen); |
||
193 | } |
||
194 | list($target, $query) = $this->explode($string); |
||
195 | $target = $this->removeSuffix($this->transform($target)); |
||
196 | $query = $this->transform($query); |
||
197 | return [$target, $query]; |
||
198 | } |
||
199 | |||
200 | private function explode($string) |
||
208 | } |
||
209 | |||
210 | private function transform($string) |
||
211 | { |
||
212 | $string = str_replace(['?','&','=','//'], ['','/','/','/0/'], $string); |
||
213 | return trim($string, ' /'); |
||
214 | } |
||
215 | |||
216 | private function removeSuffix($string) |
||
217 | { |
||
218 | $suffixes = $this->setting('suffixes'); |
||
219 | if (is_array($suffixes)) { |
||
220 | $stringRev = strrev($string); |
||
221 | foreach ($suffixes as $suffix) { |
||
222 | $suffixRev = strrev($suffix); |
||
223 | $suffixLen = strlen($suffix); |
||
224 | if (0 === strncasecmp($suffixRev, $stringRev, $suffixLen)) { |
||
225 | return strrev(substr($stringRev, $suffixLen)); |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | return $string; |
||
230 | } |
||
231 | |||
232 | private function format($string) |
||
233 | { |
||
234 | $data = []; |
||
235 | $parts = $this->split($string); |
||
236 | $num = count($parts); |
||
237 | for ($position = 0; $position < $num; $position +=2) { |
||
238 | $data[$parts[$position]] = $position == $num -1 ? null : |
||
239 | $parts[$position + 1]; |
||
240 | } |
||
241 | return $data; |
||
242 | } |
||
243 | |||
244 | public function split($string) |
||
245 | { |
||
246 | $parts = explode('/', $string); |
||
247 | $parts = array_map('urldecode', $parts); |
||
248 | return array_diff($parts, ['']); |
||
249 | } |
||
250 | |||
251 | public function getSchema() |
||
252 | { |
||
253 | if (\WebServCo\Framework\Framework::isCLI()) { |
||
254 | return null; |
||
255 | } |
||
256 | |||
257 | if (isset($this->server['HTTPS']) && 'off' != $this->server['HTTPS']) { |
||
258 | return 'https'; |
||
259 | } elseif (isset($this->server['HTTP_X_FORWARDED_PROTO']) && |
||
260 | 'https' == $this->server['HTTP_X_FORWARDED_PROTO']) { |
||
261 | return 'https'; |
||
262 | } elseif (isset($this->server['HTTP_X_FORWARDED_SSL']) && |
||
263 | 'on' == $this->server['HTTP_X_FORWARDED_SSL']) { |
||
264 | return 'https'; |
||
265 | } |
||
266 | return 'http'; |
||
267 | } |
||
268 | |||
269 | public function getReferer() |
||
270 | { |
||
271 | return isset($this->server['HTTP_REFERER']) ? $this->server['HTTP_REFERER'] : null; |
||
272 | } |
||
273 | |||
274 | public function getHost() |
||
284 | } |
||
285 | |||
286 | public function guessAppUrl() |
||
296 | } |
||
297 | } |
||
298 |