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() { |
||
39 | // Initialize empty payment. |
||
40 | $this->payment = new Give_Payment( 0 ); |
||
41 | |||
42 | $this->load( array( |
||
43 | 'id' => 'new-donation', |
||
44 | 'label' => __( 'New Donation', 'give' ), |
||
45 | 'description' => __( 'Sent to designated recipient(s) when a new donation is received or a pending donation is marked as complete.', 'give' ), |
||
46 | 'has_recipient_field' => true, |
||
47 | 'notification_status' => 'enabled', |
||
48 | 'form_metabox_setting' => true, |
||
49 | 'default_email_subject' => esc_attr__( 'New Donation - #{payment_id}', 'give' ), |
||
50 | 'default_email_message' => ( false !== give_get_option( 'new-donation_email_message' ) ) ? give_get_option( 'new-donation_email_message' ) : give_get_default_donation_notification_email(), |
||
51 | 'default_email_header' => __( 'New Donation!', 'give' ), |
||
52 | ) ); |
||
53 | |||
54 | add_action( "give_{$this->config['id']}_email_notification", array( $this, 'setup_email_notification' ) ); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Get email subject. |
||
59 | * |
||
60 | * @since 2.0 |
||
61 | * @access public |
||
62 | * |
||
63 | * @param int $form_id |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | View Code Duplication | public function get_email_subject( $form_id = null ) { |
|
94 | |||
95 | |||
96 | /** |
||
97 | * Get email attachment. |
||
98 | * |
||
99 | * @since 2.0 |
||
100 | * @access public |
||
101 | * |
||
102 | * @param int $form_id |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | View Code Duplication | public function get_email_message( $form_id = null ) { |
|
141 | |||
142 | |||
143 | /** |
||
144 | * Get email attachment. |
||
145 | * |
||
146 | * @since 2.0 |
||
147 | * @access public |
||
148 | * |
||
149 | * @param int $form_id |
||
150 | * @return array |
||
151 | */ |
||
152 | View Code Duplication | public function get_email_attachments( $form_id = null ) { |
|
182 | |||
183 | /** |
||
184 | * Set email data |
||
185 | * |
||
186 | * @since 2.0 |
||
187 | */ |
||
188 | public function setup_email_data() { |
||
232 | |||
233 | /** |
||
234 | * Setup email notification. |
||
235 | * |
||
236 | * @since 2.0 |
||
237 | * @access public |
||
238 | * |
||
239 | * @param int $payment_id |
||
240 | */ |
||
241 | public function setup_email_notification( $payment_id ) { |
||
252 | } |
||
253 | |||
257 |
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.