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 Google_Admin { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Google calendar feed object. |
||
| 26 | * |
||
| 27 | * @access private |
||
| 28 | * @var Google |
||
| 29 | */ |
||
| 30 | private $feed = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Google Api Key. |
||
| 34 | * |
||
| 35 | * @access private |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $google_api_key = ''; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Google Calendar id. |
||
| 42 | * |
||
| 43 | * @access private |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $google_calendar_id = ''; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Hook in tabs. |
||
| 50 | * |
||
| 51 | * @since 3.0.0 |
||
| 52 | * |
||
| 53 | * @param Google $feed |
||
| 54 | * @param string $google_api_key |
||
| 55 | * @param string $google_calendar_id |
||
| 56 | */ |
||
| 57 | public function __construct( Google $feed, $google_api_key, $google_calendar_id ) { |
||
| 58 | |||
| 59 | $this->feed = $feed; |
||
| 60 | $this->google_api_key = $google_api_key; |
||
| 61 | $this->google_calendar_id = $google_calendar_id; |
||
| 62 | |||
| 63 | $screen = simcal_is_admin_screen(); |
||
| 64 | |||
| 65 | View Code Duplication | if ( 'calendar' == $screen ) { |
|
|
|
|||
| 66 | $this->test_api_key_connection( $this->google_calendar_id ); |
||
| 67 | add_filter( 'simcal_settings_meta_tabs_li', array( $this, 'add_settings_meta_tab_li' ), 10, 1 ); |
||
| 68 | add_action( 'simcal_settings_meta_panels', array( $this, 'add_settings_meta_panel' ), 10, 1 ); |
||
| 69 | } |
||
| 70 | |||
| 71 | add_action( 'simcal_process_settings_meta', array( $this, 'process_meta' ), 10, 1 ); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Feed settings page fields. |
||
| 76 | * |
||
| 77 | * @since 3.0.0 |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | public function settings_fields() { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Check if there's a Google API Key or a legacy key is being used. |
||
| 105 | * |
||
| 106 | * This method only checks if the api key setting is not empty. |
||
| 107 | * It is not currently possible to check or validate an API Key without performing a full request. |
||
| 108 | * On the settings page there are no known calendars to use for this so we can only check if there is a string. |
||
| 109 | * |
||
| 110 | * @since 3.0.0 |
||
| 111 | * |
||
| 112 | * @param string $api_key Google API key. |
||
| 113 | * |
||
| 114 | * @return true|string |
||
| 115 | */ |
||
| 116 | public function check_google_api_key( $api_key = '' ) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Add a tab to the settings meta box. |
||
| 144 | * |
||
| 145 | * @since 3.0.0 |
||
| 146 | * |
||
| 147 | * @param array $tabs |
||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public function add_settings_meta_tab_li( $tabs ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Add a panel to the settings meta box. |
||
| 164 | * |
||
| 165 | * @since 3.0.0 |
||
| 166 | * |
||
| 167 | * @param int $post_id |
||
| 168 | */ |
||
| 169 | public function add_settings_meta_panel( $post_id ) { |
||
| 170 | |||
| 171 | $inputs = array( |
||
| 172 | $this->feed->type => array( |
||
| 173 | '_google_calendar_id' => array( |
||
| 174 | 'type' => 'standard', |
||
| 175 | 'subtype' => 'text', |
||
| 176 | 'name' => '_google_calendar_id', |
||
| 177 | 'id' => '_google_calendar_id', |
||
| 178 | 'title' => __( 'Calendar ID', 'google-calendar-events' ), |
||
| 179 | 'tooltip' => __( 'Visit your Google Calendar account, copy your public calendar ID, then paste it here.', 'google-calendar-events' ), |
||
| 180 | 'placeholder' => __( 'Enter a valid Google Calendar ID from a public calendar', 'google-calendar-events' ), |
||
| 181 | 'escaping' => array( $this->feed, 'esc_google_calendar_id' ), |
||
| 182 | 'validation' => array( $this, 'test_api_key_connection' ), |
||
| 183 | ), |
||
| 184 | '_google_events_search_query' => array( |
||
| 185 | 'type' => 'standard', |
||
| 186 | 'subtype' => 'text', |
||
| 187 | 'name' => '_google_events_search_query', |
||
| 188 | 'id' => '_google_events_search_query', |
||
| 189 | 'title' => __( 'Search Query', 'google-calendar-events' ), |
||
| 190 | 'tooltip' => __( 'Type in keywords if you only want display events that match these terms. You can use basic boolean search operators too.', 'google-calendar-events' ), |
||
| 191 | 'placeholder' => __( 'Filter events to display by search terms...', 'google-calendar-events' ), |
||
| 192 | ), |
||
| 193 | '_google_events_recurring' => array( |
||
| 194 | 'type' => 'select', |
||
| 195 | 'name' => '_google_events_recurring', |
||
| 196 | 'id' => '_google_events_recurring', |
||
| 197 | 'title' => __( 'Recurring Events', 'google-calendar-events' ), |
||
| 198 | 'tooltip' => __( 'Events that are programmed to repeat themselves periodically.', 'google-calendar-events' ), |
||
| 199 | 'options' => array( |
||
| 200 | 'show' => __( 'Show all', 'google-calendar-events' ), |
||
| 201 | 'first-only' => __( 'Only show first occurrence', 'google-calendar-events' ), |
||
| 202 | ), |
||
| 203 | ), |
||
| 204 | '_google_events_max_results' => array( |
||
| 205 | 'type' => 'standard', |
||
| 206 | 'subtype' => 'number', |
||
| 207 | 'name' => '_google_events_max_results', |
||
| 208 | 'id' => '_google_events_max_results', |
||
| 209 | 'title' => __( 'Maximum Events', 'google-calendar-events' ), |
||
| 210 | 'tooltip' => __( 'Google Calendar only allows to query for a maximum amount of 2500 events from a calendar each time.', 'google-calendar-events' ), |
||
| 211 | 'class' => array( |
||
| 212 | 'simcal-field-small', |
||
| 213 | ), |
||
| 214 | 'default' => '2500', |
||
| 215 | 'attributes' => array( |
||
| 216 | 'min' => '0', |
||
| 217 | 'max' => '2500', |
||
| 218 | ), |
||
| 219 | ), |
||
| 220 | ), |
||
| 221 | ); |
||
| 222 | |||
| 223 | ?> |
||
| 224 | <div id="google-settings-panel" class="simcal-panel"> |
||
| 225 | <table> |
||
| 226 | <thead> |
||
| 227 | <tr><th colspan="2"><?php _e( 'Google Calendar Settings', 'google-calendar-events' ); ?></th></tr> |
||
| 228 | </thead> |
||
| 229 | <?php Settings::print_panel_fields( $inputs, $post_id ); ?> |
||
| 230 | </table> |
||
| 231 | </div> |
||
| 232 | <?php |
||
| 233 | |||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Test a connection to Google Calendar API. |
||
| 238 | * |
||
| 239 | * @since 3.0.0 |
||
| 240 | * |
||
| 241 | * @param string $google_calendar_id |
||
| 242 | * |
||
| 243 | * @return true|string |
||
| 244 | */ |
||
| 245 | public function test_api_key_connection( $google_calendar_id ) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Process meta fields. |
||
| 333 | * |
||
| 334 | * @since 3.0.0 |
||
| 335 | * |
||
| 336 | * @param int $post_id |
||
| 337 | */ |
||
| 338 | public function process_meta( $post_id ) { |
||
| 354 | |||
| 355 | } |
||
| 356 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.