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 |
||
17 | class Request implements InterminableInterface, RequestInterface, ReservedInstanceInterface |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * $_GET parameters |
||
22 | * |
||
23 | * @var array $get |
||
24 | */ |
||
25 | private $get; |
||
26 | |||
27 | /** |
||
28 | * $_POST parameters |
||
29 | * |
||
30 | * @var array $post |
||
31 | */ |
||
32 | private $post; |
||
33 | |||
34 | /** |
||
35 | * $_COOKIE parameters |
||
36 | * |
||
37 | * @var array $cookie |
||
38 | */ |
||
39 | private $cookie; |
||
40 | |||
41 | /** |
||
42 | * $_SERVER parameters |
||
43 | * |
||
44 | * @var array $server |
||
45 | */ |
||
46 | private $server; |
||
47 | |||
48 | /** |
||
49 | * $_FILES parameters |
||
50 | * |
||
51 | * @var array $files |
||
52 | */ |
||
53 | private $files; |
||
54 | |||
55 | /** |
||
56 | * $_REQUEST parameters |
||
57 | * |
||
58 | * @var array $request |
||
59 | */ |
||
60 | private $request; |
||
61 | |||
62 | /** |
||
63 | * @var RequestTypeContextCheckerInterface |
||
64 | */ |
||
65 | private $request_type; |
||
66 | |||
67 | /** |
||
68 | * IP address for request |
||
69 | * |
||
70 | * @var string $ip_address |
||
71 | */ |
||
72 | private $ip_address; |
||
73 | |||
74 | /** |
||
75 | * @var string $user_agent |
||
76 | */ |
||
77 | private $user_agent; |
||
78 | |||
79 | /** |
||
80 | * true if current user appears to be some kind of bot |
||
81 | * |
||
82 | * @var bool $is_bot |
||
83 | */ |
||
84 | private $is_bot; |
||
85 | |||
86 | |||
87 | /** |
||
88 | * @param array $get |
||
89 | * @param array $post |
||
90 | * @param array $cookie |
||
91 | * @param array $server |
||
92 | * @param array $files |
||
93 | */ |
||
94 | public function __construct(array $get, array $post, array $cookie, array $server, array $files = array()) |
||
105 | |||
106 | |||
107 | /** |
||
108 | * @param RequestTypeContextCheckerInterface $type |
||
109 | */ |
||
110 | public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type) |
||
114 | |||
115 | |||
116 | /** |
||
117 | * @return array |
||
118 | */ |
||
119 | public function getParams() |
||
123 | |||
124 | |||
125 | /** |
||
126 | * @return array |
||
127 | */ |
||
128 | public function postParams() |
||
132 | |||
133 | |||
134 | /** |
||
135 | * @return array |
||
136 | */ |
||
137 | public function cookieParams() |
||
141 | |||
142 | |||
143 | /** |
||
144 | * @return array |
||
145 | */ |
||
146 | public function serverParams() |
||
150 | |||
151 | |||
152 | /** |
||
153 | * @return array |
||
154 | */ |
||
155 | public function filesParams() |
||
159 | |||
160 | |||
161 | /** |
||
162 | * returns contents of $_REQUEST |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | public function requestParams() |
||
170 | |||
171 | |||
172 | /** |
||
173 | * @param $key |
||
174 | * @param $value |
||
175 | * @param bool $override_ee |
||
176 | * @return void |
||
177 | */ |
||
178 | public function setRequestParam($key, $value, $override_ee = false) |
||
188 | |||
189 | |||
190 | /** |
||
191 | * returns the value for a request param if the given key exists |
||
192 | * |
||
193 | * @param $key |
||
194 | * @param null $default |
||
195 | * @return mixed |
||
196 | */ |
||
197 | public function getRequestParam($key, $default = null) |
||
201 | |||
202 | |||
203 | /** |
||
204 | * check if param exists |
||
205 | * |
||
206 | * @param $key |
||
207 | * @return bool |
||
208 | */ |
||
209 | public function requestParamIsSet($key) |
||
213 | |||
214 | |||
215 | /** |
||
216 | * check if a request parameter exists whose key that matches the supplied wildcard pattern |
||
217 | * and return the value for the first match found |
||
218 | * wildcards can be either of the following: |
||
219 | * ? to represent a single character of any type |
||
220 | * * to represent one or more characters of any type |
||
221 | * |
||
222 | * @param string $pattern |
||
223 | * @param null|mixed $default |
||
224 | * @return mixed |
||
225 | */ |
||
226 | public function getMatch($pattern, $default = null) |
||
230 | |||
231 | |||
232 | /** |
||
233 | * check if a request parameter exists whose key matches the supplied wildcard pattern |
||
234 | * wildcards can be either of the following: |
||
235 | * ? to represent a single character of any type |
||
236 | * * to represent one or more characters of any type |
||
237 | * returns true if a match is found or false if not |
||
238 | * |
||
239 | * @param string $pattern |
||
240 | * @return bool |
||
241 | */ |
||
242 | public function matches($pattern) |
||
246 | |||
247 | |||
248 | /** |
||
249 | * @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard |
||
250 | * @param string $pattern A string including wildcards to be converted to a regex pattern |
||
251 | * and used to search through the current request's parameter keys |
||
252 | * @param array $request_params The array of request parameters to search through |
||
253 | * @param mixed $default [optional] The value to be returned if no match is found. |
||
254 | * Default is null |
||
255 | * @param string $return [optional] Controls what kind of value is returned. |
||
256 | * Options are: |
||
257 | * 'bool' will return true or false if match is found or not |
||
258 | * 'key' will return the first key found that matches the supplied pattern |
||
259 | * 'value' will return the value for the first request parameter |
||
260 | * whose key matches the supplied pattern |
||
261 | * Default is 'value' |
||
262 | * @return boolean|string |
||
263 | */ |
||
264 | private function match($pattern, array $request_params, $default = null, $return = 'value') |
||
288 | |||
289 | |||
290 | /** |
||
291 | * the supplied key can be a simple string to represent a "top-level" request parameter |
||
292 | * or represent a key for a request parameter that is nested deeper within the request parameter array, |
||
293 | * by using square brackets to surround keys for deeper array elements. |
||
294 | * For example : |
||
295 | * if the supplied $key was: "first[second][third]" |
||
296 | * then this will attempt to drill down into the request parameter array to find a value. |
||
297 | * Given the following request parameters: |
||
298 | * array( |
||
299 | * 'first' => array( |
||
300 | * 'second' => array( |
||
301 | * 'third' => 'has a value' |
||
302 | * ) |
||
303 | * ) |
||
304 | * ) |
||
305 | * would return true if default parameters were set |
||
306 | * |
||
307 | * @param string $callback |
||
308 | * @param $key |
||
309 | * @param null $default |
||
310 | * @param array $request_params |
||
311 | * @return bool|mixed|null |
||
312 | */ |
||
313 | private function requestParameterDrillDown( |
||
361 | |||
362 | |||
363 | /** |
||
364 | * remove param |
||
365 | * |
||
366 | * @param $key |
||
367 | * @param bool $unset_from_global_too |
||
368 | */ |
||
369 | public function unSetRequestParam($key, $unset_from_global_too = false) |
||
376 | |||
377 | |||
378 | /** |
||
379 | * @return string |
||
380 | */ |
||
381 | public function ipAddress() |
||
385 | |||
386 | |||
387 | /** |
||
388 | * attempt to get IP address of current visitor from server |
||
389 | * plz see: http://stackoverflow.com/a/2031935/1475279 |
||
390 | * |
||
391 | * @access public |
||
392 | * @return string |
||
393 | */ |
||
394 | private function visitorIp() |
||
417 | |||
418 | |||
419 | /** |
||
420 | * @return string |
||
421 | */ |
||
422 | public function requestUri() |
||
436 | |||
437 | |||
438 | /** |
||
439 | * @return string |
||
440 | */ |
||
441 | public function userAgent() |
||
445 | |||
446 | |||
447 | /** |
||
448 | * @param string $user_agent |
||
449 | */ |
||
450 | public function setUserAgent($user_agent = '') |
||
457 | |||
458 | |||
459 | /** |
||
460 | * @return bool |
||
461 | */ |
||
462 | public function isBot() |
||
466 | |||
467 | |||
468 | /** |
||
469 | * @param bool $is_bot |
||
470 | */ |
||
471 | public function setIsBot($is_bot) |
||
475 | |||
476 | |||
477 | /** |
||
478 | * @return bool |
||
479 | */ |
||
480 | public function isActivation() |
||
484 | |||
485 | |||
486 | /** |
||
487 | * @param $is_activation |
||
488 | * @return bool |
||
489 | */ |
||
490 | public function setIsActivation($is_activation) |
||
494 | |||
495 | |||
496 | /** |
||
497 | * @return bool |
||
498 | */ |
||
499 | public function isAdmin() |
||
503 | |||
504 | |||
505 | /** |
||
506 | * @return bool |
||
507 | */ |
||
508 | public function isAdminAjax() |
||
512 | |||
513 | |||
514 | /** |
||
515 | * @return bool |
||
516 | */ |
||
517 | public function isAjax() |
||
521 | |||
522 | |||
523 | /** |
||
524 | * @return bool |
||
525 | */ |
||
526 | public function isEeAjax() |
||
530 | |||
531 | |||
532 | /** |
||
533 | * @return bool |
||
534 | */ |
||
535 | public function isOtherAjax() |
||
539 | |||
540 | |||
541 | /** |
||
542 | * @return bool |
||
543 | */ |
||
544 | public function isApi() |
||
548 | |||
549 | |||
550 | /** |
||
551 | * @return bool |
||
552 | */ |
||
553 | public function isCli() |
||
557 | |||
558 | |||
559 | /** |
||
560 | * @return bool |
||
561 | */ |
||
562 | public function isCron() |
||
566 | |||
567 | |||
568 | /** |
||
569 | * @return bool |
||
570 | */ |
||
571 | public function isFeed() |
||
575 | |||
576 | |||
577 | /** |
||
578 | * @return bool |
||
579 | */ |
||
580 | public function isFrontend() |
||
584 | |||
585 | |||
586 | /** |
||
587 | * @return bool |
||
588 | */ |
||
589 | public function isFrontAjax() |
||
593 | |||
594 | |||
595 | /** |
||
596 | * @return bool |
||
597 | */ |
||
598 | public function isIframe() |
||
602 | |||
603 | |||
604 | /** |
||
605 | * @return bool |
||
606 | */ |
||
607 | public function isWordPressApi() |
||
611 | |||
612 | |||
613 | |||
614 | /** |
||
615 | * @return bool |
||
616 | */ |
||
617 | public function isWordPressHeartbeat() |
||
621 | |||
622 | |||
623 | |||
624 | /** |
||
625 | * @return bool |
||
626 | */ |
||
627 | public function isWordPressScrape() |
||
631 | |||
632 | |||
633 | /** |
||
634 | * @return string |
||
635 | */ |
||
636 | public function slug() |
||
640 | } |
||
641 |