File UploadBase.php
has 1378 lines of code (exceeds 250 allowed). Consider refactoring. Open
<?php
/**
* Base class for the backend of file upload.
*
* This program is free software; you can redistribute it and/or modify
UploadBase
has 73 functions (exceeds 20 allowed). Consider refactoring. Open
abstract class UploadBase {
use ProtectedHookAccessorTrait;
/** @var string|null Local file system path to the file to upload (or a local copy) */
protected $mTempPath;
Method checkSvgScriptCallback
has 162 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function checkSvgScriptCallback( $element, $attribs, $data = null ) {
[ $namespace, $strippedElement ] = self::splitXmlNamespace( $element );
// We specifically don't include:
// http://www.w3.org/1999/xhtml (T62771)
Function checkSvgScriptCallback
has a Cognitive Complexity of 36 (exceeds 5 allowed). Consider refactoring. Open
public function checkSvgScriptCallback( $element, $attribs, $data = null ) {
[ $namespace, $strippedElement ] = self::splitXmlNamespace( $element );
// We specifically don't include:
// http://www.w3.org/1999/xhtml (T62771)
- 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 getTitle
has a Cognitive Complexity of 27 (exceeds 5 allowed). Consider refactoring. Open
public function getTitle() {
if ( $this->mTitle !== false ) {
return $this->mTitle;
}
if ( !is_string( $this->mDesiredDestName ) ) {
- 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 getTitle
has 94 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function getTitle() {
if ( $this->mTitle !== false ) {
return $this->mTitle;
}
if ( !is_string( $this->mDesiredDestName ) ) {
Method detectVirus
has 71 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function detectVirus( $file ) {
global $wgOut;
$mainConfig = MediaWikiServices::getInstance()->getMainConfig();
$antivirus = $mainConfig->get( MainConfigNames::Antivirus );
$antivirusSetup = $mainConfig->get( MainConfigNames::AntivirusSetup );
Function detectScript
has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring. Open
public static function detectScript( $file, $mime, $extension ) {
# ugly hack: for text files, always look at the entire file.
# For binary field, just check the first K.
if ( str_starts_with( $mime ?? '', 'text/' ) ) {
- 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 detectScript
has 66 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function detectScript( $file, $mime, $extension ) {
# ugly hack: for text files, always look at the entire file.
# For binary field, just check the first K.
if ( str_starts_with( $mime ?? '', 'text/' ) ) {
Function detectVirus
has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring. Open
public static function detectVirus( $file ) {
global $wgOut;
$mainConfig = MediaWikiServices::getInstance()->getMainConfig();
$antivirus = $mainConfig->get( MainConfigNames::Antivirus );
$antivirusSetup = $mainConfig->get( MainConfigNames::AntivirusSetup );
- 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 verifyFile
has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring. Open
protected function verifyFile() {
$config = MediaWikiServices::getInstance()->getMainConfig();
$verifyMimeType = $config->get( MainConfigNames::VerifyMimeType );
$disableUploadScriptChecks = $config->get( MainConfigNames::DisableUploadScriptChecks );
$status = $this->verifyPartialFile();
- 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 getExistsWarning
has 64 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function getExistsWarning( $file ) {
if ( $file->exists() ) {
return [ 'warning' => 'exists', 'file' => $file ];
}
Method verifyFile
has 47 lines of code (exceeds 25 allowed). Consider refactoring. Open
protected function verifyFile() {
$config = MediaWikiServices::getInstance()->getMainConfig();
$verifyMimeType = $config->get( MainConfigNames::VerifyMimeType );
$disableUploadScriptChecks = $config->get( MainConfigNames::DisableUploadScriptChecks );
$status = $this->verifyPartialFile();
Function getExistsWarning
has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring. Open
public static function getExistsWarning( $file ) {
if ( $file->exists() ) {
return [ 'warning' => 'exists', 'file' => $file ];
}
- 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 checkXMLEncodingMissmatch
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
public static function checkXMLEncodingMissmatch( $file ) {
// https://mimesniff.spec.whatwg.org/#resource-header says browsers
// should read the first 1445 bytes. Do 4096 bytes for good measure.
// XML Spec says XML declaration if present must be first thing in file
// other than BOM
- 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 setSessionStatus
has 39 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function setSessionStatus( UserIdentity $user, $statusKey, $value ) {
$store = self::getUploadSessionStore();
$key = self::getUploadSessionKey( $store, $user, $statusKey );
$logger = LoggerFactory::getInstance( 'upload' );
Method checkWarnings
has 37 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function checkWarnings( $user = null ) {
if ( $user === null ) {
// TODO check uses and hard deprecate
$user = RequestContext::getMain()->getUser();
}
Function verifyPartialFile
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
protected function verifyPartialFile() {
$config = MediaWikiServices::getInstance()->getMainConfig();
$disableUploadScriptChecks = $config->get( MainConfigNames::DisableUploadScriptChecks );
# getTitle() sets some internal parameters like $this->mFinalExtension
$this->getTitle();
- 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 checkCssFragment
has 35 lines of code (exceeds 25 allowed). Consider refactoring. Open
private static function checkCssFragment( $value ) {
# Forbid external stylesheets, for both reliability and to protect viewer's privacy
if ( stripos( $value, '@import' ) !== false ) {
return true;
}
Method checkXMLEncodingMissmatch
has 34 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function checkXMLEncodingMissmatch( $file ) {
// https://mimesniff.spec.whatwg.org/#resource-header says browsers
// should read the first 1445 bytes. Do 4096 bytes for good measure.
// XML Spec says XML declaration if present must be first thing in file
// other than BOM
Method verifyPartialFile
has 32 lines of code (exceeds 25 allowed). Consider refactoring. Open
protected function verifyPartialFile() {
$config = MediaWikiServices::getInstance()->getMainConfig();
$disableUploadScriptChecks = $config->get( MainConfigNames::DisableUploadScriptChecks );
# getTitle() sets some internal parameters like $this->mFinalExtension
$this->getTitle();
Method performUpload
has 32 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function performUpload(
$comment, $pageText, $watch, $user, $tags = [], ?string $watchlistExpiry = null
) {
$this->getLocalFile()->load( IDBAccessObject::READ_LATEST );
$props = $this->mFileProps;
Function getFilenamePrefixBlacklist
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
public static function getFilenamePrefixBlacklist() {
$list = [];
$message = wfMessage( 'filename-prefix-blacklist' )->inContentLanguage();
if ( !$message->isDisabled() ) {
$lines = explode( "\n", $message->plain() );
- 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 verifyExtension
has 27 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function verifyExtension( $mime, $extension ) {
$magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
if ( !$mime || $mime === 'unknown' || $mime === 'unknown/unknown' ) {
if ( !$magic->isRecognizableExtension( $extension ) ) {
Function checkLocalFileExists
has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open
private function checkLocalFileExists( LocalFile $localFile, $hash ) {
$warnings = [];
$exists = self::getExistsWarning( $localFile );
if ( $exists !== false ) {
- 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 checkCssFragment
has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open
private static function checkCssFragment( $value ) {
# Forbid external stylesheets, for both reliability and to protect viewer's privacy
if ( stripos( $value, '@import' ) !== false ) {
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
Function unserializeWarnings
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public static function unserializeWarnings( $warnings ) {
foreach ( $warnings as $key => $value ) {
if ( is_array( $value ) ) {
if ( isset( $value['fileName'] ) && isset( $value['timestamp'] ) ) {
$warnings[$key] = MediaWikiServices::getInstance()->getRepoGroup()->findFile(
- 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 verifyExtension
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public static function verifyExtension( $mime, $extension ) {
$magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
if ( !$mime || $mime === 'unknown' || $mime === 'unknown/unknown' ) {
if ( !$magic->isRecognizableExtension( $extension ) ) {
- 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 validateName
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public function validateName() {
$nt = $this->getTitle();
if ( $nt === null ) {
$result = [ 'status' => $this->mTitleError ];
if ( $this->mTitleError === self::ILLEGAL_FILENAME ) {
- 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 checkWarnings
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public function checkWarnings( $user = null ) {
if ( $user === null ) {
// TODO check uses and hard deprecate
$user = RequestContext::getMain()->getUser();
}
- 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 performUpload
has 6 arguments (exceeds 4 allowed). Consider refactoring. Open
$comment, $pageText, $watch, $user, $tags = [], ?string $watchlistExpiry = null
Consider simplifying this complex logical expression. Open
if (
$stripped === 'href'
&& $value !== ''
&& !str_starts_with( $value, 'data:' )
&& !str_starts_with( $value, '#' )
Avoid too many return
statements within this method. Open
return [ 'uploaded-hostile-svg' ];
Avoid too many return
statements within this method. Open
return [ 'uploaded-event-handler-on-svg', $attrib, $value ];
Avoid too many return
statements within this method. Open
return [ 'uploaded-image-filter-svg', $strippedElement, $stripped, $value ];
Avoid too many return
statements within this method. Open
return [
'warning' => 'thumb-name',
'file' => $file,
'thumbFile' => $file_thb
];
Avoid too many return
statements within this method. Open
return $this->mTitle;
Avoid too many return
statements within this method. Open
return true;
Avoid too many return
statements within this method. Open
return [ 'uploaded-remote-url-svg', $attrib, $value ];
Avoid too many return
statements within this method. Open
return $this->mTitle;
Avoid too many return
statements within this method. Open
return false;
Avoid too many return
statements within this method. Open
return [ 'uploaded-setting-event-handler-svg', $strippedElement, $stripped, $value ];
Avoid too many return
statements within this method. Open
return [ 'uploaded-script-svg', $strippedElement ];
Avoid too many return
statements within this method. Open
return [ 'uploaded-wrong-setting-svg', $value ];
Avoid too many return
statements within this method. Open
return true;
Avoid too many return
statements within this method. Open
return true;
Avoid too many return
statements within this method. Open
return [ 'status' => self::OK ];
Avoid too many return
statements within this method. Open
return [ 'uploaded-href-unsafe-target-svg', $strippedElement, $attrib, $value ];
Avoid too many return
statements within this method. Open
return false;
Avoid too many return
statements within this method. Open
return true;
Avoid too many return
statements within this method. Open
return false;
Avoid too many return
statements within this method. Open
return false;
Avoid too many return
statements within this method. Open
return [ 'uploaded-remote-url-svg', $attrib, $value ];
Avoid too many return
statements within this method. Open
return false; // No scripts detected
Avoid too many return
statements within this method. Open
return $this->mTitle;
Avoid too many return
statements within this method. Open
return true;
Avoid too many return
statements within this method. Open
return [ 'uploaded-setting-href-svg' ];
Avoid too many return
statements within this method. Open
return $error;
Avoid too many return
statements within this method. Open
return true;
Avoid too many return
statements within this method. Open
return $this->mTitle;
Avoid too many return
statements within this method. Open
return [ 'uploaded-href-attribute-svg', $strippedElement, $attrib, $value ];
Avoid too many return
statements within this method. Open
return $this->mTitle;
Avoid too many return
statements within this method. Open
return [ 'uploaded-setting-handler-svg', $attrib, $value ];
Avoid too many return
statements within this method. Open
return [
'warning' => 'bad-prefix',
'file' => $file,
'prefix' => $prefix
];
Avoid too many return
statements within this method. Open
return true;
Avoid too many return
statements within this method. Open
return true;
Avoid too many return
statements within this method. Open
return [ 'uploaded-animate-svg', $strippedElement, $attrib, $value ];
Avoid too many return
statements within this method. Open
return false;
Avoid too many return
statements within this method. Open
return [
'warning' => 'thumb',
'file' => $file,
'thumbFile' => $file_thb
];
Function performUpload
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public function performUpload(
$comment, $pageText, $watch, $user, $tags = [], ?string $watchlistExpiry = null
) {
$this->getLocalFile()->load( IDBAccessObject::READ_LATEST );
$props = $this->mFileProps;
- 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 setSessionStatus
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public static function setSessionStatus( UserIdentity $user, $statusKey, $value ) {
$store = self::getUploadSessionStore();
$key = self::getUploadSessionKey( $store, $user, $statusKey );
$logger = LoggerFactory::getInstance( 'upload' );
- 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 createFromRequest
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public static function createFromRequest( &$request, $type = null ) {
$type = $type ?: $request->getVal( 'wpSourceType', 'File' );
if ( !$type ) {
return 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"