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 EED_Event_Single 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 EED_Event_Single, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
||
24 | class EED_Event_Single extends EED_Module { |
||
25 | |||
26 | /** |
||
27 | * @type bool $using_get_the_excerpt |
||
28 | */ |
||
29 | protected static $using_get_the_excerpt = false; |
||
30 | |||
31 | |||
32 | /** |
||
33 | * @type EE_Template_Part_Manager $template_parts |
||
34 | */ |
||
35 | protected $template_parts; |
||
36 | |||
37 | |||
38 | |||
39 | /** |
||
40 | * @return EED_Event_Single |
||
41 | */ |
||
42 | public static function instance() { |
||
45 | |||
46 | |||
47 | |||
48 | /** |
||
49 | * set_hooks - for hooking into EE Core, other modules, etc |
||
50 | * |
||
51 | * @access public |
||
52 | * @return void |
||
53 | */ |
||
54 | public static function set_hooks() { |
||
55 | add_filter( 'FHEE_run_EE_wp', '__return_true' ); |
||
56 | add_action( 'wp_loaded', array( 'EED_Event_Single', 'set_definitions' ), 2 ); |
||
57 | EE_Config::register_route( __( 'event', 'event_espresso' ), 'Event_Single', 'run' ); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
||
62 | * |
||
63 | * @access public |
||
64 | * @return void |
||
65 | */ |
||
66 | public static function set_hooks_admin() { |
||
69 | |||
70 | |||
71 | |||
72 | |||
73 | /** |
||
74 | * set_definitions |
||
75 | * |
||
76 | * @access public |
||
77 | * @static |
||
78 | * @return void |
||
79 | */ |
||
80 | public static function set_definitions() { |
||
84 | |||
85 | |||
86 | |||
87 | /** |
||
88 | * set_config |
||
89 | * |
||
90 | * @void |
||
91 | */ |
||
92 | protected function set_config(){ |
||
93 | $this->set_config_section( 'template_settings' ); |
||
94 | $this->set_config_class( 'EE_Event_Single_Config' ); |
||
95 | $this->set_config_name( 'EED_Event_Single' ); |
||
96 | } |
||
97 | |||
98 | |||
99 | |||
100 | |||
101 | /** |
||
102 | * initialize_template_parts |
||
103 | * |
||
104 | * @access public |
||
105 | * @param \EE_Event_Single_Config $config |
||
106 | * @return \EE_Template_Part_Manager |
||
107 | */ |
||
108 | View Code Duplication | public function initialize_template_parts( EE_Event_Single_Config $config = null ) { |
|
109 | $config = $config instanceof EE_Event_Single_Config ? $config : $this->config(); |
||
110 | EEH_Autoloader::instance()->register_template_part_autoloaders(); |
||
111 | $template_parts = new EE_Template_Part_Manager(); |
||
112 | $template_parts->add_template_part( |
||
113 | 'tickets', |
||
114 | __( 'Ticket Selector', 'event_espresso' ), |
||
115 | 'content-espresso_events-tickets.php', |
||
116 | $config->display_order_tickets |
||
117 | ); |
||
118 | $template_parts->add_template_part( |
||
119 | 'datetimes', |
||
120 | __( 'Dates and Times', 'event_espresso' ), |
||
121 | 'content-espresso_events-datetimes.php', |
||
122 | $config->display_order_datetimes |
||
123 | ); |
||
124 | $template_parts->add_template_part( |
||
125 | 'event', |
||
126 | __( 'Event Description', 'event_espresso' ), |
||
127 | 'content-espresso_events-details.php', |
||
128 | $config->display_order_event |
||
129 | ); |
||
130 | $template_parts->add_template_part( |
||
131 | 'venue', |
||
132 | __( 'Venue Information', 'event_espresso' ), |
||
133 | 'content-espresso_events-venues.php', |
||
134 | $config->display_order_venue |
||
135 | ); |
||
136 | do_action( 'AHEE__EED_Event_Single__initialize_template_parts', $template_parts ); |
||
137 | return $template_parts; |
||
138 | } |
||
139 | |||
140 | |||
141 | |||
142 | |||
143 | /** |
||
144 | * run - initial module setup |
||
145 | * |
||
146 | * @access public |
||
147 | * @param WP $WP |
||
148 | * @return void |
||
149 | */ |
||
150 | public function run( $WP ) { |
||
151 | // ensure valid EE_Events_Single_Config() object exists |
||
152 | $this->set_config(); |
||
153 | // check what template is loaded |
||
154 | add_filter( 'template_include', array( $this, 'template_include' ), 999, 1 ); |
||
155 | add_filter( 'FHEE__EED_Ticket_Selector__load_tckt_slctr_assets', '__return_true' ); |
||
156 | // load css |
||
157 | add_action('wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ), 10 ); |
||
158 | EE_Registry::instance()->load_helper( 'Venue_View' ); |
||
159 | } |
||
160 | |||
161 | |||
162 | |||
163 | /** |
||
164 | * template_include |
||
165 | * |
||
166 | * @access public |
||
167 | * @param string $template |
||
168 | * @return string |
||
169 | */ |
||
170 | public function template_include( $template ) { |
||
171 | global $post; |
||
172 | /** @type EE_Event_Single_Config $config */ |
||
173 | $config = $this->config(); |
||
174 | if ( $config->display_status_banner_single ) { |
||
175 | add_filter( 'the_title', array( 'EED_Event_Single', 'the_title' ), 100, 2 ); |
||
176 | } |
||
177 | // not a custom template? |
||
178 | if ( |
||
179 | EE_Front_Controller::instance()->get_selected_template() != 'single-espresso_events.php' |
||
180 | && ! post_password_required( $post ) |
||
181 | ) { |
||
182 | EEH_Template::load_espresso_theme_functions(); |
||
183 | // then add extra event data via hooks |
||
184 | add_action( 'loop_start', array( 'EED_Event_Single', 'loop_start' )); |
||
185 | add_filter( 'get_the_excerpt', array( 'EED_Event_Single', 'get_the_excerpt' ), 1, 1 ); |
||
186 | add_filter( 'the_content', array( 'EED_Event_Single', 'event_details' ), 100 ); |
||
187 | add_action( 'loop_end', array( 'EED_Event_Single', 'loop_end' )); |
||
188 | // don't display entry meta because the existing theme will take car of that |
||
189 | add_filter( 'FHEE__content_espresso_events_details_template__display_entry_meta', '__return_false' ); |
||
190 | } |
||
191 | return $template; |
||
192 | } |
||
193 | |||
194 | |||
195 | |||
196 | /** |
||
197 | * loop_start |
||
198 | * |
||
199 | * @access public |
||
200 | * @param array $wp_query_array an array containing the WP_Query object |
||
201 | * @return void |
||
202 | */ |
||
203 | public static function loop_start( $wp_query_array ) { |
||
207 | |||
208 | |||
209 | |||
210 | /** |
||
211 | * the_title |
||
212 | * |
||
213 | * @access public |
||
214 | * @param string $title |
||
215 | * @param int $id |
||
216 | * @return string |
||
217 | */ |
||
218 | public static function the_title( $title = '', $id = 0 ) { |
||
222 | |||
223 | |||
224 | |||
225 | /** |
||
226 | * get_the_excerpt - kinda hacky, but if a theme is using get_the_excerpt(), then we need to remove our filters on the_content() |
||
227 | * |
||
228 | * @access public |
||
229 | * @param string $excerpt |
||
230 | * @return string |
||
231 | */ |
||
232 | public static function get_the_excerpt( $excerpt = '' ) { |
||
233 | EED_Event_Single::$using_get_the_excerpt = true; |
||
234 | add_filter( 'wp_trim_excerpt', array( 'EED_Event_Single', 'end_get_the_excerpt' ), 999, 1 ); |
||
235 | return $excerpt; |
||
236 | } |
||
237 | |||
238 | |||
239 | |||
240 | /** |
||
241 | * end_get_the_excerpt |
||
242 | * |
||
243 | * @access public |
||
244 | * @param string $text |
||
245 | * @return string |
||
246 | */ |
||
247 | public static function end_get_the_excerpt( $text = '' ) { |
||
251 | |||
252 | |||
253 | |||
254 | /** |
||
255 | * event_details |
||
256 | * |
||
257 | * @access public |
||
258 | * @param string $content |
||
259 | * @return string |
||
260 | */ |
||
261 | public static function event_details( $content ) { |
||
262 | global $post; |
||
263 | static $current_post_ID = 0; |
||
264 | if ( |
||
265 | $current_post_ID != $post->ID |
||
266 | && $post->post_type == 'espresso_events' |
||
267 | && ! EED_Event_Single::$using_get_the_excerpt |
||
268 | && ! post_password_required() |
||
269 | ) { |
||
270 | // Set current post ID to prevent showing content twice, but only if headers have definitely been sent. |
||
271 | // Reason being is that some plugins, like Yoast, need to run through a copy of the loop early |
||
272 | // BEFORE headers are sent in order to examine the post content and generate content for the HTML header. |
||
273 | // We want to allow those plugins to still do their thing and have access to our content, but depending on |
||
274 | // how your event content is being displayed (shortcode, CPT route, etc), this filter can get applied twice, |
||
275 | // so the following allows this filter to be applied multiple times, but only once for real |
||
276 | $current_post_ID = did_action( 'loop_start' ) ? $post->ID : 0; |
||
277 | if ( EE_Registry::instance()->CFG->template_settings->EED_Event_Single->use_sortable_display_order ) { |
||
278 | // we need to first remove this callback from being applied to the_content() |
||
279 | // (otherwise it will recurse and blow up the interweb) |
||
280 | remove_filter( 'the_content', array( 'EED_Event_Single', 'event_details' ), 100 ); |
||
281 | EED_Event_Single::instance()->template_parts = EED_Event_Single::instance()->initialize_template_parts(); |
||
282 | $content = EEH_Template::locate_template( 'content-espresso_events-details.php' ); |
||
283 | $content = EED_Event_Single::instance()->template_parts->apply_template_part_filters( $content ); |
||
284 | add_filter( 'the_content', array( 'EED_Event_Single', 'event_details' ), 100 ); |
||
285 | } else { |
||
286 | $content = EED_Event_Single::use_filterable_display_order(); |
||
287 | } |
||
288 | } |
||
289 | return $content; |
||
290 | } |
||
291 | |||
292 | |||
293 | |||
294 | /** |
||
295 | * use_filterable_display_order |
||
296 | * |
||
297 | * @access protected |
||
298 | * @return string |
||
299 | */ |
||
300 | protected static function use_filterable_display_order() { |
||
301 | // since the 'content-espresso_events-details.php' template might be used directly from within a theme, |
||
302 | // it uses the_content() for displaying the $post->post_content |
||
303 | // so in order to load a template that uses the_content() from within a callback being used to filter the_content(), |
||
304 | // we need to first remove this callback from being applied to the_content() (otherwise it will recurse and blow up the interweb) |
||
305 | remove_filter( 'the_content', array( 'EED_Event_Single', 'event_details' ), 100 ); |
||
306 | //now add additional content |
||
307 | add_filter( 'the_content', array( 'EED_Event_Single', 'event_datetimes' ), 110, 1 ); |
||
308 | add_filter( 'the_content', array( 'EED_Event_Single', 'event_tickets' ), 120, 1 ); |
||
309 | add_filter( 'the_content', array( 'EED_Event_Single', 'event_venues' ), 130, 1 ); |
||
310 | do_action( 'AHEE__EED_Event_Single__use_filterable_display_order__after_add_filters' ); |
||
311 | // now load our template |
||
312 | $content = EEH_Template::locate_template( 'content-espresso_events-details.php' ); |
||
313 | //now add our filter back in, plus some others |
||
314 | add_filter( 'the_content', array( 'EED_Event_Single', 'event_details' ), 100 ); |
||
315 | remove_filter( 'the_content', array( 'EED_Event_Single', 'event_datetimes' ), 110 ); |
||
316 | remove_filter( 'the_content', array( 'EED_Event_Single', 'event_tickets' ), 120 ); |
||
317 | remove_filter( 'the_content', array( 'EED_Event_Single', 'event_venues' ), 130 ); |
||
318 | // we're not returning the $content directly because the template we are loading uses the_content (or the_excerpt) |
||
319 | return $content; |
||
320 | } |
||
321 | |||
322 | |||
323 | |||
324 | /** |
||
325 | * event_datetimes - adds datetimes ABOVE content |
||
326 | * |
||
327 | * @access public |
||
328 | * @param string $content |
||
329 | * @return string |
||
330 | */ |
||
331 | public static function event_datetimes( $content ) { |
||
334 | |||
335 | |||
336 | |||
337 | /** |
||
338 | * event_tickets - adds tickets ABOVE content (which includes datetimes) |
||
339 | * |
||
340 | * @access public |
||
341 | * @param string $content |
||
342 | * @return string |
||
343 | */ |
||
344 | public static function event_tickets( $content ) { |
||
347 | |||
348 | |||
349 | |||
350 | /** |
||
351 | * event_venues |
||
352 | * |
||
353 | * @access public |
||
354 | * @param string $content |
||
355 | * @return string |
||
356 | */ |
||
357 | public static function event_venue( $content ) { |
||
360 | |||
361 | |||
362 | |||
363 | /** |
||
364 | * event_venues - adds venues BELOW content |
||
365 | * |
||
366 | * @access public |
||
367 | * @param string $content |
||
368 | * @return string |
||
369 | */ |
||
370 | public static function event_venues( $content ) { |
||
373 | |||
374 | |||
375 | |||
376 | /** |
||
377 | * loop_end |
||
378 | * |
||
379 | * @access public |
||
380 | * @param array $wp_query_array an array containing the WP_Query object |
||
381 | * @return void |
||
382 | */ |
||
383 | public static function loop_end( $wp_query_array ) { |
||
387 | |||
388 | |||
389 | |||
390 | /** |
||
391 | * wp_enqueue_scripts |
||
392 | * |
||
393 | * @access public |
||
394 | * @return void |
||
395 | */ |
||
396 | public function wp_enqueue_scripts() { |
||
397 | // get some style |
||
398 | if ( apply_filters( 'FHEE_enable_default_espresso_css', TRUE ) && apply_filters( 'FHEE__EED_Event_Single__wp_enqueue_scripts__enable_css', TRUE )) { |
||
399 | EE_Registry::instance()->load_helper( 'File' ); |
||
400 | // first check uploads folder |
||
401 | if ( is_readable( get_stylesheet_directory() . $this->theme . DS . 'style.css' )) { |
||
402 | wp_register_style( $this->theme, get_stylesheet_directory_uri() . $this->theme . DS . 'style.css', array( 'dashicons', 'espresso_default' )); |
||
403 | } else { |
||
404 | wp_register_style( $this->theme, EE_TEMPLATES_URL . $this->theme . DS . 'style.css', array( 'dashicons', 'espresso_default' )); |
||
405 | } |
||
406 | wp_enqueue_script( $this->theme ); |
||
407 | if ( EE_Registry::instance()->CFG->map_settings->use_google_maps ) { |
||
408 | EE_Registry::instance()->load_helper( 'Maps' ); |
||
409 | add_action('wp_enqueue_scripts', array( 'EEH_Maps', 'espresso_google_map_js' ), 11 ); |
||
410 | } |
||
411 | } |
||
412 | } |
||
413 | |||
414 | |||
415 | |||
416 | |||
417 | |||
418 | |||
419 | |||
420 | |||
421 | /** |
||
422 | * display_venue |
||
423 | * |
||
424 | * @access public |
||
425 | * @return bool |
||
426 | */ |
||
427 | public static function display_venue() { |
||
435 | |||
436 | |||
437 | |||
438 | } |
||
439 | |||
440 | |||
441 | |||
442 | |||
443 | |||
444 | /** |
||
457 | // Location: /modules/event_details/EED_Event_Single.module.php |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.