Troubleshooting of SonarQube issue: Difference between revisions

+ How to resolve "Define a constant instead of duplicating this literal"
Tag: wikieditor
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
Troubleshooting of SonarQube issue
Troubleshooting of SonarQube issue
{{Draft}}


{{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. (Source: [https://en.wikipedia.org/wiki/SonarQube Wikipedia])}}
Line 61: Line 59:
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>
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:
Original problematic code (Python):
<pre>
<pre>
def parse_date(row):
def parse_date(row):
Line 73: Line 71:
</pre>
</pre>


Possible solution:
Possible solution: define the constants
<pre>
<pre>
# Date format constants
# Date format constants
Line 92: Line 90:
# Consistency: All date formatting uses the same format definitions
# Consistency: All date formatting uses the same format definitions
# Error Prevention: Reduces risk of typos in repeated string literals
# 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 211: 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'''