librenms/librenms

View on GitHub
LibreNMS/Data/Source/NetSnmpQuery.php

Summary

Maintainability
C
1 day
Test Coverage

File NetSnmpQuery.php has 337 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
/*
 * SNMP.php
 *
 * -Description-
Severity: Minor
Found in LibreNMS/Data/Source/NetSnmpQuery.php - About 4 hrs to fix

    NetSnmpQuery has 31 functions (exceeds 20 allowed). Consider refactoring.
    Open

    class NetSnmpQuery implements SnmpQueryInterface
    {
        private const DEFAULT_FLAGS = '-OQXUte';
    
        /**
    Severity: Minor
    Found in LibreNMS/Data/Source/NetSnmpQuery.php - About 3 hrs to fix

      The class NetSnmpQuery has 29 non-getter- and setter-methods. Consider refactoring NetSnmpQuery to keep number of methods under 25.
      Open

      class NetSnmpQuery implements SnmpQueryInterface
      {
          private const DEFAULT_FLAGS = '-OQXUte';
      
          /**
      Severity: Minor
      Found in LibreNMS/Data/Source/NetSnmpQuery.php by phpmd

      TooManyMethods

      Since: 0.1

      A class with too many 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'.

      The default was changed from 10 to 25 in PHPMD 2.3.

      Example

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

      The class NetSnmpQuery has 18 public methods. Consider refactoring NetSnmpQuery to keep number of public methods under 10.
      Open

      class NetSnmpQuery implements SnmpQueryInterface
      {
          private const DEFAULT_FLAGS = '-OQXUte';
      
          /**
      Severity: Minor
      Found in LibreNMS/Data/Source/NetSnmpQuery.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 NetSnmpQuery has an overall complexity of 90 which is very high. The configured complexity threshold is 50.
      Open

      class NetSnmpQuery implements SnmpQueryInterface
      {
          private const DEFAULT_FLAGS = '-OQXUte';
      
          /**
      Severity: Minor
      Found in LibreNMS/Data/Source/NetSnmpQuery.php by phpmd

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

          private function initCommand(string $binary, array $oids): array
          {
              if ($binary == 'snmpwalk') {
                  // allow unordered responses for specific oids
                  if (! empty(array_intersect($oids, Config::getCombined($this->device->os, 'oids.unordered', 'snmp.')))) {
      Severity: Minor
      Found in LibreNMS/Data/Source/NetSnmpQuery.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 mibDirectories has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
      Open

          private function mibDirectories(): string
          {
              $base = Config::get('mib_dir');
              $dirs = [$base];
      
      
      Severity: Minor
      Found in LibreNMS/Data/Source/NetSnmpQuery.php - About 35 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 buildAuth has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
      Open

          private function buildAuth(array &$cmd): void
          {
              if ($this->device->snmpver === 'v3') {
                  array_push($cmd, '-v3', '-l', $this->device->authlevel);
                  array_push($cmd, '-n', $this->context);
      Severity: Minor
      Found in LibreNMS/Data/Source/NetSnmpQuery.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

      The class NetSnmpQuery has a coupling between objects value of 16. Consider to reduce the number of dependencies under 13.
      Open

      class NetSnmpQuery implements SnmpQueryInterface
      {
          private const DEFAULT_FLAGS = '-OQXUte';
      
          /**
      Severity: Minor
      Found in LibreNMS/Data/Source/NetSnmpQuery.php by phpmd

      CouplingBetweenObjects

      Since: 1.1.0

      A class with too many dependencies has negative impacts on several quality aspects of a class. This includes quality criteria like stability, maintainability and understandability

      Example

      class Foo {
          /**
           * @var \foo\bar\X
           */
          private $x = null;
      
          /**
           * @var \foo\bar\Y
           */
          private $y = null;
      
          /**
           * @var \foo\bar\Z
           */
          private $z = null;
      
          public function setFoo(\Foo $foo) {}
          public function setBar(\Bar $bar) {}
          public function setBaz(\Baz $baz) {}
      
          /**
           * @return \SplObjectStorage
           * @throws \OutOfRangeException
           * @throws \InvalidArgumentException
           * @throws \ErrorException
           */
          public function process(\Iterator $it) {}
      
          // ...
      }

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

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

          public function mibs(array $mibs, bool $append = true): SnmpQueryInterface
      Severity: Minor
      Found in LibreNMS/Data/Source/NetSnmpQuery.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 numeric has a boolean flag argument $numeric, which is a certain sign of a Single Responsibility Principle violation.
      Open

          public function numeric(bool $numeric = true): SnmpQueryInterface
      Severity: Minor
      Found in LibreNMS/Data/Source/NetSnmpQuery.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 numericIndex has a boolean flag argument $numericIndex, which is a certain sign of a Single Responsibility Principle violation.
      Open

          public function numericIndex(bool $numericIndex = true): SnmpQueryInterface
      Severity: Minor
      Found in LibreNMS/Data/Source/NetSnmpQuery.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

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

          private function mibDirectories(): string
          {
              $base = Config::get('mib_dir');
              $dirs = [$base];
      
      
      Severity: Minor
      Found in LibreNMS/Data/Source/NetSnmpQuery.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 buildAuth uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
      Open

              } else {
                  Log::debug("Unsupported SNMP Version: {$this->device->snmpver}");
              }
      Severity: Minor
      Found in LibreNMS/Data/Source/NetSnmpQuery.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

      Space before opening parenthesis of function call prohibited
      Open

                      $oid_list = implode(',', array_map(fn ($group) => is_array($group) ? implode(',', $group) : $group, $oids));

      There are no issues that match your filters.

      Category
      Status