| Conditions | 3 |
| Paths | 4 |
| Total Lines | 192 |
| Code Lines | 73 |
| Lines | 11 |
| Ratio | 5.73 % |
| 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 |
||
| 363 | public function get_main_chart() { |
||
| 364 | global $wp_locale; |
||
| 365 | |||
| 366 | // Get orders and dates in range - we want the SUM of order totals, COUNT of order items, COUNT of orders, and the date |
||
| 367 | $order_coupon_counts_query = array( |
||
| 368 | 'data' => array( |
||
| 369 | 'order_item_name' => array( |
||
| 370 | 'type' => 'order_item', |
||
| 371 | 'order_item_type' => 'coupon', |
||
| 372 | 'function' => 'COUNT', |
||
| 373 | 'name' => 'order_coupon_count' |
||
| 374 | ), |
||
| 375 | 'post_date' => array( |
||
| 376 | 'type' => 'post_data', |
||
| 377 | 'function' => '', |
||
| 378 | 'name' => 'post_date' |
||
| 379 | ), |
||
| 380 | ), |
||
| 381 | 'where' => array( |
||
| 382 | array( |
||
| 383 | 'key' => 'order_item_type', |
||
| 384 | 'value' => 'coupon', |
||
| 385 | 'operator' => '=' |
||
| 386 | ) |
||
| 387 | ), |
||
| 388 | 'group_by' => $this->group_by_query, |
||
| 389 | 'order_by' => 'post_date ASC', |
||
| 390 | 'query_type' => 'get_results', |
||
| 391 | 'filter_range' => true, |
||
| 392 | 'order_types' => wc_get_order_types( 'order-count' ) |
||
| 393 | ); |
||
| 394 | |||
| 395 | $order_discount_amounts_query = array( |
||
| 396 | 'data' => array( |
||
| 397 | 'discount_amount' => array( |
||
| 398 | 'type' => 'order_item_meta', |
||
| 399 | 'order_item_type' => 'coupon', |
||
| 400 | 'function' => 'SUM', |
||
| 401 | 'name' => 'discount_amount' |
||
| 402 | ), |
||
| 403 | 'post_date' => array( |
||
| 404 | 'type' => 'post_data', |
||
| 405 | 'function' => '', |
||
| 406 | 'name' => 'post_date' |
||
| 407 | ), |
||
| 408 | ), |
||
| 409 | 'where' => array( |
||
| 410 | array( |
||
| 411 | 'key' => 'order_item_type', |
||
| 412 | 'value' => 'coupon', |
||
| 413 | 'operator' => '=' |
||
| 414 | ) |
||
| 415 | ), |
||
| 416 | 'group_by' => $this->group_by_query . ', order_item_name', |
||
| 417 | 'order_by' => 'post_date ASC', |
||
| 418 | 'query_type' => 'get_results', |
||
| 419 | 'filter_range' => true, |
||
| 420 | 'order_types' => wc_get_order_types( 'order-count' ) |
||
| 421 | ); |
||
| 422 | |||
| 423 | View Code Duplication | if ( ! empty( $this->coupon_codes ) ) { |
|
| 424 | $coupon_code_query = array( |
||
| 425 | 'type' => 'order_item', |
||
| 426 | 'key' => 'order_item_name', |
||
| 427 | 'value' => $this->coupon_codes, |
||
| 428 | 'operator' => 'IN' |
||
| 429 | ); |
||
| 430 | |||
| 431 | $order_coupon_counts_query['where'][] = $coupon_code_query; |
||
| 432 | $order_discount_amounts_query['where'][] = $coupon_code_query; |
||
| 433 | } |
||
| 434 | |||
| 435 | $order_coupon_counts = $this->get_order_report_data( $order_coupon_counts_query ); |
||
| 436 | $order_discount_amounts = $this->get_order_report_data( $order_discount_amounts_query ); |
||
| 437 | |||
| 438 | // Prepare data for report |
||
| 439 | $order_coupon_counts = $this->prepare_chart_data( $order_coupon_counts, 'post_date', 'order_coupon_count' , $this->chart_interval, $this->start_date, $this->chart_groupby ); |
||
| 440 | $order_discount_amounts = $this->prepare_chart_data( $order_discount_amounts, 'post_date', 'discount_amount', $this->chart_interval, $this->start_date, $this->chart_groupby ); |
||
| 441 | |||
| 442 | // Encode in json format |
||
| 443 | $chart_data = json_encode( array( |
||
| 444 | 'order_coupon_counts' => array_values( $order_coupon_counts ), |
||
| 445 | 'order_discount_amounts' => array_values( $order_discount_amounts ) |
||
| 446 | ) ); |
||
| 447 | ?> |
||
| 448 | <div class="chart-container"> |
||
| 449 | <div class="chart-placeholder main"></div> |
||
| 450 | </div> |
||
| 451 | <script type="text/javascript"> |
||
| 452 | var main_chart; |
||
| 453 | |||
| 454 | jQuery(function(){ |
||
| 455 | var order_data = jQuery.parseJSON( '<?php echo $chart_data; ?>' ); |
||
| 456 | |||
| 457 | var drawGraph = function( highlight ) { |
||
| 458 | var series = [ |
||
| 459 | { |
||
| 460 | label: "<?php echo esc_js( __( 'Number of coupons used', 'woocommerce' ) ) ?>", |
||
| 461 | data: order_data.order_coupon_counts, |
||
| 462 | color: '<?php echo $this->chart_colours['coupon_count' ]; ?>', |
||
| 463 | bars: { fillColor: '<?php echo $this->chart_colours['coupon_count' ]; ?>', fill: true, show: true, lineWidth: 0, barWidth: <?php echo $this->barwidth; ?> * 0.5, align: 'center' }, |
||
| 464 | shadowSize: 0, |
||
| 465 | hoverable: false |
||
| 466 | }, |
||
| 467 | { |
||
| 468 | label: "<?php echo esc_js( __( 'Discount amount', 'woocommerce' ) ) ?>", |
||
| 469 | data: order_data.order_discount_amounts, |
||
| 470 | yaxis: 2, |
||
| 471 | color: '<?php echo $this->chart_colours['discount_amount']; ?>', |
||
| 472 | points: { show: true, radius: 5, lineWidth: 3, fillColor: '#fff', fill: true }, |
||
| 473 | lines: { show: true, lineWidth: 4, fill: false }, |
||
| 474 | shadowSize: 0, |
||
| 475 | <?php echo $this->get_currency_tooltip(); ?> |
||
| 476 | } |
||
| 477 | ]; |
||
| 478 | |||
| 479 | if ( highlight !== 'undefined' && series[ highlight ] ) { |
||
| 480 | highlight_series = series[ highlight ]; |
||
| 481 | |||
| 482 | highlight_series.color = '#9c5d90'; |
||
| 483 | |||
| 484 | if ( highlight_series.bars ) |
||
| 485 | highlight_series.bars.fillColor = '#9c5d90'; |
||
| 486 | |||
| 487 | if ( highlight_series.lines ) { |
||
| 488 | highlight_series.lines.lineWidth = 5; |
||
| 489 | } |
||
| 490 | } |
||
| 491 | |||
| 492 | main_chart = jQuery.plot( |
||
| 493 | jQuery('.chart-placeholder.main'), |
||
| 494 | series, |
||
| 495 | { |
||
| 496 | legend: { |
||
| 497 | show: false |
||
| 498 | }, |
||
| 499 | grid: { |
||
| 500 | color: '#aaa', |
||
| 501 | borderColor: 'transparent', |
||
| 502 | borderWidth: 0, |
||
| 503 | hoverable: true |
||
| 504 | }, |
||
| 505 | xaxes: [ { |
||
| 506 | color: '#aaa', |
||
| 507 | position: "bottom", |
||
| 508 | tickColor: 'transparent', |
||
| 509 | mode: "time", |
||
| 510 | timeformat: "<?php if ( $this->chart_groupby == 'day' ) echo '%d %b'; else echo '%b'; ?>", |
||
| 511 | monthNames: <?php echo json_encode( array_values( $wp_locale->month_abbrev ) ) ?>, |
||
| 512 | tickLength: 1, |
||
| 513 | minTickSize: [1, "<?php echo $this->chart_groupby; ?>"], |
||
| 514 | font: { |
||
| 515 | color: "#aaa" |
||
| 516 | } |
||
| 517 | } ], |
||
| 518 | yaxes: [ |
||
| 519 | { |
||
| 520 | min: 0, |
||
| 521 | minTickSize: 1, |
||
| 522 | tickDecimals: 0, |
||
| 523 | color: '#ecf0f1', |
||
| 524 | font: { color: "#aaa" } |
||
| 525 | }, |
||
| 526 | { |
||
| 527 | position: "right", |
||
| 528 | min: 0, |
||
| 529 | tickDecimals: 2, |
||
| 530 | alignTicksWithAxis: 1, |
||
| 531 | color: 'transparent', |
||
| 532 | font: { color: "#aaa" } |
||
| 533 | } |
||
| 534 | ], |
||
| 535 | } |
||
| 536 | ); |
||
| 537 | |||
| 538 | jQuery('.chart-placeholder').resize(); |
||
| 539 | } |
||
| 540 | |||
| 541 | drawGraph(); |
||
| 542 | |||
| 543 | jQuery('.highlight_series').hover( |
||
| 544 | function() { |
||
| 545 | drawGraph( jQuery(this).data('series') ); |
||
| 546 | }, |
||
| 547 | function() { |
||
| 548 | drawGraph(); |
||
| 549 | } |
||
| 550 | ); |
||
| 551 | }); |
||
| 552 | </script> |
||
| 553 | <?php |
||
| 554 | } |
||
| 555 | } |
||
| 556 |
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.