Troubleshooting of Sonar issue

From LemonWiki共筆
Jump to navigation Jump to search

Troubleshooting of SonarQube issue


icon_scale_pencil.png This article "Troubleshooting of Sonar issue" is still being written. If there are any incomplete parts, you are welcome to directly edit them. 這篇文章「Troubleshooting of Sonar issue」內容還在撰寫中,如果有不完整的部分,歡迎你直接動手修改


SonarQube issues

How to resolve "Add a new line at the end of this file."

Error message: Files should contain an empty newline at the end[1]

Solution:

Another solution:

  • Add an new file add_newline_to_php_files.sh with the following content
#!/bin/bash

# Check if a directory path argument has been provided
if [ "$#" -ne 1 ]; then
    echo "Usage: ./add_newline_to_php_files.sh [Directory Path]"
    exit 1
fi

# Retrieve the directory path argument
dir_path="$1"

# Initialize a counter for modified files
modified_count=0

# Iterate through all .php files in the specified directory
for file in "$dir_path"/*.php; do
    # Check if the file exists
    if [ -f "$file" ]; then
        # Read the last line of the file
        last_line=$(tail -n 1 "$file")
        
        # Check if the last line is empty
        if [ "$last_line" != "" ]; then
            # If it's not empty, append a newline character
            echo "" >> "$file"
            # Increment the modified files counter
            ((modified_count++))
        fi
    fi
done

# Display the number of modified files
echo "Number of modified files: $modified_count"



  • Grant the execution permission chmod +x add_newline_to_php_files.sh
  • Usage ./add_newline_to_php_files.sh /path/to/php/directory_of_scripts

How to resolve "Define and throw a dedicated exception instead of using a generic one"

Error condition which met "Define and throw a dedicated exception instead of using a generic one" [2]

if(is_null($some_variable)){
            $error = 'The variable $some_variable is not defined.';
            throw new Exception($error);
        }

Possible solution

if(!is_null($some_variable)){
            $error = 'The variable $some_variable is not defined.';
            throw new InvalidArgumentException($error);
        }

How to resolve "Merge this if statement with the enclosing one."

Example of Code Not Meeting Standards

if (isTrueCondition1) {
  if (isTrueCondition2) {
    ...
  }
}

Example of Code Meeting Standards

if (isTrueCondition1 && isTrueCondition2) {
  ...
}

Example of Code Not Meeting Standards

if (isTrueCondition1) {
  if (isTrueCondition2) {
    ...
  }

  if (isTrueCondition3) {
    ...
  }
}

Example of Code Meeting Standards

if (isTrueCondition1 && isTrueCondition2) {
    ...
}

if (isTrueCondition1 && isTrueCondition3) {
    ...
}

How to resolve "Method visibility should be explicitly declared"

Possible solution

  • Use a text editor that supports regular expressions
  • Find: ^(\s+)(function)(\s)
  • Replace with: $1public function$3

How to resolve "replace all tab characters in this file by sequences of white-spaces (Tabulation characters should not be used)"

Solution: Using the editor which supports regular expression[3]

  • Replace \t
  • with (four whitespaces)

How to resolve "Refactor this function to reduce its Cognitive Complexity from XX to the 15 allowed."

Possible solution[4][5]

  • Created smaller, more specific functions (following Single Responsibility Principle) to handle specific parts of the logic. Use these secondary functions to make the main function easier to read and understand.
  • Avoid nested conditionals
  • Simplify boolean expressions

How to resolve "This image might run with root as the default user. Make sure it is safe here."

🛑 Problem Condition

During the CI/CD process, security scanning tools such as Anchore or Checkmarx report the following warning:

This image might run with root as the default user. Make sure it is safe here.

This warning appears because the Docker image is configured to run as the root user by default, which poses a potential security risk—especially in production or publicly exposed environments.

✅ Solution

Modify the Dockerfile to create a non-root user and configure the container to run using this user. Here's a general example:

FROM alpine:3.19

# Create a non-root user and group =
RUN addgroup -S appgroup && adduser -S appuser -G appgroup

# Create the working directory and set permissions =
RUN mkdir -p /app && chown -R appuser:appgroup /app

# Copy application files =
COPY app /app
WORKDIR /app

# Switch to the non-root user =
USER appuser

# Expose application port (if applicable) =
EXPOSE 80

# Start your application (example only) =
CMD ["your-app-command"]

💡 Explanation

By default, Docker containers run as the "root" user, which increases the risk of privilege escalation if an attacker exploits a vulnerability. To avoid the risk by creating a dedicated, unprivileged user and ensuring that your application directory has the correct ownership and permissions. Then, you configure the container to run under this non-root user.


References

Unresolved issues

  • SonarLint: Replace "require_once" with namespace import mechanism through the "use" keyword.

Further reading

References


Troubleshooting of ...

Template