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:
1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
||
24 | class EED_Venues_Archive extends EED_Module { |
||
25 | |||
26 | /** |
||
27 | * @return EED_Venues_Archive |
||
28 | */ |
||
29 | public static function instance() { |
||
32 | |||
33 | /** |
||
34 | * set_hooks - for hooking into EE Core, other modules, etc |
||
35 | * |
||
36 | * @access public |
||
37 | * @return void |
||
38 | */ |
||
39 | public static function set_hooks() { |
||
43 | |||
44 | /** |
||
45 | * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
||
46 | * |
||
47 | * @access public |
||
48 | * @return void |
||
49 | */ |
||
50 | public static function set_hooks_admin() { |
||
52 | |||
53 | |||
54 | |||
55 | /** |
||
56 | * run - initial module setup |
||
57 | * |
||
58 | * @access public |
||
59 | * @param \WP $WP |
||
60 | */ |
||
61 | public function run( $WP ) { |
||
66 | |||
67 | |||
68 | |||
69 | /** |
||
70 | * template_include |
||
71 | * |
||
72 | * @access public |
||
73 | * @param string $template |
||
74 | * @return string |
||
75 | */ |
||
76 | public function template_include( $template ) { |
||
77 | // not a custom template? |
||
78 | if ( EE_Registry::instance()->load_core( 'Front_Controller', array(), false, true )->get_selected_template() != 'archive-espresso_venues.php' ) { |
||
79 | EEH_Template::load_espresso_theme_functions(); |
||
80 | // then add extra event data via hooks |
||
81 | add_filter( 'the_title', array( $this, 'the_title' ), 100, 1 ); |
||
82 | // don't know if theme uses the_excerpt |
||
83 | add_filter( 'the_excerpt', array( $this, 'venue_details' ), 100 ); |
||
84 | // or the_content |
||
85 | add_filter( 'the_content', array( $this, 'venue_details' ), 100 ); |
||
86 | // don't display entry meta because the existing theme will take care of that |
||
87 | add_filter( 'FHEE__content_espresso_venues_details_template__display_entry_meta', '__return_false' ); |
||
88 | } |
||
89 | return $template; |
||
90 | } |
||
91 | |||
92 | |||
93 | |||
94 | /** |
||
95 | * the_title |
||
96 | * |
||
97 | * @access public |
||
98 | * @param string $title |
||
99 | * @return string |
||
100 | */ |
||
101 | public function the_title( $title = '' ) { |
||
104 | |||
105 | |||
106 | /** |
||
107 | * venue_details |
||
108 | * |
||
109 | * @access public |
||
110 | * @param string $content |
||
111 | * @return string |
||
112 | */ |
||
113 | public function venue_details( $content ) { |
||
114 | global $post; |
||
115 | if ( |
||
116 | $post->post_type == 'espresso_venues' |
||
117 | && ! post_password_required() |
||
118 | ) { |
||
119 | // since the 'content-espresso_venues-details.php' template might be used directly from within a theme, |
||
120 | // it uses the_content() for displaying the $post->post_content |
||
121 | // so in order to load a template that uses the_content() from within a callback being used to filter the_content(), |
||
122 | // we need to first remove this callback from being applied to the_content() (otherwise it will recurse and blow up the interweb) |
||
123 | remove_filter( 'the_excerpt', array( $this, 'venue_details' ), 100 ); |
||
124 | remove_filter( 'the_content', array( $this, 'venue_details' ), 100 ); |
||
125 | // add filters we want |
||
126 | add_filter( 'the_content', array( $this, 'venue_location' ), 110 ); |
||
127 | add_filter( 'the_excerpt', array( $this, 'venue_location' ), 110 ); |
||
128 | // now load our template |
||
129 | $template = EEH_Template::locate_template( 'content-espresso_venues-details.php' ); |
||
130 | //now add our filter back in, plus some others |
||
131 | add_filter( 'the_excerpt', array( $this, 'venue_details' ), 100 ); |
||
132 | add_filter( 'the_content', array( $this, 'venue_details' ), 100 ); |
||
133 | // remove other filters we added so they won't get applied to the next post |
||
134 | remove_filter( 'the_content', array( $this, 'venue_location' ), 110 ); |
||
135 | remove_filter( 'the_excerpt', array( $this, 'venue_location' ), 110 ); |
||
136 | // we're not returning the $content directly because the template we are loading uses the_content (or the_excerpt) |
||
137 | } |
||
138 | return ! empty( $template ) ? $template : $content; |
||
139 | } |
||
140 | |||
141 | |||
142 | |||
143 | /** |
||
144 | * venue_location |
||
145 | * |
||
146 | * @access public |
||
147 | * @param string $content |
||
148 | * @return string |
||
149 | */ |
||
150 | public function venue_location( $content ) { |
||
153 | |||
154 | |||
155 | |||
156 | |||
157 | /** |
||
158 | * wp_enqueue_scripts |
||
159 | * |
||
160 | * @access public |
||
161 | * @return void |
||
162 | */ |
||
163 | public function wp_enqueue_scripts() { |
||
175 | |||
176 | |||
177 | |||
178 | |||
179 | } |
||
180 | // End of file EED_Venues_Archive.module.php |
||
181 | // Location: /modules/venues_archive/EED_Venues_Archive.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.