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; |
||
| 11 | final class EE_Request_Handler implements InterminableInterface { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var array $_params $_REQUEST parameters |
||
| 15 | * @access private |
||
| 16 | */ |
||
| 17 | private $_params = array(); |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array $_notice |
||
| 21 | * @access private |
||
| 22 | */ |
||
| 23 | private $_notice = array(); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * rendered output to be returned to WP |
||
| 27 | * @var string |
||
| 28 | * @access private |
||
| 29 | */ |
||
| 30 | private $_output = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * whether current request is via AJAX |
||
| 34 | * @var boolean |
||
| 35 | * @access public |
||
| 36 | */ |
||
| 37 | public $ajax = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * whether current request is via AJAX from the frontend of the site |
||
| 41 | * @var boolean |
||
| 42 | * @access public |
||
| 43 | */ |
||
| 44 | public $front_ajax = false; |
||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * class constructor |
||
| 50 | * |
||
| 51 | * @access public |
||
| 52 | * @param EE_Request $request |
||
| 53 | */ |
||
| 54 | public function __construct( EE_Request $request ) { |
||
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * set_request_vars |
||
| 67 | * |
||
| 68 | * @access public |
||
| 69 | * @param WP $wp |
||
| 70 | * @return void |
||
| 71 | */ |
||
| 72 | public function parse_request( $wp = null ) { |
||
| 79 | |||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * set_request_vars |
||
| 84 | * |
||
| 85 | * @access public |
||
| 86 | * @param WP $wp |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | public function set_request_vars( $wp = null ) { |
||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * get_post_id_from_request |
||
| 106 | * |
||
| 107 | * @access public |
||
| 108 | * @param WP $wp |
||
| 109 | * @return int |
||
| 110 | */ |
||
| 111 | public function get_post_id_from_request( $wp = null ) { |
||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * get_post_name_from_request |
||
| 132 | * |
||
| 133 | * @access public |
||
| 134 | * @param WP $wp |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public function get_post_name_from_request( $wp = null ) { |
||
| 171 | |||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * get_post_type_from_request |
||
| 176 | * |
||
| 177 | * @access public |
||
| 178 | * @param WP $wp |
||
| 179 | * @return mixed |
||
| 180 | */ |
||
| 181 | public function get_post_type_from_request( $wp = null ) { |
||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * Just a helper method for getting the url for the displayed page. |
||
| 192 | * @param WP $wp |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function get_current_page_permalink( $wp = null ) { |
||
| 211 | |||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * test_for_espresso_page |
||
| 216 | * |
||
| 217 | * @access public |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function test_for_espresso_page() { |
||
| 255 | |||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * is_espresso_page |
||
| 260 | * |
||
| 261 | * @access public |
||
| 262 | * @param null|bool $value |
||
| 263 | * @return void |
||
| 264 | */ |
||
| 265 | public function set_espresso_page( $value = null ) { |
||
| 268 | |||
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * is_espresso_page |
||
| 273 | * |
||
| 274 | * @access public |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | public function is_espresso_page() { |
||
| 280 | |||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * returns contents of $_REQUEST |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | public function params() { |
||
| 290 | |||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * setter |
||
| 295 | * |
||
| 296 | * @access public |
||
| 297 | * @param $key |
||
| 298 | * @param $value |
||
| 299 | * @param bool $override_ee |
||
| 300 | * @return void |
||
| 301 | */ |
||
| 302 | View Code Duplication | public function set( $key, $value, $override_ee = false ) { |
|
| 312 | |||
| 313 | |||
| 314 | |||
| 315 | /** |
||
| 316 | * getter |
||
| 317 | * |
||
| 318 | * @access public |
||
| 319 | * @param $key |
||
| 320 | * @param null $default |
||
| 321 | * @return mixed |
||
| 322 | */ |
||
| 323 | public function get( $key, $default = null ) { |
||
| 326 | |||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * check if param exists |
||
| 331 | * |
||
| 332 | * @access public |
||
| 333 | * @param $key |
||
| 334 | * @return boolean |
||
| 335 | */ |
||
| 336 | public function is_set( $key ) { |
||
| 339 | |||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * remove param |
||
| 344 | * |
||
| 345 | * @access public |
||
| 346 | * @param $key |
||
| 347 | * @return void |
||
| 348 | */ |
||
| 349 | public function un_set( $key ) { |
||
| 352 | |||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * set_notice |
||
| 357 | * |
||
| 358 | * @access public |
||
| 359 | * @param $key |
||
| 360 | * @param $value |
||
| 361 | * @return void |
||
| 362 | */ |
||
| 363 | public function set_notice( $key, $value ) { |
||
| 366 | |||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * get_notice |
||
| 371 | * |
||
| 372 | * @access public |
||
| 373 | * @param $key |
||
| 374 | * @return mixed |
||
| 375 | */ |
||
| 376 | public function get_notice( $key ) { |
||
| 379 | |||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * add_output |
||
| 384 | * |
||
| 385 | * @access public |
||
| 386 | * @param $string |
||
| 387 | * @return void |
||
| 388 | */ |
||
| 389 | public function add_output( $string ) { |
||
| 392 | |||
| 393 | |||
| 394 | |||
| 395 | /** |
||
| 396 | * get_output |
||
| 397 | * |
||
| 398 | * @access public |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | public function get_output() { |
||
| 404 | |||
| 405 | |||
| 406 | |||
| 407 | /** |
||
| 408 | * @param $item |
||
| 409 | * @param $key |
||
| 410 | */ |
||
| 411 | public function sanitize_text_field_for_array_walk( &$item, &$key ) { |
||
| 414 | |||
| 415 | |||
| 416 | |||
| 417 | /** |
||
| 418 | * @param $a |
||
| 419 | * @param $b |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | public function __set($a,$b) { return false; } |
||
| 423 | |||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * @param $a |
||
| 428 | * @return bool |
||
| 429 | */ |
||
| 430 | public function __get($a) { return false; } |
||
| 431 | |||
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * @param $a |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | public function __isset($a) { return false; } |
||
| 439 | |||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * @param $a |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | public function __unset($a) { return false; } |
||
| 447 | |||
| 448 | |||
| 449 | |||
| 450 | /** |
||
| 451 | * @return void |
||
| 452 | */ |
||
| 453 | public function __clone() {} |
||
| 454 | |||
| 455 | |||
| 456 | |||
| 457 | /** |
||
| 458 | * @return void |
||
| 459 | */ |
||
| 460 | public function __wakeup() {} |
||
| 461 | |||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * |
||
| 466 | */ |
||
| 467 | public function __destruct() {} |
||
| 468 | |||
| 469 | |||
| 470 | } |
||
| 471 | // End of file EE_Request_Handler.core.php |
||
| 473 |