Troubleshooting of Sonar issue: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
|||
| Line 13: | Line 13: | ||
#!/bin/bash | #!/bin/bash | ||
# | # Check if a directory path argument has been provided | ||
if [ "$#" -ne 1 ]; then | if [ "$#" -ne 1 ]; then | ||
echo " | echo "Usage: ./add_newline_to_php_files.sh [Directory Path]" | ||
exit 1 | exit 1 | ||
fi | fi | ||
# | # Retrieve the directory path argument | ||
dir_path="$1" | dir_path="$1" | ||
# | # Iterate through all .php files in the specified directory | ||
for file in "$dir_path"/*.php; do | for file in "$dir_path"/*.php; do | ||
# | # Check if the file exists | ||
if [ -f "$file" ]; then | if [ -f "$file" ]; then | ||
# | # Read the last line of the file | ||
last_line=$(tail -n 1 "$file") | last_line=$(tail -n 1 "$file") | ||
# | # Check if the last line is empty | ||
if [ "$last_line" != "" ]; then | if [ "$last_line" != "" ]; then | ||
# | # If it's not empty, append a newline character | ||
echo "" >> "$file" | echo "" >> "$file" | ||
fi | fi | ||
fi | fi | ||
done | done | ||
</pre> | </pre> | ||
Revision as of 14:21, 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
# 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"
# 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"
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