librenms/librenms

View on GitHub
app/Models/Device.php

Summary

Maintainability
F
5 days
Test Coverage

Device has 121 functions (exceeds 20 allowed). Consider refactoring.
Open

class Device extends BaseModel
{
    use PivotEventTrait, HasFactory;

    public $timestamps = false;
Severity: Major
Found in app/Models/Device.php - About 2 days to fix

    File Device.php has 798 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    <?php
    
    namespace App\Models;
    
    use App\View\SimpleTemplate;
    Severity: Major
    Found in app/Models/Device.php - About 1 day to fix

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

      class Device extends BaseModel
      {
          use PivotEventTrait, HasFactory;
      
          public $timestamps = false;
      Severity: Minor
      Found in app/Models/Device.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 Device has an overall complexity of 183 which is very high. The configured complexity threshold is 50.
      Open

      class Device extends BaseModel
      {
          use PivotEventTrait, HasFactory;
      
          public $timestamps = false;
      Severity: Minor
      Found in app/Models/Device.php by phpmd

      The class Device has 107 public methods. Consider refactoring Device to keep number of public methods under 10.
      Open

      class Device extends BaseModel
      {
          use PivotEventTrait, HasFactory;
      
          public $timestamps = false;
      Severity: Minor
      Found in app/Models/Device.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

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

