petenelson/wp-rest-api-toolbox

View on GitHub
includes/class-rest-api-toolbox-common.php

Summary

Maintainability
C
1 day
Test Coverage

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

        static public function endpoint_requires_authentication_filter( $result, $rest_server, $request ) {

            // Get the route for the request.
            $route = $request->get_route();

Severity: Minor
Found in includes/class-rest-api-toolbox-common.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

The class REST_API_Toolbox_Common has 14 public methods. Consider refactoring REST_API_Toolbox_Common to keep number of public methods under 10.
Open

    class REST_API_Toolbox_Common {

        static public function plugins_loaded() {

            // Setup disabled filter based on the changing in WP 4.7

TooManyPublicMethods

Since: 0.1

A class with too many public methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.

By default it ignores methods starting with 'get' or 'set'.

Example

Source https://phpmd.org/rules/codesize.html#toomanypublicmethods

The class REST_API_Toolbox_Common has an overall complexity of 57 which is very high. The configured complexity threshold is 50.
Open

    class REST_API_Toolbox_Common {

        static public function plugins_loaded() {

            // Setup disabled filter based on the changing in WP 4.7

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

        static public function remove_selected_endpoints_filter( $routes ) {

            // Get the list of core endpoints.
            $core_settings = get_option( REST_API_Toolbox_Settings::options_key( 'core' ) );
            $core_settings = ! is_array( $core_settings ) ? array() : $core_settings;
Severity: Minor
Found in includes/class-rest-api-toolbox-common.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 remove_wordpress_core_namespace_filter has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
Open

        static public function remove_wordpress_core_namespace_filter( $response ) {

            $remove_all = REST_API_Toolbox_Settings::setting_is_enabled( 'core', 'remove-all-core-routes' );
            if ( $remove_all ) {
                if ( ! empty( $response->data ) && ! empty( $response->data['namespaces'] ) ) {
Severity: Minor
Found in includes/class-rest-api-toolbox-common.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 endpoint_requires_authentication_filter has 31 lines of code (exceeds 25 allowed). Consider refactoring.
Open

        static public function endpoint_requires_authentication_filter( $result, $rest_server, $request ) {

            // Get the route for the request.
            $route = $request->get_route();

Severity: Minor
Found in includes/class-rest-api-toolbox-common.php - About 1 hr to fix

    Avoid deeply nested control flow statements.
    Open

                            if ( self::core_namespace() === $response->data['namespaces'][ $i ] ) {
                                unset( $response->data['namespaces'][ $i ] );
                                $response->data['namespaces'] = array_values( $response->data['namespaces'] );
                                break;
                            }
    Severity: Major
    Found in includes/class-rest-api-toolbox-common.php - About 45 mins to fix

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

              static public function endpoint_exists( $endpoint ) {
      
                  rest_api_loaded();
      
                  $wp_rest_server = self::get_rest_api_server();
      Severity: Minor
      Found in includes/class-rest-api-toolbox-common.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

      The method endpoint_requires_authentication_filter() has a Cyclomatic Complexity of 12. The configured cyclomatic complexity threshold is 10.
      Open

              static public function endpoint_requires_authentication_filter( $result, $rest_server, $request ) {
      
                  // Get the route for the request.
                  $route = $request->get_route();
      
      

      CyclomaticComplexity

      Since: 0.1

      Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.

      Example

      // Cyclomatic Complexity = 11
      class Foo {
      1   public function example() {
      2       if ($a == $b) {
      3           if ($a1 == $b1) {
                      fiddle();
      4           } elseif ($a2 == $b2) {
                      fiddle();
                  } else {
                      fiddle();
                  }
      5       } elseif ($c == $d) {
      6           while ($c == $d) {
                      fiddle();
                  }
      7        } elseif ($e == $f) {
      8           for ($n = 0; $n < $h; $n++) {
                      fiddle();
                  }
              } else {
                  switch ($z) {
      9               case 1:
                          fiddle();
                          break;
      10              case 2:
                          fiddle();
                          break;
      11              case 3:
                          fiddle();
                          break;
                      default:
                          fiddle();
                          break;
                  }
              }
          }
      }

      Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity

      The method remove_selected_endpoints_filter() has a Cyclomatic Complexity of 10. The configured cyclomatic complexity threshold is 10.
      Open

              static public function remove_selected_endpoints_filter( $routes ) {
      
                  // Get the list of core endpoints.
                  $core_settings = get_option( REST_API_Toolbox_Settings::options_key( 'core' ) );
                  $core_settings = ! is_array( $core_settings ) ? array() : $core_settings;

      CyclomaticComplexity

      Since: 0.1

      Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.

      Example

      // Cyclomatic Complexity = 11
      class Foo {
      1   public function example() {
      2       if ($a == $b) {
      3           if ($a1 == $b1) {
                      fiddle();
      4           } elseif ($a2 == $b2) {
                      fiddle();
                  } else {
                      fiddle();
                  }
      5       } elseif ($c == $d) {
      6           while ($c == $d) {
                      fiddle();
                  }
      7        } elseif ($e == $f) {
      8           for ($n = 0; $n < $h; $n++) {
                      fiddle();
                  }
              } else {
                  switch ($z) {
      9               case 1:
                          fiddle();
                          break;
      10              case 2:
                          fiddle();
                          break;
      11              case 3:
                          fiddle();
                          break;
                      default:
                          fiddle();
                          break;
                  }
              }
          }
      }

      Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity

      Missing class import via use statement (line '163', column '22').
      Open

                          $response = new WP_Error( 'rest_forbidden', __( "SSL is required to access the REST API" ), array( 'status' => 403 ) );

      MissingImport

      Since: 2.7.0

      Importing all external classes in a file through use statements makes them clearly visible.

      Example

      function make() {
          return new \stdClass();
      }

      Source http://phpmd.org/rules/cleancode.html#MissingImport

      Missing class import via use statement (line '118', column '19').
      Open

                          $error = new WP_Error( 'rest_disabled', __( 'The REST API is disabled on this site.' ), array( 'status' => 404 ) );

      MissingImport

      Since: 2.7.0

      Importing all external classes in a file through use statements makes them clearly visible.

      Example

      function make() {
          return new \stdClass();
      }

      Source http://phpmd.org/rules/cleancode.html#MissingImport

      Missing class import via use statement (line '304', column '18').
      Open

                              return new WP_Error(

      MissingImport

      Since: 2.7.0

      Importing all external classes in a file through use statements makes them clearly visible.

      Example

      function make() {
          return new \stdClass();
      }

      Source http://phpmd.org/rules/cleancode.html#MissingImport

      Avoid unused parameters such as '$request'.
      Open

              static public function disallow_non_ssl_filter( $response, $server, $request ) {

      UnusedFormalParameter

      Since: 0.2

      Avoid passing parameters to methods or constructors and then not using those parameters.

      Example

      class Foo
      {
          private function bar($howdy)
          {
              // $howdy is not used
          }
      }

      Source https://phpmd.org/rules/unusedcode.html#unusedformalparameter

      Avoid unused parameters such as '$rest_server'.
      Open

              static public function endpoint_requires_authentication_filter( $result, $rest_server, $request ) {

      UnusedFormalParameter

      Since: 0.2

      Avoid passing parameters to methods or constructors and then not using those parameters.

      Example

      class Foo
      {
          private function bar($howdy)
          {
              // $howdy is not used
          }
      }

      Source https://phpmd.org/rules/unusedcode.html#unusedformalparameter

      Avoid unused parameters such as '$server'.
      Open

              static public function disallow_non_ssl_filter( $response, $server, $request ) {

      UnusedFormalParameter

      Since: 0.2

      Avoid passing parameters to methods or constructors and then not using those parameters.

      Example

      class Foo
      {
          private function bar($howdy)
          {
              // $howdy is not used
          }
      }

      Source https://phpmd.org/rules/unusedcode.html#unusedformalparameter

      There are no issues that match your filters.

      Category
      Status