howardjones/network-weathermap

View on GitHub
lib/Weathermap/UI/UIBase.php

Summary

Maintainability
C
1 day
Test Coverage

File UIBase.php has 295 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php

namespace Weathermap\UI;

use Weathermap\Core\WeathermapInternalFail;
Severity: Minor
Found in lib/Weathermap/UI/UIBase.php - About 3 hrs to fix

    UIBase has 23 functions (exceeds 20 allowed). Consider refactoring.
    Open

    class UIBase
    {
        const ARG_OPTIONAL = 2;
        const ARG_TYPE = 1;
        const ARG_NAME = 0;
    Severity: Minor
    Found in lib/Weathermap/UI/UIBase.php - About 2 hrs to fix

      Function selfValidate has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
      Open

          public function selfValidate()
          {
              $result = true;
      
              foreach ($this->types as $type => $validator) {
      Severity: Minor
      Found in lib/Weathermap/UI/UIBase.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

      Function validateRequest has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
      Open

          public function validateRequest($action, $request)
          {
              if (!array_key_exists($action, $this->commands)) {
                  error_log("Given action does not exist!");
                  return false;
      Severity: Minor
      Found in lib/Weathermap/UI/UIBase.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 selfValidate has 27 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          public function selfValidate()
          {
              $result = true;
      
              foreach ($this->types as $type => $validator) {
      Severity: Minor
      Found in lib/Weathermap/UI/UIBase.php - About 1 hr to fix

        Function dispatchRequest has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
        Open

            public function dispatchRequest($action, $request, $appObject)
            {
                if (!array_key_exists($action, $this->commands)) {
                    if (array_key_exists(":: DEFAULT ::", $this->commands)) {
                        $action = ":: DEFAULT ::";
        Severity: Minor
        Found in lib/Weathermap/UI/UIBase.php - About 45 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 wmeValidateOneOf has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
        Open

            public static function wmeValidateOneOf($input, $validChoices = array(), $caseSensitive = false)
            {
                if (!$caseSensitive) {
                    $input = strtolower($input);
                }
        Severity: Minor
        Found in lib/Weathermap/UI/UIBase.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

        Reduce the number of returns of this function 4, down to the maximum allowed 3.
        Open

            public function validateRequest($action, $request)
        Severity: Major
        Found in lib/Weathermap/UI/UIBase.php by sonar-php

        Having too many return statements in a function increases the function's essential complexity because the flow of execution is broken each time a return statement is encountered. This makes it harder to read and understand the logic of the function.

        Noncompliant Code Example

        With the default threshold of 3:

        function myFunction(){ // Noncompliant as there are 4 return statements
          if (condition1) {
            return true;
          } else {
            if (condition2) {
              return false;
            } else {
              return true;
            }
          }
          return false;
        }
        

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

        class UIBase
        Severity: Major
        Found in lib/Weathermap/UI/UIBase.php by sonar-php

        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.

        Remove the unused function parameter "$value".
        Open

            private function validateArgString($value)
        Severity: Major
        Found in lib/Weathermap/UI/UIBase.php by sonar-php

        Unused parameters are misleading. Whatever the value passed to such parameters is, the behavior will be the same.

        Noncompliant Code Example

        function doSomething($a, $b) { // "$a" is unused
          return compute($b);
        }
        

        Compliant Solution

        function doSomething($b) {
          return compute($b);
        }
        

        Exceptions

        Functions in classes that override a class or implement interfaces are ignored.

        class C extends B {
        
          function doSomething($a, $b) {     // no issue reported on $b
            compute($a);
          }
        
        }
        

        See

        • MISRA C++:2008, 0-1-11 - There shall be no unused parameters (named or unnamed) in nonvirtual functions.
        • MISRA C:2012, 2.7 - There should be no unused parameters in functions
        • CERT, MSC12-C. - Detect and remove code that has no effect or is never executed
        • CERT, MSC12-CPP. - Detect and remove code that has no effect

        Define a constant instead of duplicating this literal "handler" 4 times.
        Open

                if (isset($command['handler'])) {
        Severity: Critical
        Found in lib/Weathermap/UI/UIBase.php by sonar-php

        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 "validateArgMaphash" method.
        Open

            private function validateArgMaphash($value)
        Severity: Major
        Found in lib/Weathermap/UI/UIBase.php by sonar-php

        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

        There are no issues that match your filters.

        Category
        Status