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:
Complex classes like EE_Request_Handler 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_Handler, and based on these observations, apply Extract Interface, too.
| 1 | <?php use EventEspresso\core\interfaces\InterminableInterface; |
||
| 13 | final class EE_Request_Handler implements InterminableInterface |
||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var EE_Request $request |
||
| 18 | */ |
||
| 19 | private $request; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var array $_notice |
||
| 23 | */ |
||
| 24 | private $_notice = array(); |
||
| 25 | |||
| 26 | /** |
||
| 27 | * rendered output to be returned to WP |
||
| 28 | * |
||
| 29 | * @var string $_output |
||
| 30 | */ |
||
| 31 | private $_output = ''; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * whether current request is via AJAX |
||
| 35 | * |
||
| 36 | * @var boolean $ajax |
||
| 37 | */ |
||
| 38 | public $ajax = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * whether current request is via AJAX from the frontend of the site |
||
| 42 | * |
||
| 43 | * @var boolean $front_ajax |
||
| 44 | */ |
||
| 45 | public $front_ajax = false; |
||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * @param EE_Request $request |
||
| 51 | */ |
||
| 52 | public function __construct(EE_Request $request) |
||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * @param WP $wp |
||
| 64 | * @return void |
||
| 65 | * @throws EE_Error |
||
| 66 | * @throws ReflectionException |
||
| 67 | */ |
||
| 68 | public function parse_request($wp = null) |
||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * @param WP $wp |
||
| 81 | * @return void |
||
| 82 | * @throws EE_Error |
||
| 83 | * @throws ReflectionException |
||
| 84 | */ |
||
| 85 | public function set_request_vars($wp = null) |
||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * @param WP $wp |
||
| 103 | * @return int |
||
| 104 | */ |
||
| 105 | public function get_post_id_from_request($wp = null) |
||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * @param WP $wp |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | public function get_post_name_from_request($wp = null) |
||
| 166 | |||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * @param WP $wp |
||
| 171 | * @return mixed |
||
| 172 | */ |
||
| 173 | public function get_post_type_from_request($wp = null) |
||
| 182 | |||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * Just a helper method for getting the url for the displayed page. |
||
| 187 | * |
||
| 188 | * @param WP $wp |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function get_current_page_permalink($wp = null) |
||
| 208 | |||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * @return bool |
||
| 213 | * @throws EE_Error |
||
| 214 | * @throws ReflectionException |
||
| 215 | */ |
||
| 216 | public function test_for_espresso_page() |
||
| 252 | /** |
||
| 253 | * @param $key |
||
| 254 | * @param $value |
||
| 255 | * @return void |
||
| 256 | */ |
||
| 257 | public function set_notice($key, $value) |
||
| 261 | |||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * @param $key |
||
| 266 | * @return mixed |
||
| 267 | */ |
||
| 268 | public function get_notice($key) |
||
| 274 | |||
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * @param $string |
||
| 279 | * @return void |
||
| 280 | */ |
||
| 281 | public function add_output($string) |
||
| 285 | |||
| 286 | |||
| 287 | |||
| 288 | /** |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function get_output() |
||
| 295 | |||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * @param $item |
||
| 300 | * @param $key |
||
| 301 | */ |
||
| 302 | public function sanitize_text_field_for_array_walk(&$item, &$key) |
||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * @param null|bool $value |
||
| 313 | * @return void |
||
| 314 | * @throws EE_Error |
||
| 315 | * @throws ReflectionException |
||
| 316 | */ |
||
| 317 | public function set_espresso_page($value = null) |
||
| 326 | |||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * @return mixed |
||
| 331 | */ |
||
| 332 | public function is_espresso_page() |
||
| 336 | |||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * returns contents of $_REQUEST |
||
| 341 | * |
||
| 342 | * @return array |
||
| 343 | */ |
||
| 344 | public function params() |
||
| 348 | |||
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * @param $key |
||
| 353 | * @param $value |
||
| 354 | * @param bool $override_ee |
||
| 355 | * @return void |
||
| 356 | */ |
||
| 357 | public function set($key, $value, $override_ee = false) |
||
| 361 | |||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * @param $key |
||
| 366 | * @param null $default |
||
| 367 | * @return mixed |
||
| 368 | */ |
||
| 369 | public function get($key, $default = null) |
||
| 373 | |||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * check if param exists |
||
| 378 | * |
||
| 379 | * @param $key |
||
| 380 | * @return boolean |
||
| 381 | */ |
||
| 382 | public function is_set($key) |
||
| 386 | |||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * remove param |
||
| 391 | * |
||
| 392 | * @param $key |
||
| 393 | * @return void |
||
| 394 | */ |
||
| 395 | public function un_set($key) |
||
| 399 | |||
| 400 | |||
| 401 | |||
| 402 | } |
||
| 403 | // End of file EE_Request_Handler.core.php |
||
| 405 |