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 |
||
11 | class WC_Stripe_Email_Failed_Preorder_Authentication extends WC_Stripe_Email_Failed_Authentication { |
||
12 | /** |
||
13 | * Holds the message, which is entered by admins when sending the email. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $custom_message; |
||
18 | |||
19 | /** |
||
20 | * Constructor. |
||
21 | * |
||
22 | * @param WC_Email[] $email_classes All existing instances of WooCommerce emails. |
||
23 | */ |
||
24 | View Code Duplication | public function __construct( $email_classes = array() ) { |
|
44 | |||
45 | /** |
||
46 | * When autnentication is required, this adds another action to `wc_pre_orders_pre_order_completed` |
||
47 | * in order to send the authentication required email when the custom pre-orders message is available. |
||
48 | * |
||
49 | * @param WC_Order $order The order whose payment is failing. |
||
50 | */ |
||
51 | public function trigger( $order ) { |
||
60 | |||
61 | /** |
||
62 | * Triggers the email while also disconnecting the original Pre-Orders email. |
||
63 | * |
||
64 | * @param WC_Order $order The order that is being paid. |
||
65 | * @param string $message The message, which should be added to the email. |
||
66 | */ |
||
67 | public function send_email( $order, $message ) { |
||
77 | |||
78 | /** |
||
79 | * Returns the default subject of the email (modifyable in settings). |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | public function get_default_subject() { |
||
86 | |||
87 | /** |
||
88 | * Returns the default heading of the email (modifyable in settings). |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | public function get_default_heading() { |
||
95 | |||
96 | /** |
||
97 | * Returns the custom message, entered by the admin. |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | public function get_custom_message() { |
||
104 | } |
||
105 |
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.