Skip to content

SONARPHP-1819 fix: remove unnecessary exception declarations and use EnumMap#1701

Open
sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20260516-050246-b6ddda3a
Open

SONARPHP-1819 fix: remove unnecessary exception declarations and use EnumMap#1701
sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20260516-050246-b6ddda3a

Conversation

@sonarqube-agent
Copy link
Copy Markdown
Contributor

Removed superfluous 'throws Exception' declarations from five test methods where no checked exceptions are actually thrown, improving code clarity and reducing false positives. Also converted a HashMap to EnumMap in RepeatedComplementOperatorCheck where the map keys are enum values, optimizing memory usage and type safety.

View Project in SonarCloud


Fixed Issues

java:S1130 - Remove the declaration of thrown exception 'java.lang.Exception', as it cannot be thrown from method's body. • MINORView issue

Location: php:php-checks/src/test/java/org/sonar/php/checks/AvoidDESCheckTest.java:25

Why is this an issue?

Superfluous exceptions within throws clauses have negative effects on the readability and maintainability of the code. An exception in a throws clause is superfluous if it is:

What changed

Removes the superfluous 'throws Exception' declaration from the defaultValue() method in AvoidDESCheckTest.java. The method body does not actually throw any checked Exception, so the throws clause was unnecessary. This fixes the code smell where a thrown exception is declared but cannot actually be thrown from the method's body.

