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