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 | * $_REQUEST parameters |
||
| 50 | * |
||
| 51 | * @var array $request |
||
| 52 | */ |
||
| 53 | private $request; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var RequestTypeContextCheckerInterface |
||
| 57 | */ |
||
| 58 | private $request_type; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * IP address for request |
||
| 62 | * |
||
| 63 | * @var string $ip_address |
||
| 64 | */ |
||
| 65 | private $ip_address; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string $user_agent |
||
| 69 | */ |
||
| 70 | private $user_agent; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * true if current user appears to be some kind of bot |
||
| 74 | * |
||
| 75 | * @var bool $is_bot |
||
| 76 | */ |
||
| 77 | private $is_bot; |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * @param array $get |
||
| 82 | * @param array $post |
||
| 83 | * @param array $cookie |
||
| 84 | * @param array $server |
||
| 85 | */ |
||
| 86 | public function __construct(array $get, array $post, array $cookie, array $server) |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * @param RequestTypeContextCheckerInterface $type |
||
| 100 | */ |
||
| 101 | public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type) |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | public function getParams() |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | public function postParams() |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | public function cookieParams() |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function serverParams() |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * returns contents of $_REQUEST |
||
| 145 | * |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | public function requestParams() |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * @param $key |
||
| 156 | * @param $value |
||
| 157 | * @param bool $override_ee |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | public function setRequestParam($key, $value, $override_ee = false) |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * returns the value for a request param if the given key exists |
||
| 174 | * |
||
| 175 | * @param $key |
||
| 176 | * @param null $default |
||
| 177 | * @return mixed |
||
| 178 | */ |
||
| 179 | public function getRequestParam($key, $default = null) |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * check if param exists |
||
| 187 | * |
||
| 188 | * @param $key |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | public function requestParamIsSet($key) |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * check if a request parameter exists whose key that matches the supplied wildcard pattern |
||
| 199 | * and return the value for the first match found |
||
| 200 | * wildcards can be either of the following: |
||
| 201 | * ? to represent a single character of any type |
||
| 202 | * * to represent one or more characters of any type |
||
| 203 | * |
||
| 204 | * @param string $pattern |
||
| 205 | * @param null|mixed $default |
||
| 206 | * @return false|int |
||
| 207 | */ |
||
| 208 | public function getMatch($pattern, $default = null) |
||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * check if a request parameter exists whose key matches the supplied wildcard pattern |
||
| 216 | * wildcards can be either of the following: |
||
| 217 | * ? to represent a single character of any type |
||
| 218 | * * to represent one or more characters of any type |
||
| 219 | * returns true if a match is found or false if not |
||
| 220 | * |
||
| 221 | * @param string $pattern |
||
| 222 | * @return false|int |
||
| 223 | */ |
||
| 224 | public function matches($pattern) |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard |
||
| 232 | * @param string $pattern A string including wildcards to be converted to a regex pattern |
||
| 233 | * and used to search through the current request's parameter keys |
||
| 234 | * @param array $request_params The array of request parameters to search through |
||
| 235 | * @param mixed $default [optional] The value to be returned if no match is found. |
||
| 236 | * Default is null |
||
| 237 | * @param string $return [optional] Controls what kind of value is returned. |
||
| 238 | * Options are: |
||
| 239 | * 'bool' will return true or false if match is found or not |
||
| 240 | * 'key' will return the first key found that matches the supplied pattern |
||
| 241 | * 'value' will return the value for the first request parameter |
||
| 242 | * whose key matches the supplied pattern |
||
| 243 | * Default is 'value' |
||
| 244 | * @return boolean|string |
||
| 245 | */ |
||
| 246 | private function match($pattern, array $request_params, $default = null, $return = 'value') |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * the supplied key can be a simple string to represent a "top-level" request parameter |
||
| 274 | * or represent a key for a request parameter that is nested deeper within the request parameter array, |
||
| 275 | * by using square brackets to surround keys for deeper array elements. |
||
| 276 | * For example : |
||
| 277 | * if the supplied $key was: "first[second][third]" |
||
| 278 | * then this will attempt to drill down into the request parameter array to find a value. |
||
| 279 | * Given the following request parameters: |
||
| 280 | * array( |
||
| 281 | * 'first' => array( |
||
| 282 | * 'second' => array( |
||
| 283 | * 'third' => 'has a value' |
||
| 284 | * ) |
||
| 285 | * ) |
||
| 286 | * ) |
||
| 287 | * would return true if default parameters were set |
||
| 288 | * |
||
| 289 | * @param string $callback |
||
| 290 | * @param $key |
||
| 291 | * @param null $default |
||
| 292 | * @param array $request_params |
||
| 293 | * @return bool|mixed|null |
||
| 294 | */ |
||
| 295 | private function requestParameterDrillDown( |
||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * remove param |
||
| 347 | * |
||
| 348 | * @param $key |
||
| 349 | * @param bool $unset_from_global_too |
||
| 350 | */ |
||
| 351 | public function unSetRequestParam($key, $unset_from_global_too = false) |
||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public function ipAddress() |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * attempt to get IP address of current visitor from server |
||
| 371 | * plz see: http://stackoverflow.com/a/2031935/1475279 |
||
| 372 | * |
||
| 373 | * @access public |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | private function visitorIp() |
||
| 399 | |||
| 400 | |||
| 401 | /** |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | public function requestUri() |
||
| 418 | |||
| 419 | |||
| 420 | /** |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | public function userAgent() |
||
| 427 | |||
| 428 | |||
| 429 | /** |
||
| 430 | * @param string $user_agent |
||
| 431 | */ |
||
| 432 | public function setUserAgent($user_agent = '') |
||
| 439 | |||
| 440 | |||
| 441 | /** |
||
| 442 | * @return bool |
||
| 443 | */ |
||
| 444 | public function isBot() |
||
| 448 | |||
| 449 | |||
| 450 | /** |
||
| 451 | * @param bool $is_bot |
||
| 452 | */ |
||
| 453 | public function setIsBot($is_bot) |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * @return bool |
||
| 461 | */ |
||
| 462 | public function isActivation() |
||
| 466 | |||
| 467 | |||
| 468 | /** |
||
| 469 | * @param $is_activation |
||
| 470 | * @return bool |
||
| 471 | */ |
||
| 472 | public function setIsActivation($is_activation) |
||
| 476 | |||
| 477 | |||
| 478 | /** |
||
| 479 | * @return bool |
||
| 480 | */ |
||
| 481 | public function isAdmin() |
||
| 485 | |||
| 486 | |||
| 487 | /** |
||
| 488 | * @return bool |
||
| 489 | */ |
||
| 490 | public function isAdminAjax() |
||
| 494 | |||
| 495 | |||
| 496 | /** |
||
| 497 | * @return bool |
||
| 498 | */ |
||
| 499 | public function isAjax() |
||
| 503 | |||
| 504 | |||
| 505 | /** |
||
| 506 | * @return bool |
||
| 507 | */ |
||
| 508 | public function isEeAjax() |
||
| 512 | |||
| 513 | |||
| 514 | /** |
||
| 515 | * @return bool |
||
| 516 | */ |
||
| 517 | public function isOtherAjax() |
||
| 521 | |||
| 522 | |||
| 523 | /** |
||
| 524 | * @return bool |
||
| 525 | */ |
||
| 526 | public function isApi() |
||
| 530 | |||
| 531 | |||
| 532 | /** |
||
| 533 | * @return bool |
||
| 534 | */ |
||
| 535 | public function isCli() |
||
| 539 | |||
| 540 | |||
| 541 | /** |
||
| 542 | * @return bool |
||
| 543 | */ |
||
| 544 | public function isCron() |
||
| 548 | |||
| 549 | |||
| 550 | /** |
||
| 551 | * @return bool |
||
| 552 | */ |
||
| 553 | public function isFeed() |
||
| 557 | |||
| 558 | |||
| 559 | /** |
||
| 560 | * @return bool |
||
| 561 | */ |
||
| 562 | public function isFrontend() |
||
| 566 | |||
| 567 | |||
| 568 | /** |
||
| 569 | * @return bool |
||
| 570 | */ |
||
| 571 | public function isFrontAjax() |
||
| 575 | |||
| 576 | |||
| 577 | /** |
||
| 578 | * @return bool |
||
| 579 | */ |
||
| 580 | public function isIframe() |
||
| 584 | |||
| 585 | |||
| 586 | /** |
||
| 587 | * @return bool |
||
| 588 | */ |
||
| 589 | public function isWordPressScrape() |
||
| 593 | |||
| 594 | |||
| 595 | /** |
||
| 596 | * @return string |
||
| 597 | */ |
||
| 598 | public function slug() |
||
| 602 | } |
||
| 603 |