15,076
edits
No edit summary |
m (Planetoid moved page Troubleshooting of Sonar issue to Troubleshooting of SonarQube issue) |
||
| (6 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
Troubleshooting of SonarQube issue | Troubleshooting of SonarQube issue | ||
{{Tip | tip='''What is SonarQube?''' SonarQube is an open-source platform for continuous code quality inspection using static program analysis to detect bugs and code smells across multiple programming languages. It provides automated analysis reports and integrates seamlessly with various development tools. (Source: [https://en.wikipedia.org/wiki/SonarQube Wikipedia])}} | |||
{{Tip | tip='''What is SonarQube?''' SonarQube is an open-source platform for continuous code quality inspection using static program analysis to detect bugs and code smells across multiple programming languages. It provides automated analysis reports and integrates seamlessly with various development tools.}} | |||
== SonarQube issues == | == SonarQube issues == | ||
| Line 55: | Line 53: | ||
* Grant the execution permission {{kbd | key=chmod +x add_newline_to_php_files.sh}} | * 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>}} | * Usage {{kbd | key=<nowiki>./add_newline_to_php_files.sh /path/to/php/directory_of_scripts</nowiki>}} | ||
=== How to resolve "Define a constant instead of duplicating this literal" === | |||
Error condition which met "Define a constant instead of duplicating this literal" - violates DRY (Don't Repeat Yourself) principle and creates maintenance issues <ref>[Clean Code principles - Avoid Magic Numbers/Strings]</ref> | |||
Original problematic code (Python): | |||
<pre> | |||
def parse_date(row): | |||
if isinstance(row['time_field'], pd.Timestamp): | |||
formatted_date = row['time_field'].strftime("%Y-%m") | |||
elif isinstance(row['time_field'], str): | |||
date_obj = datetime.strptime(row['time_field'], "%Y-%m-%d %H:%M:%S") | |||
formatted_date = date_obj.strftime("%Y-%m") | |||
elif isinstance(row['time_field'], datetime): | |||
formatted_date = row['time_field'].strftime("%Y-%m") | |||
</pre> | |||
Possible solution: define the constants | |||
<pre> | |||
# Date format constants | |||
DATE_FORMAT_YEAR_MONTH = "%Y-%m" | |||
def parse_date(row): | |||
if isinstance(row['time_field'], pd.Timestamp): | |||
formatted_date = row['time_field'].strftime(DATE_FORMAT_YEAR_MONTH) | |||
elif isinstance(row['time_field'], str): | |||
date_obj = datetime.strptime(row['time_field'], "%Y-%m-%d %H:%M:%S") | |||
formatted_date = date_obj.strftime(DATE_FORMAT_YEAR_MONTH) | |||
elif isinstance(row['time_field'], datetime): | |||
formatted_date = row['time_field'].strftime(DATE_FORMAT_YEAR_MONTH) | |||
</pre> | |||
Benefits: | |||
# Maintainability: Format strings can be updated in one central location | |||
# Consistency: All date formatting uses the same format definitions | |||
# Error Prevention: Reduces risk of typos in repeated string literals | |||
=== 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" === | ||
| Line 173: | Line 208: | ||
CMD ["your-app-command"] | CMD ["your-app-command"] | ||
</pre> | </pre> | ||
Another example | |||
<pre> | |||
FROM dockerhub/library/php:8.3.4-cli-alpine3.19 | |||
# Update and upgrade all packages to the latest secure versions | |||
RUN apk update && apk upgrade | |||
# Create a non-root system group and user to run the application securely | |||
# -S flag creates a system account (no home directory, no login shell) | |||
RUN addgroup -S appgroup && adduser -S appuser -G appgroup | |||
# Copy application source code into the container | |||
COPY app /var/www/html | |||
# Transfer ownership of the app directory to the non-root user | |||
# so the app can read/write files without requiring root privileges | |||
RUN chown -R appuser:appgroup /var/www/html | |||
# Set the working directory for subsequent commands | |||
WORKDIR /var/www/html | |||
# Switch to the non-root user before running the application | |||
# This follows the Principle of Least Privilege | |||
USER appuser | |||
# Expose port 80 for incoming HTTP traffic | |||
EXPOSE 80 | |||
# Start the PHP built-in web server, listening on all interfaces | |||
CMD ["php", "-S", "0.0.0.0:80"] | |||
</pre> | |||
'''💡 Explanation''' | '''💡 Explanation''' | ||