librenms/librenms

View on GitHub
LibreNMS/Util/CiHelper.php

Summary

Maintainability
C
1 day
Test Coverage

File CiHelper.php has 358 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
/**
 * CiHelper.php
 *
 * Code for CI operation
Severity: Minor
Found in LibreNMS/Util/CiHelper.php - About 4 hrs to fix

    The class CiHelper has 13 public methods. Consider refactoring CiHelper to keep number of public methods under 10.
    Open

    class CiHelper
    {
        private $changed;
        private $os;
        private $unitEnv = [];
    Severity: Minor
    Found in LibreNMS/Util/CiHelper.php by phpmd

    TooManyPublicMethods

    Since: 0.1

    A class with too many public methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.

    By default it ignores methods starting with 'get' or 'set'.

    Example

    Source https://phpmd.org/rules/codesize.html#toomanypublicmethods

    The class CiHelper has an overall complexity of 91 which is very high. The configured complexity threshold is 50.
    Open

    class CiHelper
    {
        private $changed;
        private $os;
        private $unitEnv = [];
    Severity: Minor
    Found in LibreNMS/Util/CiHelper.php by phpmd

    CiHelper has 24 functions (exceeds 20 allowed). Consider refactoring.
    Open

    class CiHelper
    {
        private $changed;
        private $os;
        private $unitEnv = [];
    Severity: Minor
    Found in LibreNMS/Util/CiHelper.php - About 2 hrs to fix

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

          private function execute(string $name, $command, $silence = false, $env = null): int
          {
              $start = microtime(true);
              $proc = new Process($command, null, $env);
      
      
      Severity: Minor
      Found in LibreNMS/Util/CiHelper.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 execute has 41 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          private function execute(string $name, $command, $silence = false, $env = null): int
          {
              $start = microtime(true);
              $proc = new Process($command, null, $env);
      
      
      Severity: Minor
      Found in LibreNMS/Util/CiHelper.php - About 1 hr to fix

        Method checkLint has 29 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            public function checkLint()
            {
                $return = 0;
                if (! $this->flags['lint_skip_php']) {
                    $php_lint_cmd = [$this->checkPhpExec('parallel-lint')];
        Severity: Minor
        Found in LibreNMS/Util/CiHelper.php - About 1 hr to fix

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

              public function checkUnit()
              {
                  $phpunit_cmd = [$this->checkPhpExec('phpunit'), '--colors=always'];
          
                  if ($this->flags['fail-fast']) {
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.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

          The method execute() has an NPath complexity of 576. The configured NPath complexity threshold is 200.
          Open

              private function execute(string $name, $command, $silence = false, $env = null): int
              {
                  $start = microtime(true);
                  $proc = new Process($command, null, $env);
          
          
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          NPathComplexity

          Since: 0.1

          The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.

          Example

          class Foo {
              function bar() {
                  // lots of complicated code
              }
          }

          Source https://phpmd.org/rules/codesize.html#npathcomplexity

          The method parseChangedFiles() has a Cyclomatic Complexity of 12. The configured cyclomatic complexity threshold is 10.
          Open

              private function parseChangedFiles()
              {
                  if ($this->flags['full'] || ! empty($this->changed['full-checks'])) {
                      $this->flags['full'] = true; // make sure full is set and skip changed file parsing
          
          
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          CyclomaticComplexity

          Since: 0.1

          Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.

          Example

          // Cyclomatic Complexity = 11
          class Foo {
          1   public function example() {
          2       if ($a == $b) {
          3           if ($a1 == $b1) {
                          fiddle();
          4           } elseif ($a2 == $b2) {
                          fiddle();
                      } else {
                          fiddle();
                      }
          5       } elseif ($c == $d) {
          6           while ($c == $d) {
                          fiddle();
                      }
          7        } elseif ($e == $f) {
          8           for ($n = 0; $n < $h; $n++) {
                          fiddle();
                      }
                  } else {
                      switch ($z) {
          9               case 1:
                              fiddle();
                              break;
          10              case 2:
                              fiddle();
                              break;
          11              case 3:
                              fiddle();
                              break;
                          default:
                              fiddle();
                              break;
                      }
                  }
              }
          }

          Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity

          The method execute() has a Cyclomatic Complexity of 13. The configured cyclomatic complexity threshold is 10.
          Open

              private function execute(string $name, $command, $silence = false, $env = null): int
              {
                  $start = microtime(true);
                  $proc = new Process($command, null, $env);
          
          
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          CyclomaticComplexity

          Since: 0.1

          Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.

          Example

          // Cyclomatic Complexity = 11
          class Foo {
          1   public function example() {
          2       if ($a == $b) {
          3           if ($a1 == $b1) {
                          fiddle();
          4           } elseif ($a2 == $b2) {
                          fiddle();
                      } else {
                          fiddle();
                      }
          5       } elseif ($c == $d) {
          6           while ($c == $d) {
                          fiddle();
                      }
          7        } elseif ($e == $f) {
          8           for ($n = 0; $n < $h; $n++) {
                          fiddle();
                      }
                  } else {
                      switch ($z) {
          9               case 1:
                              fiddle();
                              break;
          10              case 2:
                              fiddle();
                              break;
          11              case 3:
                              fiddle();
                              break;
                          default:
                              fiddle();
                              break;
                      }
                  }
              }
          }

          Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity

          The method execute has a boolean flag argument $silence, which is a certain sign of a Single Responsibility Principle violation.
          Open

              private function execute(string $name, $command, $silence = false, $env = null): int
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          BooleanArgumentFlag

          Since: 1.4.0

          A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.

          Example

          class Foo {
              public function bar($flag = true) {
              }
          }

          Source https://phpmd.org/rules/cleancode.html#booleanargumentflag

          The method enable has a boolean flag argument $enabled, which is a certain sign of a Single Responsibility Principle violation.
          Open

              public function enable($check, $enabled = true)
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          BooleanArgumentFlag

          Since: 1.4.0

          A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.

          Example

          class Foo {
              public function bar($flag = true) {
              }
          }

          Source https://phpmd.org/rules/cleancode.html#booleanargumentflag

          The method execute uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
          Open

                  } else {
                      $proc->run();
                  }
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          ElseExpression

          Since: 1.4.0

          An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.

          Example

          class Foo
          {
              public function bar($flag)
              {
                  if ($flag) {
                      // one branch
                  } else {
                      // another branch
                  }
              }
          }

          Source https://phpmd.org/rules/cleancode.html#elseexpression

          The method run uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
          Open

                      } else {
                          $return += $ret;
                      }
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          ElseExpression

          Since: 1.4.0

          An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.

          Example

          class Foo
          {
              public function bar($flag)
              {
                  if ($flag) {
                      // one branch
                  } else {
                      // another branch
                  }
              }
          }

          Source https://phpmd.org/rules/cleancode.html#elseexpression

          Avoid assigning values to variables in if clauses and the like (line '326', column '13').
          Open

              private function runCheck($type)
              {
                  if ($method = $this->canCheck($type)) {
                      $ret = $this->$method();
                      $this->completedChecks[$type] = true;
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          IfStatementAssignment

          Since: 2.7.0

          Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.

          Example

          class Foo
          {
              public function bar($flag)
              {
                  if ($foo = 'bar') { // possible typo
                      // ...
                  }
                  if ($baz = 0) { // always false
                      // ...
                  }
              }
          }

          Source http://phpmd.org/rules/cleancode.html#ifstatementassignment

          The method execute uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
          Open

                      } else {
                          $proc->run(function ($type, $buffer) {
                              echo $buffer;
                          });
                      }
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          ElseExpression

          Since: 1.4.0

          An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.

          Example

          class Foo
          {
              public function bar($flag)
              {
                  if ($flag) {
                      // one branch
                  } else {
                      // another branch
                  }
              }
          }

          Source https://phpmd.org/rules/cleancode.html#elseexpression

          The method checkUnit uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
          Open

                      } else {
                          array_push($phpunit_cmd, '--filter', "/::test[A-Za-z]+$|::testOSDetection|::test[A-Za-z]+ with data set \"$filter.*\"$/");
                      }
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          ElseExpression

          Since: 1.4.0

          An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.

          Example

          class Foo
          {
              public function bar($flag)
              {
                  if ($flag) {
                      // one branch
                  } else {
                      // another branch
                  }
              }
          }

          Source https://phpmd.org/rules/cleancode.html#elseexpression

          The method checkPythonExec() contains an exit expression.
          Open

                  exit(1);
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          ExitExpression

          Since: 0.2

          An exit-expression within regular code is untestable and therefore it should be avoided. Consider to move the exit-expression into some kind of startup script where an error/exception code is returned to the calling environment.

          Example

          class Foo {
              public function bar($param)  {
                  if ($param === 42) {
                      exit(23);
                  }
              }
          }

          Source https://phpmd.org/rules/design.html#exitexpression

          The method checkPhpExec() contains an exit expression.
          Open

                  exit(1);
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          ExitExpression

          Since: 0.2

          An exit-expression within regular code is untestable and therefore it should be avoided. Consider to move the exit-expression into some kind of startup script where an error/exception code is returned to the calling environment.

          Example

          class Foo {
              public function bar($param)  {
                  if ($param === 42) {
                      exit(23);
                  }
              }
          }

          Source https://phpmd.org/rules/design.html#exitexpression

          Avoid unused parameters such as '$type'.
          Open

                      $server->waitUntil(function ($type, $output) {
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          UnusedFormalParameter

          Since: 0.2

          Avoid passing parameters to methods or constructors and then not using those parameters.

          Example

          class Foo
          {
              private function bar($howdy)
              {
                  // $howdy is not used
              }
          }

          Source https://phpmd.org/rules/unusedcode.html#unusedformalparameter

          Avoid variables with short names like $os. Configured minimum length is 3.
          Open

              public function setOS(array $os)
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          ShortVariable

          Since: 0.2

          Detects when a field, local, or parameter has a very short name.

          Example

          class Something {
              private $q = 15; // VIOLATION - Field
              public static function main( array $as ) { // VIOLATION - Formal
                  $r = 20 + $this->q; // VIOLATION - Local
                  for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
                      $r += $this->q;
                  }
              }
          }

          Source https://phpmd.org/rules/naming.html#shortvariable

          Avoid variables with short names like $os. Configured minimum length is 3.
          Open

              private $os;
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          ShortVariable

          Since: 0.2

          Detects when a field, local, or parameter has a very short name.

          Example

          class Something {
              private $q = 15; // VIOLATION - Field
              public static function main( array $as ) { // VIOLATION - Formal
                  $r = 20 + $this->q; // VIOLATION - Local
                  for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
                      $r += $this->q;
                  }
              }
          }

          Source https://phpmd.org/rules/naming.html#shortvariable

          The 'getFlag()' method which returns a boolean should be named 'is...()' or 'has...()'
          Open

              public function getFlag($name)
              {
                  return $this->flags[$name] ?? null;
              }
          Severity: Minor
          Found in LibreNMS/Util/CiHelper.php by phpmd

          BooleanGetMethodName

          Since: 0.2

          Looks for methods named 'getX()' with 'boolean' as the return type. The convention is to name these methods 'isX()' or 'hasX()'.

          Example

          class Foo {
              /**
               * @return boolean
               */
              public function getFoo() {} // bad
              /**
               * @return bool
               */
              public function isFoo(); // ok
              /**
               * @return boolean
               */
              public function getFoo($bar); // ok, unless checkParameterizedMethods=true
          }

          Source https://phpmd.org/rules/naming.html#booleangetmethodname

          There are no issues that match your filters.

          Category
          Status