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 |
||
| 28 | class Give_New_Donation_Email extends Give_Email_Notification { |
||
| 29 | /* @var Give_Payment $payment */ |
||
| 30 | public $payment; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Create a class instance. |
||
| 34 | * |
||
| 35 | * @access public |
||
| 36 | * @since 2.0 |
||
| 37 | */ |
||
| 38 | public function init() { |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Get email subject. |
||
| 60 | * |
||
| 61 | * @since 2.0 |
||
| 62 | * @access public |
||
| 63 | * |
||
| 64 | * @param int $form_id |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | View Code Duplication | public function get_email_subject( $form_id = null ) { |
|
|
|
|||
| 69 | $subject = wp_strip_all_tags( |
||
| 70 | Give_Email_Notification_Util::get_value( |
||
| 71 | $this, |
||
| 72 | Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_subject', |
||
| 73 | $form_id, |
||
| 74 | $this->config['default_email_subject'] |
||
| 75 | ) |
||
| 76 | ); |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Filters the donation notification subject. |
||
| 80 | * Note: This filter will deprecate soon. |
||
| 81 | * |
||
| 82 | * @since 1.0 |
||
| 83 | */ |
||
| 84 | $subject = apply_filters( 'give_admin_donation_notification_subject', $subject, $this->payment->ID ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Filters the donation notification subject. |
||
| 88 | * |
||
| 89 | * @since 2.0 |
||
| 90 | */ |
||
| 91 | $subject = apply_filters( "give_{$this->config['id']}_get_email_subject", $subject, $this, $form_id ); |
||
| 92 | |||
| 93 | return $subject; |
||
| 94 | } |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * Get email attachment. |
||
| 99 | * |
||
| 100 | * @since 2.0 |
||
| 101 | * @access public |
||
| 102 | * |
||
| 103 | * @param int $form_id |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | View Code Duplication | public function get_email_message( $form_id = null ) { |
|
| 108 | $message = Give_Email_Notification_Util::get_value( |
||
| 109 | $this, |
||
| 110 | Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_message', |
||
| 111 | $form_id, |
||
| 112 | $this->config['default_email_message'] |
||
| 113 | ); |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Filter the email message |
||
| 117 | * Note: This filter will deprecate soon. |
||
| 118 | * |
||
| 119 | * @since 1.0 |
||
| 120 | */ |
||
| 121 | $message = apply_filters( |
||
| 122 | 'give_donation_notification', |
||
| 123 | $message, |
||
| 124 | $this->payment->ID, |
||
| 125 | $this->payment->payment_meta |
||
| 126 | ); |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Filter the email message |
||
| 130 | * |
||
| 131 | * @since 2.0 |
||
| 132 | */ |
||
| 133 | $message = apply_filters( |
||
| 134 | "give_{$this->config['id']}_get_default_email_message", |
||
| 135 | $message, |
||
| 136 | $this, |
||
| 137 | $form_id |
||
| 138 | ); |
||
| 139 | |||
| 140 | return $message; |
||
| 141 | } |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Get email attachment. |
||
| 146 | * |
||
| 147 | * @since 2.0 |
||
| 148 | * @access public |
||
| 149 | * |
||
| 150 | * @param int $form_id |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | View Code Duplication | public function get_email_attachments( $form_id = null ) { |
|
| 154 | /** |
||
| 155 | * Filters the donation notification email attachments. |
||
| 156 | * By default, there is no attachment but plugins can hook in to provide one more multiple. |
||
| 157 | * Note: This filter will deprecate soon. |
||
| 158 | * |
||
| 159 | * @since 1.0 |
||
| 160 | */ |
||
| 161 | $attachments = apply_filters( |
||
| 162 | 'give_admin_donation_notification_attachments', |
||
| 163 | array(), |
||
| 164 | $this->payment->ID, |
||
| 165 | $this->payment->payment_meta |
||
| 166 | ); |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Filters the donation notification email attachments. |
||
| 170 | * By default, there is no attachment but plugins can hook in to provide one more multiple. |
||
| 171 | * |
||
| 172 | * @since 2.0 |
||
| 173 | */ |
||
| 174 | $attachments = apply_filters( |
||
| 175 | "give_{$this->config['id']}_get_email_attachments", |
||
| 176 | $attachments, |
||
| 177 | $this, |
||
| 178 | $form_id |
||
| 179 | ); |
||
| 180 | |||
| 181 | return $attachments; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Set email data |
||
| 186 | * |
||
| 187 | * @since 2.0 |
||
| 188 | */ |
||
| 189 | public function setup_email_data() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Setup email notification. |
||
| 236 | * |
||
| 237 | * @since 2.0 |
||
| 238 | * @access public |
||
| 239 | * |
||
| 240 | * @param int $payment_id |
||
| 241 | */ |
||
| 242 | public function setup_email_notification( $payment_id ) { |
||
| 253 | } |
||
| 254 | |||
| 255 | endif; // End class_exists check |
||
| 256 | |||
| 258 |
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.