File MessageCache.php
has 916 lines of code (exceeds 250 allowed). Consider refactoring. Open
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
Function loadUnguarded
has a Cognitive Complexity of 40 (exceeds 5 allowed). Consider refactoring. Open
private function loadUnguarded( $code, $mode ) {
$success = false; // Keep track of success
$staleCache = false; // a cache array with expired data, or false if none has been loaded
$where = []; // Debug info, delayed to avoid spamming debug log too much
- 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
MessageCache
has 41 functions (exceeds 20 allowed). Consider refactoring. Open
class MessageCache implements LoggerAwareInterface {
/**
* Options to be included in the ServiceOptions
*/
public const CONSTRUCTOR_OPTIONS = [
Method loadFromDB
has 98 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function loadFromDB( $code, $mode = null ) {
$icp = MediaWikiServices::getInstance()->getConnectionProvider();
$dbr = ( $mode === self::FOR_UPDATE ) ? $icp->getPrimaryDatabase() : $icp->getReplicaDatabase();
Function getMessageForLang
has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring. Open
private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
$langcode = $lang->getCode();
// Try checking the database for the requested language
if ( $useDB ) {
- 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 loadUnguarded
has 84 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function loadUnguarded( $code, $mode ) {
$success = false; // Keep track of success
$staleCache = false; // a cache array with expired data, or false if none has been loaded
$where = []; // Debug info, delayed to avoid spamming debug log too much
Function loadFromDB
has a Cognitive Complexity of 22 (exceeds 5 allowed). Consider refactoring. Open
private function loadFromDB( $code, $mode = null ) {
$icp = MediaWikiServices::getInstance()->getConnectionProvider();
$dbr = ( $mode === self::FOR_UPDATE ) ? $icp->getPrimaryDatabase() : $icp->getReplicaDatabase();
- 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 getMsgFromNamespace
has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring. Open
public function getMsgFromNamespace( $title, $code ) {
// Load all MediaWiki page definitions into cache. Note that individual keys
// already loaded into the cache during this request remain in the cache, which
// includes the value of hook-defined messages.
$this->load( $code );
- 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 get
has 52 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function get( $key, $useDB = true, $langcode = true, &$usedKey = '' ) {
if ( is_int( $key ) ) {
// Fix numerical strings that somehow become ints on their way here
$key = (string)$key;
} elseif ( !is_string( $key ) ) {
Method getMessageForLang
has 50 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
$langcode = $lang->getCode();
// Try checking the database for the requested language
if ( $useDB ) {
Method refreshAndReplaceInternal
has 49 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function refreshAndReplaceInternal( string $code, array $replacements ) {
// Allow one caller at a time to avoid race conditions
[ $scopedLock ] = $this->getReentrantScopedLock( $code );
if ( !$scopedLock ) {
foreach ( $replacements as [ $title ] ) {
Method getMsgFromNamespace
has 44 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function getMsgFromNamespace( $title, $code ) {
// Load all MediaWiki page definitions into cache. Note that individual keys
// already loaded into the cache during this request remain in the cache, which
// includes the value of hook-defined messages.
$this->load( $code );
Method __construct
has 13 arguments (exceeds 4 allowed). Consider refactoring. Open
WANObjectCache $wanCache,
BagOStuff $clusterCache,
BagOStuff $serverCache,
Language $contLang,
LanguageConverterFactory $langConverterFactory,
Function get
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
public function get( $key, $useDB = true, $langcode = true, &$usedKey = '' ) {
if ( is_int( $key ) ) {
// Fix numerical strings that somehow become ints on their way here
$key = (string)$key;
} elseif ( !is_string( $key ) ) {
- 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 loadCachedMessagePageEntry
has 38 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function loadCachedMessagePageEntry( $dbKey, $code, $hash ) {
$fname = __METHOD__;
return $this->srvCache->getWithSetCallback(
$this->srvCache->makeKey( 'messages-big', $hash, $dbKey ),
BagOStuff::TTL_HOUR,
Function refreshAndReplaceInternal
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
public function refreshAndReplaceInternal( string $code, array $replacements ) {
// Allow one caller at a time to avoid race conditions
[ $scopedLock ] = $this->getReentrantScopedLock( $code );
if ( !$scopedLock ) {
foreach ( $replacements as [ $title ] ) {
- 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 parse
has 29 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function parse( $text, PageReference $page = null, $linestart = true,
$interface = false, $language = null
) {
global $wgTitle;
Method parse
has 5 arguments (exceeds 4 allowed). Consider refactoring. Open
public function parse( $text, PageReference $page = null, $linestart = true,
$interface = false, $language = null
Avoid too many return
statements within this method. Open
return false;
Avoid too many return
statements within this method. Open
return $message;
Avoid too many return
statements within this method. Open
return $this->contLang;
Avoid too many return
statements within this method. Open
return $defaultMessage ?? false;
Avoid too many return
statements within this method. Open
return $defaultMessage;
Function load
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
private function load( string $code, $mode = null ) {
// Don't do double loading...
if ( $this->isLanguageLoaded( $code ) && $mode !== self::FOR_UPDATE ) {
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 loadFromDBWithMainLock
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
private function loadFromDBWithMainLock( $code, array &$where, $mode = null ) {
// If cache updates on all levels fail, give up on message overrides.
// This is to avoid easy site outages; see $saveSuccess comments below.
$statusKey = $this->clusterCache->makeKey( 'messages', $code, 'status' );
$status = $this->clusterCache->get( $statusKey );
- 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
Similar blocks of code found in 4 locations. Consider refactoring. Open
public function __construct(
WANObjectCache $wanCache,
BagOStuff $clusterCache,
BagOStuff $serverCache,
Language $contLang,
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 92.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76