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() { |
||
| 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 ) { |
|
| 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 | public function setup_email_data() { |
||
| 206 | // Set recipient email. |
||
| 207 | $this->recipient_email = $this->payment->email; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Filters the from name. |
||
| 211 | * |
||
| 212 | * @param int $payment_id Payment id. |
||
| 213 | * @param mixed $payment_data Payment meta data. |
||
| 214 | * |
||
| 215 | * @since 1.0 |
||
| 216 | */ |
||
| 217 | $from_name = apply_filters( |
||
| 218 | 'give_donation_from_name', |
||
| 219 | Give()->emails->get_from_name(), |
||
| 220 | $this->payment->ID, |
||
| 221 | $this->payment->payment_meta |
||
| 222 | ); |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Filters the from email. |
||
| 226 | * |
||
| 227 | * @param int $payment_id Payment id. |
||
| 228 | * @param mixed $payment_data Payment meta data. |
||
| 229 | * |
||
| 230 | * @since 1.0 |
||
| 231 | */ |
||
| 232 | $from_email = apply_filters( |
||
| 233 | 'give_donation_from_address', |
||
| 234 | Give()->emails->get_from_address(), |
||
| 235 | $this->payment->ID, |
||
| 236 | $this->payment->payment_meta |
||
| 237 | ); |
||
| 238 | |||
| 239 | Give()->emails->__set( 'from_name', $from_name ); |
||
| 240 | Give()->emails->__set( 'from_email', $from_email ); |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Filters the donation receipt's email headers. |
||
| 244 | * |
||
| 245 | * @param int $payment_id Payment id. |
||
| 246 | * @param mixed $payment_data Payment meta data. |
||
| 247 | * |
||
| 248 | * @since 1.0 |
||
| 249 | */ |
||
| 250 | $headers = apply_filters( |
||
| 251 | 'give_receipt_headers', |
||
| 252 | Give()->emails->get_headers(), |
||
| 253 | $this->payment->ID, |
||
| 254 | $this->payment->payment_meta |
||
| 255 | ); |
||
| 256 | |||
| 257 | Give()->emails->__set( 'headers', $headers ); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Send donation receipt |
||
| 262 | * |
||
| 263 | * @since 2.0 |
||
| 264 | * @access public |
||
| 265 | * |
||
| 266 | * @param $payment_id |
||
| 267 | */ |
||
| 268 | View Code Duplication | public function send_donation_receipt( $payment_id ) { |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Resend payment receipt by row action. |
||
| 282 | * |
||
| 283 | * @since 2.0 |
||
| 284 | * @access public |
||
| 285 | * |
||
| 286 | * @param array $data Donation details. |
||
| 287 | */ |
||
| 288 | public function resend_donation_receipt( $data ) { |
||
| 319 | } |
||
| 320 | |||
| 324 |
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.