librenms/librenms

View on GitHub
LibreNMS/Alert/RunAlerts.php

Summary

Maintainability
F
5 days
Test Coverage

Function runAlerts has a Cognitive Complexity of 67 (exceeds 5 allowed). Consider refactoring.
Open

    public function runAlerts()
    {
        foreach ($this->loadAlerts('alerts.state != ' . AlertState::ACKNOWLEDGED . ' && alerts.open = 1') as $alert) {
            $noiss = false;
            $noacc = false;
Severity: Minor
Found in LibreNMS/Alert/RunAlerts.php - About 1 day 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

File RunAlerts.php has 435 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
/*
 * RunAlerts.php
 *
 * Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org>
Severity: Minor
Found in LibreNMS/Alert/RunAlerts.php - About 6 hrs to fix

    Function describeAlert has a Cognitive Complexity of 37 (exceeds 5 allowed). Consider refactoring.
    Open

        public function describeAlert($alert)
        {
            $obj = [];
            $i = 0;
            $device = DeviceCache::get($alert['device_id']);
    Severity: Minor
    Found in LibreNMS/Alert/RunAlerts.php - About 5 hrs 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 runFollowUp has a Cognitive Complexity of 30 (exceeds 5 allowed). Consider refactoring.
    Open

        public function runFollowUp()
        {
            foreach ($this->loadAlerts('alerts.state > ' . AlertState::CLEAR . ' && alerts.open = 0') as $alert) {
                if ($alert['state'] != AlertState::ACKNOWLEDGED || ($alert['info']['until_clear'] === false)) {
                    $rextra = json_decode($alert['extra'], true);
    Severity: Minor
    Found in LibreNMS/Alert/RunAlerts.php - About 4 hrs 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 describeAlert has 102 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        public function describeAlert($alert)
        {
            $obj = [];
            $i = 0;
            $device = DeviceCache::get($alert['device_id']);
    Severity: Major
    Found in LibreNMS/Alert/RunAlerts.php - About 4 hrs to fix

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

      class RunAlerts
      {
          /**
           * Populate variables
           *
      Severity: Minor
      Found in LibreNMS/Alert/RunAlerts.php by phpmd

      Method runAlerts has 80 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          public function runAlerts()
          {
              foreach ($this->loadAlerts('alerts.state != ' . AlertState::ACKNOWLEDGED . ' && alerts.open = 1') as $alert) {
                  $noiss = false;
                  $noacc = false;
      Severity: Major
      Found in LibreNMS/Alert/RunAlerts.php - About 3 hrs to fix

        Method runFollowUp has 43 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            public function runFollowUp()
            {
                foreach ($this->loadAlerts('alerts.state > ' . AlertState::CLEAR . ' && alerts.open = 0') as $alert) {
                    if ($alert['state'] != AlertState::ACKNOWLEDGED || ($alert['info']['until_clear'] === false)) {
                        $rextra = json_decode($alert['extra'], true);
        Severity: Minor
        Found in LibreNMS/Alert/RunAlerts.php - About 1 hr to fix

          Method extTransports has 40 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              public function extTransports($obj)
              {
                  $type = new Template;
          
                  // If alert transport mapping exists, override the default transports
          Severity: Minor
          Found in LibreNMS/Alert/RunAlerts.php - About 1 hr to fix

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

                public function extTransports($obj)
                {
                    $type = new Template;
            
                    // If alert transport mapping exists, override the default transports
            Severity: Minor
            Found in LibreNMS/Alert/RunAlerts.php - About 1 hr to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Function populate has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
            Open

                public function populate($txt, $wrap = true)
                {
                    preg_match_all('/%([\w\.]+)/', $txt, $m);
                    foreach ($m[1] as $tmp) {
                        $orig = $tmp;
            Severity: Minor
            Found in LibreNMS/Alert/RunAlerts.php - About 1 hr to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

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

                public function loadAlerts($where)
                {
                    $alerts = [];
                    foreach (dbFetchRows("SELECT alerts.id, alerts.alerted, alerts.device_id, alerts.rule_id, alerts.state, alerts.note, alerts.info FROM alerts WHERE $where") as $alert_status) {
                        $alert = dbFetchRow(
            Severity: Minor
            Found in LibreNMS/Alert/RunAlerts.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

            Consider simplifying this complex logical expression.
            Open

                                if (! empty($v) && $k != 'device_id' && (stristr($k, 'id') || stristr($k, 'desc') || stristr($k, 'msg')) && substr_count($k, '_') <= 1) {
                                    $obj['faults'][$i]['string'] .= $k . ' => ' . $v . '; ';
                                }
            Severity: Major
            Found in LibreNMS/Alert/RunAlerts.php - About 40 mins to fix

              Consider simplifying this complex logical expression.
              Open

                                  if (! empty($v) && $k != 'device_id' && (stristr($k, 'id') || stristr($k, 'desc') || stristr($k, 'msg')) && substr_count($k, '_') <= 1) {
                                      $obj['faults'][$i]['string'] .= $k . ' = ' . $v . '; ';
                                  }
              Severity: Major
              Found in LibreNMS/Alert/RunAlerts.php - About 40 mins to fix

                Function issueAlert has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                Open

                    public function issueAlert($alert)
                    {
                        if (Config::get('alert.fixed-contacts') == false) {
                            if (empty($alert['query'])) {
                                $alert['query'] = AlertDB::genSQL($alert['rule'], $alert['builder']);
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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

                The method runAlerts() has 102 lines of code. Current threshold is set to 100. Avoid really long methods.
                Open

                    public function runAlerts()
                    {
                        foreach ($this->loadAlerts('alerts.state != ' . AlertState::ACKNOWLEDGED . ' && alerts.open = 1') as $alert) {
                            $noiss = false;
                            $noacc = false;
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by phpmd

                The method describeAlert() has 115 lines of code. Current threshold is set to 100. Avoid really long methods.
                Open

                    public function describeAlert($alert)
                    {
                        $obj = [];
                        $i = 0;
                        $device = DeviceCache::get($alert['device_id']);
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by phpmd

                The method describeAlert() has an NPath complexity of 3460. The configured NPath complexity threshold is 200.
                Open

                    public function describeAlert($alert)
                    {
                        $obj = [];
                        $i = 0;
                        $device = DeviceCache::get($alert['device_id']);
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 runAlerts() has an NPath complexity of 306433. The configured NPath complexity threshold is 200.
                Open

                    public function runAlerts()
                    {
                        foreach ($this->loadAlerts('alerts.state != ' . AlertState::ACKNOWLEDGED . ' && alerts.open = 1') as $alert) {
                            $noiss = false;
                            $noacc = false;
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 describeAlert() has a Cyclomatic Complexity of 33. The configured cyclomatic complexity threshold is 10.
                Open

                    public function describeAlert($alert)
                    {
                        $obj = [];
                        $i = 0;
                        $device = DeviceCache::get($alert['device_id']);
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 runAlerts() has a Cyclomatic Complexity of 37. The configured cyclomatic complexity threshold is 10.
                Open

                    public function runAlerts()
                    {
                        foreach ($this->loadAlerts('alerts.state != ' . AlertState::ACKNOWLEDGED . ' && alerts.open = 1') as $alert) {
                            $noiss = false;
                            $noacc = false;
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 runFollowUp() has a Cyclomatic Complexity of 13. The configured cyclomatic complexity threshold is 10.
                Open

                    public function runFollowUp()
                    {
                        foreach ($this->loadAlerts('alerts.state > ' . AlertState::CLEAR . ' && alerts.open = 0') as $alert) {
                            if ($alert['state'] != AlertState::ACKNOWLEDGED || ($alert['info']['until_clear'] === false)) {
                                $rextra = json_decode($alert['extra'], true);
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 RunAlerts has a coupling between objects value of 15. Consider to reduce the number of dependencies under 13.
                Open

                class RunAlerts
                {
                    /**
                     * Populate variables
                     *
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 populate has a boolean flag argument $wrap, which is a certain sign of a Single Responsibility Principle violation.
                Open

                    public function populate($txt, $wrap = true)
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 runAlerts uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                            } else {
                                // This is the new way
                                if (! empty($rextra['delay']) && (time() - strtotime($alert['time_logged']) + $tolerence_window) < $rextra['delay'] && $alert['state'] != AlertState::RECOVERED) {
                                    continue;
                                }
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 populate uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                            } else {
                                if (strstr($tmp, '.')) {
                                    $tmp = explode('.', $tmp, 2);
                                    $pre = '$' . $tmp[0];
                                    $tmp = $tmp[1];
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 alertLog uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                        } else {
                            echo "ERROR: $result\r\n";
                            Eventlog::log('Could not issue ' . $prefix[$obj['state']] . " for rule '" . $obj['name'] . "' to transport '" . $transport . "' Error: " . $result, $obj['device_id'], 'error', Severity::Error);
                        }
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 describeAlert uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                        } else {
                            return 'Unknown State';
                        }//end if
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 loadAlerts uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                            } else {
                                $alert['alert_id'] = $alert_status['id'];
                                $alert['state'] = $alert_status['state'];
                                $alert['alerted'] = $alert_status['alerted'];
                                $alert['note'] = $alert_status['note'];
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 runAlerts uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                                    } else {
                                        $alert['details']['interval'] = time();
                                        $updet = true;
                                    }
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 populate uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                                } else {
                                    $pre = '$obj';
                                }
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 runAlerts uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
                Open

                                    } else {
                                        $alert['details']['delay'] = time();
                                        $updet = true;
                                    }
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 remove Deprecated transport
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by fixme

                Similar blocks of code found in 2 locations. Consider refactoring.
                Open

                                foreach ($incident as $k => $v) {
                                    if (! empty($v) && $k != 'device_id' && (stristr($k, 'id') || stristr($k, 'desc') || stristr($k, 'msg')) && substr_count($k, '_') <= 1) {
                                        $obj['faults'][$i]['string'] .= $k . ' => ' . $v . '; ';
                                    }
                                }
                Severity: Major
                Found in LibreNMS/Alert/RunAlerts.php and 1 other location - About 1 hr to fix
                LibreNMS/Alert/RunAlerts.php on lines 146..150

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 116.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                Similar blocks of code found in 2 locations. Consider refactoring.
                Open

                                foreach ($incident as $k => $v) {
                                    if (! empty($v) && $k != 'device_id' && (stristr($k, 'id') || stristr($k, 'desc') || stristr($k, 'msg')) && substr_count($k, '_') <= 1) {
                                        $obj['faults'][$i]['string'] .= $k . ' = ' . $v . '; ';
                                    }
                                }
                Severity: Major
                Found in LibreNMS/Alert/RunAlerts.php and 1 other location - About 1 hr to fix
                LibreNMS/Alert/RunAlerts.php on lines 179..183

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 116.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

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

                                $n = count($chk);
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 $id. Configured minimum length is 3.
                Open

                            $id = dbFetchRow('SELECT alert_log.id,alert_log.time_logged,alert_log.details FROM alert_log WHERE alert_log.state != ? && alert_log.state != ? && alert_log.rule_id = ? && alert_log.device_id = ? && alert_log.id < ? ORDER BY id DESC LIMIT 1', [AlertState::ACKNOWLEDGED, AlertState::RECOVERED, $alert['rule_id'], $alert['device_id'], $alert['id']]);
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 $i. Configured minimum length is 3.
                Open

                        $i = 0;
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 $m. Configured minimum length is 3.
                Open

                        preg_match_all('/%([\w\.]+)/', $txt, $m);
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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 $o. Configured minimum length is 3.
                Open

                                $o = count($alert['details']['rule']);
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.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

                Line indented incorrectly; expected 12 spaces, found 8
                Open

                        } else {
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by phpcodesniffer

                Line indented incorrectly; expected 8 spaces, found 12
                Open

                            default => Severity::Unknown,
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by phpcodesniffer

                Line indented incorrectly; expected at least 16 spaces, found 12
                Open

                            Eventlog::log('Issued ' . $prefix[$obj['state']] . " for rule '" . $obj['name'] . "' to transport '" . $transport . "'", $obj['device_id'], 'alert', $severity);
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by phpcodesniffer

                Line indented incorrectly; expected at least 16 spaces, found 12
                Open

                            echo 'ERROR';
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by phpcodesniffer

                Space before opening parenthesis of function call prohibited
                Open

                        $severity = match ($obj['state']) {
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by phpcodesniffer

                Line indented incorrectly; expected at least 16 spaces, found 12
                Open

                            echo "ERROR: $result\r\n";
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by phpcodesniffer

                Line indented incorrectly; expected 12 spaces, found 8
                Open

                        } elseif ($result === false) {
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by phpcodesniffer

                Line indented incorrectly; expected 12 spaces, found 8
                Open

                        if ($result === true) {
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by phpcodesniffer

                Line indented incorrectly; expected at least 16 spaces, found 12
                Open

                            echo 'OK';
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by phpcodesniffer

                Line indented incorrectly; expected at least 16 spaces, found 12
                Open

                            Eventlog::log('Could not issue ' . $prefix[$obj['state']] . " for rule '" . $obj['name'] . "' to transport '" . $transport . "'", $obj['device_id'], null, Severity::Error);
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by phpcodesniffer

                Line indented incorrectly; expected at least 16 spaces, found 12
                Open

                            Eventlog::log('Could not issue ' . $prefix[$obj['state']] . " for rule '" . $obj['name'] . "' to transport '" . $transport . "' Error: " . $result, $obj['device_id'], 'error', Severity::Error);
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by phpcodesniffer

                Line indented incorrectly; expected 12 spaces, found 8
                Open

                        }
                Severity: Minor
                Found in LibreNMS/Alert/RunAlerts.php by phpcodesniffer

                There are no issues that match your filters.

                Category
                Status