vindi/vindi-woocommerce

View on GitHub
src/utils/InterestPriceHandler.php

Summary

Maintainability
A
25 mins
Test Coverage

Function calculate_cost has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
Open

  public function calculate_cost($cart) {
    global $woocommerce;
        if (is_admin() && !is_ajax()) {
          return;
        }
Severity: Minor
Found in src/utils/InterestPriceHandler.php - About 25 mins to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Avoid unused local variables such as '$woocommerce'.
Open

    global $woocommerce;
Severity: Minor
Found in src/utils/InterestPriceHandler.php by phpmd

UnusedLocalVariable

Since: 0.2

Detects when a local variable is declared and/or assigned, but not used.

Example

class Foo {
    public function doSomething()
    {
        $i = 5; // Unused
    }
}

Source https://phpmd.org/rules/unusedcode.html#unusedlocalvariable

The method calculate_cost uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
Open

    } else {
      $post_data = $_POST;
    }
Severity: Minor
Found in src/utils/InterestPriceHandler.php by phpmd

ElseExpression

Since: 1.4.0

An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.

Example

class Foo
{
    public function bar($flag)
    {
        if ($flag) {
            // one branch
        } else {
            // another branch
        }
    }
}

Source https://phpmd.org/rules/cleancode.html#elseexpression

A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 10 and the first side effect is on line 6.
Open

<?php

Visibility must be declared on method "__construct"
Open

  function __construct() {

Opening brace of a class must be on the line after the definition
Open

class InterestPriceHandler {

Expected 0 spaces before closing bracket; 1 found
Open

    if (isset($_POST['post_data'] ) ) {

Spaces must be used to indent lines; tabs are not allowed
Open

    exit; // Exit if accessed directly

Expected 1 space after closing parenthesis; found 0
Open

    if (is_checkout()): ?>

Line indented incorrectly; expected at least 8 spaces, found 4
Open

    ) {

Line indented incorrectly; expected at least 8 spaces, found 6
Open

      $tax_total     = 0;

Expected 0 spaces before closing bracket; 1 found
Open

    if (isset($_POST['post_data'] ) ) {

Line indented incorrectly; expected 8 spaces, found 4
Open

    }

Line indented incorrectly; expected at least 8 spaces, found 6
Open

      $cart_total     = ($cart->get_cart_contents_total() + $cart->get_shipping_total() + $tax_total);

Line indented incorrectly; expected 8 spaces, found 4
Open

    if (is_checkout()): ?>

Line indented incorrectly; expected at least 8 spaces, found 6
Open

      WC()->cart->add_fee(__('Juros', VINDI), $interest_price);

Line indented incorrectly; expected 8 spaces, found 4
Open

    if (isset($_POST['post_data'] ) ) {

Line indented incorrectly; expected at least 8 spaces, found 4
Open

    add_action('wp_footer', array($this, 'add_installment_change_script'));

Line indented incorrectly; expected 8 spaces, found 4
Open

    if (isset($post_data['vindi_cc_installments']) &&

Line indented incorrectly; expected at least 12 spaces, found 10
Open

          return;

Line indented incorrectly; expected at least 8 spaces, found 6
Open

      $interest_rate = get_option('woocommerce_vindi-credit-card_settings', true)['interest_rate'];

Line indented incorrectly; expected 4 spaces, found 2
Open

  function __construct() {

Line indented incorrectly; expected 4 spaces, found 2
Open

  }

Line indented incorrectly; expected 8 spaces, found 4
Open

    } else {

Line indented incorrectly; expected at least 12 spaces, found 6
Open

      $post_data = $_POST;

Line indented incorrectly; expected 4 spaces, found 2
Open

  public function calculate_cost($cart) {

Line indented incorrectly; expected at least 12 spaces, found 6
Open

      parse_str(sanitize_text_field($_POST['post_data']), $post_data);

Line indented incorrectly; expected at least 8 spaces, found 6
Open

      $installments  = intval(filter_var($post_data['vindi_cc_installments'], FILTER_SANITIZE_NUMBER_INT));

Line indented incorrectly; expected at least 4 spaces, found 2
Open

  die();

Line indented incorrectly; expected at least 8 spaces, found 4
Open

    global $woocommerce;

Line indented incorrectly; expected at least 8 spaces, found 6
Open

      $interest_price = (float) $total_price - $cart_total;

Opening brace should be on a new line
Open

  function __construct() {

Line indented incorrectly; expected 4 spaces, found 2
Open

  public function add_installment_change_script() {

Line indented incorrectly; expected at least 8 spaces, found 4
Open

    add_action('woocommerce_cart_calculate_fees', array($this, 'calculate_cost'));

Line indented incorrectly; expected 4 spaces, found 2
Open

  }

Opening brace should be on a new line
Open

  public function calculate_cost($cart) {

Line indented incorrectly; expected at least 8 spaces, found 6
Open

      $total_price    = $cart_total * (1 + (($interest_rate / 100) * ($installments - 1)));

Line indented incorrectly; expected 4 spaces, found 2
Open

  }

Opening brace should be on a new line
Open

  public function add_installment_change_script() {

Line indented incorrectly; expected 8 spaces, found 4
Open

    }

Line indented incorrectly; expected at least 8 spaces, found 6
Open

      $taxes         = $cart->get_taxes();

There are no issues that match your filters.

Category
Status