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 |
||
13 | class WC_Payment_Gateway_CC extends WC_Payment_Gateway { |
||
14 | |||
15 | /** |
||
16 | * Builds our payment fields area - including tokenization fields for logged |
||
17 | * in users, and the actual payment fields. |
||
18 | * @since 2.6.0 |
||
19 | */ |
||
20 | View Code Duplication | public function payment_fields() { |
|
|
|||
21 | if ( $this->supports( 'tokenization' ) && is_checkout() && is_user_logged_in() ) { |
||
22 | $this->tokenization_script(); |
||
23 | $this->saved_payment_methods(); |
||
24 | $this->form(); |
||
25 | $this->save_payment_method_checkbox(); |
||
26 | } else { |
||
27 | $this->form(); |
||
28 | } |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Output field name HTML |
||
33 | * |
||
34 | * Gateways which support tokenization do not require names - we don't want the data to post to the server. |
||
35 | * |
||
36 | * @since 2.6.0 |
||
37 | * @param string $name |
||
38 | * @return string |
||
39 | */ |
||
40 | public function field_name( $name ) { |
||
43 | |||
44 | /** |
||
45 | * Outputs fields for entering credit card information. |
||
46 | * @since 2.6.0 |
||
47 | */ |
||
48 | public function form() { |
||
92 | |||
93 | } |
||
94 |
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.