woothemes/woocommerce

View on GitHub
includes/rest-api/Controllers/Version1/class-wc-rest-webhooks-v1-controller.php

Summary

Maintainability
F
3 days
Test Coverage

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

<?php
/**
 * REST API Webhooks controller
 *
 * Handles requests to the /webhooks endpoint.

    Method get_item_schema has 76 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'      => 'webhook',
                'type'       => 'object',

      Method register_routes has 70 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,
                      'callback'            => array( $this, 'get_items' ),

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

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

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

              public function update_item( $request ) {
                  $id      = (int) $request['id'];
                  $webhook = wc_get_webhook( $id );
          
                  if ( empty( $webhook ) || is_null( $webhook ) ) {

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

              public function get_items( $request ) {
                  $args            = array();
                  $args['order']   = $request['order'];
                  $args['orderby'] = $request['orderby'];
                  $args['status']  = 'all' === $request['status'] ? '' : $request['status'];

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

                public function update_item( $request ) {
                    $id      = (int) $request['id'];
                    $webhook = wc_get_webhook( $id );
            
                    if ( empty( $webhook ) || is_null( $webhook ) ) {

              Method create_item has 31 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 ) );
                      }

                Avoid too many return statements within this method.
                Open

                        return rest_ensure_response( $response );

                  Avoid too many return statements within this method.
                  Open

                              return $post;

                    Avoid too many return statements within this method.
                    Open

                            return $response;

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

                          public function get_items( $request ) {
                              $args            = array();
                              $args['order']   = $request['order'];
                              $args['orderby'] = $request['orderby'];
                              $args['status']  = 'all' === $request['status'] ? '' : $request['status'];

                      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 create_item has a Cognitive Complexity of 6 (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

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

                          protected function prepare_item_for_database( $request ) {
                              $data = new stdClass;
                      
                              // Post ID.
                              if ( isset( $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

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

                              if ( ! empty( $request['topic'] ) ) {
                                  if ( wc_is_webhook_valid_topic( strtolower( $request['topic'] ) ) ) {
                                      $webhook->set_topic( $request['topic'] );
                                  } else {
                                      return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_topic", __( 'Webhook topic must be valid.', 'woocommerce' ), array( 'status' => 400 ) );
                      includes/rest-api/Controllers/Version1/class-wc-rest-webhooks-v1-controller.php on lines 407..413

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

                      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 2 locations. Consider refactoring.
                      Open

                              if ( ! empty( $request['status'] ) ) {
                                  if ( wc_is_webhook_valid_status( strtolower( $request['status'] ) ) ) {
                                      $webhook->set_status( $request['status'] );
                                  } else {
                                      return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_status", __( 'Webhook status must be valid.', 'woocommerce' ), array( 'status' => 400 ) );
                      includes/rest-api/Controllers/Version1/class-wc-rest-webhooks-v1-controller.php on lines 384..390

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

                      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