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 |
||
21 | class Request implements InterminableInterface, RequestInterface, ReservedInstanceInterface |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * $_GET parameters |
||
26 | * |
||
27 | * @var array $get |
||
28 | */ |
||
29 | private $get; |
||
30 | |||
31 | /** |
||
32 | * $_POST parameters |
||
33 | * |
||
34 | * @var array $post |
||
35 | */ |
||
36 | private $post; |
||
37 | |||
38 | /** |
||
39 | * $_COOKIE parameters |
||
40 | * |
||
41 | * @var array $cookie |
||
42 | */ |
||
43 | private $cookie; |
||
44 | |||
45 | /** |
||
46 | * $_SERVER parameters |
||
47 | * |
||
48 | * @var array $server |
||
49 | */ |
||
50 | private $server; |
||
51 | |||
52 | /** |
||
53 | * $_REQUEST parameters |
||
54 | * |
||
55 | * @var array $request |
||
56 | */ |
||
57 | private $request; |
||
58 | |||
59 | /** |
||
60 | * @var RequestTypeContextCheckerInterface |
||
61 | */ |
||
62 | private $request_type; |
||
63 | |||
64 | /** |
||
65 | * IP address for request |
||
66 | * |
||
67 | * @var string $ip_address |
||
68 | */ |
||
69 | private $ip_address; |
||
70 | |||
71 | /** |
||
72 | * @var string $user_agent |
||
73 | */ |
||
74 | private $user_agent; |
||
75 | |||
76 | /** |
||
77 | * true if current user appears to be some kind of bot |
||
78 | * |
||
79 | * @var bool $is_bot |
||
80 | */ |
||
81 | private $is_bot; |
||
82 | |||
83 | |||
84 | |||
85 | /** |
||
86 | * @param array $get |
||
87 | * @param array $post |
||
88 | * @param array $cookie |
||
89 | * @param array $server |
||
90 | */ |
||
91 | public function __construct(array $get, array $post, array $cookie, array $server) |
||
101 | |||
102 | |||
103 | /** |
||
104 | * @param RequestTypeContextCheckerInterface $type |
||
105 | */ |
||
106 | public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type) |
||
110 | |||
111 | |||
112 | |||
113 | /** |
||
114 | * @return array |
||
115 | */ |
||
116 | public function getParams() |
||
120 | |||
121 | |||
122 | |||
123 | /** |
||
124 | * @return array |
||
125 | */ |
||
126 | public function postParams() |
||
130 | |||
131 | |||
132 | |||
133 | /** |
||
134 | * @return array |
||
135 | */ |
||
136 | public function cookieParams() |
||
140 | |||
141 | |||
142 | /** |
||
143 | * @return array |
||
144 | */ |
||
145 | public function serverParams() |
||
149 | |||
150 | |||
151 | |||
152 | /** |
||
153 | * returns contents of $_REQUEST |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | public function requestParams() |
||
161 | |||
162 | |||
163 | |||
164 | /** |
||
165 | * @param $key |
||
166 | * @param $value |
||
167 | * @param bool $override_ee |
||
168 | * @return void |
||
169 | */ |
||
170 | public function setRequestParam($key, $value, $override_ee = false) |
||
181 | |||
182 | |||
183 | |||
184 | /** |
||
185 | * returns the value for a request param if the given key exists |
||
186 | * |
||
187 | * @param $key |
||
188 | * @param null $default |
||
189 | * @return mixed |
||
190 | */ |
||
191 | public function getRequestParam($key, $default = null) |
||
195 | |||
196 | |||
197 | |||
198 | /** |
||
199 | * check if param exists |
||
200 | * |
||
201 | * @param $key |
||
202 | * @return bool |
||
203 | */ |
||
204 | public function requestParamIsSet($key) |
||
208 | |||
209 | |||
210 | /** |
||
211 | * check if a request parameter exists whose key that matches the supplied wildcard pattern |
||
212 | * and return the value for the first match found |
||
213 | * wildcards can be either of the following: |
||
214 | * ? to represent a single character of any type |
||
215 | * * to represent one or more characters of any type |
||
216 | * |
||
217 | * @param string $pattern |
||
218 | * @param null|mixed $default |
||
219 | * @return false|int |
||
220 | */ |
||
221 | public function getMatch($pattern, $default = null) |
||
225 | |||
226 | |||
227 | /** |
||
228 | * check if a request parameter exists whose key matches the supplied wildcard pattern |
||
229 | * wildcards can be either of the following: |
||
230 | * ? to represent a single character of any type |
||
231 | * * to represent one or more characters of any type |
||
232 | * returns true if a match is found or false if not |
||
233 | * |
||
234 | * @param string $pattern |
||
235 | * @return false|int |
||
236 | */ |
||
237 | public function matches($pattern) |
||
241 | |||
242 | |||
243 | /** |
||
244 | * @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard |
||
245 | * @param string $pattern A string including wildcards to be converted to a regex pattern |
||
246 | * and used to search through the current request's parameter keys |
||
247 | * @param array $request_params The array of request parameters to search through |
||
248 | * @param mixed $default [optional] The value to be returned if no match is found. |
||
249 | * Default is null |
||
250 | * @param string $return [optional] Controls what kind of value is returned. |
||
251 | * Options are: |
||
252 | * 'bool' will return true or false if match is found or not |
||
253 | * 'key' will return the first key found that matches the supplied pattern |
||
254 | * 'value' will return the value for the first request parameter |
||
255 | * whose key matches the supplied pattern |
||
256 | * Default is 'value' |
||
257 | * @return boolean|string |
||
258 | */ |
||
259 | private function match($pattern, array $request_params, $default = null, $return = 'value') |
||
283 | |||
284 | |||
285 | /** |
||
286 | * the supplied key can be a simple string to represent a "top-level" request parameter |
||
287 | * or represent a key for a request parameter that is nested deeper within the request parameter array, |
||
288 | * by using square brackets to surround keys for deeper array elements. |
||
289 | * For example : |
||
290 | * if the supplied $key was: "first[second][third]" |
||
291 | * then this will attempt to drill down into the request parameter array to find a value. |
||
292 | * Given the following request parameters: |
||
293 | * array( |
||
294 | * 'first' => array( |
||
295 | * 'second' => array( |
||
296 | * 'third' => 'has a value' |
||
297 | * ) |
||
298 | * ) |
||
299 | * ) |
||
300 | * would return true if default parameters were set |
||
301 | * |
||
302 | * @param string $callback |
||
303 | * @param $key |
||
304 | * @param null $default |
||
305 | * @param array $request_params |
||
306 | * @return bool|mixed|null |
||
307 | */ |
||
308 | private function requestParameterDrillDown( |
||
356 | |||
357 | |||
358 | /** |
||
359 | * remove param |
||
360 | * |
||
361 | * @param $key |
||
362 | * @param bool $unset_from_global_too |
||
363 | */ |
||
364 | public function unSetRequestParam($key, $unset_from_global_too = false) |
||
371 | |||
372 | |||
373 | |||
374 | /** |
||
375 | * @return string |
||
376 | */ |
||
377 | public function ipAddress() |
||
381 | |||
382 | |||
383 | /** |
||
384 | * attempt to get IP address of current visitor from server |
||
385 | * plz see: http://stackoverflow.com/a/2031935/1475279 |
||
386 | * |
||
387 | * @access public |
||
388 | * @return string |
||
389 | */ |
||
390 | private function visitorIp() |
||
413 | |||
414 | |||
415 | /** |
||
416 | * @return string |
||
417 | */ |
||
418 | public function requestUri() |
||
432 | |||
433 | |||
434 | /** |
||
435 | * @return string |
||
436 | */ |
||
437 | public function userAgent() |
||
441 | |||
442 | |||
443 | /** |
||
444 | * @param string $user_agent |
||
445 | */ |
||
446 | public function setUserAgent($user_agent = '') |
||
453 | |||
454 | |||
455 | /** |
||
456 | * @return bool |
||
457 | */ |
||
458 | public function isBot() |
||
462 | |||
463 | |||
464 | /** |
||
465 | * @param bool $is_bot |
||
466 | */ |
||
467 | public function setIsBot($is_bot) |
||
471 | |||
472 | |||
473 | /** |
||
474 | * @return bool |
||
475 | */ |
||
476 | public function isActivation() |
||
480 | |||
481 | |||
482 | /** |
||
483 | * @param $is_activation |
||
484 | * @return bool |
||
485 | */ |
||
486 | public function setIsActivation($is_activation) |
||
490 | |||
491 | |||
492 | /** |
||
493 | * @return bool |
||
494 | */ |
||
495 | public function isAdmin() |
||
499 | |||
500 | |||
501 | /** |
||
502 | * @return bool |
||
503 | */ |
||
504 | public function isAdminAjax() |
||
508 | |||
509 | |||
510 | /** |
||
511 | * @return bool |
||
512 | */ |
||
513 | public function isAjax() |
||
517 | |||
518 | |||
519 | /** |
||
520 | * @return bool |
||
521 | */ |
||
522 | public function isEeAjax() |
||
526 | |||
527 | |||
528 | /** |
||
529 | * @return bool |
||
530 | */ |
||
531 | public function isOtherAjax() |
||
535 | |||
536 | |||
537 | /** |
||
538 | * @return bool |
||
539 | */ |
||
540 | public function isApi() |
||
544 | |||
545 | |||
546 | /** |
||
547 | * @return bool |
||
548 | */ |
||
549 | public function isCli() |
||
553 | |||
554 | |||
555 | /** |
||
556 | * @return bool |
||
557 | */ |
||
558 | public function isCron() |
||
562 | |||
563 | |||
564 | /** |
||
565 | * @return bool |
||
566 | */ |
||
567 | public function isFeed() |
||
571 | |||
572 | |||
573 | /** |
||
574 | * @return bool |
||
575 | */ |
||
576 | public function isFrontend() |
||
580 | |||
581 | |||
582 | /** |
||
583 | * @return bool |
||
584 | */ |
||
585 | public function isFrontAjax() |
||
589 | |||
590 | |||
591 | |||
592 | /** |
||
593 | * @return bool |
||
594 | */ |
||
595 | public function isIframe() |
||
599 | |||
600 | |||
601 | /** |
||
602 | * @return string |
||
603 | */ |
||
604 | public function slug() |
||
608 | |||
609 | |||
610 | } |
||
611 | // Location: Request.php |
||
612 |