wikimedia/mediawiki-core

View on GitHub
includes/api/ApiMain.php

Summary

Maintainability
F
1 wk
Test Coverage

File ApiMain.php has 1673 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
/**
 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
 *
 * This program is free software; you can redistribute it and/or modify
Severity: Major
Found in includes/api/ApiMain.php - About 4 days to fix

    ApiMain has 56 functions (exceeds 20 allowed). Consider refactoring.
    Open

    class ApiMain extends ApiBase {
        /**
         * When no format parameter is given, this format will be used
         */
        private const API_DEFAULT_FORMAT = 'jsonfm';
    Severity: Major
    Found in includes/api/ApiMain.php - About 1 day to fix

      Function checkConditionalRequestHeaders has a Cognitive Complexity of 30 (exceeds 5 allowed). Consider refactoring.
      Open

          protected function checkConditionalRequestHeaders( $module ) {
              if ( $this->mInternalMode ) {
                  // No headers to check in internal mode
                  return true;
              }
      Severity: Minor
      Found in includes/api/ApiMain.php - About 4 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 modifyHelp has 107 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          public function modifyHelp( array &$help, array $options, array &$tocData ) {
              // Wish PHP had an "array_insert_before". Instead, we have to manually
              // reindex the array to get 'permissions' in the right place.
              $oldHelp = $help;
              $help = [];
      Severity: Major
      Found in includes/api/ApiMain.php - About 4 hrs to fix

        Function sendCacheHeaders has a Cognitive Complexity of 27 (exceeds 5 allowed). Consider refactoring.
        Open

            protected function sendCacheHeaders( $isError ) {
                $response = $this->getRequest()->response();
                $out = $this->getOutput();
        
                $out->addVaryHeader( 'Treat-as-Untrusted' );
        Severity: Minor
        Found in includes/api/ApiMain.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 handleCORS has a Cognitive Complexity of 25 (exceeds 5 allowed). Consider refactoring.
        Open

            protected function handleCORS() {
                $originParam = $this->getParameter( 'origin' ); // defaults to null
                if ( $originParam === null ) {
                    // No origin parameter, nothing to do
                    return true;
        Severity: Minor
        Found in includes/api/ApiMain.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

        Method __construct has 80 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            public function __construct( $context = null, $enableWrite = false, $internal = null ) {
                if ( $context === null ) {
                    $context = RequestContext::getMain();
                } elseif ( $context instanceof WebRequest ) {
                    // BC for pre-1.19
        Severity: Major
        Found in includes/api/ApiMain.php - About 3 hrs to fix

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

              protected function checkConditionalRequestHeaders( $module ) {
                  if ( $this->mInternalMode ) {
                      // No headers to check in internal mode
                      return true;
                  }
          Severity: Major
          Found in includes/api/ApiMain.php - About 2 hrs to fix

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

                protected function handleCORS() {
                    $originParam = $this->getParameter( 'origin' ); // defaults to null
                    if ( $originParam === null ) {
                        // No origin parameter, nothing to do
                        return true;
            Severity: Major
            Found in includes/api/ApiMain.php - About 2 hrs to fix

              Method sendCacheHeaders has 69 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                  protected function sendCacheHeaders( $isError ) {
                      $response = $this->getRequest()->response();
                      $out = $this->getOutput();
              
                      $out->addVaryHeader( 'Treat-as-Untrusted' );
              Severity: Major
              Found in includes/api/ApiMain.php - About 2 hrs to fix

                Function __construct has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
                Open

                    public function __construct( $context = null, $enableWrite = false, $internal = null ) {
                        if ( $context === null ) {
                            $context = RequestContext::getMain();
                        } elseif ( $context instanceof WebRequest ) {
                            // BC for pre-1.19
                Severity: Minor
                Found in includes/api/ApiMain.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 substituteResultWithError has 64 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                    protected function substituteResultWithError( Throwable $e ) {
                        $result = $this->getResult();
                        $formatter = $this->getErrorFormatter();
                        $config = $this->getConfig();
                        $errorCodes = [];
                Severity: Major
                Found in includes/api/ApiMain.php - About 2 hrs to fix

                  Method logRequest has 62 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                      protected function logRequest( $time, Throwable $e = null ) {
                          $request = $this->getRequest();
                  
                          $user = $this->getUser();
                          $performer = [
                  Severity: Major
                  Found in includes/api/ApiMain.php - About 2 hrs to fix

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

                        public function modifyHelp( array &$help, array $options, array &$tocData ) {
                            // Wish PHP had an "array_insert_before". Instead, we have to manually
                            // reindex the array to get 'permissions' in the right place.
                            $oldHelp = $help;
                            $help = [];
                    Severity: Minor
                    Found in includes/api/ApiMain.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 substituteResultWithError has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
                    Open

                        protected function substituteResultWithError( Throwable $e ) {
                            $result = $this->getResult();
                            $formatter = $this->getErrorFormatter();
                            $config = $this->getConfig();
                            $errorCodes = [];
                    Severity: Minor
                    Found in includes/api/ApiMain.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 getAllowedParams has 50 lines of code (exceeds 25 allowed). Consider refactoring.
                    Open

                        public function getAllowedParams() {
                            return [
                                'action' => [
                                    ParamValidator::PARAM_DEFAULT => 'help',
                                    ParamValidator::PARAM_TYPE => 'submodule',
                    Severity: Minor
                    Found in includes/api/ApiMain.php - About 2 hrs to fix

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

                          protected function logRequest( $time, Throwable $e = null ) {
                              $request = $this->getRequest();
                      
                              $user = $this->getUser();
                              $performer = [
                      Severity: Minor
                      Found in includes/api/ApiMain.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 checkAsserts has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
                      Open

                          protected function checkAsserts( $params ) {
                              if ( isset( $params['assert'] ) ) {
                                  $user = $this->getUser();
                                  switch ( $params['assert'] ) {
                                      case 'anon':
                      Severity: Minor
                      Found in includes/api/ApiMain.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 errorMessagesFromException has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
                      Open

                          protected function errorMessagesFromException( Throwable $e, $type = 'error' ) {
                              $messages = [];
                              if ( $e instanceof ApiUsageException ) {
                                  foreach ( $e->getStatusValue()->getErrorsByType( $type ) as $error ) {
                                      $messages[] = ApiMessage::create( $error );
                      Severity: Minor
                      Found in includes/api/ApiMain.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 handleException has 38 lines of code (exceeds 25 allowed). Consider refactoring.
                      Open

                          protected function handleException( Throwable $e ) {
                              // T65145: Rollback any open database transactions
                              if ( !$e instanceof ApiUsageException ) {
                                  // ApiUsageExceptions are intentional, so don't rollback if that's the case
                                  MWExceptionHandler::rollbackPrimaryChangesAndLog(
                      Severity: Minor
                      Found in includes/api/ApiMain.php - About 1 hr to fix

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

                            protected function executeActionWithErrorHandling() {
                                // Verify the CORS header before executing the action
                                if ( !$this->handleCORS() ) {
                                    // handleCORS() has sent a 403, abort
                                    return;
                        Severity: Minor
                        Found in includes/api/ApiMain.php - About 1 hr to fix

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

                              protected function setupModule() {
                                  // Instantiate the module requested by the user
                                  $module = $this->mModuleMgr->getModule( $this->mAction, 'action' );
                                  if ( $module === null ) {
                                      // Probably can't happen
                          Severity: Minor
                          Found in includes/api/ApiMain.php - About 1 hr to fix

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

                                protected function checkAsserts( $params ) {
                                    if ( isset( $params['assert'] ) ) {
                                        $user = $this->getUser();
                                        switch ( $params['assert'] ) {
                                            case 'anon':
                            Severity: Minor
                            Found in includes/api/ApiMain.php - About 1 hr to fix

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

                                  private function checkBotReadOnly() {
                                      // Figure out how many servers have passed the lag threshold
                                      $numLagged = 0;
                                      $lagLimit = $this->getConfig()->get( MainConfigNames::APIMaxLagThreshold );
                                      $laggedServers = [];
                              Severity: Minor
                              Found in includes/api/ApiMain.php - About 1 hr to fix

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

                                    protected function handleException( Throwable $e ) {
                                        // T65145: Rollback any open database transactions
                                        if ( !$e instanceof ApiUsageException ) {
                                            // ApiUsageExceptions are intentional, so don't rollback if that's the case
                                            MWExceptionHandler::rollbackPrimaryChangesAndLog(
                                Severity: Minor
                                Found in includes/api/ApiMain.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 addRequestedFields has 27 lines of code (exceeds 25 allowed). Consider refactoring.
                                Open

                                    protected function addRequestedFields( $force = [] ) {
                                        $result = $this->getResult();
                                
                                        $requestid = $this->getParameter( 'requestid' );
                                        if ( $requestid !== null ) {
                                Severity: Minor
                                Found in includes/api/ApiMain.php - About 1 hr to fix

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

                                      protected function setupModule() {
                                          // Instantiate the module requested by the user
                                          $module = $this->mModuleMgr->getModule( $this->mAction, 'action' );
                                          if ( $module === null ) {
                                              // Probably can't happen
                                  Severity: Minor
                                  Found in includes/api/ApiMain.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 checkExecutePermissions has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                                  Open

                                      protected function checkExecutePermissions( $module ) {
                                          $user = $this->getUser();
                                          if ( $module->isReadMode() && !$this->getPermissionManager()->isEveryoneAllowed( 'read' ) &&
                                              !$this->getAuthority()->isAllowed( 'read' )
                                          ) {
                                  Severity: Minor
                                  Found in includes/api/ApiMain.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 executeActionWithErrorHandling has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                                  Open

                                      protected function executeActionWithErrorHandling() {
                                          // Verify the CORS header before executing the action
                                          if ( !$this->handleCORS() ) {
                                              // handleCORS() has sent a 403, abort
                                              return;
                                  Severity: Minor
                                  Found in includes/api/ApiMain.php - About 35 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 checkMaxLag has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                                  Open

                                      protected function checkMaxLag( $module, $params ) {
                                          if ( $module->shouldCheckMaxlag() && isset( $params['maxlag'] ) ) {
                                              $maxLag = $params['maxlag'];
                                              $lagInfo = $this->getMaxLag();
                                              if ( $lagInfo['lag'] > $maxLag ) {
                                  Severity: Minor
                                  Found in includes/api/ApiMain.php - About 35 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

                                  Avoid too many return statements within this method.
                                  Open

                                          return true;
                                  Severity: Major
                                  Found in includes/api/ApiMain.php - About 30 mins to fix

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

                                        public function setCacheMode( $mode ) {
                                            if ( !in_array( $mode, [ 'private', 'public', 'anon-public-user-private' ] ) ) {
                                                wfDebug( __METHOD__ . ": unrecognised cache mode \"$mode\"" );
                                    
                                                // Ignore for forwards-compatibility
                                    Severity: Minor
                                    Found in includes/api/ApiMain.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 addRequestedFields has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                                    Open

                                        protected function addRequestedFields( $force = [] ) {
                                            $result = $this->getResult();
                                    
                                            $requestid = $this->getParameter( 'requestid' );
                                            if ( $requestid !== null ) {
                                    Severity: Minor
                                    Found in includes/api/ApiMain.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 setupExternalResponse has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                                    Open

                                        protected function setupExternalResponse( $module, $params ) {
                                            $validMethods = [ 'GET', 'HEAD', 'POST', 'OPTIONS' ];
                                            $request = $this->getRequest();
                                    
                                            if ( !in_array( $request->getMethod(), $validMethods ) ) {
                                    Severity: Minor
                                    Found in includes/api/ApiMain.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

                                    There are no issues that match your filters.

                                    Category
                                    Status