woothemes/woocommerce

View on GitHub
includes/rest-api/Controllers/Version3/class-wc-rest-posts-controller.php

Summary

Maintainability
F
4 days
Test Coverage

File class-wc-rest-posts-controller.php has 389 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
/**
 * Abstract Rest Posts Controller Class
 *
 * @class WC_REST_Posts_Controller

    Method get_collection_params has 87 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        public function get_collection_params() {
            $params = parent::get_collection_params();
    
            $params['context']['default'] = 'view';
    
    

      Method get_items has 70 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          public function get_items( $request ) {
              $args                         = array();
              $args['offset']               = $request['offset'];
              $args['order']                = $request['order'];
              $args['orderby']              = $request['orderby'];

        Function get_items has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
        Open

            public function get_items( $request ) {
                $args                         = array();
                $args['offset']               = $request['offset'];
                $args['order']                = $request['order'];
                $args['orderby']              = $request['orderby'];

        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 create_item has 32 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            public function create_item( $request ) {
                if ( ! empty( $request['id'] ) ) {
                    /* translators: %s: post type */
                    return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
                }

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

              public function update_item( $request ) {
                  $id   = (int) $request['id'];
                  $post = get_post( $id );
          
                  if ( ! empty( $post->post_type ) && 'product_variation' === $post->post_type && 'product' === $this->post_type ) {

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

                public function delete_item( $request ) {
                    $id    = (int) $request['id'];
                    $force = (bool) $request['force'];
                    $post  = get_post( $id );
            
            

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

                  public function update_item( $request ) {
                      $id   = (int) $request['id'];
                      $post = get_post( $id );
              
                      if ( ! empty( $post->post_type ) && 'product_variation' === $post->post_type && 'product' === $this->post_type ) {

              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 delete_item has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
              Open

                  public function delete_item( $request ) {
                      $id    = (int) $request['id'];
                      $force = (bool) $request['force'];
                      $post  = get_post( $id );
              
              

              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 get_allowed_query_vars has 27 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                  protected function get_allowed_query_vars() {
                      global $wp;
              
                      /**
                       * Filter the publicly allowed query vars.

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

                    public function create_item( $request ) {
                        if ( ! empty( $request['id'] ) ) {
                            /* translators: %s: post type */
                            return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
                        }

                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 $meta_fields;

                  Avoid too many return statements within this method.
                  Open

                          return rest_ensure_response( $response );

                    Avoid too many return statements within this method.
                    Open

                            return $response;

                      Avoid too many return statements within this method.
                      Open

                              return $response;

                        Avoid too many return statements within this method.
                        Open

                                    return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) );

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

                              protected function prepare_items_query( $prepared_args = array(), $request = null ) {
                          
                                  $valid_vars = array_flip( $this->get_allowed_query_vars() );
                                  $query_args = array();
                                  foreach ( $valid_vars as $var => $index ) {

                          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

                                  if ( ! empty( $post->post_type ) && 'product_variation' === $post->post_type && 'product' === $this->post_type ) {
                                      return new WP_Error( "woocommerce_rest_invalid_{$this->post_type}_id", __( 'To manipulate product variations you should use the /products/&lt;product_id&gt;/variations/&lt;id&gt; endpoint.', 'woocommerce' ), array( 'status' => 404 ) );
                                  } elseif ( empty( $id ) || empty( $post->ID ) || $post->post_type !== $this->post_type ) {
                                      return new WP_Error( "woocommerce_rest_invalid_{$this->post_type}_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
                                  }
                          includes/rest-api/Controllers/Version1/class-wc-rest-products-v1-controller.php on lines 1636..1640
                          includes/rest-api/Controllers/Version3/class-wc-rest-posts-controller.php on lines 257..261

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

                          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

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

                                  if ( ! empty( $post->post_type ) && 'product_variation' === $post->post_type && 'product' === $this->post_type ) {
                                      return new WP_Error( "woocommerce_rest_invalid_{$this->post_type}_id", __( 'To manipulate product variations you should use the /products/&lt;product_id&gt;/variations/&lt;id&gt; endpoint.', 'woocommerce' ), array( 'status' => 404 ) );
                                  } elseif ( empty( $id ) || empty( $post->ID ) || $post->post_type !== $this->post_type ) {
                                      return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
                                  }
                          includes/rest-api/Controllers/Version1/class-wc-rest-products-v1-controller.php on lines 1636..1640
                          includes/rest-api/Controllers/Version3/class-wc-rest-posts-controller.php on lines 150..154

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

                          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

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

                                  if ( isset( $post_type_obj->hierarchical ) && $post_type_obj->hierarchical ) {
                                      $params['parent'] = array(
                                          'description'       => __( 'Limit result set to those of particular parent IDs.', 'woocommerce' ),
                                          'type'              => 'array',
                                          'items'             => array(
                          includes/rest-api/Controllers/Version3/class-wc-rest-crud-controller.php on lines 611..630

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

                          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

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

                              public function delete_item_permissions_check( $request ) {
                                  $post = get_post( (int) $request['id'] );
                          
                                  if ( $post && ! wc_rest_check_post_permissions( $this->post_type, 'delete', $post->ID ) ) {
                                      return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
                          includes/rest-api/Controllers/Version3/class-wc-rest-posts-controller.php on lines 83..91
                          includes/rest-api/Controllers/Version3/class-wc-rest-posts-controller.php on lines 99..107

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

                          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

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

                              public function update_item_permissions_check( $request ) {
                                  $post = get_post( (int) $request['id'] );
                          
                                  if ( $post && ! wc_rest_check_post_permissions( $this->post_type, 'edit', $post->ID ) ) {
                                      return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
                          includes/rest-api/Controllers/Version3/class-wc-rest-posts-controller.php on lines 83..91
                          includes/rest-api/Controllers/Version3/class-wc-rest-posts-controller.php on lines 115..123

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

                          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

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

                              protected function prepare_links( $post, $request ) {
                                  $links = array(
                                      'self' => array(
                                          'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ),
                                      ),
                          includes/rest-api/Controllers/Version2/class-wc-rest-payment-gateways-v2-controller.php on lines 329..340
                          includes/rest-api/Controllers/Version2/class-wc-rest-shipping-methods-v2-controller.php on lines 173..184

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

                          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

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

                              public function get_item_permissions_check( $request ) {
                                  $post = get_post( (int) $request['id'] );
                          
                                  if ( $post && ! wc_rest_check_post_permissions( $this->post_type, 'read', $post->ID ) ) {
                                      return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
                          includes/rest-api/Controllers/Version3/class-wc-rest-posts-controller.php on lines 99..107
                          includes/rest-api/Controllers/Version3/class-wc-rest-posts-controller.php on lines 115..123

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

                          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