Conditions | 12 |
Paths | 56 |
Total Lines | 162 |
Code Lines | 57 |
Lines | 9 |
Ratio | 5.56 % |
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 |
||
276 | public function get_main_chart() { |
||
277 | global $wp_locale; |
||
278 | |||
279 | if ( empty( $this->show_categories ) ) { |
||
280 | ?> |
||
281 | <div class="chart-container"> |
||
282 | <p class="chart-prompt"><?php _e( '← Choose a category to view stats', 'woocommerce' ); ?></p> |
||
283 | </div> |
||
284 | <?php |
||
285 | } else { |
||
286 | $chart_data = array(); |
||
287 | $index = 0; |
||
288 | |||
289 | foreach ( $this->show_categories as $category ) { |
||
290 | |||
291 | $category = get_term( $category, 'product_cat' ); |
||
292 | $product_ids = $this->get_products_in_category( $category->term_id ); |
||
293 | $category_chart_data = array(); |
||
294 | |||
295 | for ( $i = 0; $i <= $this->chart_interval; $i ++ ) { |
||
296 | |||
297 | $interval_total = 0; |
||
298 | |||
299 | View Code Duplication | switch ( $this->chart_groupby ) { |
|
300 | case 'day' : |
||
301 | $time = strtotime( date( 'Ymd', strtotime( "+{$i} DAY", $this->start_date ) ) ) * 1000; |
||
302 | break; |
||
303 | case 'month' : |
||
304 | default : |
||
305 | $time = strtotime( date( 'Ym', strtotime( "+{$i} MONTH", $this->start_date ) ) . '01' ) * 1000; |
||
306 | break; |
||
307 | } |
||
308 | |||
309 | foreach ( $product_ids as $id ) { |
||
310 | |||
311 | if ( isset( $this->item_sales_and_times[ $time ][ $id ] ) ) { |
||
312 | $interval_total += $this->item_sales_and_times[ $time ][ $id ]; |
||
313 | } |
||
314 | } |
||
315 | |||
316 | $category_chart_data[] = array( $time, (float) wc_format_decimal( $interval_total, wc_get_price_decimals() ) ); |
||
317 | } |
||
318 | |||
319 | $chart_data[ $category->term_id ]['category'] = $category->name; |
||
320 | $chart_data[ $category->term_id ]['data'] = $category_chart_data; |
||
321 | |||
322 | $index++; |
||
323 | } |
||
324 | ?> |
||
325 | <div class="chart-container"> |
||
326 | <div class="chart-placeholder main"></div> |
||
327 | </div> |
||
328 | <script type="text/javascript"> |
||
329 | var main_chart; |
||
330 | |||
331 | jQuery(function(){ |
||
332 | var drawGraph = function( highlight ) { |
||
333 | var series = [ |
||
334 | <?php |
||
335 | $index = 0; |
||
336 | foreach ( $chart_data as $data ) { |
||
337 | $color = isset( $this->chart_colours[ $index ] ) ? $this->chart_colours[ $index ] : $this->chart_colours[0]; |
||
338 | $width = $this->barwidth / sizeof( $chart_data ); |
||
339 | $offset = ( $width * $index ); |
||
340 | $series = $data['data']; |
||
341 | foreach ( $series as $key => $series_data ) { |
||
342 | $series[ $key ][0] = $series_data[0] + $offset; |
||
343 | } |
||
344 | echo '{ |
||
345 | label: "' . esc_js( $data['category'] ) . '", |
||
346 | data: jQuery.parseJSON( "' . json_encode( $series ) . '" ), |
||
347 | color: "' . $color . '", |
||
348 | bars: { |
||
349 | fillColor: "' . $color . '", |
||
350 | fill: true, |
||
351 | show: true, |
||
352 | lineWidth: 1, |
||
353 | align: "center", |
||
354 | barWidth: ' . $width * 0.75 . ', |
||
355 | stack: false |
||
356 | }, |
||
357 | ' . $this->get_currency_tooltip() . ', |
||
358 | enable_tooltip: true, |
||
359 | prepend_label: true |
||
360 | },'; |
||
361 | $index++; |
||
362 | } |
||
363 | ?> |
||
364 | ]; |
||
365 | |||
366 | if ( highlight !== 'undefined' && series[ highlight ] ) { |
||
367 | highlight_series = series[ highlight ]; |
||
368 | |||
369 | highlight_series.color = '#9c5d90'; |
||
370 | |||
371 | if ( highlight_series.bars ) { |
||
372 | highlight_series.bars.fillColor = '#9c5d90'; |
||
373 | } |
||
374 | |||
375 | if ( highlight_series.lines ) { |
||
376 | highlight_series.lines.lineWidth = 5; |
||
377 | } |
||
378 | } |
||
379 | |||
380 | main_chart = jQuery.plot( |
||
381 | jQuery('.chart-placeholder.main'), |
||
382 | series, |
||
383 | { |
||
384 | legend: { |
||
385 | show: false |
||
386 | }, |
||
387 | grid: { |
||
388 | color: '#aaa', |
||
389 | borderColor: 'transparent', |
||
390 | borderWidth: 0, |
||
391 | hoverable: true |
||
392 | }, |
||
393 | xaxes: [ { |
||
394 | color: '#aaa', |
||
395 | reserveSpace: true, |
||
396 | position: "bottom", |
||
397 | tickColor: 'transparent', |
||
398 | mode: "time", |
||
399 | timeformat: "<?php echo ( $this->chart_groupby == 'day' ) ? '%d %b' : '%b'; ?>", |
||
400 | monthNames: <?php echo json_encode( array_values( $wp_locale->month_abbrev ) ); ?>, |
||
401 | tickLength: 1, |
||
402 | minTickSize: [1, "<?php echo $this->chart_groupby; ?>"], |
||
403 | tickSize: [1, "<?php echo $this->chart_groupby; ?>"], |
||
404 | font: { |
||
405 | color: "#aaa" |
||
406 | } |
||
407 | } ], |
||
408 | yaxes: [ |
||
409 | { |
||
410 | min: 0, |
||
411 | tickDecimals: 2, |
||
412 | color: 'transparent', |
||
413 | font: { color: "#aaa" } |
||
414 | } |
||
415 | ], |
||
416 | } |
||
417 | ); |
||
418 | |||
419 | jQuery('.chart-placeholder').resize(); |
||
420 | |||
421 | } |
||
422 | |||
423 | drawGraph(); |
||
424 | |||
425 | jQuery('.highlight_series').hover( |
||
426 | function() { |
||
427 | drawGraph( jQuery(this).data('series') ); |
||
428 | }, |
||
429 | function() { |
||
430 | drawGraph(); |
||
431 | } |
||
432 | ); |
||
433 | }); |
||
434 | </script> |
||
435 | <?php |
||
436 | } |
||
437 | } |
||
438 | } |
||
439 |
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.