          public function setLocation($new_location, bool $doLookup = false)
          {
              $new_location = $new_location instanceof Location ? $new_location : new Location(['location' => $new_location]);
              $new_location->location = $new_location->location ? Rewrite::location($new_location->location) : null;
              $coord = array_filter($new_location->only(['lat', 'lng']));
      Severity: Minor
      Found in app/Models/Device.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 findByIp has 30 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          public static function findByIp(?string $ip): ?Device
          {
              if (! IP::isValid($ip)) {
                  return null;
              }
      Severity: Minor
      Found in app/Models/Device.php - About 1 hr to fix

        The class Device has 122 public methods and attributes. Consider reducing the number of public items to less than 45.
        Open

        class Device extends BaseModel
        {
            use PivotEventTrait, HasFactory;
        
            public $timestamps = false;
        Severity: Minor
        Found in app/Models/Device.php by phpmd

        ExcessivePublicCount

        Since: 0.1

        A large number of public methods and attributes declared in a class can indicate the class may need to be broken up as increased effort will be required to thoroughly test it.

        Example

        public class Foo {
            public $value;
            public $something;
            public $var;
            // [... more more public attributes ...]
        
            public function doWork() {}
            public function doMoreWork() {}
            public function doWorkAgain() {}
            // [... more more public methods ...]
        }

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

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

            public static function findByIp(?string $ip): ?Device
            {
                if (! IP::isValid($ip)) {
                    return null;
                }
        Severity: Minor
        Found in app/Models/Device.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

        Avoid too many return statements within this method.
        Open

                return null;
        Severity: Major
        Found in app/Models/Device.php - About 30 mins to fix

          Avoid too many return statements within this method.
          Open

                      return $query->where('device_id', $deviceSpec);
          Severity: Major
          Found in app/Models/Device.php - About 30 mins to fix

            Avoid too many return statements within this method.
            Open

                    return $query->where('hostname', $deviceSpec);
            Severity: Major
            Found in app/Models/Device.php - About 30 mins to fix

              Avoid too many return statements within this method.
              Open

                          return $query->where('hostname', 'like', str_replace('*', '%', $deviceSpec));
              Severity: Major
              Found in app/Models/Device.php - About 30 mins to fix

                Function updateMaxDepth has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                    public function updateMaxDepth($exclude = null)
                    {
                        // optimize for memory instead of time
                        $query = $this->parents()->getQuery();
                        if (! is_null($exclude)) {
                Severity: Minor
                Found in app/Models/Device.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

                Function scopeWhereDeviceSpec has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                    public function scopeWhereDeviceSpec(Builder $query, ?string $deviceSpec): Builder
                    {
                        if (empty($deviceSpec)) {
                            return $query;
                        } elseif ($deviceSpec == 'all') {
                Severity: Minor
                Found in app/Models/Device.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 Device has 1012 lines of code. Current threshold is 1000. Avoid really long classes.
                Open

                class Device extends BaseModel
                {
                    use PivotEventTrait, HasFactory;
                
                    public $timestamps = false;
                Severity: Minor
                Found in app/Models/Device.php by phpmd

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

                    public function setLocation($new_location, bool $doLookup = false)
                    {
                        $new_location = $new_location instanceof Location ? $new_location : new Location(['location' => $new_location]);
                        $new_location->location = $new_location->location ? Rewrite::location($new_location->location) : null;
                        $coord = array_filter($new_location->only(['lat', 'lng']));
                Severity: Minor
                Found in app/Models/Device.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 class Device has a coupling between objects value of 97. Consider to reduce the number of dependencies under 13.
                Open

                class Device extends BaseModel
                {
                    use PivotEventTrait, HasFactory;
                
                    public $timestamps = false;
                Severity: Minor
                Found in app/Models/Device.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 formatDownUptime has a boolean flag argument $short, which is a certain sign of a Single Responsibility Principle violation.
                Open

                    public function formatDownUptime($short = false): string
                Severity: Minor
                Found in app/Models/Device.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 setLocation has a boolean flag argument $doLookup, which is a certain sign of a Single Responsibility Principle violation.
                Open

                    public function setLocation($new_location, bool $doLookup = false)
                Severity: Minor
                Found in app/Models/Device.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

                Remove error control operator '@' on line 473.
                Open

                    public function getIpAttribute($ip): ?string
                    {
                        if (empty($ip)) {
                            return null;
                        }
                Severity: Minor
                Found in app/Models/Device.php by phpmd

                ErrorControlOperator

                Error suppression should be avoided if possible as it doesn't just suppress the error, that you are trying to stop, but will also suppress errors that you didn't predict would ever occur. Consider changing error_reporting() level and/or setting up your own error handler.

                Example

                function foo($filePath) {
                    $file = @fopen($filPath); // hides exceptions
                    $key = @$array[$notExistingKey]; // assigns null to $key
                }

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

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

                        } else {
                            $parents_max_depth = $query->max('max_depth');
                            $this->max_depth = $parents_max_depth + 1;
                        }
                Severity: Minor
                Found in app/Models/Device.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 pollerTarget uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                        } else {
                            return $device['hostname'];
                        }
                Severity: Minor
                Found in app/Models/Device.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 updateMaxDepth uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                            } else {
                                $this->max_depth = 1; // has children
                            }
                Severity: Minor
                Found in app/Models/Device.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

                FIXME found
                Open

                        // FIXME does not include global read
                Severity: Minor
                Found in app/Models/Device.php by fixme

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

                    public function setIpAttribute($ip): void
                Severity: Minor
                Found in app/Models/Device.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 $ip. Configured minimum length is 3.
                Open

                    public function getIpAttribute($ip): ?string
                Severity: Minor
                Found in app/Models/Device.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 $ip. Configured minimum length is 3.
                Open

                    public static function findByIp(?string $ip): ?Device
                Severity: Minor
                Found in app/Models/Device.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

                Only one argument is allowed per line in a multi-line function call
                Open

                            $query->qualifyColumn('device_id'), function ($query) use ($deviceGroup) {
                Severity: Minor
                Found in app/Models/Device.php by phpcodesniffer

                Only one argument is allowed per line in a multi-line function call
                Open

                            $query->qualifyColumn('device_id'), function ($query) use ($serviceTemplate) {
                Severity: Minor
                Found in app/Models/Device.php by phpcodesniffer

                Only one argument is allowed per line in a multi-line function call
                Open

                            $query->qualifyColumn('device_id'), function ($query) use ($serviceTemplate) {
                Severity: Minor
                Found in app/Models/Device.php by phpcodesniffer

                Only one argument is allowed per line in a multi-line function call
                Open

                            $query->qualifyColumn('device_id'), function ($query) use ($deviceGroup) {
                Severity: Minor
                Found in app/Models/Device.php by phpcodesniffer

                There are no issues that match your filters.

                Category
                Status