Complex classes like EE_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 EE_Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class EE_Request implements InterminableInterface |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * $_GET parameters |
||
| 21 | * |
||
| 22 | * @var array $_get |
||
| 23 | */ |
||
| 24 | private $_get; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * $_POST parameters |
||
| 28 | * |
||
| 29 | * @var array $_post |
||
| 30 | */ |
||
| 31 | private $_post; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * $_COOKIE parameters |
||
| 35 | * |
||
| 36 | * @var array $_cookie |
||
| 37 | */ |
||
| 38 | private $_cookie; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * $_REQUEST parameters |
||
| 42 | * |
||
| 43 | * @var array $_params |
||
| 44 | */ |
||
| 45 | private $_params; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * whether current request is for the admin but NOT via AJAX |
||
| 49 | * |
||
| 50 | * @var boolean $admin |
||
| 51 | */ |
||
| 52 | public $admin = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * whether current request is via AJAX |
||
| 56 | * |
||
| 57 | * @var boolean $ajax |
||
| 58 | */ |
||
| 59 | public $ajax = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * whether current request is via AJAX from the frontend of the site |
||
| 63 | * |
||
| 64 | * @var boolean $front_ajax |
||
| 65 | */ |
||
| 66 | public $front_ajax = false; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * IP address for request |
||
| 70 | * |
||
| 71 | * @var string $_ip_address |
||
| 72 | */ |
||
| 73 | private $_ip_address; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string $user_agent |
||
| 77 | */ |
||
| 78 | private $user_agent; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * true if current user appears to be some kind of bot |
||
| 82 | * |
||
| 83 | * @var bool $is_bot |
||
| 84 | */ |
||
| 85 | private $is_bot; |
||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * class constructor |
||
| 91 | * |
||
| 92 | * @access public |
||
| 93 | * @param array $get |
||
| 94 | * @param array $post |
||
| 95 | * @param array $cookie |
||
| 96 | */ |
||
| 97 | public function __construct(array $get, array $post, array $cookie) |
||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | public function get_params() |
||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | public function post_params() |
||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function cookie_params() |
||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * returns contents of $_REQUEST |
||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public function params() |
||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * @param $key |
||
| 160 | * @param $value |
||
| 161 | * @param bool $override_ee |
||
| 162 | * @return void |
||
| 163 | */ |
||
| 164 | public function set($key, $value, $override_ee = false) |
||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * returns the value for a request param if the given key exists |
||
| 180 | * |
||
| 181 | * @param $key |
||
| 182 | * @param null $default |
||
| 183 | * @return mixed |
||
| 184 | */ |
||
| 185 | public function get($key, $default = null) |
||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * check if param exists |
||
| 194 | * |
||
| 195 | * @param $key |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | public function is_set($key) |
||
| 202 | |||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * the supplied key can be a simple string to represent a "top-level" request parameter |
||
| 207 | * or represent a key for a request parameter that is nested deeper within the request parameter array, |
||
| 208 | * by using square brackets to surround keys for deeper array elements. |
||
| 209 | * For example : |
||
| 210 | * if the supplied $key was: "first[second][third]" |
||
| 211 | * then this will attempt to drill down into the request parameter array to find a value. |
||
| 212 | * Given the following request parameters: |
||
| 213 | * array( |
||
| 214 | * 'first' => array( |
||
| 215 | * 'second' => array( |
||
| 216 | * 'third' => 'has a value' |
||
| 217 | * ) |
||
| 218 | * ) |
||
| 219 | * ) |
||
| 220 | * would return true |
||
| 221 | * |
||
| 222 | * @param string $is_set_or_get |
||
| 223 | * @param $key |
||
| 224 | * @param null $default |
||
| 225 | * @param array $request_params |
||
| 226 | * @return bool|mixed|null |
||
| 227 | */ |
||
| 228 | private function request_parameter_drill_down( |
||
| 266 | |||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * remove param |
||
| 271 | * |
||
| 272 | * @param $key |
||
| 273 | * @param bool $unset_from_global_too |
||
| 274 | */ |
||
| 275 | public function un_set($key, $unset_from_global_too = false) |
||
| 282 | |||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function ip_address() |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | public function isAdmin() |
||
| 301 | |||
| 302 | |||
| 303 | /** |
||
| 304 | * @return mixed |
||
| 305 | */ |
||
| 306 | public function isAjax() |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * @return mixed |
||
| 314 | */ |
||
| 315 | public function isFrontAjax() |
||
| 319 | |||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * _visitor_ip |
||
| 324 | * attempt to get IP address of current visitor from server |
||
| 325 | * plz see: http://stackoverflow.com/a/2031935/1475279 |
||
| 326 | * |
||
| 327 | * @access public |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | private function _visitor_ip() |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public function userAgent() |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * @param string $user_agent |
||
| 366 | */ |
||
| 367 | public function setUserAgent($user_agent = '') |
||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | public function isBot() |
||
| 383 | |||
| 384 | |||
| 385 | /** |
||
| 386 | * @param bool $is_bot |
||
| 387 | */ |
||
| 388 | public function setIsBot($is_bot) |
||
| 392 | |||
| 393 | |||
| 394 | |||
| 395 | } |
||
| 396 | // End of file EE_Request.core.php |
||
| 398 |