Troubleshooting of Sonar issue: Difference between revisions
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
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:
- Add new line at the end of this file
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
- PHP: Exceptions - Manual
- PHP Exceptions 種類與使用情境說明 | Asika Lab 飛鳥實驗室
- Single-responsibility principle - Wikipedia
References
Troubleshooting of ...
- PHP, cUrl, Python, selenium, HTTP status code errors
- Database: SQL syntax debug, MySQL errors, MySQLTuner errors or PostgreSQL errors
- HTML/Javascript: Troubleshooting of javascript, XPath
- Software: Mediawiki, Docker, FTP problems, online conference software
- Test connectivity for the web service, Web Ping, Network problem, Web user behavior, Web scrape troubleshooting
Template