wikimedia/mediawiki-core

View on GitHub
includes/language/MessageCache.php

Summary

Maintainability
F
1 wk
Test Coverage

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
Severity: Major
Found in includes/language/MessageCache.php - About 2 days to fix

    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
    
    
    Severity: Minor
    Found in includes/language/MessageCache.php - About 6 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

    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 = [
    Severity: Minor
    Found in includes/language/MessageCache.php - About 5 hrs to fix

      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();
      
      
      Severity: Major
      Found in includes/language/MessageCache.php - About 3 hrs to fix

        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 ) {
        Severity: Minor
        Found in includes/language/MessageCache.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 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
        
        
        Severity: Major
        Found in includes/language/MessageCache.php - About 3 hrs to fix

          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();
          
          
          Severity: Minor
          Found in includes/language/MessageCache.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 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 );
          Severity: Minor
          Found in includes/language/MessageCache.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 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 ) ) {
          Severity: Major
          Found in includes/language/MessageCache.php - About 2 hrs to fix

            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 ) {
            Severity: Minor
            Found in includes/language/MessageCache.php - About 2 hrs to fix

              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 ] ) {
              Severity: Minor
              Found in includes/language/MessageCache.php - About 1 hr to fix

                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 );
                Severity: Minor
                Found in includes/language/MessageCache.php - About 1 hr to fix

                  Method __construct has 13 arguments (exceeds 4 allowed). Consider refactoring.
                  Open

                          WANObjectCache $wanCache,
                          BagOStuff $clusterCache,
                          BagOStuff $serverCache,
                          Language $contLang,
                          LanguageConverterFactory $langConverterFactory,
                  Severity: Major
                  Found in includes/language/MessageCache.php - About 1 hr to fix

                    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 ) ) {
                    Severity: Minor
                    Found in includes/language/MessageCache.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 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,
                    Severity: Minor
                    Found in includes/language/MessageCache.php - About 1 hr to fix

                      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 ] ) {
                      Severity: Minor
                      Found in includes/language/MessageCache.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 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;
                      
                      
                      Severity: Minor
                      Found in includes/language/MessageCache.php - About 1 hr to fix

                        Method parse has 5 arguments (exceeds 4 allowed). Consider refactoring.
                        Open

                            public function parse( $text, PageReference $page = null, $linestart = true,
                                $interface = false, $language = null
                        Severity: Minor
                        Found in includes/language/MessageCache.php - About 35 mins to fix

                          Avoid too many return statements within this method.
                          Open

                                  return false;
                          Severity: Major
                          Found in includes/language/MessageCache.php - About 30 mins to fix

                            Avoid too many return statements within this method.
                            Open

                                                return $message;
                            Severity: Major
                            Found in includes/language/MessageCache.php - About 30 mins to fix

                              Avoid too many return statements within this method.
                              Open

                                      return $this->contLang;
                              Severity: Major
                              Found in includes/language/MessageCache.php - About 30 mins to fix

                                Avoid too many return statements within this method.
                                Open

                                        return $defaultMessage ?? false;
                                Severity: Major
                                Found in includes/language/MessageCache.php - About 30 mins to fix

                                  Avoid too many return statements within this method.
                                  Open

                                                      return $defaultMessage;
                                  Severity: Major
                                  Found in includes/language/MessageCache.php - About 30 mins to fix

                                    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;
                                            }
                                    Severity: Minor
                                    Found in includes/language/MessageCache.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 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 );
                                    Severity: Minor
                                    Found in includes/language/MessageCache.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

                                    Similar blocks of code found in 4 locations. Consider refactoring.
                                    Open

                                        public function __construct(
                                            WANObjectCache $wanCache,
                                            BagOStuff $clusterCache,
                                            BagOStuff $serverCache,
                                            Language $contLang,
                                    Severity: Major
                                    Found in includes/language/MessageCache.php and 3 other locations - About 35 mins to fix
                                    includes/Revision/RevisionStoreFactory.php on lines 106..134
                                    includes/page/RollbackPage.php on lines 133..163
                                    includes/specials/SpecialMovePage.php on lines 129..158

                                    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

                                    Further Reading

                                    There are no issues that match your filters.

                                    Category
                                    Status