howardjones/network-weathermap

View on GitHub
lib/Weathermap/Integrations/MapManager.php

Summary

Maintainability
F
3 days
Test Coverage

File MapManager.php has 675 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php

namespace Weathermap\Integrations;

use PDO;
Severity: Major
Found in lib/Weathermap/Integrations/MapManager.php - About 1 day to fix

    MapManager has 54 functions (exceeds 20 allowed). Consider refactoring.
    Open

    class MapManager
    {
    
        /** @var PDO $pdo */
        private $pdo;
    Severity: Major
    Found in lib/Weathermap/Integrations/MapManager.php - About 7 hrs to fix

      Function initializeDatabase has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
      Open

          public function initializeDatabase()
          {
              // only bother with all this if it's a new install, a new version, or we're in a development version
              // - saves a handful of db hits per request!
      
      
      Severity: Minor
      Found in lib/Weathermap/Integrations/MapManager.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 createMissingTables has 64 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          private function createMissingTables($tables)
          {
              $databaseUpdates = array();
      
              if (!in_array('weathermap_maps', $tables)) {
      Severity: Major
      Found in lib/Weathermap/Integrations/MapManager.php - About 2 hrs to fix

        Method initializeDatabase has 61 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            public function initializeDatabase()
            {
                // only bother with all this if it's a new install, a new version, or we're in a development version
                // - saves a handful of db hits per request!
        
        
        Severity: Major
        Found in lib/Weathermap/Integrations/MapManager.php - About 2 hrs to fix

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

              public function extractMapTitle($filename)
              {
                  $title = "(no file)";
          
                  if (file_exists($filename)) {
          Severity: Minor
          Found in lib/Weathermap/Integrations/MapManager.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 resortMaps has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
          Open

              public function resortMaps()
              {
                  $stmt = $this->pdo->query('SELECT * FROM weathermap_maps ORDER BY group_id,sortorder;');
          
                  $newMapOrder = array();
          Severity: Minor
          Found in lib/Weathermap/Integrations/MapManager.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

          Refactor this function to reduce its Cognitive Complexity from 21 to the 15 allowed.
          Open

              public function initializeDatabase()

          Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.

          See

          Class "MapManager" has 54 methods, which is greater than 20 authorized. Split it into smaller classes.
          Open

          class MapManager

          A class that grows too much tends to aggregate too many responsibilities and inevitably becomes harder to understand and therefore to maintain. Above a specific threshold, it is strongly advised to refactor the class into smaller ones which focus on well defined topics.

          Define and throw a dedicated exception instead of using a generic one.
          Open

                      throw new \Exception("Path mismatch - $fileDirectory != " . $this->configDirectory);

          If you throw a general exception type, such as ErrorException, RuntimeException, or Exception in a library or framework, it forces consumers to catch all exceptions, including unknown exceptions that they do not know how to handle.

          Instead, either throw a subtype that already exists in the Standard PHP Library, or create your own type that derives from Exception.

          Noncompliant Code Example

          throw new Exception();  // Noncompliant
          

          Compliant Solution

          throw new InvalidArgumentException();
          // or
          throw new UnexpectedValueException();
          

          See

          Define a constant instead of duplicating this literal "value" 3 times.
          Open

                  $data = array("id" => $mapId, "name" => $name, "value" => $value);

          Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

          On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

          Noncompliant Code Example

          With the default threshold of 3:

          function run() {
            prepare('action1');                              // Non-Compliant - 'action1' is duplicated 3 times
            execute('action1');
            release('action1');
          }
          

          Compliant Solution

          ACTION_1 = 'action1';
          
          function run() {
            prepare(ACTION_1);
            execute(ACTION_1);
            release(ACTION_1);
          }
          

          Exceptions

          To prevent generating some false-positives, literals having less than 5 characters are excluded.

          Remove this unused private "camelize" method.
          Open

              private function camelize($input, $separator = '_')

          private methods that are never executed are dead code: unnecessary, inoperative code that should be removed. Cleaning out dead code decreases the size of the maintained codebase, making it easier to understand the program and preventing bugs from being introduced.

          Noncompliant Code Example

          public class Foo
          {
            private function Foo() {}   // Compliant, private empty constructor intentionally used to prevent any direct instantiation of a class.
          
            public static function doSomething()
            {
              $foo = new Foo();
              ...
            }
          
            private function unusedPrivateFunction() {  // Noncompliant
            }
          }
          

          Compliant Solution

          public class Foo
          {
            private function Foo(){}   // Compliant, private empty constructor intentionally used to prevent any direct instantiation of a class.
          
            public static function doSomething()
            {
              $foo = new Foo();
            }
          }
          

          See

          Merge this if statement with the enclosing one.
          Open

                      if (!in_array('usergroupid', $columns)) {

          Merging collapsible if statements increases the code's readability.

          Noncompliant Code Example

          if (condition1) {
            if (condition2) {
              ...
            }
          }
          

          Compliant Solution

          if (condition1 && condition2) {
            ...
          }
          

          Define a constant instead of duplicating this literal "group_id" 5 times.
          Open

                      "group_id",

          Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

          On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

          Noncompliant Code Example

          With the default threshold of 3:

          function run() {
            prepare('action1');                              // Non-Compliant - 'action1' is duplicated 3 times
            execute('action1');
            release('action1');
          }
          

          Compliant Solution

          ACTION_1 = 'action1';
          
          function run() {
            prepare(ACTION_1);
            execute(ACTION_1);
            release(ACTION_1);
          }
          

          Exceptions

          To prevent generating some false-positives, literals having less than 5 characters are excluded.

          Remove this commented out code.
          Open

                      // $statement = $this->pdo->prepare("SELECT DISTINCT weathermap_maps.* FROM weathermap_auth,weathermap_maps WHERE weathermap_maps.id=weathermap_auth.mapid AND active='on' AND  (userid=? OR userid=0) ORDER BY sortorder, id");

          Programmers should not comment out code as it bloats programs and reduces readability.

          Unused code should be deleted and can be retrieved from source control history if required.

          See

          • MISRA C:2004, 2.4 - Sections of code should not be "commented out".
          • MISRA C++:2008, 2-7-2 - Sections of code shall not be "commented out" using C-style comments.
          • MISRA C++:2008, 2-7-3 - Sections of code should not be "commented out" using C++ comments.
          • MISRA C:2012, Dir. 4.4 - Sections of code should not be "commented out"

          Define a constant instead of duplicating this literal "active" 3 times.
          Open

                      "active",

          Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

          On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

          Noncompliant Code Example

          With the default threshold of 3:

          function run() {
            prepare('action1');                              // Non-Compliant - 'action1' is duplicated 3 times
            execute('action1');
            release('action1');
          }
          

          Compliant Solution

          ACTION_1 = 'action1';
          
          function run() {
            prepare(ACTION_1);
            execute(ACTION_1);
            release(ACTION_1);
          }
          

          Exceptions

          To prevent generating some false-positives, literals having less than 5 characters are excluded.

          Remove this commented out code.
          Open

                      //$statement = $this->pdo->prepare("SELECT DISTINCT weathermap_maps.* FROM weathermap_auth,weathermap_maps WHERE weathermap_maps.id=weathermap_auth.mapid AND active='on' AND  weathermap_maps.group_id=? AND  (userid=? OR userid=0) ORDER BY sortorder, id");

          Programmers should not comment out code as it bloats programs and reduces readability.

          Unused code should be deleted and can be retrieved from source control history if required.

          See

          • MISRA C:2004, 2.4 - Sections of code should not be "commented out".
          • MISRA C++:2008, 2-7-2 - Sections of code shall not be "commented out" using C-style comments.
          • MISRA C++:2008, 2-7-3 - Sections of code should not be "commented out" using C++ comments.
          • MISRA C:2012, Dir. 4.4 - Sections of code should not be "commented out"

          There are no issues that match your filters.

          Category
          Status