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 |
||
| 26 | class Give_Email_Access_Email extends Give_Email_Notification { |
||
| 27 | /** |
||
| 28 | * Create a class instance. |
||
| 29 | * |
||
| 30 | * @access public |
||
| 31 | * @since 2.0 |
||
| 32 | */ |
||
| 33 | public function init() { |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * Get email subject. |
||
| 55 | * |
||
| 56 | * @since 2.0 |
||
| 57 | * @access public |
||
| 58 | * |
||
| 59 | * @param int $form_id |
||
| 60 | * |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function get_email_subject( $form_id = null ) { |
|
|
|
|||
| 64 | $subject = wp_strip_all_tags( |
||
| 65 | Give_Email_Notification_Util::get_value( |
||
| 66 | $this, |
||
| 67 | Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_subject', |
||
| 68 | $form_id, |
||
| 69 | $this->config['default_email_subject'] |
||
| 70 | ) |
||
| 71 | ); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Filters the donation notification subject. |
||
| 75 | * Note: This filter will deprecate soon. |
||
| 76 | * |
||
| 77 | * @since 1.0 |
||
| 78 | */ |
||
| 79 | $subject = apply_filters( 'give_email_access_token_subject', $subject ); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Filters the donation notification subject. |
||
| 83 | * |
||
| 84 | * @since 2.0 |
||
| 85 | */ |
||
| 86 | $subject = apply_filters( "give_{$this->config['id']}_get_email_subject", $subject, $this, $form_id ); |
||
| 87 | |||
| 88 | return $subject; |
||
| 89 | } |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * Get email attachment. |
||
| 94 | * |
||
| 95 | * @since 2.0 |
||
| 96 | * @access public |
||
| 97 | * |
||
| 98 | * @param int $form_id |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | View Code Duplication | public function get_email_message( $form_id = null ) { |
|
| 103 | $message = Give_Email_Notification_Util::get_value( |
||
| 104 | $this, |
||
| 105 | Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_message', |
||
| 106 | $form_id, |
||
| 107 | $this->config['default_email_message'] |
||
| 108 | ); |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Filter the email message |
||
| 112 | * Note: This filter will deprecate soon. |
||
| 113 | * |
||
| 114 | * @since 1.0 |
||
| 115 | */ |
||
| 116 | $message = apply_filters( 'give_email_access_token_message', $message ); |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Filter the email message |
||
| 120 | * |
||
| 121 | * @since 2.0 |
||
| 122 | */ |
||
| 123 | $message = apply_filters( "give_{$this->config['id']}_get_default_email_message", $message, $this, $form_id ); |
||
| 124 | |||
| 125 | return $message; |
||
| 126 | } |
||
| 127 | |||
| 128 | |||
| 129 | /** |
||
| 130 | * Get email attachment. |
||
| 131 | * |
||
| 132 | * @since 2.0 |
||
| 133 | * @access public |
||
| 134 | * |
||
| 135 | * @param int $form_id |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | public function get_email_attachments( $form_id = null ) { |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * Get default email message. |
||
| 162 | * |
||
| 163 | * @since 2.0 |
||
| 164 | * @access public |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function get_default_email_message() { |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * Set email data |
||
| 188 | * |
||
| 189 | * @since 2.0 |
||
| 190 | */ |
||
| 191 | public function setup_email_data() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Setup email notification. |
||
| 224 | * |
||
| 225 | * @since 2.0 |
||
| 226 | * @access public |
||
| 227 | * |
||
| 228 | * @param int $donor_id |
||
| 229 | * @param string $email |
||
| 230 | */ |
||
| 231 | public function setup_email_notification( $donor_id, $email ) { |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * Set notification status |
||
| 250 | * |
||
| 251 | * @since 2.0 |
||
| 252 | * @access public |
||
| 253 | * |
||
| 254 | * @param $update_options |
||
| 255 | * @param $option_name |
||
| 256 | */ |
||
| 257 | public function set_notification_status( $update_options, $option_name ) { |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * email preview header. |
||
| 274 | * |
||
| 275 | * @since 2.0 |
||
| 276 | * @access public |
||
| 277 | * |
||
| 278 | * @param string $email_preview_header |
||
| 279 | * @param Give_Email_Access_Email $email |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function email_preview_header( $email_preview_header, $email ) { |
||
| 289 | } |
||
| 290 | |||
| 291 | endif; // End class_exists check |
||
| 294 |
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.