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 |
||
16 | class WC_Shipping_Free_Shipping extends WC_Shipping_Method { |
||
17 | |||
18 | /** |
||
19 | * Min amount to be valid. |
||
20 | * |
||
21 | * @var integer |
||
22 | */ |
||
23 | public $min_amount = 0; |
||
24 | |||
25 | /** |
||
26 | * Requires option. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | public $requires = ''; |
||
31 | |||
32 | /** |
||
33 | * Constructor. |
||
34 | * |
||
35 | * @param int $instance_id Shipping method instance. |
||
36 | */ |
||
37 | View Code Duplication | public function __construct( $instance_id = 0 ) { |
|
50 | |||
51 | /** |
||
52 | * Initialize free shipping. |
||
53 | */ |
||
54 | View Code Duplication | public function init() { |
|
67 | |||
68 | /** |
||
69 | * Init form fields. |
||
70 | */ |
||
71 | public function init_form_fields() { |
||
103 | |||
104 | /** |
||
105 | * Get setting form fields for instances of this shipping method within zones. |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public function get_instance_form_fields() { |
||
110 | wc_enqueue_js( " |
||
111 | jQuery( function( $ ) { |
||
112 | function wcFreeShippingShowHideMinAmountField( el ) { |
||
113 | var form = $( el ).closest( 'form' ); |
||
114 | var minAmountField = $( '#woocommerce_free_shipping_min_amount', form ).closest( 'tr' ); |
||
115 | if ( 'coupon' === $( el ).val() || '' === $( el ).val() ) { |
||
116 | minAmountField.hide(); |
||
117 | } else { |
||
118 | minAmountField.show(); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | $( document.body ).on( 'change', '#woocommerce_free_shipping_requires', function() { |
||
123 | wcFreeShippingShowHideMinAmountField( this ); |
||
124 | }); |
||
125 | |||
126 | // Change while load. |
||
127 | $( '#woocommerce_free_shipping_requires' ).change(); |
||
128 | $( document.body ).on( 'wc_backbone_modal_loaded', function( evt, target ) { |
||
129 | if ( 'wc-modal-shipping-method-settings' === target ) { |
||
130 | wcFreeShippingShowHideMinAmountField( $( '#wc-backbone-modal-dialog #woocommerce_free_shipping_requires', evt.currentTarget ) ); |
||
131 | } |
||
132 | } ); |
||
133 | }); |
||
134 | " ); |
||
135 | |||
136 | return parent::get_instance_form_fields(); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * See if free shipping is available based on the package and cart. |
||
141 | * |
||
142 | * @param array $package Shipping package. |
||
143 | * @return bool |
||
144 | */ |
||
145 | public function is_available( $package ) { |
||
194 | |||
195 | /** |
||
196 | * Called to calculate shipping rates for this method. Rates can be added using the add_rate() method. |
||
197 | * |
||
198 | * @uses WC_Shipping_Method::add_rate() |
||
199 | * |
||
200 | * @param array $package Shipping package. |
||
201 | */ |
||
202 | public function calculate_shipping( $package = array() ) { |
||
210 | } |
||
211 |
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.