howardjones/network-weathermap

View on GitHub
random-bits/cacti-integrate.php

Summary

Maintainability
C
1 day
Test Coverage

File cacti-integrate.php has 306 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
/** cacti-integrate.php
 *
 * Auto-fill a basic map file with as much information as possible from the
 * Cacti database, using interface names and node ip/names as clues.
Severity: Minor
Found in random-bits/cacti-integrate.php - About 3 hrs to fix

    Define a constant instead of duplicating this literal "cacti_id" 5 times.
    Open

        $host_id = $node->get_hint("cacti_id");
    Severity: Critical
    Found in random-bits/cacti-integrate.php by sonar-php

    Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

    On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

    Noncompliant Code Example

    With the default threshold of 3:

    function run() {
      prepare('action1');                              // Non-Compliant - 'action1' is duplicated 3 times
      execute('action1');
      release('action1');
    }
    

    Compliant Solution

    ACTION_1 = 'action1';
    
    function run() {
      prepare(ACTION_1);
      execute(ACTION_1);
      release(ACTION_1);
    }
    

    Exceptions

    To prevent generating some false-positives, literals having less than 5 characters are excluded.

    Define a constant instead of duplicating this literal "url_path" 5 times.
    Open

    $cacti_url = $config['url_path'];
    Severity: Critical
    Found in random-bits/cacti-integrate.php by sonar-php

    Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

    On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

    Noncompliant Code Example

    With the default threshold of 3:

    function run() {
      prepare('action1');                              // Non-Compliant - 'action1' is duplicated 3 times
      execute('action1');
      release('action1');
    }
    

    Compliant Solution

    ACTION_1 = 'action1';
    
    function run() {
      prepare(ACTION_1);
      execute(ACTION_1);
      release(ACTION_1);
    }
    

    Exceptions

    To prevent generating some false-positives, literals having less than 5 characters are excluded.

    Define a constant instead of duplicating this literal "include/global.php" 4 times.
    Open

    @include_once $cacti_root."include/global.php";
    Severity: Critical
    Found in random-bits/cacti-integrate.php by sonar-php

    Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

    On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

    Noncompliant Code Example

    With the default threshold of 3:

    function run() {
      prepare('action1');                              // Non-Compliant - 'action1' is duplicated 3 times
      execute('action1');
      release('action1');
    }
    

    Compliant Solution

    ACTION_1 = 'action1';
    
    function run() {
      prepare(ACTION_1);
      execute(ACTION_1);
      release(ACTION_1);
    }
    

    Exceptions

    To prevent generating some false-positives, literals having less than 5 characters are excluded.

    Remove this commented out code.
    Open

    // $cacti_root = "/var/www/docs/cacti/";
    Severity: Major
    Found in random-bits/cacti-integrate.php by sonar-php

    Programmers should not comment out code as it bloats programs and reduces readability.

    Unused code should be deleted and can be retrieved from source control history if required.

    See

    • MISRA C:2004, 2.4 - Sections of code should not be "commented out".
    • MISRA C++:2008, 2-7-2 - Sections of code shall not be "commented out" using C-style comments.
    • MISRA C++:2008, 2-7-3 - Sections of code should not be "commented out" using C++ comments.
    • MISRA C:2012, Dir. 4.4 - Sections of code should not be "commented out"

    Define a constant instead of duplicating this literal "base_url" 3 times.
    Open

        $config['base_url'] = (isset($config['url_path']) ? $config['url_path'] : $cacti_url);
    Severity: Critical
    Found in random-bits/cacti-integrate.php by sonar-php

    Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

    On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

    Noncompliant Code Example

    With the default threshold of 3:

    function run() {
      prepare('action1');                              // Non-Compliant - 'action1' is duplicated 3 times
      execute('action1');
      release('action1');
    }
    

    Compliant Solution

    ACTION_1 = 'action1';
    
    function run() {
      prepare(ACTION_1);
      execute(ACTION_1);
      release(ACTION_1);
    }
    

    Exceptions

    To prevent generating some false-positives, literals having less than 5 characters are excluded.

    Define a constant instead of duplicating this literal "include/config.php" 6 times.
    Open

    @include_once $cacti_root."include/config.php";
    Severity: Critical
    Found in random-bits/cacti-integrate.php by sonar-php

    Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

    On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

    Noncompliant Code Example

    With the default threshold of 3:

    function run() {
      prepare('action1');                              // Non-Compliant - 'action1' is duplicated 3 times
      execute('action1');
      release('action1');
    }
    

    Compliant Solution

    ACTION_1 = 'action1';
    
    function run() {
      prepare(ACTION_1);
      execute(ACTION_1);
      release(ACTION_1);
    }
    

    Exceptions

    To prevent generating some false-positives, literals having less than 5 characters are excluded.

    Add curly braces around the nested statement(s).
    Open

                            if ($hspeed && intval($hspeed) > 20)
    Severity: Critical
    Found in random-bits/cacti-integrate.php by sonar-php

    While not technically incorrect, the omission of curly braces can be misleading, and may lead to the introduction of errors during maintenance.

    Noncompliant Code Example

    if (condition)  // Noncompliant
      executeSomething();
    

    Compliant Solution

    if (condition) {
      executeSomething();
    }
    

    See

    • MISRA C:2004, 14.8 - The statement forming the body of a switch, while, do ... while or for statement shall be a compound statement
    • MISRA C:2004, 14.9 - An if (expression) construct shall be followed by a compound statement. The else keyword shall be followed by either a compound statement, or another if statement
    • MISRA C++:2008, 6-3-1 - The statement forming the body of a switch, while, do ... while or for statement shall be a compound statement
    • MISRA C++:2008, 6-4-1 - An if (condition) construct shall be followed by a compound statement. The else keyword shall be followed by either a compound statement, or another if statement
    • MISRA C:2012, 15.6 - The body of an iteration-statement or a selection-statement shall be a compound-statement
    • CERT, EXP19-C. - Use braces for the body of an if, for, or while statement
    • CERT, EXP52-J. - Use braces for the body of an if, for, or while statement

    Add a "case default" clause to this "switch" statement.
    Open

            switch ($o[0]) {
    Severity: Critical
    Found in random-bits/cacti-integrate.php by sonar-php

    The requirement for a final case default clause is defensive programming. The clause should either take appropriate action, or contain a suitable comment as to why no action is taken. Even when the switch covers all current values of an enum, a default case should still be used because there is no guarantee that the enum won't be extended.

    Noncompliant Code Example

    switch ($param) {  //missing default clause
      case 0:
        do_something();
        break;
      case 1:
        do_something_else();
        break;
    }
    
    switch ($param) {
      default: // default clause should be the last one
        error();
        break;
      case 0:
        do_something();
        break;
      case 1:
        do_something_else();
        break;
    }
    

    Compliant Solution

    switch ($param) {
      case 0:
        do_something();
        break;
      case 1:
        do_something_else();
        break;
      default:
        error();
        break;
    }
    

    See

    • MISRA C:2004, 15.0 - The MISRA C switch syntax shall be used.
    • MISRA C:2004, 15.3 - The final clause of a switch statement shall be the default clause
    • MISRA C++:2008, 6-4-3 - A switch statement shall be a well-formed switch statement.
    • MISRA C++:2008, 6-4-6 - The final clause of a switch statement shall be the default-clause
    • MISRA C:2012, 16.1 - All switch statements shall be well-formed
    • MISRA C:2012, 16.4 - Every switch statement shall have a default label
    • MISRA C:2012, 16.5 - A default label shall appear as either the first or the last switch label of a switch statement
    • MITRE, CWE-478 - Missing Default Case in Switch Statement
    • CERT, MSC01-C. - Strive for logical completeness
    • CERT, MSC01-CPP. - Strive for logical completeness

    Define a constant instead of duplicating this literal "address" 3 times.
    Open

        $address = $node->get_hint("address");
    Severity: Critical
    Found in random-bits/cacti-integrate.php by sonar-php

    Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

    On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

    Noncompliant Code Example

    With the default threshold of 3:

    function run() {
      prepare('action1');                              // Non-Compliant - 'action1' is duplicated 3 times
      execute('action1');
      release('action1');
    }
    

    Compliant Solution

    ACTION_1 = 'action1';
    
    function run() {
      prepare(ACTION_1);
      execute(ACTION_1);
      release(ACTION_1);
    }
    

    Exceptions

    To prevent generating some false-positives, literals having less than 5 characters are excluded.

    Add curly braces around the nested statement(s).
    Open

                            if ($res4)
    Severity: Critical
    Found in random-bits/cacti-integrate.php by sonar-php

    While not technically incorrect, the omission of curly braces can be misleading, and may lead to the introduction of errors during maintenance.

    Noncompliant Code Example

    if (condition)  // Noncompliant
      executeSomething();
    

    Compliant Solution

    if (condition) {
      executeSomething();
    }
    

    See

    • MISRA C:2004, 14.8 - The statement forming the body of a switch, while, do ... while or for statement shall be a compound statement
    • MISRA C:2004, 14.9 - An if (expression) construct shall be followed by a compound statement. The else keyword shall be followed by either a compound statement, or another if statement
    • MISRA C++:2008, 6-3-1 - The statement forming the body of a switch, while, do ... while or for statement shall be a compound statement
    • MISRA C++:2008, 6-4-1 - An if (condition) construct shall be followed by a compound statement. The else keyword shall be followed by either a compound statement, or another if statement
    • MISRA C:2012, 15.6 - The body of an iteration-statement or a selection-statement shall be a compound-statement
    • CERT, EXP19-C. - Use braces for the body of an if, for, or while statement
    • CERT, EXP52-J. - Use braces for the body of an if, for, or while statement

    Define a constant instead of duplicating this literal "hostname" 5 times.
    Open

        $hostname = $node->get_hint("hostname");
    Severity: Critical
    Found in random-bits/cacti-integrate.php by sonar-php

    Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

    On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

    Noncompliant Code Example

    With the default threshold of 3:

    function run() {
      prepare('action1');                              // Non-Compliant - 'action1' is duplicated 3 times
      execute('action1');
      release('action1');
    }
    

    Compliant Solution

    ACTION_1 = 'action1';
    
    function run() {
      prepare(ACTION_1);
      execute(ACTION_1);
      release(ACTION_1);
    }
    

    Exceptions

    To prevent generating some false-positives, literals having less than 5 characters are excluded.

    Add curly braces around the nested statement(s).
    Open

                            else if ($speed)
    Severity: Critical
    Found in random-bits/cacti-integrate.php by sonar-php

    While not technically incorrect, the omission of curly braces can be misleading, and may lead to the introduction of errors during maintenance.

    Noncompliant Code Example

    if (condition)  // Noncompliant
      executeSomething();
    

    Compliant Solution

    if (condition) {
      executeSomething();
    }
    

    See

    • MISRA C:2004, 14.8 - The statement forming the body of a switch, while, do ... while or for statement shall be a compound statement
    • MISRA C:2004, 14.9 - An if (expression) construct shall be followed by a compound statement. The else keyword shall be followed by either a compound statement, or another if statement
    • MISRA C++:2008, 6-3-1 - The statement forming the body of a switch, while, do ... while or for statement shall be a compound statement
    • MISRA C++:2008, 6-4-1 - An if (condition) construct shall be followed by a compound statement. The else keyword shall be followed by either a compound statement, or another if statement
    • MISRA C:2012, 15.6 - The body of an iteration-statement or a selection-statement shall be a compound-statement
    • CERT, EXP19-C. - Use braces for the body of an if, for, or while statement
    • CERT, EXP52-J. - Use braces for the body of an if, for, or while statement

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

    if (is_dir($cacti_base) && file_exists($cacti_base . "include/global.php")) {
        // include the cacti-config, so we know about the database
        include_once $cacti_base . "include/global.php";
        $config['base_url'] = (isset($config['url_path']) ? $config['url_path'] : $cacti_url);
        $cacti_found = true;
    Severity: Major
    Found in random-bits/cacti-integrate.php and 1 other location - About 3 hrs to fix
    random-bits/cacti-mapper.php on lines 14..27

    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 156.

    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

            if ($address != '') {
                $res2 = db_fetch_row("select id,description from host where hostname='" . mysql_real_escape_string($address)
                    . "'");
    
                if ($res2) {
    Severity: Major
    Found in random-bits/cacti-integrate.php and 1 other location - About 1 hr to fix
    random-bits/cacti-integrate.php on lines 224..238

    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 106.

    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

            } elseif ($hostname != '') {
                $res3 =
                    db_fetch_row("select id,hostname from host where description='" . mysql_real_escape_string($hostname)
                        . "'");
    
    
    Severity: Major
    Found in random-bits/cacti-integrate.php and 1 other location - About 1 hr to fix
    random-bits/cacti-integrate.php on lines 211..238

    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 106.

    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

    There are no issues that match your filters.

    Category
    Status