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() { |
||
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(){ |
||
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 ) { |
|
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 ) { |
||
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 ) { |
||
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 = '' ) { |
||
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 ) { |
||
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() { |
||
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() { |
||
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 | |||
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.