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 | * $_REQUEST parameters |
||
| 18 | * |
||
| 19 | * @var array $_params |
||
| 20 | */ |
||
| 21 | private $_params; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array $_notice |
||
| 25 | */ |
||
| 26 | private $_notice = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * rendered output to be returned to WP |
||
| 30 | * |
||
| 31 | * @var string $_output |
||
| 32 | */ |
||
| 33 | private $_output = ''; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * whether current request is via AJAX |
||
| 37 | * |
||
| 38 | * @var boolean $ajax |
||
| 39 | */ |
||
| 40 | public $ajax = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * whether current request is via AJAX from the frontend of the site |
||
| 44 | * |
||
| 45 | * @var boolean $front_ajax |
||
| 46 | */ |
||
| 47 | public $front_ajax = false; |
||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * @param EE_Request $request |
||
| 53 | */ |
||
| 54 | public function __construct(EE_Request $request) |
||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * @param WP $wp |
||
| 68 | * @return void |
||
| 69 | * @throws EE_Error |
||
| 70 | * @throws ReflectionException |
||
| 71 | */ |
||
| 72 | public function parse_request($wp = null) |
||
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * @param WP $wp |
||
| 85 | * @return void |
||
| 86 | * @throws EE_Error |
||
| 87 | * @throws ReflectionException |
||
| 88 | */ |
||
| 89 | public function set_request_vars($wp = null) |
||
| 102 | |||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * @param WP $wp |
||
| 107 | * @return int |
||
| 108 | */ |
||
| 109 | public function get_post_id_from_request($wp = null) |
||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | /** |
||
| 130 | * @param WP $wp |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function get_post_name_from_request($wp = null) |
||
| 170 | |||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * @param WP $wp |
||
| 175 | * @return mixed |
||
| 176 | */ |
||
| 177 | public function get_post_type_from_request($wp = null) |
||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * Just a helper method for getting the url for the displayed page. |
||
| 191 | * |
||
| 192 | * @param WP $wp |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function get_current_page_permalink($wp = null) |
||
| 212 | |||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * @return bool |
||
| 217 | * @throws EE_Error |
||
| 218 | * @throws ReflectionException |
||
| 219 | */ |
||
| 220 | public function test_for_espresso_page() |
||
| 256 | |||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * @param null|bool $value |
||
| 261 | * @return void |
||
| 262 | * @throws EE_Error |
||
| 263 | * @throws ReflectionException |
||
| 264 | */ |
||
| 265 | public function set_espresso_page($value = null) |
||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | public function is_espresso_page() |
||
| 283 | |||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * returns contents of $_REQUEST |
||
| 288 | * |
||
| 289 | * @return array |
||
| 290 | */ |
||
| 291 | public function params() |
||
| 295 | |||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * @param $key |
||
| 300 | * @param $value |
||
| 301 | * @param bool $override_ee |
||
| 302 | * @return void |
||
| 303 | */ |
||
| 304 | View Code Duplication | public function set($key, $value, $override_ee = false) |
|
| 315 | |||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * @param $key |
||
| 320 | * @param null $default |
||
| 321 | * @return mixed |
||
| 322 | */ |
||
| 323 | public function get($key, $default = null) |
||
| 329 | |||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * check if param exists |
||
| 334 | * |
||
| 335 | * @param $key |
||
| 336 | * @return boolean |
||
| 337 | */ |
||
| 338 | public function is_set($key) |
||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * remove param |
||
| 349 | * |
||
| 350 | * @param $key |
||
| 351 | * @return void |
||
| 352 | */ |
||
| 353 | public function un_set($key) |
||
| 357 | |||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * @param $key |
||
| 362 | * @param $value |
||
| 363 | * @return void |
||
| 364 | */ |
||
| 365 | public function set_notice($key, $value) |
||
| 369 | |||
| 370 | |||
| 371 | |||
| 372 | /** |
||
| 373 | * @param $key |
||
| 374 | * @return mixed |
||
| 375 | */ |
||
| 376 | public function get_notice($key) |
||
| 382 | |||
| 383 | |||
| 384 | |||
| 385 | /** |
||
| 386 | * @param $string |
||
| 387 | * @return void |
||
| 388 | */ |
||
| 389 | public function add_output($string) |
||
| 393 | |||
| 394 | |||
| 395 | |||
| 396 | /** |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | public function get_output() |
||
| 403 | |||
| 404 | |||
| 405 | |||
| 406 | /** |
||
| 407 | * @param $item |
||
| 408 | * @param $key |
||
| 409 | */ |
||
| 410 | public function sanitize_text_field_for_array_walk(&$item, &$key) |
||
| 416 | |||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * @param $a |
||
| 421 | * @param $b |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | public function __set($a, $b) |
||
| 428 | |||
| 429 | |||
| 430 | |||
| 431 | /** |
||
| 432 | * @param $a |
||
| 433 | * @return bool |
||
| 434 | */ |
||
| 435 | public function __get($a) |
||
| 439 | |||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * @param $a |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | public function __isset($a) |
||
| 450 | |||
| 451 | |||
| 452 | |||
| 453 | /** |
||
| 454 | * @param $a |
||
| 455 | * @return bool |
||
| 456 | */ |
||
| 457 | public function __unset($a) |
||
| 461 | |||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * @return void |
||
| 466 | */ |
||
| 467 | public function __clone() |
||
| 470 | |||
| 471 | |||
| 472 | |||
| 473 | /** |
||
| 474 | * @return void |
||
| 475 | */ |
||
| 476 | public function __wakeup() |
||
| 479 | |||
| 480 | |||
| 481 | |||
| 482 | /** |
||
| 483 | * |
||
| 484 | */ |
||
| 485 | public function __destruct() |
||
| 488 | |||
| 489 | |||
| 490 | } |
||
| 491 | // End of file EE_Request_Handler.core.php |
||
| 493 |