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 ) { |
||
| 194 | |||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * loop_start |
||
| 199 | * |
||
| 200 | * @access public |
||
| 201 | * @param array $wp_query_array an array containing the WP_Query object |
||
| 202 | * @return void |
||
| 203 | */ |
||
| 204 | public static function loop_start( $wp_query_array ) { |
||
| 208 | |||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * the_title |
||
| 213 | * |
||
| 214 | * @access public |
||
| 215 | * @param string $title |
||
| 216 | * @param int $id |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public static function the_title( $title = '', $id = 0 ) { |
||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * get_the_excerpt - kinda hacky, but if a theme is using get_the_excerpt(), then we need to remove our filters on the_content() |
||
| 228 | * |
||
| 229 | * @access public |
||
| 230 | * @param string $excerpt |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public static function get_the_excerpt( $excerpt = '' ) { |
||
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * end_get_the_excerpt |
||
| 243 | * |
||
| 244 | * @access public |
||
| 245 | * @param string $text |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | public static function end_get_the_excerpt( $text = '' ) { |
||
| 252 | |||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * event_details |
||
| 257 | * |
||
| 258 | * @access public |
||
| 259 | * @param string $content |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public static function event_details( $content ) { |
||
| 292 | |||
| 293 | |||
| 294 | |||
| 295 | /** |
||
| 296 | * use_filterable_display_order |
||
| 297 | * |
||
| 298 | * @access protected |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | protected static function use_filterable_display_order() { |
||
| 322 | |||
| 323 | |||
| 324 | |||
| 325 | /** |
||
| 326 | * event_datetimes - adds datetimes ABOVE content |
||
| 327 | * |
||
| 328 | * @access public |
||
| 329 | * @param string $content |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public static function event_datetimes( $content ) { |
||
| 335 | |||
| 336 | |||
| 337 | |||
| 338 | /** |
||
| 339 | * event_tickets - adds tickets ABOVE content (which includes datetimes) |
||
| 340 | * |
||
| 341 | * @access public |
||
| 342 | * @param string $content |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | public static function event_tickets( $content ) { |
||
| 348 | |||
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * event_venues |
||
| 353 | * |
||
| 354 | * @access public |
||
| 355 | * @param string $content |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public static function event_venue( $content ) { |
||
| 361 | |||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * event_venues - adds venues BELOW content |
||
| 366 | * |
||
| 367 | * @access public |
||
| 368 | * @param string $content |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | public static function event_venues( $content ) { |
||
| 374 | |||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * loop_end |
||
| 379 | * |
||
| 380 | * @access public |
||
| 381 | * @param array $wp_query_array an array containing the WP_Query object |
||
| 382 | * @return void |
||
| 383 | */ |
||
| 384 | public static function loop_end( $wp_query_array ) { |
||
| 388 | |||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * wp_enqueue_scripts |
||
| 393 | * |
||
| 394 | * @access public |
||
| 395 | * @return void |
||
| 396 | */ |
||
| 397 | public function wp_enqueue_scripts() { |
||
| 414 | |||
| 415 | |||
| 416 | |||
| 417 | |||
| 418 | |||
| 419 | |||
| 420 | |||
| 421 | |||
| 422 | /** |
||
| 423 | * display_venue |
||
| 424 | * |
||
| 425 | * @access public |
||
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | public static function display_venue() { |
||
| 436 | |||
| 437 | |||
| 438 | |||
| 439 | } |
||
| 440 | |||
| 458 | // 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 theSoncalls the wrong method in the parent class.