librenms/librenms

View on GitHub
LibreNMS/Device/Sensor.php

Summary

Maintainability
D
2 days
Test Coverage

File Sensor.php has 395 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
/**
 * Sensor.php
 *
 * Base Sensor class
Severity: Minor
Found in LibreNMS/Device/Sensor.php - About 5 hrs to fix

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

    class Sensor implements DiscoveryModule, PollerModule
    {
        protected static $name = 'Sensor';
        protected static $table = 'sensors';
        protected static $data_name = 'sensor';
    Severity: Minor
    Found in LibreNMS/Device/Sensor.php by phpmd

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

    class Sensor implements DiscoveryModule, PollerModule
    {
        protected static $name = 'Sensor';
        protected static $table = 'sensors';
        protected static $data_name = 'sensor';
    Severity: Minor
    Found in LibreNMS/Device/Sensor.php - About 2 hrs to fix

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

              $type,
              $device_id,
              $oids,
              $subtype,
              $index,
      Severity: Major
      Found in LibreNMS/Device/Sensor.php - About 2 hrs to fix

        The class Sensor has 23 fields. Consider redesigning Sensor to keep the number of fields under 15.
        Open

        class Sensor implements DiscoveryModule, PollerModule
        {
            protected static $name = 'Sensor';
            protected static $table = 'sensors';
            protected static $data_name = 'sensor';
        Severity: Minor
        Found in LibreNMS/Device/Sensor.php by phpmd

        TooManyFields

        Since: 0.1

        Classes that have too many fields could be redesigned to have fewer fields, possibly through some nested object grouping of some of the information. For example, a class with city/state/zip fields could instead have one Address field.

        Example

        class Person {
           protected $one;
           private $two;
           private $three;
           [... many more fields ...]
        }

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

        Method poll has 32 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            public static function poll(OS $os)
            {
                $table = static::$table;
        
                $query = "SELECT * FROM `$table` WHERE `device_id` = ?";
        Severity: Minor
        Found in LibreNMS/Device/Sensor.php - About 1 hr to fix

          Method recordSensorData has 31 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              protected static function recordSensorData(OS $os, $sensors, $data)
              {
                  $types = static::getTypes();
          
                  foreach ($sensors as $sensor) {
          Severity: Minor
          Found in LibreNMS/Device/Sensor.php - About 1 hr to fix

            Method __construct has 28 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                public function __construct(
                    $type,
                    $device_id,
                    $oids,
                    $subtype,
            Severity: Minor
            Found in LibreNMS/Device/Sensor.php - About 1 hr to fix

              Method save has 27 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                  final public function save()
                  {
                      $db_sensor = $this->fetch();
              
                      $new_sensor = $this->toArray();
              Severity: Minor
              Found in LibreNMS/Device/Sensor.php - About 1 hr to fix

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

                    protected static function processSensorValue(array $data, string $aggregator, int $divisor, int $multiplier): mixed
                    {
                        if (empty($data)) {
                            return null;
                        }
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 save has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                Open

                    final public function save()
                    {
                        $db_sensor = $this->fetch();
                
                        $new_sensor = $this->toArray();
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 poll has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                Open

                    public static function poll(OS $os)
                    {
                        $table = static::$table;
                
                        $query = "SELECT * FROM `$table` WHERE `device_id` = ?";
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 discoverType has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                    protected static function discoverType(OS $os, $type)
                    {
                        $typeInterface = static::getDiscoveryInterface($type);
                        if (! interface_exists($typeInterface)) {
                            echo "ERROR: Discovery Interface doesn't exist! $typeInterface\n";
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 method __construct has 17 parameters. Consider reducing the number of parameters to less than 10.
                Open

                    public function __construct(
                        $type,
                        $device_id,
                        $oids,
                        $subtype,
                Severity: Minor
                Found in LibreNMS/Device/Sensor.php by phpmd

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

                    public static function getTypes($valid = false, $device_id = null)
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 save uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                        } else {
                            $this->sensor_id = dbInsert($new_sensor, $this->getTable());
                            if ($this->sensor_id !== null) {
                                $name = static::$name;
                                $message = "$name Discovered: {$this->type} {$this->subtype} {$this->index} {$this->description}";
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 discoverType uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                        } else {
                            $sensors = [];  // delete non existent sensors
                        }
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 fetchSnmpData uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                            } else {
                                $oid = trim('"', $oid); // allow string only values
                            }
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 save uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                            } else {
                                dbUpdate($update, $this->getTable(), '`sensor_id`=?', [$this->sensor_id]);
                                echo 'U';
                            }
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 pollSensorType uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                        } else {
                            $data = static::processSensorData($sensors, $prefetch);
                        }
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 processSensorValue uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                            } else {
                                // sum
                                $sensor_value = array_sum($data);
                            }
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 processSensorValue uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                        } else {
                            $sensor_value = current($data);
                        }
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 unused parameters such as '$valid'.
                Open

                    public static function getTypes($valid = false, $device_id = null)
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 unused parameters such as '$device_id'.
                Open

                    public static function getTypes($valid = false, $device_id = null)
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 unused parameters such as '$os'.
                Open

                    public static function runDiscovery(OS $os)
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 unused private methods such as 'getUniqueId'.
                Open

                    private function getUniqueId()
                    {
                        return $this->type . '-' . $this->subtype . '-' . $this->index;
                    }
                Severity: Minor
                Found in LibreNMS/Device/Sensor.php by phpmd

                UnusedPrivateMethod

                Since: 0.2

                Unused Private Method detects when a private method is declared but is unused.

                Example

                class Something
                {
                    private function foo() {} // unused
                }

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

                The property $data_name is not named in camelCase.
                Open

                class Sensor implements DiscoveryModule, PollerModule
                {
                    protected static $name = 'Sensor';
                    protected static $table = 'sensors';
                    protected static $data_name = 'sensor';
                Severity: Minor
                Found in LibreNMS/Device/Sensor.php by phpmd

                CamelCasePropertyName

                Since: 0.2

                It is considered best practice to use the camelCase notation to name attributes.

                Example

                class ClassName {
                    protected $property_name;
                }

                Source

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

                    public static function poll(OS $os)
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 property $high_warn is not named in camelCase.
                Open

                class Sensor implements DiscoveryModule, PollerModule
                {
                    protected static $name = 'Sensor';
                    protected static $table = 'sensors';
                    protected static $data_name = 'sensor';
                Severity: Minor
                Found in LibreNMS/Device/Sensor.php by phpmd

                CamelCasePropertyName

                Since: 0.2

                It is considered best practice to use the camelCase notation to name attributes.

                Example

                class ClassName {
                    protected $property_name;
                }

                Source

                The property $translation_prefix is not named in camelCase.
                Open

                class Sensor implements DiscoveryModule, PollerModule
                {
                    protected static $name = 'Sensor';
                    protected static $table = 'sensors';
                    protected static $data_name = 'sensor';
                Severity: Minor
                Found in LibreNMS/Device/Sensor.php by phpmd

                CamelCasePropertyName

                Since: 0.2

                It is considered best practice to use the camelCase notation to name attributes.

                Example

                class ClassName {
                    protected $property_name;
                }

                Source

                The property $rrd_type is not named in camelCase.
                Open

                class Sensor implements DiscoveryModule, PollerModule
                {
                    protected static $name = 'Sensor';
                    protected static $table = 'sensors';
                    protected static $data_name = 'sensor';
                Severity: Minor
                Found in LibreNMS/Device/Sensor.php by phpmd

                CamelCasePropertyName

                Since: 0.2

                It is considered best practice to use the camelCase notation to name attributes.

                Example

                class ClassName {
                    protected $property_name;
                }

                Source

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

                    protected static function pollSensorType($os, $type, $sensors, $prefetch = [])
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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

                    protected static function recordSensorData(OS $os, $sensors, $data)
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 property $high_limit is not named in camelCase.
                Open

                class Sensor implements DiscoveryModule, PollerModule
                {
                    protected static $name = 'Sensor';
                    protected static $table = 'sensors';
                    protected static $data_name = 'sensor';
                Severity: Minor
                Found in LibreNMS/Device/Sensor.php by phpmd

                CamelCasePropertyName

                Since: 0.2

                It is considered best practice to use the camelCase notation to name attributes.

                Example

                class ClassName {
                    protected $property_name;
                }

                Source

                The property $sensor_id is not named in camelCase.
                Open

                class Sensor implements DiscoveryModule, PollerModule
                {
                    protected static $name = 'Sensor';
                    protected static $table = 'sensors';
                    protected static $data_name = 'sensor';
                Severity: Minor
                Found in LibreNMS/Device/Sensor.php by phpmd

                CamelCasePropertyName

                Since: 0.2

                It is considered best practice to use the camelCase notation to name attributes.

                Example

                class ClassName {
                    protected $property_name;
                }

                Source

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

                    public static function runDiscovery(OS $os)
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 property $low_limit is not named in camelCase.
                Open

                class Sensor implements DiscoveryModule, PollerModule
                {
                    protected static $name = 'Sensor';
                    protected static $table = 'sensors';
                    protected static $data_name = 'sensor';
                Severity: Minor
                Found in LibreNMS/Device/Sensor.php by phpmd

                CamelCasePropertyName

                Since: 0.2

                It is considered best practice to use the camelCase notation to name attributes.

                Example

                class ClassName {
                    protected $property_name;
                }

                Source

                The property $low_warn is not named in camelCase.
                Open

                class Sensor implements DiscoveryModule, PollerModule
                {
                    protected static $name = 'Sensor';
                    protected static $table = 'sensors';
                    protected static $data_name = 'sensor';
                Severity: Minor
                Found in LibreNMS/Device/Sensor.php by phpmd

                CamelCasePropertyName

                Since: 0.2

                It is considered best practice to use the camelCase notation to name attributes.

                Example

                class ClassName {
                    protected $property_name;
                }

                Source

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

                    protected static function discoverType(OS $os, $type)
                Severity: Minor
                Found in LibreNMS/Device/Sensor.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 property $device_id is not named in camelCase.
                Open

                class Sensor implements DiscoveryModule, PollerModule
                {
                    protected static $name = 'Sensor';
                    protected static $table = 'sensors';
                    protected static $data_name = 'sensor';
                Severity: Minor
                Found in LibreNMS/Device/Sensor.php by phpmd

                CamelCasePropertyName

                Since: 0.2

                It is considered best practice to use the camelCase notation to name attributes.

                Example

                class ClassName {
                    protected $property_name;
                }

                Source

                There are no issues that match your filters.

                Category
                Status