SiLeBAT/FSK-Lab

View on GitHub
org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java

Summary

Maintainability
F
3 days
Test Coverage

Method fire has a Cognitive Complexity of 85 (exceeds 5 allowed). Consider refactoring.
Open

    @Override
    public void fire(final int triggerType, final String triggerName, final String tableName, final Object rowBefore[], final Object rowAfter[]) {
        try {
            if (triggerType == Trigger.INSERT_BEFORE_ROW || triggerType == Trigger.UPDATE_BEFORE_ROW || triggerType == Trigger.DELETE_BEFORE_ROW) {
                if (tableName.equals("Users")) {
Severity: Minor
Found in org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java - 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

Method changeUser has a Cognitive Complexity of 25 (exceeds 5 allowed). Consider refactoring.
Open

  private void changeUser(final Object oldUser[], final Object newUser[]) {
        if (newUser != null && newUser[1] != null && newUser[1].toString().length() > 0) {
            String newUsername = newUser[1].toString();
            int newAccRight = Users.READ_ONLY;
            if (newUser[4] != null && newUser[4] instanceof Integer) {
Severity: Minor
Found in org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java - About 3 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 fire has 68 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    @Override
    public void fire(final int triggerType, final String triggerName, final String tableName, final Object rowBefore[], final Object rowAfter[]) {
        try {
            if (triggerType == Trigger.INSERT_BEFORE_ROW || triggerType == Trigger.UPDATE_BEFORE_ROW || triggerType == Trigger.DELETE_BEFORE_ROW) {
                if (tableName.equals("Users")) {
Severity: Major
Found in org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java - About 2 hrs to fix

Method insertIntoChangeLog has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
Open

    private boolean insertIntoChangeLog(final String tablename, final Object[] rowBefore, final Object[] rowAfter, final boolean suppressWarnings) {
        if (MainKernel.dontLog || DBKernel.dontLog) return true;
        else {
            boolean diff = different(rowBefore, rowAfter);
            if (!diff) return true;
Severity: Minor
Found in org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java - About 2 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

File MyTrigger.java has 253 lines of code (exceeds 250 allowed). Consider refactoring.
Open

/*******************************************************************************
 * Copyright (c) 2015 Federal Institute for Risk Assessment (BfR), Germany
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
Severity: Minor
Found in org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java - About 2 hrs to fix

Method different has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
Open

    private boolean different(final Object[] rowBefore,
            final Object[] rowAfter) {
        if (rowBefore == null && rowAfter == null) {
            return false;
        }
Severity: Minor
Found in org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java - 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 insertIntoChangeLog has 40 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    private boolean insertIntoChangeLog(final String tablename, final Object[] rowBefore, final Object[] rowAfter, final boolean suppressWarnings) {
        if (MainKernel.dontLog || DBKernel.dontLog) return true;
        else {
            boolean diff = different(rowBefore, rowAfter);
            if (!diff) return true;
Severity: Minor
Found in org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java - About 1 hr to fix

Method fire has 5 arguments (exceeds 4 allowed). Consider refactoring.
Open

    public void fire(final int triggerType, final String triggerName, final String tableName, final Object rowBefore[], final Object rowAfter[]) {
Severity: Minor
Found in org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java - About 35 mins to fix

Avoid too many return statements within this method.
Open

        return false;
Severity: Major
Found in org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java - About 30 mins to fix

Refactor this method to reduce its Cognitive Complexity from 25 to the 15 allowed.
Open

  private void changeUser(final Object oldUser[], final Object newUser[]) {

Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.

See

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.
Open

    private boolean different(final Object[] rowBefore,

Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.

See

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.
Open

    private boolean insertIntoChangeLog(final String tablename, final Object[] rowBefore, final Object[] rowAfter, final boolean suppressWarnings) {

Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.

See

Refactor this method to reduce its Cognitive Complexity from 85 to the 15 allowed.
Open

    public void fire(final int triggerType, final String triggerName, final String tableName, final Object rowBefore[], final Object rowAfter[]) {

Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.

See

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

        success = MainKernel.sendRequest("REVOKE " + MainKernel.delimitL("DBA") + " FROM " + MainKernel.delimitL(username) + " RESTRICT", false);

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:

public void run() {
  prepare("action1");                              // Noncompliant - "action1" is duplicated 3 times
  execute("action1");
  release("action1");
}

@SuppressWarning("all")                            // Compliant - annotations are excluded
private void method1() { /* ... */ }
@SuppressWarning("all")
private void method2() { /* ... */ }

public String method3(String a) {
  System.out.println("'" + a + "'");               // Compliant - literal "'" has less than 5 characters and is excluded
  return "";                                       // Compliant - literal "" has less than 5 characters and is excluded
}

Compliant Solution

private static final String ACTION_1 = "action1";  // Compliant

public void run() {
  prepare(ACTION_1);                               // Compliant
  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 "GRANT " 3 times.
Open

        success = MainKernel.sendRequest("GRANT " + MainKernel.delimitL("DBA") + " TO " + MainKernel.delimitL(username), false);

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:

public void run() {
  prepare("action1");                              // Noncompliant - "action1" is duplicated 3 times
  execute("action1");
  release("action1");
}

@SuppressWarning("all")                            // Compliant - annotations are excluded
private void method1() { /* ... */ }
@SuppressWarning("all")
private void method2() { /* ... */ }

public String method3(String a) {
  System.out.println("'" + a + "'");               // Compliant - literal "'" has less than 5 characters and is excluded
  return "";                                       // Compliant - literal "" has less than 5 characters and is excluded
}

Compliant Solution

private static final String ACTION_1 = "action1";  // Compliant

public void run() {
  prepare(ACTION_1);                               // Compliant
  execute(ACTION_1);
  release(ACTION_1);
}

Exceptions

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

Make the enclosing method "static" or remove this set.
Open

                triggerFired = System.currentTimeMillis();

Correctly updating a static field from a non-static method is tricky to get right and could easily lead to bugs if there are multiple class instances and/or multiple threads in play. Ideally, static fields are only updated from synchronized static methods.

This rule raises an issue each time a static field is updated from a non-static method.

Noncompliant Code Example

public class MyClass {

  private static int count = 0;

  public void doSomething() {
    //...
    count++;  // Noncompliant
  }
}

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

        success = MainKernel.sendRequest("REVOKE " + MainKernel.delimitL("DBA") + " FROM " + MainKernel.delimitL(username) + " RESTRICT", false);

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:

public void run() {
  prepare("action1");                              // Noncompliant - "action1" is duplicated 3 times
  execute("action1");
  release("action1");
}

@SuppressWarning("all")                            // Compliant - annotations are excluded
private void method1() { /* ... */ }
@SuppressWarning("all")
private void method2() { /* ... */ }

public String method3(String a) {
  System.out.println("'" + a + "'");               // Compliant - literal "'" has less than 5 characters and is excluded
  return "";                                       // Compliant - literal "" has less than 5 characters and is excluded
}

Compliant Solution

private static final String ACTION_1 = "action1";  // Compliant

public void run() {
  prepare(ACTION_1);                               // Compliant
  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 " RESTRICT" 3 times.
Open

        success = MainKernel.sendRequest("REVOKE " + MainKernel.delimitL("DBA") + " FROM " + MainKernel.delimitL(username) + " RESTRICT", false);

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:

public void run() {
  prepare("action1");                              // Noncompliant - "action1" is duplicated 3 times
  execute("action1");
  release("action1");
}

@SuppressWarning("all")                            // Compliant - annotations are excluded
private void method1() { /* ... */ }
@SuppressWarning("all")
private void method2() { /* ... */ }

public String method3(String a) {
  System.out.println("'" + a + "'");               // Compliant - literal "'" has less than 5 characters and is excluded
  return "";                                       // Compliant - literal "" has less than 5 characters and is excluded
}

Compliant Solution

private static final String ACTION_1 = "action1";  // Compliant

public void run() {
  prepare(ACTION_1);                               // Compliant
  execute(ACTION_1);
  release(ACTION_1);
}

Exceptions

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

Use try-with-resources or close this "!Unknown!" in a "finally" clause.
Open

                PreparedStatement ps = conn.prepareStatement("INSERT INTO " + MainKernel.delimitL("ChangeLog") + " (" + MainKernel.delimitL("ID") + ", "
                        + MainKernel.delimitL("Zeitstempel") + ", " + MainKernel.delimitL("Username") + ", " + MainKernel.delimitL("Tabelle") + ", "
                        + MainKernel.delimitL("TabellenID") + ", " + MainKernel.delimitL("Alteintrag") + ") VALUES (NEXT VALUE FOR "
                        + MainKernel.delimitL("ChangeLogSEQ") + ", ?, ?, ?, ?, ?)");

Connections, streams, files, and other classes that implement the Closeable interface or its super-interface, AutoCloseable, needs to be closed after use. Further, that close call must be made in a finally block otherwise an exception could keep the call from being made. Preferably, when class implements AutoCloseable, resource should be created using "try-with-resources" pattern and will be closed automatically.

Failure to properly close resources will result in a resource leak which could bring first the application and then perhaps the box the application is on to their knees.

Noncompliant Code Example

private void readTheFile() throws IOException {
  Path path = Paths.get(this.fileName);
  BufferedReader reader = Files.newBufferedReader(path, this.charset);
  // ...
  reader.close();  // Noncompliant
  // ...
  Files.lines("input.txt").forEach(System.out::println); // Noncompliant: The stream needs to be closed
}

private void doSomething() {
  OutputStream stream = null;
  try {
    for (String property : propertyList) {
      stream = new FileOutputStream("myfile.txt");  // Noncompliant
      // ...
    }
  } catch (Exception e) {
    // ...
  } finally {
    stream.close();  // Multiple streams were opened. Only the last is closed.
  }
}

Compliant Solution

private void readTheFile(String fileName) throws IOException {
    Path path = Paths.get(fileName);
    try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
      reader.readLine();
      // ...
    }
    // ..
    try (Stream<String> input = Files.lines("input.txt"))  {
      input.forEach(System.out::println);
    }
}

private void doSomething() {
  OutputStream stream = null;
  try {
    stream = new FileOutputStream("myfile.txt");
    for (String property : propertyList) {
      // ...
    }
  } catch (Exception e) {
    // ...
  } finally {
    stream.close();
  }
}

Exceptions

Instances of the following classes are ignored by this rule because close has no effect:

  • java.io.ByteArrayOutputStream
  • java.io.ByteArrayInputStream
  • java.io.CharArrayReader
  • java.io.CharArrayWriter
  • java.io.StringReader
  • java.io.StringWriter

Java 7 introduced the try-with-resources statement, which implicitly closes Closeables. All resources opened in a try-with-resources statement are ignored by this rule.

try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
  //...
}
catch ( ... ) {
  //...
}

See

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

      private Connection getDefaultConnection() {
        Connection result = null;
        String connStr = "jdbc:default:connection";
        try {
            result = DriverManager.getConnection(connStr);
Severity: Minor
Found in org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java and 1 other location - About 40 mins to fix
org.hsh.bfr.db/src/org/hsh/bfr/db/MainKernel.java on lines 195..205

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

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 3 locations. Consider refactoring.
Open

    } else if (oldAccRight == Users.WRITE_ACCESS) {
        success = MainKernel.sendRequest("REVOKE " + MainKernel.delimitL("WRITE_ACCESS") + " FROM " + MainKernel.delimitL(username) + " RESTRICT", false);
    }            
Severity: Minor
Found in org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java and 2 other locations - About 30 mins to fix
org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java on lines 170..172
org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java on lines 168..170

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

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 3 locations. Consider refactoring.
Open

    } else if (oldAccRight == Users.SUPER_WRITE_ACCESS) {
        success = MainKernel.sendRequest("REVOKE " + MainKernel.delimitL("SUPER_WRITE_ACCESS") + " FROM " + MainKernel.delimitL(username) + " RESTRICT", false);
    } else if (oldAccRight == Users.WRITE_ACCESS) {
Severity: Minor
Found in org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java and 2 other locations - About 30 mins to fix
org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java on lines 172..174
org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java on lines 168..170

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

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 3 locations. Consider refactoring.
Open

      if (oldAccRight == Users.ADMIN) {
        success = MainKernel.sendRequest("REVOKE " + MainKernel.delimitL("DBA") + " FROM " + MainKernel.delimitL(username) + " RESTRICT", false);
    } else if (oldAccRight == Users.SUPER_WRITE_ACCESS) {
Severity: Minor
Found in org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java and 2 other locations - About 30 mins to fix
org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java on lines 172..174
org.hsh.bfr.db/src/org/hsh/bfr/db/MyTrigger.java on lines 170..172

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

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