Troubleshooting of Sonar issue: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
No edit summary
mNo edit summary
Line 1: Line 1:


{{Draft}}
{{Draft}}
== How to resolve "Add a new line at the end of this file." ==
Error message: Files should contain an empty newline at the end<ref>[https://gist.github.com/OleksiyRudenko/d51388345ea55767b7672307fe35adf3 Why should text files end with a newline?]</ref>
Solution:
* Add [[Return symbol | new line]] at the end of this file
Another solution:
* Add an new file {{kbd | key=add_newline_to_php_files.sh}} with the following content
<pre>
#!/bin/bash
# 檢查是否有傳入目錄參數
if [ "$#" -ne 1 ]; then
    echo "使用方法:./add_newline_to_php_files.sh [目錄路徑]"
    exit 1
fi
# 獲取目錄路徑參數
dir_path="$1"
# 遍歷指定目錄下的所有 .php 檔案
for file in "$dir_path"/*.php; do
    # 檢查檔案是否存在
    if [ -f "$file" ]; then
        # 讀取檔案的最後一行
        last_line=$(tail -n 1 "$file")
       
        # 檢查最後一行是否是空白行
        if [ "$last_line" != "" ]; then
            # 如果不是空白行,則添加一個換行符
            echo "" >> "$file"
        fi
    fi
done
</pre>
* Grant the execution permission {{kbd | key=chmod +x add_newline_to_php_files.sh}}
* Usage {{kbd | key=<nowiki>./add_newline_to_php_files.sh /path/to/php/directory_of_scripts</nowiki>}}


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

Revision as of 14:19, 8 September 2023


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」內容還在撰寫中,如果有不完整的部分,歡迎你直接動手修改


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

# 檢查是否有傳入目錄參數
if [ "$#" -ne 1 ]; then
    echo "使用方法:./add_newline_to_php_files.sh [目錄路徑]"
    exit 1
fi

# 獲取目錄路徑參數
dir_path="$1"

# 遍歷指定目錄下的所有 .php 檔案
for file in "$dir_path"/*.php; do
    # 檢查檔案是否存在
    if [ -f "$file" ]; then
        # 讀取檔案的最後一行
        last_line=$(tail -n 1 "$file")
        
        # 檢查最後一行是否是空白行
        if [ "$last_line" != "" ]; then
            # 如果不是空白行,則添加一個換行符
            echo "" >> "$file"
        fi
    fi
done

  • 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 "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 17 to the 15 allowed."

Possible solution

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

Further reading

References


Troubleshooting of ...

Template