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 | /** |
||
| 211 | * the supplied key can be a simple string to represent a "top-level" request parameter |
||
| 212 | * or represent a key for a request parameter that is nested deeper within the request parameter array, |
||
| 213 | * by using square brackets to surround keys for deeper array elements. |
||
| 214 | * For example : |
||
| 215 | * if the supplied $key was: "first[second][third]" |
||
| 216 | * then this will attempt to drill down into the request parameter array to find a value. |
||
| 217 | * Given the following request parameters: |
||
| 218 | * array( |
||
| 219 | * 'first' => array( |
||
| 220 | * 'second' => array( |
||
| 221 | * 'third' => 'has a value' |
||
| 222 | * ) |
||
| 223 | * ) |
||
| 224 | * ) |
||
| 225 | * would return true |
||
| 226 | * |
||
| 227 | * @param $key |
||
| 228 | * @param null $default |
||
| 229 | * @param string $is_set_or_get |
||
| 230 | * @param array $request_params |
||
| 231 | * @return bool|mixed|null |
||
| 232 | */ |
||
| 233 | private function requestParameterDrillDown( |
||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * remove param |
||
| 276 | * |
||
| 277 | * @param $key |
||
| 278 | * @param bool $unset_from_global_too |
||
| 279 | */ |
||
| 280 | public function unSetRequestParam($key, $unset_from_global_too = false) |
||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | public function ipAddress() |
||
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * attempt to get IP address of current visitor from server |
||
| 301 | * plz see: http://stackoverflow.com/a/2031935/1475279 |
||
| 302 | * |
||
| 303 | * @access public |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | private function visitorIp() |
||
| 329 | |||
| 330 | |||
| 331 | /** |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function requestUri() |
||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function userAgent() |
||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $user_agent |
||
| 361 | */ |
||
| 362 | public function setUserAgent($user_agent = '') |
||
| 369 | |||
| 370 | |||
| 371 | /** |
||
| 372 | * @return bool |
||
| 373 | */ |
||
| 374 | public function isBot() |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * @param bool $is_bot |
||
| 382 | */ |
||
| 383 | public function setIsBot($is_bot) |
||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * @return bool |
||
| 391 | */ |
||
| 392 | public function isActivation() |
||
| 396 | |||
| 397 | |||
| 398 | /** |
||
| 399 | * @param $is_activation |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | public function setIsActivation($is_activation) |
||
| 406 | |||
| 407 | |||
| 408 | /** |
||
| 409 | * @return bool |
||
| 410 | */ |
||
| 411 | public function isAdmin() |
||
| 415 | |||
| 416 | |||
| 417 | /** |
||
| 418 | * @return bool |
||
| 419 | */ |
||
| 420 | public function isAdminAjax() |
||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * @return bool |
||
| 428 | */ |
||
| 429 | public function isAjax() |
||
| 433 | |||
| 434 | |||
| 435 | /** |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | public function isEeAjax() |
||
| 442 | |||
| 443 | |||
| 444 | /** |
||
| 445 | * @return bool |
||
| 446 | */ |
||
| 447 | public function isOtherAjax() |
||
| 451 | |||
| 452 | |||
| 453 | /** |
||
| 454 | * @return bool |
||
| 455 | */ |
||
| 456 | public function isApi() |
||
| 460 | |||
| 461 | |||
| 462 | /** |
||
| 463 | * @return bool |
||
| 464 | */ |
||
| 465 | public function isCli() |
||
| 469 | |||
| 470 | |||
| 471 | /** |
||
| 472 | * @return bool |
||
| 473 | */ |
||
| 474 | public function isCron() |
||
| 478 | |||
| 479 | |||
| 480 | /** |
||
| 481 | * @return bool |
||
| 482 | */ |
||
| 483 | public function isFeed() |
||
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * @return bool |
||
| 491 | */ |
||
| 492 | public function isFrontend() |
||
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * @return bool |
||
| 500 | */ |
||
| 501 | public function isFrontAjax() |
||
| 505 | |||
| 506 | |||
| 507 | |||
| 508 | /** |
||
| 509 | * @return bool |
||
| 510 | */ |
||
| 511 | public function isIframe() |
||
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | public function slug() |
||
| 524 | |||
| 525 | |||
| 526 | } |
||
| 527 | // Location: Request.php |
||
| 528 |