woocommerce/woocommerce

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

Summary

Maintainability
F
1 wk
Test Coverage

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

<?php
/**
 * WooCommerce Core Functions
 *
 * General core functions available on both the front-end and admin.
Severity: Major
Found in includes/wc-core-functions.php - About 3 days to fix

    Method get_woocommerce_currencies has 174 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    function get_woocommerce_currencies() {
        static $currencies;
    
        if ( ! isset( $currencies ) ) {
            $currencies = array_unique(
    Severity: Major
    Found in includes/wc-core-functions.php - About 6 hrs to fix

      Method get_woocommerce_currency_symbols has 170 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

      function get_woocommerce_currency_symbols() {
      
          $symbols = apply_filters(
              'woocommerce_currency_symbols',
              array(
      Severity: Major
      Found in includes/wc-core-functions.php - About 6 hrs to fix

        Function wc_array_cartesian has a Cognitive Complexity of 26 (exceeds 5 allowed). Consider refactoring.
        Open

        function wc_array_cartesian( $input ) {
            $input   = array_filter( $input );
            $results = array();
            $indexes = array();
            $index   = 0;
        Severity: Minor
        Found in includes/wc-core-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_theme_support has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
        Open

        function wc_get_theme_support( $prop = '', $default = null ) {
            $theme_support = get_theme_support( 'woocommerce' );
            $theme_support = is_array( $theme_support ) ? $theme_support[0] : false;
        
            if ( ! $theme_support ) {
        Severity: Minor
        Found in includes/wc-core-functions.php - About 2 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

        Method wc_get_image_size has 57 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

        function wc_get_image_size( $image_size ) {
            $cache_key = 'size-' . ( is_array( $image_size ) ? implode( '-', $image_size ) : $image_size );
            $size      = wp_cache_get( $cache_key, 'woocommerce' );
        
            if ( $size ) {
        Severity: Major
        Found in includes/wc-core-functions.php - About 2 hrs to fix

          Function wc_postcode_location_matcher has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
          Open

          function wc_postcode_location_matcher( $postcode, $objects, $object_id_key, $object_compare_key, $country = '' ) {
              $postcode           = wc_normalize_postcode( $postcode );
              $wildcard_postcodes = array_map( 'wc_clean', wc_get_wildcard_postcodes( $postcode, $country ) );
              $matches            = array();
          
          
          Severity: Minor
          Found in includes/wc-core-functions.php - About 2 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_image_size has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
          Open

          function wc_get_image_size( $image_size ) {
              $cache_key = 'size-' . ( is_array( $image_size ) ? implode( '-', $image_size ) : $image_size );
              $size      = wp_cache_get( $cache_key, 'woocommerce' );
          
              if ( $size ) {
          Severity: Minor
          Found in includes/wc-core-functions.php - About 2 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_fix_rewrite_rules has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
          Open

          function wc_fix_rewrite_rules( $rules ) {
              global $wp_rewrite;
          
              $permalinks = wc_get_permalink_structure();
          
          
          Severity: Minor
          Found in includes/wc-core-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_create_order has 41 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

          function wc_create_order( $args = array() ) {
              $default_args = array(
                  'status'        => null,
                  'customer_id'   => null,
                  'customer_note' => null,
          Severity: Minor
          Found in includes/wc-core-functions.php - About 1 hr to fix

            Method wc_array_cartesian has 38 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

            function wc_array_cartesian( $input ) {
                $input   = array_filter( $input );
                $results = array();
                $indexes = array();
                $index   = 0;
            Severity: Minor
            Found in includes/wc-core-functions.php - About 1 hr to fix

              Method wc_get_template has 37 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

              function wc_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
                  $cache_key = sanitize_key( implode( '-', array( 'template', $template_name, $template_path, $default_path, Constants::get_constant( 'WC_VERSION' ) ) ) );
                  $template  = (string) wp_cache_get( $cache_key, 'woocommerce' );
              
                  if ( ! $template ) {
              Severity: Minor
              Found in includes/wc-core-functions.php - About 1 hr to fix

                Method wc_get_template_part has 32 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                function wc_get_template_part( $slug, $name = '' ) {
                    $cache_key = sanitize_key( implode( '-', array( 'template-part', $slug, $name, Constants::get_constant( 'WC_VERSION' ) ) ) );
                    $template  = (string) wp_cache_get( $cache_key, 'woocommerce' );
                
                    if ( ! $template ) {
                Severity: Minor
                Found in includes/wc-core-functions.php - About 1 hr to fix

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

                  function wc_locate_template( $template_name, $template_path = '', $default_path = '' ) {
                      if ( ! $template_path ) {
                          $template_path = WC()->template_path();
                      }
                  
                  
                  Severity: Minor
                  Found in includes/wc-core-functions.php - About 1 hr to fix

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

                    function wc_print_r( $expression, $return = false ) {
                        $alternatives = array(
                            array(
                                'func' => 'print_r',
                                'args' => array( $expression, true ),
                    Severity: Minor
                    Found in includes/wc-core-functions.php - About 1 hr to fix

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

                      function wc_postcode_location_matcher( $postcode, $objects, $object_id_key, $object_compare_key, $country = '' ) {
                          $postcode           = wc_normalize_postcode( $postcode );
                          $wildcard_postcodes = array_map( 'wc_clean', wc_get_wildcard_postcodes( $postcode, $country ) );
                          $matches            = array();
                      
                      
                      Severity: Minor
                      Found in includes/wc-core-functions.php - About 1 hr to fix

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

                        function wc_fix_rewrite_rules( $rules ) {
                            global $wp_rewrite;
                        
                            $permalinks = wc_get_permalink_structure();
                        
                        
                        Severity: Minor
                        Found in includes/wc-core-functions.php - About 1 hr to fix

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

                          function wc_list_pluck( $list, $callback_or_field, $index_key = null ) {
                              // Use wp_list_pluck if this isn't a callback.
                              $first_el = current( $list );
                              if ( ! is_object( $first_el ) || ! is_callable( array( $first_el, $callback_or_field ) ) ) {
                                  return wp_list_pluck( $list, $callback_or_field, $index_key );
                          Severity: Minor
                          Found in includes/wc-core-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_get_template_part has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
                          Open

                          function wc_get_template_part( $slug, $name = '' ) {
                              $cache_key = sanitize_key( implode( '-', array( 'template-part', $slug, $name, Constants::get_constant( 'WC_VERSION' ) ) ) );
                              $template  = (string) wp_cache_get( $cache_key, 'woocommerce' );
                          
                              if ( ! $template ) {
                          Severity: Minor
                          Found in includes/wc-core-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_theme_support has 26 lines of code (exceeds 25 allowed). Consider refactoring.
                          Open

                          function wc_get_theme_support( $prop = '', $default = null ) {
                              $theme_support = get_theme_support( 'woocommerce' );
                              $theme_support = is_array( $theme_support ) ? $theme_support[0] : false;
                          
                              if ( ! $theme_support ) {
                          Severity: Minor
                          Found in includes/wc-core-functions.php - About 1 hr to fix

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

                            function wc_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
                                $cache_key = sanitize_key( implode( '-', array( 'template', $template_name, $template_path, $default_path, Constants::get_constant( 'WC_VERSION' ) ) ) );
                                $template  = (string) wp_cache_get( $cache_key, 'woocommerce' );
                            
                                if ( ! $template ) {
                            Severity: Minor
                            Found in includes/wc-core-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_locate_template has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                            Open

                            function wc_locate_template( $template_name, $template_path = '', $default_path = '' ) {
                                if ( ! $template_path ) {
                                    $template_path = WC()->template_path();
                                }
                            
                            
                            Severity: Minor
                            Found in includes/wc-core-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_get_customer_default_location has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                            Open

                            function wc_get_customer_default_location() {
                                $set_default_location_to = get_option( 'woocommerce_default_customer_address', 'base' );
                                $default_location        = '' === $set_default_location_to ? '' : get_option( 'woocommerce_default_country', '' );
                                $location                = wc_format_country_state_string( apply_filters( 'woocommerce_customer_default_location', $default_location ) );
                            
                            
                            Severity: Minor
                            Found in includes/wc-core-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_get_shipping_method_count has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                            Open

                            function wc_get_shipping_method_count( $include_legacy = false, $enabled_only = false ) {
                                global $wpdb;
                            
                                $transient_name    = $include_legacy ? 'wc_shipping_method_count_legacy' : 'wc_shipping_method_count';
                                $transient_version = WC_Cache_Helper::get_transient_version( 'shipping' );
                            Severity: Minor
                            Found in includes/wc-core-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_create_order has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                            Open

                            function wc_create_order( $args = array() ) {
                                $default_args = array(
                                    'status'        => null,
                                    'customer_id'   => null,
                                    'customer_note' => null,
                            Severity: Minor
                            Found in includes/wc-core-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

                            Method wc_setcookie has 5 arguments (exceeds 4 allowed). Consider refactoring.
                            Open

                            function wc_setcookie( $name, $value, $expire = 0, $secure = false, $httponly = false ) {
                            Severity: Minor
                            Found in includes/wc-core-functions.php - About 35 mins to fix

                              Method wc_postcode_location_matcher has 5 arguments (exceeds 4 allowed). Consider refactoring.
                              Open

                              function wc_postcode_location_matcher( $postcode, $objects, $object_id_key, $object_compare_key, $country = '' ) {
                              Severity: Minor
                              Found in includes/wc-core-functions.php - About 35 mins to fix

                                Method wc_mail has 5 arguments (exceeds 4 allowed). Consider refactoring.
                                Open

                                function wc_mail( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = '' ) {
                                Severity: Minor
                                Found in includes/wc-core-functions.php - About 35 mins to fix

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

                                  function flush_rewrite_rules_on_shop_page_save() {
                                      $screen    = get_current_screen();
                                      $screen_id = $screen ? $screen->id : '';
                                  
                                      // Check if this is the edit page.
                                  Severity: Minor
                                  Found in includes/wc-core-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

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

                                  function wc_print_r( $expression, $return = false ) {
                                      $alternatives = array(
                                          array(
                                              'func' => 'print_r',
                                              'args' => array( $expression, true ),
                                  Severity: Minor
                                  Found in includes/wc-core-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

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

                                  function wc_transaction_query( $type = 'start', $force = false ) {
                                      global $wpdb;
                                  
                                      $wpdb->hide_errors();
                                  
                                  
                                  Severity: Minor
                                  Found in includes/wc-core-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 3 locations. Consider refactoring.
                                  Open

                                      $labels = apply_filters(
                                          'woocommerce_credit_card_type_labels',
                                          array(
                                              'mastercard'       => __( 'MasterCard', 'woocommerce' ),
                                              'visa'             => __( 'Visa', 'woocommerce' ),
                                  Severity: Major
                                  Found in includes/wc-core-functions.php and 2 other locations - About 1 hr to fix
                                  includes/customizer/class-wc-shop-customizer.php on lines 273..283
                                  includes/wc-template-functions.php on lines 1402..1412

                                  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 108.

                                  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