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
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';
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;
}
- Read upRead up
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 = [];
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' );
- Read upRead up
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;
- Read upRead up
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
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;
}
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;
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' );
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
- Read upRead up
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 = [];
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 = [
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 = [];
- Read upRead up
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 = [];
- Read upRead up
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',
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 = [
- Read upRead up
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':
- Read upRead up
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 );
- Read upRead up
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(
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;
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
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':
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 = [];
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(
- Read upRead up
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 ) {
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
- Read upRead up
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' )
) {
- Read upRead up
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;
- Read upRead up
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 ) {
- Read upRead up
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;
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
- Read upRead up
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 ) {
- Read upRead up
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 ) ) {
- Read upRead up
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"