Troubleshooting of SonarQube issue: Difference between revisions

mNo edit summary
Tag: wikieditor
 
(3 intermediate revisions by the same user not shown)
Line 59: 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 71: Line 71:
</pre>
</pre>


Possible solution:
Possible solution: define the constants
<pre>
<pre>
# Date format constants
# Date format constants
Line 90: 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 209: 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'''