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_Donation_Receipt_Email extends Give_Email_Notification { |
||
| 27 | /* @var Give_Payment $payment */ |
||
| 28 | public $payment; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Create a class instance. |
||
| 32 | * |
||
| 33 | * @access public |
||
| 34 | * @since 2.0 |
||
| 35 | */ |
||
| 36 | public function init() { |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * Get email subject. |
||
| 58 | * |
||
| 59 | * @since 2.0 |
||
| 60 | * @access public |
||
| 61 | * |
||
| 62 | * @param int $form_id |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | View Code Duplication | public function get_email_subject( $form_id = null ) { |
|
|
|
|||
| 66 | $subject = wp_strip_all_tags( |
||
| 67 | Give_Email_Notification_Util::get_value( |
||
| 68 | $this, |
||
| 69 | Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_subject', |
||
| 70 | $form_id, |
||
| 71 | $this->config['default_email_subject'] |
||
| 72 | ) |
||
| 73 | ); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Filters the donation email receipt subject. |
||
| 77 | * Note: This filter will deprecate soon. |
||
| 78 | * |
||
| 79 | * @since 1.0 |
||
| 80 | */ |
||
| 81 | $subject = apply_filters( |
||
| 82 | 'give_donation_subject', |
||
| 83 | $subject, |
||
| 84 | $this->payment->ID |
||
| 85 | ); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Filters the donation email receipt subject. |
||
| 89 | * |
||
| 90 | * @since 2.0 |
||
| 91 | */ |
||
| 92 | $subject = apply_filters( |
||
| 93 | "give_{$this->config['id']}_get_email_subject", |
||
| 94 | $subject, |
||
| 95 | $this, |
||
| 96 | $form_id |
||
| 97 | ); |
||
| 98 | |||
| 99 | return $subject; |
||
| 100 | } |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Get email message. |
||
| 105 | * |
||
| 106 | * @since 2.0 |
||
| 107 | * @access public |
||
| 108 | * |
||
| 109 | * @param int $form_id |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function get_email_message( $form_id = null ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get the recipient attachments. |
||
| 163 | * |
||
| 164 | * @since 2.0 |
||
| 165 | * @access public |
||
| 166 | * |
||
| 167 | * @param int $form_id |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | View Code Duplication | public function get_email_attachments( $form_id = null) { |
|
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * Set email data. |
||
| 202 | * |
||
| 203 | * @since 2.0 |
||
| 204 | */ |
||
| 205 | View Code Duplication | public function setup_email_data() { |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Send donation receipt |
||
| 263 | * |
||
| 264 | * @since 2.0 |
||
| 265 | * @access public |
||
| 266 | * |
||
| 267 | * @param $payment_id |
||
| 268 | */ |
||
| 269 | View Code Duplication | public function send_donation_receipt( $payment_id ) { |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Resend payment receipt by row action. |
||
| 283 | * |
||
| 284 | * @since 2.0 |
||
| 285 | * @access public |
||
| 286 | * |
||
| 287 | * @param array $data |
||
| 288 | */ |
||
| 289 | public function resend_donation_receipt( $data ) { |
||
| 320 | } |
||
| 321 | |||
| 325 |
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.