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 |
||
22 | class Post_Types { |
||
23 | |||
24 | /** |
||
25 | * Hook in tabs. |
||
26 | * |
||
27 | * @since 3.0.0 |
||
28 | */ |
||
29 | public function __construct() { |
||
56 | |||
57 | /** |
||
58 | * Add column headers to Calendar feeds custom post type page. |
||
59 | * |
||
60 | * @since 3.0.0 |
||
61 | * |
||
62 | * @param array $columns Default columns. |
||
63 | * |
||
64 | * @return array Filtered output. |
||
65 | */ |
||
66 | public function add_calendar_feed_column_headers( $columns ) { |
||
78 | |||
79 | /** |
||
80 | * Fill out the Calendar feed post type columns. |
||
81 | * |
||
82 | * @since 3.0.0 |
||
83 | * |
||
84 | * @param string $column_name Column identifier. |
||
85 | * @param int $post_id The calendar feed post id. |
||
86 | * |
||
87 | * @return void |
||
88 | */ |
||
89 | public function calendar_feed_column_content( $column_name, $post_id ) { |
||
128 | |||
129 | /** |
||
130 | * Add actions to Calendar feed post type row view. |
||
131 | * |
||
132 | * @since 3.0.0 |
||
133 | * |
||
134 | * @param array $actions Default actions |
||
135 | * @param \WP_Post $post Post object. |
||
136 | * |
||
137 | * @return array Filtered output. |
||
138 | */ |
||
139 | public function row_actions( $actions, $post ) { |
||
149 | |||
150 | /** |
||
151 | * Bulk actions. |
||
152 | * |
||
153 | * @since 3.0.0 |
||
154 | */ |
||
155 | public function bulk_actions() { |
||
156 | |||
157 | // Clear an individual feed cache. |
||
158 | // @todo Convert the clear cache request to ajax. |
||
159 | View Code Duplication | if ( isset( $_REQUEST['clear_cache'] ) ) { |
|
160 | |||
161 | $id = intval( $_REQUEST['clear_cache'] ); |
||
162 | |||
163 | if ( $id > 0 ) { |
||
164 | simcal_delete_feed_transients( $id ); |
||
165 | } |
||
166 | |||
167 | wp_redirect( remove_query_arg( 'clear_cache' ) ); |
||
168 | } |
||
169 | |||
170 | // Duplicate a feed post type. |
||
171 | View Code Duplication | if ( isset( $_REQUEST['duplicate_feed'] ) ) { |
|
172 | |||
173 | $id = intval( $_REQUEST['duplicate_feed'] ); |
||
174 | |||
175 | if ( $id > 0 ) { |
||
176 | $this->duplicate_feed( $id ); |
||
177 | } |
||
178 | |||
179 | wp_redirect( remove_query_arg( 'duplicate_feed' ) ); |
||
180 | } |
||
181 | |||
182 | $bulk_actions = new Bulk_Actions( 'calendar' ); |
||
183 | |||
184 | $bulk_actions->register_bulk_action( array( |
||
185 | 'menu_text' => __( 'Clear cache', 'google-calendar-events' ), |
||
186 | 'action_name' => 'clear_calendars_cache', |
||
187 | 'callback' => function( $post_ids ) { |
||
188 | simcal_delete_feed_transients( $post_ids ); |
||
189 | }, |
||
190 | 'admin_notice' => __( 'Cache cleared.', 'google-calendar-events' ), |
||
191 | ) |
||
192 | ); |
||
193 | |||
194 | $bulk_actions->init(); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Edit calendars table hooks. |
||
199 | * |
||
200 | * @since 3.0.0 |
||
201 | * @internal |
||
202 | */ |
||
203 | public function edit_table_hooks() { |
||
213 | |||
214 | /** |
||
215 | * Clone a feed post type. |
||
216 | * |
||
217 | * @since 3.0.0 |
||
218 | * |
||
219 | * @param int $post_id |
||
220 | */ |
||
221 | private function duplicate_feed( $post_id ) { |
||
249 | |||
250 | /** |
||
251 | * Default event template builder content. |
||
252 | * |
||
253 | * @since 3.0.0 |
||
254 | * |
||
255 | * @param string $content |
||
256 | * @param \WP_Post $post |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | public function default_event_template( $content, $post ) { |
||
263 | |||
264 | /** |
||
265 | * Clear cache button. |
||
266 | * |
||
267 | * @since 3.0.0 |
||
268 | */ |
||
269 | public function clear_cache_button() { |
||
280 | |||
281 | /** |
||
282 | * Add a shortcode button. |
||
283 | * |
||
284 | * Adds a button to add a calendar shortcode in WordPress content editor. |
||
285 | * Uses Thickbox. http://codex.wordpress.org/ThickBox |
||
286 | * |
||
287 | * @since 3.0.0 |
||
288 | */ |
||
289 | public function add_shortcode_button() { |
||
313 | |||
314 | /** |
||
315 | * Panel for the add shortcode media button. |
||
316 | * |
||
317 | * Prints the panel for choosing a calendar to insert as a shortcode in a page or post. |
||
318 | * |
||
319 | * @since 3.0.0 |
||
320 | */ |
||
321 | public function add_shortcode_panel() { |
||
353 | |||
354 | } |
||
355 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.