woothemes/woocommerce

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

Summary

Maintainability
F
1 wk
Test Coverage

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

<?php
/**
 * REST API Product Reviews Controller
 *
 * Handles requests to /products/reviews.

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

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

      Method register_routes has 88 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          public function register_routes() {
              register_rest_route(
                  $this->namespace, '/' . $this->rest_base, array(
                      array(
                          'methods'             => WP_REST_Server::READABLE,

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

            public function get_item_schema() {
                $schema = array(
                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
                    'title'      => 'product_review',
                    'type'       => 'object',

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

              public function update_item( $request ) {
                  $review = $this->get_review( $request['id'] );
                  if ( is_wp_error( $review ) ) {
                      return $review;
                  }

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

              public function get_items( $request ) {
                  // Retrieve the list of registered collection query parameters.
                  $registered = $this->get_collection_params();
          
                  /*

            Method create_item has 74 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                public function create_item( $request ) {
                    if ( ! empty( $request['id'] ) ) {
                        return new WP_Error( 'woocommerce_rest_review_exists', __( 'Cannot create existing product review.', 'woocommerce' ), array( 'status' => 400 ) );
                    }
            
            

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

                  public function create_item( $request ) {
                      if ( ! empty( $request['id'] ) ) {
                          return new WP_Error( 'woocommerce_rest_review_exists', __( 'Cannot create existing product review.', 'woocommerce' ), 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

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

                  public function get_items( $request ) {
                      // Retrieve the list of registered collection query parameters.
                      $registered = $this->get_collection_params();
              
                      /*

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

                  public function update_item( $request ) {
                      $review = $this->get_review( $request['id'] );
                      if ( is_wp_error( $review ) ) {
                          return $review;
                      }

                WC_REST_Product_Reviews_Controller has 21 functions (exceeds 20 allowed). Consider refactoring.
                Open

                class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
                
                    /**
                     * Endpoint namespace.
                     *

                  Method prepare_item_for_response has 41 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                      public function prepare_item_for_response( $review, $request ) {
                          $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
                          $fields  = $this->get_fields_for_response( $request );
                          $data    = array();
                  
                  

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

                        public function delete_item( $request ) {
                            $review = $this->get_review( $request['id'] );
                            if ( is_wp_error( $review ) ) {
                                return $review;
                            }

                      Function prepare_item_for_response has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
                      Open

                          public function prepare_item_for_response( $review, $request ) {
                              $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
                              $fields  = $this->get_fields_for_response( $request );
                              $data    = array();
                      
                      

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

                          protected function prepare_item_for_database( $request ) {
                              if ( isset( $request['id'] ) ) {
                                  $prepared_review['comment_ID'] = (int) $request['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 handle_status_param has 31 lines of code (exceeds 25 allowed). Consider refactoring.
                      Open

                          protected function handle_status_param( $new_status, $id ) {
                              $old_status = wp_get_comment_status( $id );
                      
                              if ( $new_status === $old_status ) {
                                  return false;

                        Method prepare_item_for_database has 27 lines of code (exceeds 25 allowed). Consider refactoring.
                        Open

                            protected function prepare_item_for_database( $request ) {
                                if ( isset( $request['id'] ) ) {
                                    $prepared_review['comment_ID'] = (int) $request['id'];
                                }
                        
                        

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

                              protected function handle_status_param( $new_status, $id ) {
                                  $old_status = wp_get_comment_status( $id );
                          
                                  if ( $new_status === $old_status ) {
                                      return false;

                          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 8 (exceeds 5 allowed). Consider refactoring.
                          Open

                              public function delete_item( $request ) {
                                  $review = $this->get_review( $request['id'] );
                                  if ( is_wp_error( $review ) ) {
                                      return $review;
                                  }

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

                            Avoid too many return statements within this method.
                            Open

                                    return $response;

                              Avoid too many return statements within this method.
                              Open

                                          return $prepared_review;

                                Avoid too many return statements within this method.
                                Open

                                                return new WP_Error( 'woocommerce_rest_' . $error_code, __( 'Product review field exceeds maximum length allowed.', 'woocommerce' ), array( 'status' => 400 ) );

                                  Avoid too many return statements within this method.
                                  Open

                                                  return new WP_Error( 'woocommerce_rest_comment_failed_edit', __( 'Updating review failed.', 'woocommerce' ), array( 'status' => 500 ) );

                                    Avoid too many return statements within this method.
                                    Open

                                                return new WP_Error( 'woocommerce_rest_' . $error_code, __( 'Product review field exceeds maximum length allowed.', 'woocommerce' ), array( 'status' => 400 ) );

                                      Avoid too many return statements within this method.
                                      Open

                                                      return new WP_Error( 'woocommerce_rest_review_failed_edit', __( 'Updating review status failed.', 'woocommerce' ), array( 'status' => 500 ) );

                                        Avoid too many return statements within this method.
                                        Open

                                                        return $prepared_args;

                                          Avoid too many return statements within this method.
                                          Open

                                                      return $fields_update;

                                            Avoid too many return statements within this method.
                                            Open

                                                    return $response;

                                              Avoid too many return statements within this method.
                                              Open

                                                      return rest_ensure_response( $response );

                                                Avoid too many return statements within this method.
                                                Open

                                                                return new WP_Error( 'woocommerce_rest_' . $error_code, $error_message, array( 'status' => 409 ) );

                                                  Avoid too many return statements within this method.
                                                  Open

                                                              return $prepared_review['comment_approved'];

                                                    Avoid too many return statements within this method.
                                                    Open

                                                                return new WP_Error( 'woocommerce_rest_review_failed_create', __( 'Creating product review failed.', 'woocommerce' ), array( 'status' => 500 ) );

                                                      Avoid too many return statements within this method.
                                                      Open

                                                                      return new WP_Error( 'woocommerce_rest_' . $error_code, $error_message, array( 'status' => 400 ) );

                                                        Avoid too many return statements within this method.
                                                        Open

                                                                        return new WP_Error( 'woocommerce_rest_review_content_invalid', __( 'Invalid review content.', 'woocommerce' ), array( 'status' => 400 ) );

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

                                                              public function update_item_permissions_check( $request ) {
                                                                  $id     = (int) $request['id'];
                                                                  $review = get_comment( $id );
                                                          
                                                                  if ( $review && ! wc_rest_check_product_reviews_permissions( 'edit', $review->comment_ID ) ) {
                                                          includes/rest-api/Controllers/Version3/class-wc-rest-product-reviews-controller.php on lines 151..160
                                                          includes/rest-api/Controllers/Version3/class-wc-rest-product-reviews-controller.php on lines 199..208

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

                                                          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 ) {
                                                                  $id     = (int) $request['id'];
                                                                  $review = get_comment( $id );
                                                          
                                                                  if ( $review && ! wc_rest_check_product_reviews_permissions( 'delete', $review->comment_ID ) ) {
                                                          includes/rest-api/Controllers/Version3/class-wc-rest-product-reviews-controller.php on lines 151..160
                                                          includes/rest-api/Controllers/Version3/class-wc-rest-product-reviews-controller.php on lines 182..191

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

                                                          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 ) {
                                                                  $id     = (int) $request['id'];
                                                                  $review = get_comment( $id );
                                                          
                                                                  if ( $review && ! wc_rest_check_product_reviews_permissions( 'read', $review->comment_ID ) ) {
                                                          includes/rest-api/Controllers/Version3/class-wc-rest-product-reviews-controller.php on lines 182..191
                                                          includes/rest-api/Controllers/Version3/class-wc-rest-product-reviews-controller.php on lines 199..208

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

                                                          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