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. 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 Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | class Request |
||
51 | { |
||
52 | /** |
||
53 | * Array of supported HTTP methods. |
||
54 | * |
||
55 | * @var array |
||
56 | * |
||
57 | * @since 2.0 |
||
58 | */ |
||
59 | private $HTTPMethods = array('HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'TRACE'); |
||
60 | |||
61 | /** |
||
62 | * The HTTP method of this request (must be in HTTPMethods array). |
||
63 | * |
||
64 | * @var string |
||
65 | * |
||
66 | * @since 2.0 |
||
67 | */ |
||
68 | private $method; |
||
69 | |||
70 | /** |
||
71 | * An associative array of HTTP headers on this request. |
||
72 | * |
||
73 | * @var array |
||
74 | * |
||
75 | * @since 2.0 |
||
76 | */ |
||
77 | private $headers; |
||
78 | |||
79 | /** |
||
80 | * An associative array of HTTP cookies on this request. |
||
81 | * |
||
82 | * @var array |
||
83 | * |
||
84 | * @since 2.0 |
||
85 | */ |
||
86 | private $cookies; |
||
87 | |||
88 | /** |
||
89 | * The HTTP params (form data and query string) on this request. |
||
90 | * |
||
91 | * @var array |
||
92 | * |
||
93 | * @since 2.0 |
||
94 | */ |
||
95 | private $params; |
||
96 | |||
97 | /** |
||
98 | * An associative 3D array of uploaded files. |
||
99 | * |
||
100 | * @var array |
||
101 | * |
||
102 | * @since 2.0 |
||
103 | */ |
||
104 | private $files; |
||
105 | |||
106 | /** |
||
107 | * The request body if one was provided. |
||
108 | * |
||
109 | * @var string |
||
110 | * |
||
111 | * @since 2.0 |
||
112 | */ |
||
113 | private $body; |
||
114 | |||
115 | /** |
||
116 | * The host header provided on the request. |
||
117 | * |
||
118 | * @var string |
||
119 | * |
||
120 | * @since 2.0 |
||
121 | */ |
||
122 | private $host; |
||
123 | |||
124 | /** |
||
125 | * The IP of the client making the request. |
||
126 | * |
||
127 | * @var string |
||
128 | * |
||
129 | * @since 2.0 |
||
130 | */ |
||
131 | private $IP; |
||
132 | |||
133 | /** |
||
134 | * The URI requested. |
||
135 | * |
||
136 | * @var string |
||
137 | * |
||
138 | * @since 2.0 |
||
139 | */ |
||
140 | private $URI; |
||
141 | |||
142 | /** |
||
143 | * The query string provided on the request (if any). |
||
144 | * |
||
145 | * @var string |
||
146 | * |
||
147 | * @since 2.0 |
||
148 | */ |
||
149 | private $queryString; |
||
150 | |||
151 | /** |
||
152 | * Builds up the request based on available PHP super globals, in addition to |
||
153 | * any overrides provided (useful for testing). |
||
154 | * |
||
155 | * @param array $overrides Hash array of PHP super globals to override |
||
156 | * |
||
157 | * @throws \Alpha\Exception\IllegalArguementException |
||
158 | * |
||
159 | * @since 2.0 |
||
160 | */ |
||
161 | public function __construct($overrides = array()) |
||
162 | { |
||
163 | // set HTTP headers |
||
164 | if (isset($overrides['headers']) && is_array($overrides['headers'])) { |
||
165 | $this->headers = $overrides['headers']; |
||
166 | } else { |
||
167 | $this->headers = $this->getGlobalHeaders(); |
||
168 | } |
||
169 | |||
170 | // set HTTP method |
||
171 | if (isset($overrides['method']) && in_array($overrides['method'], $this->HTTPMethods)) { |
||
172 | $this->method = $overrides['method']; |
||
173 | } else { |
||
174 | $method = $this->getGlobalServerValue('REQUEST_METHOD'); |
||
175 | if (in_array($method, $this->HTTPMethods)) { |
||
176 | $this->method = $method; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | // allow the POST param _METHOD to override the HTTP method |
||
181 | if (isset($_POST['_METHOD']) && in_array($_POST['_METHOD'], $this->HTTPMethods)) { |
||
182 | $this->method = $_POST['_METHOD']; |
||
183 | } |
||
184 | |||
185 | // allow the POST param X-HTTP-Method-Override to override the HTTP method |
||
186 | if (isset($this->headers['X-HTTP-Method-Override']) && in_array($this->headers['X-HTTP-Method-Override'], $this->HTTPMethods)) { |
||
187 | $this->method = $this->headers['X-HTTP-Method-Override']; |
||
188 | } |
||
189 | |||
190 | if ($this->method == '') { |
||
191 | throw new IllegalArguementException('No valid HTTP method provided when creating new Request object'); |
||
192 | } |
||
193 | |||
194 | // set HTTP cookies |
||
195 | if (isset($overrides['cookies']) && is_array($overrides['cookies'])) { |
||
196 | $this->cookies = $overrides['cookies']; |
||
197 | } elseif (isset($_COOKIE)) { |
||
198 | $this->cookies = $_COOKIE; |
||
199 | } else { |
||
200 | $this->cookies = array(); |
||
201 | } |
||
202 | |||
203 | // set HTTP params |
||
204 | if (isset($overrides['params']) && is_array($overrides['params'])) { |
||
205 | $this->params = $overrides['params']; |
||
206 | } else { |
||
207 | $this->params = array(); |
||
208 | |||
209 | if (isset($_GET)) { |
||
210 | $this->params = array_merge($this->params, $_GET); |
||
211 | } |
||
212 | |||
213 | if (isset($_POST)) { |
||
214 | $this->params = array_merge($this->params, $_POST); |
||
215 | } |
||
216 | } |
||
217 | |||
218 | // set HTTP body |
||
219 | if (isset($overrides['body'])) { |
||
220 | $this->body = $overrides['body']; |
||
221 | } else { |
||
222 | $this->body = $this->getGlobalBody(); |
||
223 | } |
||
224 | |||
225 | // set HTTP host |
||
226 | if (isset($overrides['host'])) { |
||
227 | $this->host = $overrides['host']; |
||
228 | } else { |
||
229 | $this->host = $this->getGlobalServerValue('HTTP_HOST'); |
||
230 | } |
||
231 | |||
232 | // set IP of the client |
||
233 | if (isset($overrides['IP'])) { |
||
234 | $this->IP = $overrides['IP']; |
||
235 | } else { |
||
236 | $this->IP = $this->getGlobalServerValue('REMOTE_ADDR'); |
||
237 | } |
||
238 | |||
239 | // set requested URI |
||
240 | if (isset($overrides['URI'])) { |
||
241 | $this->URI = $overrides['URI']; |
||
242 | } else { |
||
243 | $this->URI = $this->getGlobalServerValue('REQUEST_URI'); |
||
244 | } |
||
245 | |||
246 | // set uploaded files (if any) |
||
247 | if (isset($overrides['files'])) { |
||
248 | $this->files = $overrides['files']; |
||
249 | } elseif (isset($_FILES)) { |
||
250 | $this->files = $_FILES; |
||
251 | } |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Tries to get the requested param from the $_SERVER super global, otherwise returns an |
||
256 | * empty string. |
||
257 | * |
||
258 | * @param string $param |
||
259 | * @return string |
||
260 | * |
||
261 | * @since 3.0 |
||
262 | */ |
||
263 | private function getGlobalServerValue($param) |
||
264 | { |
||
265 | $server = $_SERVER; |
||
266 | |||
267 | if (isset($server[$param])) { |
||
268 | return $server[$param]; |
||
269 | } else { |
||
270 | return ''; |
||
271 | } |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Get the HTTP method of this request. |
||
276 | * |
||
277 | * @return string |
||
278 | * |
||
279 | * @since 2.0 |
||
280 | */ |
||
281 | public function getMethod() |
||
285 | |||
286 | /** |
||
287 | * Set the HTTP method of this request. |
||
288 | * |
||
289 | * @param string $method |
||
290 | * |
||
291 | * @throws \Alpha\Exception\IllegalArguementException |
||
292 | * |
||
293 | * @since 2.0 |
||
294 | */ |
||
295 | public function setMethod($method) |
||
303 | |||
304 | /** |
||
305 | * Return all headers on this request. |
||
306 | * |
||
307 | * @return array |
||
308 | * |
||
309 | * @since 2.0 |
||
310 | */ |
||
311 | public function getHeaders() |
||
315 | |||
316 | /** |
||
317 | * Get the header matching the key provided. |
||
318 | * |
||
319 | * @param string $key The key to search for |
||
320 | * @param mixed $default If key is not found, return this instead |
||
321 | * |
||
322 | * @return string |
||
323 | * |
||
324 | * @since 2.0 |
||
325 | */ |
||
326 | public function getHeader($key, $default = null) |
||
334 | |||
335 | /** |
||
336 | * Tries to get the current HTTP request headers from super globals. |
||
337 | * |
||
338 | * @return array |
||
339 | * |
||
340 | * @since 2.0 |
||
341 | */ |
||
342 | private function getGlobalHeaders() |
||
363 | |||
364 | /** |
||
365 | * Return all cookies on this request. |
||
366 | * |
||
367 | * @return array |
||
368 | * |
||
369 | * @since 2.0 |
||
370 | */ |
||
371 | public function getCookies() |
||
375 | |||
376 | /** |
||
377 | * Get the cookie matching the key provided. |
||
378 | * |
||
379 | * @param string $key The key to search for |
||
380 | * @param mixed $default If key is not found, return this instead |
||
381 | * |
||
382 | * @return mixed |
||
383 | * |
||
384 | * @since 2.0 |
||
385 | */ |
||
386 | public function getCookie($key, $default = null) |
||
394 | |||
395 | /** |
||
396 | * Return all params on this request. |
||
397 | * |
||
398 | * @return array |
||
399 | * |
||
400 | * @since 2.0 |
||
401 | */ |
||
402 | public function getParams() |
||
406 | |||
407 | /** |
||
408 | * Get the param matching the key provided. |
||
409 | * |
||
410 | * @param string $key The key to search for |
||
411 | * @param mixed $default If key is not found, return this instead |
||
412 | * |
||
413 | * @return string |
||
414 | * |
||
415 | * @since 2.0 |
||
416 | */ |
||
417 | public function getParam($key, $default = null) |
||
425 | |||
426 | /** |
||
427 | * Append the hash array provided to the params for this request. |
||
428 | * |
||
429 | * @param array A hash array of values to add to the request params |
||
430 | * |
||
431 | * @since 2.0 |
||
432 | */ |
||
433 | public function addParams($params) |
||
439 | |||
440 | /** |
||
441 | * Set the params array. |
||
442 | * |
||
443 | * @param array A hash array of values to set as the request params |
||
444 | * |
||
445 | * @since 2.0 |
||
446 | */ |
||
447 | public function setParams($params) |
||
453 | |||
454 | /** |
||
455 | * Return all files on this request. |
||
456 | * |
||
457 | * @return array |
||
458 | * |
||
459 | * @since 2.0 |
||
460 | */ |
||
461 | public function getFiles() |
||
465 | |||
466 | /** |
||
467 | * Get the file matching the key provided. |
||
468 | * |
||
469 | * @param string $key The key to search for |
||
470 | * @param mixed $default If key is not found, return this instead |
||
471 | * |
||
472 | * @return mixed |
||
473 | * |
||
474 | * @since 2.0 |
||
475 | */ |
||
476 | public function getFile($key, $default = null) |
||
484 | |||
485 | /** |
||
486 | * Get the request body if one was provided. |
||
487 | * |
||
488 | * @return string |
||
489 | * |
||
490 | * @since 2.0 |
||
491 | */ |
||
492 | public function getBody() |
||
496 | |||
497 | /** |
||
498 | * Attempts to get the raw body of the current request from super globals. |
||
499 | * |
||
500 | * @return string |
||
501 | * |
||
502 | * @since 2.0 |
||
503 | */ |
||
504 | private function getGlobalBody() |
||
512 | |||
513 | /** |
||
514 | * Get the Accept header of the request. |
||
515 | * |
||
516 | * @return string |
||
517 | * |
||
518 | * @since 2.0 |
||
519 | */ |
||
520 | public function getAccept() |
||
524 | |||
525 | /** |
||
526 | * Get the Content-Type header of the request. |
||
527 | * |
||
528 | * @return string |
||
529 | * |
||
530 | * @since 2.0 |
||
531 | */ |
||
532 | public function getContentType() |
||
536 | |||
537 | /** |
||
538 | * Get the Content-Length header of the request. |
||
539 | * |
||
540 | * @return string |
||
541 | * |
||
542 | * @since 2.0 |
||
543 | */ |
||
544 | public function getContentLength() |
||
548 | |||
549 | /** |
||
550 | * Get the host name of the client that sent the request. |
||
551 | * |
||
552 | * @return string |
||
553 | * |
||
554 | * @since 2.0 |
||
555 | */ |
||
556 | public function getHost() |
||
560 | |||
561 | /** |
||
562 | * Get the URI that was requested. |
||
563 | * |
||
564 | * @return string |
||
565 | * |
||
566 | * @since 2.0 |
||
567 | */ |
||
568 | public function getURI() |
||
572 | |||
573 | /** |
||
574 | * Get the URL that was requested. |
||
575 | * |
||
576 | * @return string |
||
577 | * |
||
578 | * @since 2.0 |
||
579 | */ |
||
580 | public function getURL() |
||
586 | |||
587 | /** |
||
588 | * Get the IP address of the client that sent the request. |
||
589 | * |
||
590 | * @return string |
||
591 | * |
||
592 | * @since 2.0 |
||
593 | */ |
||
594 | public function getIP() |
||
598 | |||
599 | /** |
||
600 | * Get the Referrer header of the request. |
||
601 | * |
||
602 | * @return string |
||
603 | * |
||
604 | * @since 2.0 |
||
605 | */ |
||
606 | public function getReferrer() |
||
610 | |||
611 | /** |
||
612 | * Get the User-Agent header of the request. |
||
613 | * |
||
614 | * @return string |
||
615 | * |
||
616 | * @since 2.0 |
||
617 | */ |
||
618 | public function getUserAgent() |
||
622 | |||
623 | /** |
||
624 | * Get the query string provided on the request. |
||
625 | * |
||
626 | * @return string |
||
627 | * |
||
628 | * @since 2.0 |
||
629 | */ |
||
630 | public function getQueryString() |
||
634 | |||
635 | /** |
||
636 | * Parses the route provided to extract matching params of the route from this request's URI. |
||
637 | * |
||
638 | * @param string $route The route with parameter names, e.g. /user/{username} |
||
639 | * @param array $defaultParams Optional hash array of default request param values to use if they are missing from URI |
||
640 | * |
||
641 | * @since 2.0 |
||
642 | */ |
||
643 | public function parseParamsFromRoute($route, $defaultParams = array()) |
||
673 | |||
674 | /** |
||
675 | * Checks to see if the request contains a secure/encrypted token. |
||
676 | * |
||
677 | * @return bool |
||
678 | * |
||
679 | * @since 2.0 |
||
680 | */ |
||
681 | public function isSecureURI() |
||
689 | } |
||
690 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: