woocommerce/woocommerce

View on GitHub
includes/wc-formatting-functions.php

Summary

Maintainability
F
4 days
Test Coverage

File wc-formatting-functions.php has 712 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
/**
 * WooCommerce Formatting
 *
 * Functions for formatting data.
Severity: Major
Found in includes/wc-formatting-functions.php - About 1 day to fix

    Function wc_array_merge_recursive_numeric has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring.
    Open

    function wc_array_merge_recursive_numeric() {
        $arrays = func_get_args();
    
        // If there's only one array, it's already merged.
        if ( 1 === count( $arrays ) ) {
    Severity: Minor
    Found in includes/wc-formatting-functions.php - About 3 hrs 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

    Function wc_get_dimension has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
    Open

    function wc_get_dimension( $dimension, $to_unit, $from_unit = '' ) {
        $to_unit = strtolower( $to_unit );
    
        if ( empty( $from_unit ) ) {
            $from_unit = strtolower( get_option( 'woocommerce_dimension_unit' ) );
    Severity: Minor
    Found in includes/wc-formatting-functions.php - About 1 hr 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

    Function wc_timezone_string has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
    Open

    function wc_timezone_string() {
        // Added in WordPress 5.3 Ref https://developer.wordpress.org/reference/functions/wp_timezone_string/.
        if ( function_exists( 'wp_timezone_string' ) ) {
            return wp_timezone_string();
        }
    Severity: Minor
    Found in includes/wc-formatting-functions.php - About 1 hr 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

    Method wc_get_dimension has 35 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    function wc_get_dimension( $dimension, $to_unit, $from_unit = '' ) {
        $to_unit = strtolower( $to_unit );
    
        if ( empty( $from_unit ) ) {
            $from_unit = strtolower( get_option( 'woocommerce_dimension_unit' ) );
    Severity: Minor
    Found in includes/wc-formatting-functions.php - About 1 hr to fix

      Method wc_array_merge_recursive_numeric has 31 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

      function wc_array_merge_recursive_numeric() {
          $arrays = func_get_args();
      
          // If there's only one array, it's already merged.
          if ( 1 === count( $arrays ) ) {
      Severity: Minor
      Found in includes/wc-formatting-functions.php - About 1 hr to fix

        Method wc_get_weight has 30 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

        function wc_get_weight( $weight, $to_unit, $from_unit = '' ) {
            $weight  = (float) $weight;
            $to_unit = strtolower( $to_unit );
        
            if ( empty( $from_unit ) ) {
        Severity: Minor
        Found in includes/wc-formatting-functions.php - About 1 hr to fix

          Method wc_price has 29 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

          function wc_price( $price, $args = array() ) {
              $args = apply_filters(
                  'wc_price_args',
                  wp_parse_args(
                      $args,
          Severity: Minor
          Found in includes/wc-formatting-functions.php - About 1 hr to fix

            Method wc_format_postcode has 28 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

            function wc_format_postcode( $postcode, $country ) {
                $postcode = wc_normalize_postcode( $postcode );
            
                switch ( $country ) {
                    case 'CA':
            Severity: Minor
            Found in includes/wc-formatting-functions.php - About 1 hr to fix

              Function wc_get_weight has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
              Open

              function wc_get_weight( $weight, $to_unit, $from_unit = '' ) {
                  $weight  = (float) $weight;
                  $to_unit = strtolower( $to_unit );
              
                  if ( empty( $from_unit ) ) {
              Severity: Minor
              Found in includes/wc-formatting-functions.php - About 1 hr 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

              Function wc_array_overlay has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
              Open

              function wc_array_overlay( $a1, $a2 ) {
                  foreach ( $a1 as $k => $v ) {
                      if ( ! array_key_exists( $k, $a2 ) ) {
                          continue;
                      }
              Severity: Minor
              Found in includes/wc-formatting-functions.php - About 55 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

              Function wc_format_postcode has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
              Open

              function wc_format_postcode( $postcode, $country ) {
                  $postcode = wc_normalize_postcode( $postcode );
              
                  switch ( $country ) {
                      case 'CA':
              Severity: Minor
              Found in includes/wc-formatting-functions.php - About 45 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

              Function wc_make_numeric_postcode has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
              Open

                  function wc_make_numeric_postcode( $postcode ) {
                      $postcode           = str_replace( array( ' ', '-' ), '', $postcode );
                      $postcode_length    = strlen( $postcode );
                      $letters_to_numbers = array_merge( array( 0 ), range( 'A', 'Z' ) );
                      $letters_to_numbers = array_flip( $letters_to_numbers );
              Severity: Minor
              Found in includes/wc-formatting-functions.php - About 35 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 too many return statements within this method.
              Open

                              return $city['timezone_id'];
              Severity: Major
              Found in includes/wc-formatting-functions.php - About 30 mins to fix

                Avoid too many return statements within this method.
                Open

                    return 'UTC';
                Severity: Major
                Found in includes/wc-formatting-functions.php - About 30 mins to fix

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

                  function wc_format_stock_for_display( $product ) {
                      $display      = __( 'In stock', 'woocommerce' );
                      $stock_amount = $product->get_stock_quantity();
                  
                      switch ( get_option( 'woocommerce_stock_format' ) ) {
                  Severity: Minor
                  Found in includes/wc-formatting-functions.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

                  Similar blocks of code found in 2 locations. Consider refactoring.
                  Open

                      if ( 1 === preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(Z|((-|\+)\d{2}:\d{2}))$/', $time_string, $date_bits ) ) {
                          $offset    = ! empty( $date_bits[7] ) ? iso8601_timezone_to_offset( $date_bits[7] ) : wc_timezone_offset();
                          $timestamp = gmmktime( $date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1] ) - $offset;
                      } else {
                          $timestamp = wc_string_to_timestamp( get_gmt_from_date( gmdate( 'Y-m-d H:i:s', wc_string_to_timestamp( $time_string ) ) ) );
                  Severity: Major
                  Found in includes/wc-formatting-functions.php and 1 other location - About 3 hrs to fix
                  includes/abstracts/abstract-wc-data.php on lines 825..830

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 156.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  There are no issues that match your filters.

                  Category
                  Status