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