--- a/php-checks/src/test/java/org/sonar/php/checks/AvoidDESCheckTest.java
+++ b/php-checks/src/test/java/org/sonar/php/checks/AvoidDESCheckTest.java
@@ -25,1 +25,1 @@ class AvoidDESCheckTest {
-  void defaultValue() throws Exception {
+  void defaultValue() {
java:S1130 - Remove the declaration of thrown exception 'java.lang.Exception', as it cannot be thrown from method's body. • MINORView issue

Location: php:php-checks/src/test/java/org/sonar/php/checks/LocalVariableShadowsClassFieldCheckTest.java:25

Why is this an issue?

Superfluous exceptions within throws clauses have negative effects on the readability and maintainability of the code. An exception in a throws clause is superfluous if it is:

What changed

Removes the superfluous 'throws Exception' declaration from the test method 'test()' in LocalVariableShadowsClassFieldCheckTest.java. The method body does not actually throw any checked exception, so declaring 'throws Exception' is unnecessary and reduces code readability. By changing the method signature from 'void test() throws Exception' to 'void test()', the code smell about a declared thrown exception that cannot be thrown from the method's body is resolved.

--- a/php-checks/src/test/java/org/sonar/php/checks/LocalVariableShadowsClassFieldCheckTest.java
+++ b/php-checks/src/test/java/org/sonar/php/checks/LocalVariableShadowsClassFieldCheckTest.java
@@ -25,1 +25,1 @@ class LocalVariableShadowsClassFieldCheckTest {
-  void test() throws Exception {
+  void test() {
java:S1130 - Remove the declaration of thrown exception 'java.lang.Exception', as it cannot be thrown from method's body. • MINORView issue

Location: php:php-checks/src/test/java/org/sonar/php/checks/SSLHostVerificationDisabledCheckTest.java:24

Why is this an issue?

Superfluous exceptions within throws clauses have negative effects on the readability and maintainability of the code. An exception in a throws clause is superfluous if it is:

What changed

Removes the superfluous 'throws Exception' declaration from the test() method signature. The method body does not actually throw any checked exception, so declaring 'throws Exception' is unnecessary and reduces code readability. This directly addresses the code smell about removing declarations of thrown exceptions that cannot be thrown from the method's body.

--- a/php-checks/src/test/java/org/sonar/php/checks/SSLHostVerificationDisabledCheckTest.java
+++ b/php-checks/src/test/java/org/sonar/php/checks/SSLHostVerificationDisabledCheckTest.java
@@ -24,1 +24,1 @@ class SSLHostVerificationDisabledCheckTest {
-  void test() throws Exception {
+  void test() {
java:S1130 - Remove the declaration of thrown exception 'java.lang.Exception', as it cannot be thrown from method's body. • MINORView issue

Location: php:php-checks/src/test/java/org/sonar/php/checks/phpini/FileUploadsCheckTest.java:38

Why is this an issue?

Superfluous exceptions within throws clauses have negative effects on the readability and maintainability of the code. An exception in a throws clause is superfluous if it is:

What changed

Removes the superfluous 'throws Exception' declaration from the 'fileIssue()' method, since no exception is actually thrown from the method's body. This addresses the code smell where a throws clause declares an exception that cannot be thrown by any execution path of the method.

--- a/php-checks/src/test/java/org/sonar/php/checks/phpini/FileUploadsCheckTest.java
+++ b/php-checks/src/test/java/org/sonar/php/checks/phpini/FileUploadsCheckTest.java
@@ -38,1 +38,1 @@ class FileUploadsCheckTest {
-  void fileIssue() throws Exception {
+  void fileIssue() {
java:S1640 - Convert this Map to an EnumMap. • MINORView issue

Location: php:php-checks/src/main/java/org/sonar/php/checks/RepeatedComplementOperatorCheck.java:45

Why is this an issue?

If all the keys in a Map are values from a single enum, it is recommended to use an EnumMap as the specific implementation. An EnumMap, which has the advantage of knowing all possible keys in advance, is more efficient compared to other implementations, as it can use a simple array as its underlying data structure.

What changed

Replaces the import of HashMap with EnumMap to support the change from using a HashMap to an EnumMap for a Map whose keys are enum values (Kind). This is necessary for the code to compile after the main fix in the other hunk.

--- a/php-checks/src/main/java/org/sonar/php/checks/RepeatedComplementOperatorCheck.java
+++ b/php-checks/src/main/java/org/sonar/php/checks/RepeatedComplementOperatorCheck.java
@@ -19,1 +19,1 @@ package org.sonar.php.checks;
-import java.util.HashMap;
+import java.util.EnumMap;

Have a suggestion or found an issue? Share your feedback here.


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AZkoU-_5IsbR56mqmhGh for java:S1130 rule
- AZkoU_a9IsbR56mqmhIN for java:S1130 rule
- AZkoU_aeIsbR56mqmhIL for java:S1130 rule
- AZkoU-96IsbR56mqmhGT for java:S1130 rule
- AZkoU-9YIsbR56mqmhGQ for java:S1640 rule

Generated by SonarQube Agent (task: de459768-98ee-4fb7-a50d-77c5620a1dab)
@hashicorp-vault-sonar-prod hashicorp-vault-sonar-prod Bot changed the title fix: remove unnecessary exception declarations and use EnumMap SONARPHP-1819 fix: remove unnecessary exception declarations and use EnumMap May 16, 2026
@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod Bot commented May 16, 2026

SONARPHP-1819

@sonar-review-alpha
Copy link
Copy Markdown

sonar-review-alpha Bot commented May 16, 2026

Summary

⚠️ The PR description exceeded the analysis limit and was truncated. The review may not reflect all context.

This PR addresses 5 SonarQube code smells (S1130) by removing unused throws Exception declarations from test methods that don't actually throw checked exceptions. It also optimizes a HashMap to EnumMap in RepeatedComplementOperatorCheck since the map keys are enum values, improving type safety and memory usage.

The changes are minimal: 4 test methods across different test files get their throws clauses removed, and 1 line in production code switches to EnumMap initialization. All modifications are non-functional code quality improvements.

What reviewers should know

What to review:

  1. Test method changes (AvoidDESCheckTest, LocalVariableShadowsClassFieldCheckTest, SSLHostVerificationDisabledCheckTest, FileUploadsCheckTest) — Verify these test methods don't actually throw checked exceptions, making the throws clauses unnecessary. These are straightforward signature cleanups.

  2. EnumMap change (RepeatedComplementOperatorCheck.castFunctions()) — Confirm that switching from HashMap to EnumMap<>(Kind.class) is correct. The map only stores Kind enum keys, so EnumMap is the appropriate type. Check that the behavior is identical (EnumMap preserves iteration order like HashMap does here).

No functional changes — this is purely addressing SonarQube code quality issues. All changes are low-risk and self-contained.


  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

Copy link
Copy Markdown

@sonar-review-alpha sonar-review-alpha Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! ✅

Clean, straightforward PR — all changes are correct and low-risk.

🗣️ Give feedback

@sonarqube-next
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants