Conditions | 11 |
Paths | 136 |
Total Lines | 76 |
Code Lines | 32 |
Lines | 28 |
Ratio | 36.84 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
123 | public function calculate_shipping( $package = array() ) { |
||
124 | $rate = array( |
||
125 | 'id' => $this->id . $this->instance_id, |
||
126 | 'label' => $this->title, |
||
127 | 'cost' => 0, |
||
128 | ); |
||
129 | |||
130 | // Calculate the costs |
||
131 | $has_costs = false; // True when a cost is set. False if all costs are blank strings. |
||
132 | $cost = $this->get_option( 'cost' ); |
||
133 | |||
134 | View Code Duplication | if ( $cost !== '' ) { |
|
135 | $has_costs = true; |
||
136 | $rate['cost'] = $this->evaluate_cost( $cost, array( |
||
137 | 'qty' => $this->get_package_item_qty( $package ), |
||
138 | 'cost' => $package['contents_cost'] |
||
139 | ) ); |
||
140 | } |
||
141 | |||
142 | // Add shipping class costs |
||
143 | $found_shipping_classes = $this->find_shipping_classes( $package ); |
||
144 | $highest_class_cost = 0; |
||
145 | |||
146 | View Code Duplication | foreach ( $found_shipping_classes as $shipping_class => $products ) { |
|
147 | // Also handles BW compatibility when slugs were used instead of ids |
||
148 | $shipping_class_term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' ); |
||
149 | $class_cost_string = $shipping_class_term && $shipping_class_term->term_id ? $this->get_option( 'class_cost_' . $shipping_class_term->term_id, $this->get_option( 'class_cost_' . $shipping_class, '' ) ) : $this->get_option( 'no_class_cost', '' ); |
||
150 | |||
151 | if ( $class_cost_string === '' ) { |
||
152 | continue; |
||
153 | } |
||
154 | |||
155 | $has_costs = true; |
||
156 | $class_cost = $this->evaluate_cost( $class_cost_string, array( |
||
157 | 'qty' => array_sum( wp_list_pluck( $products, 'quantity' ) ), |
||
158 | 'cost' => array_sum( wp_list_pluck( $products, 'line_total' ) ) |
||
159 | ) ); |
||
160 | |||
161 | if ( $this->type === 'class' ) { |
||
162 | $rate['cost'] += $class_cost; |
||
163 | } else { |
||
164 | $highest_class_cost = $class_cost > $highest_class_cost ? $class_cost : $highest_class_cost; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | if ( $this->type === 'order' && $highest_class_cost ) { |
||
169 | $rate['cost'] += $highest_class_cost; |
||
170 | } |
||
171 | |||
172 | // Add the rate |
||
173 | if ( $has_costs ) { |
||
174 | $this->add_rate( $rate ); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Developers can add additional flat rates based on this one via this action since @version 2.4. |
||
179 | * |
||
180 | * Previously there were (overly complex) options to add additional rates however this was not user. |
||
181 | * friendly and goes against what Flat Rate Shipping was originally intended for. |
||
182 | * |
||
183 | * This example shows how you can add an extra rate based on this flat rate via custom function: |
||
184 | * |
||
185 | * add_action( 'woocommerce_flat_rate_shipping_add_rate', 'add_another_custom_flat_rate', 10, 2 ); |
||
186 | * |
||
187 | * function add_another_custom_flat_rate( $method, $rate ) { |
||
188 | * $new_rate = $rate; |
||
189 | * $new_rate['id'] .= ':' . 'custom_rate_name'; // Append a custom ID. |
||
190 | * $new_rate['label'] = 'Rushed Shipping'; // Rename to 'Rushed Shipping'. |
||
191 | * $new_rate['cost'] += 2; // Add $2 to the cost. |
||
192 | * |
||
193 | * // Add it to WC. |
||
194 | * $method->add_rate( $new_rate ); |
||
195 | * }. |
||
196 | */ |
||
197 | do_action( 'woocommerce_' . $this->id . '_shipping_add_rate', $this, $rate, $package ); |
||
198 | } |
||
199 | |||
238 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.