Conditions | 19 |
Paths | 520 |
Total Lines | 168 |
Code Lines | 114 |
Lines | 20 |
Ratio | 11.9 % |
Changes | 0 |
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 |
||
168 | public function coupons_widget() { |
||
169 | ?> |
||
170 | <h4 class="section_title"><span><?php _e( 'Filter by coupon', 'woocommerce' ); ?></span></h4> |
||
171 | <div class="section"> |
||
172 | <form method="GET"> |
||
173 | <div> |
||
174 | <?php |
||
175 | $used_coupons = $this->get_order_report_data( array( |
||
176 | 'data' => array( |
||
177 | 'order_item_name' => array( |
||
178 | 'type' => 'order_item', |
||
179 | 'order_item_type' => 'coupon', |
||
180 | 'function' => '', |
||
181 | 'distinct' => true, |
||
182 | 'name' => 'order_item_name' |
||
183 | ) |
||
184 | ), |
||
185 | 'where' => array( |
||
186 | array( |
||
187 | 'key' => 'order_item_type', |
||
188 | 'value' => 'coupon', |
||
189 | 'operator' => '=' |
||
190 | ) |
||
191 | ), |
||
192 | 'query_type' => 'get_col', |
||
193 | 'filter_range' => false |
||
194 | ) ); |
||
195 | |||
196 | if ( ! empty( $used_coupons ) && is_array( $used_coupons ) ) : |
||
197 | ?> |
||
198 | <select id="coupon_codes" name="coupon_codes" class="wc-enhanced-select" data-placeholder="<?php esc_attr_e( 'Choose coupons…', 'woocommerce' ); ?>" style="width:100%;"> |
||
199 | <option value=""><?php _e( 'All coupons', 'woocommerce' ); ?></option> |
||
200 | <?php |
||
201 | foreach ( $used_coupons as $coupon ) { |
||
202 | echo '<option value="' . esc_attr( $coupon ) . '" ' . selected( in_array( $coupon, $this->coupon_codes ), true, false ) . '>' . $coupon . '</option>'; |
||
203 | } |
||
204 | ?> |
||
205 | </select> |
||
206 | <input type="submit" class="submit button" value="<?php esc_attr_e( 'Show', 'woocommerce' ); ?>" /> |
||
207 | <input type="hidden" name="range" value="<?php if ( ! empty( $_GET['range'] ) ) echo esc_attr( $_GET['range'] ); ?>" /> |
||
208 | <input type="hidden" name="start_date" value="<?php if ( ! empty( $_GET['start_date'] ) ) echo esc_attr( $_GET['start_date'] ); ?>" /> |
||
209 | <input type="hidden" name="end_date" value="<?php if ( ! empty( $_GET['end_date'] ) ) echo esc_attr( $_GET['end_date'] ); ?>" /> |
||
210 | <input type="hidden" name="page" value="<?php if ( ! empty( $_GET['page'] ) ) echo esc_attr( $_GET['page'] ); ?>" /> |
||
211 | <input type="hidden" name="tab" value="<?php if ( ! empty( $_GET['tab'] ) ) echo esc_attr( $_GET['tab'] ); ?>" /> |
||
212 | <input type="hidden" name="report" value="<?php if ( ! empty( $_GET['report'] ) ) echo esc_attr( $_GET['report'] ); ?>" /> |
||
213 | <?php else : ?> |
||
214 | <span><?php _e( 'No used coupons found', 'woocommerce' ); ?></span> |
||
215 | <?php endif; ?> |
||
216 | </div> |
||
217 | </form> |
||
218 | </div> |
||
219 | <h4 class="section_title"><span><?php _e( 'Most Popular', 'woocommerce' ); ?></span></h4> |
||
220 | <div class="section"> |
||
221 | <table cellspacing="0"> |
||
222 | <?php |
||
223 | $most_popular = $this->get_order_report_data( array( |
||
224 | 'data' => array( |
||
225 | 'order_item_name' => array( |
||
226 | 'type' => 'order_item', |
||
227 | 'order_item_type' => 'coupon', |
||
228 | 'function' => '', |
||
229 | 'name' => 'coupon_code' |
||
230 | ), |
||
231 | 'order_item_id' => array( |
||
232 | 'type' => 'order_item', |
||
233 | 'order_item_type' => 'coupon', |
||
234 | 'function' => 'COUNT', |
||
235 | 'name' => 'coupon_count' |
||
236 | ), |
||
237 | ), |
||
238 | 'where' => array( |
||
239 | array( |
||
240 | 'type' => 'order_item', |
||
241 | 'key' => 'order_item_type', |
||
242 | 'value' => 'coupon', |
||
243 | 'operator' => '=' |
||
244 | ) |
||
245 | ), |
||
246 | 'order_by' => 'coupon_count DESC', |
||
247 | 'group_by' => 'order_item_name', |
||
248 | 'limit' => 12, |
||
249 | 'query_type' => 'get_results', |
||
250 | 'filter_range' => true |
||
251 | ) ); |
||
252 | |||
253 | View Code Duplication | if ( ! empty( $most_popular ) && is_array( $most_popular ) ) { |
|
254 | foreach ( $most_popular as $coupon ) { |
||
255 | echo '<tr class="' . ( in_array( $coupon->coupon_code, $this->coupon_codes ) ? 'active' : '' ) . '"> |
||
256 | <td class="count" width="1%">' . $coupon->coupon_count . '</td> |
||
257 | <td class="name"><a href="' . esc_url( add_query_arg( 'coupon_codes', $coupon->coupon_code ) ) . '">' . $coupon->coupon_code . '</a></td> |
||
258 | </tr>'; |
||
259 | } |
||
260 | } else { |
||
261 | echo '<tr><td colspan="2">' . __( 'No coupons found in range', 'woocommerce' ) . '</td></tr>'; |
||
262 | } |
||
263 | ?> |
||
264 | </table> |
||
265 | </div> |
||
266 | <h4 class="section_title"><span><?php _e( 'Most Discount', 'woocommerce' ); ?></span></h4> |
||
267 | <div class="section"> |
||
268 | <table cellspacing="0"> |
||
269 | <?php |
||
270 | $most_discount = $this->get_order_report_data( array( |
||
271 | 'data' => array( |
||
272 | 'order_item_name' => array( |
||
273 | 'type' => 'order_item', |
||
274 | 'order_item_type' => 'coupon', |
||
275 | 'function' => '', |
||
276 | 'name' => 'coupon_code' |
||
277 | ), |
||
278 | 'discount_amount' => array( |
||
279 | 'type' => 'order_item_meta', |
||
280 | 'order_item_type' => 'coupon', |
||
281 | 'function' => 'SUM', |
||
282 | 'name' => 'discount_amount' |
||
283 | ) |
||
284 | ), |
||
285 | 'where' => array( |
||
286 | array( |
||
287 | 'type' => 'order_item', |
||
288 | 'key' => 'order_item_type', |
||
289 | 'value' => 'coupon', |
||
290 | 'operator' => '=' |
||
291 | ) |
||
292 | ), |
||
293 | 'order_by' => 'discount_amount DESC', |
||
294 | 'group_by' => 'order_item_name', |
||
295 | 'limit' => 12, |
||
296 | 'query_type' => 'get_results', |
||
297 | 'filter_range' => true |
||
298 | ) ); |
||
299 | |||
300 | View Code Duplication | if ( ! empty( $most_discount ) && is_array( $most_discount ) ) { |
|
301 | foreach ( $most_discount as $coupon ) { |
||
302 | echo '<tr class="' . ( in_array( $coupon->coupon_code, $this->coupon_codes ) ? 'active' : '' ) . '"> |
||
303 | <td class="count" width="1%">' . wc_price( $coupon->discount_amount ) . '</td> |
||
304 | <td class="name"><a href="' . esc_url( add_query_arg( 'coupon_codes', $coupon->coupon_code ) ) . '">' . $coupon->coupon_code . '</a></td> |
||
305 | </tr>'; |
||
306 | } |
||
307 | } else { |
||
308 | echo '<tr><td colspan="3">' . __( 'No coupons found in range', 'woocommerce' ) . '</td></tr>'; |
||
309 | } |
||
310 | ?> |
||
311 | </table> |
||
312 | </div> |
||
313 | <script type="text/javascript"> |
||
314 | jQuery('.section_title').click(function(){ |
||
315 | var next_section = jQuery(this).next('.section'); |
||
316 | |||
317 | if ( jQuery(next_section).is(':visible') ) |
||
318 | return false; |
||
319 | |||
320 | jQuery('.section:visible').slideUp(); |
||
321 | jQuery('.section_title').removeClass('open'); |
||
322 | jQuery(this).addClass('open').next('.section').slideDown(); |
||
323 | |||
324 | return false; |
||
325 | }); |
||
326 | jQuery('.section').slideUp( 100, function() { |
||
327 | <?php if ( empty( $this->coupon_codes ) ) : ?> |
||
328 | jQuery('.section_title:eq(1)').click(); |
||
329 | <?php else : ?> |
||
330 | jQuery('.section_title:eq(0)').click(); |
||
331 | <?php endif; ?> |
||
332 | }); |
||
333 | </script> |
||
334 | <?php |
||
335 | } |
||
336 | |||
555 |
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.