Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 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 | |||
| 77 | /** |
||
| 78 | * class constructor |
||
| 79 | * |
||
| 80 | * @access public |
||
| 81 | * @param array $get |
||
| 82 | * @param array $post |
||
| 83 | * @param array $cookie |
||
| 84 | */ |
||
| 85 | public function __construct(array $get, array $post, array $cookie) |
||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | public function get_params() |
||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | public function post_params() |
||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function cookie_params() |
||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * returns contents of $_REQUEST |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function params() |
||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * @param $key |
||
| 148 | * @param $value |
||
| 149 | * @param bool $override_ee |
||
| 150 | * @return void |
||
| 151 | */ |
||
| 152 | public function set($key, $value, $override_ee = false) |
||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * returns the value for a request param if the given key exists |
||
| 168 | * |
||
| 169 | * @param $key |
||
| 170 | * @param null $default |
||
| 171 | * @return mixed |
||
| 172 | */ |
||
| 173 | public function get($key, $default = null) |
||
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * check if param exists |
||
| 182 | * |
||
| 183 | * @param $key |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function is_set($key) |
||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * the supplied key can be a simple string to represent a "top-level" request parameter |
||
| 195 | * or represent a key for a request parameter that is nested deeper within the request parameter array, |
||
| 196 | * by using square brackets to surround keys for deeper array elements. |
||
| 197 | * For example : |
||
| 198 | * if the supplied $key was: "first[second][third]" |
||
| 199 | * then this will attempt to drill down into the request parameter array to find a value. |
||
| 200 | * Given the following request parameters: |
||
| 201 | * array( |
||
| 202 | * 'first' => array( |
||
| 203 | * 'second' => array( |
||
| 204 | * 'third' => 'has a value' |
||
| 205 | * ) |
||
| 206 | * ) |
||
| 207 | * ) |
||
| 208 | * would return true |
||
| 209 | * |
||
| 210 | * @param string $is_set_or_get |
||
| 211 | * @param $key |
||
| 212 | * @param null $default |
||
| 213 | * @param array $request_params |
||
| 214 | * @return bool|mixed|null |
||
| 215 | */ |
||
| 216 | private function request_parameter_drill_down( |
||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * remove param |
||
| 259 | * |
||
| 260 | * @param $key |
||
| 261 | * @param bool $unset_from_global_too |
||
| 262 | */ |
||
| 263 | public function un_set($key, $unset_from_global_too = false) |
||
| 270 | |||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function ip_address() |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | public function isAdmin() |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * @return mixed |
||
| 293 | */ |
||
| 294 | public function isAjax() |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * @return mixed |
||
| 302 | */ |
||
| 303 | public function isFrontAjax() |
||
| 307 | |||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * _visitor_ip |
||
| 312 | * attempt to get IP address of current visitor from server |
||
| 313 | * plz see: http://stackoverflow.com/a/2031935/1475279 |
||
| 314 | * |
||
| 315 | * @access public |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | View Code Duplication | private function _visitor_ip() |
|
| 341 | |||
| 342 | |||
| 343 | |||
| 344 | } |
||
| 345 | // End of file EE_Request.core.php |
||
| 347 |