fix: refactor switch cases and reduce method complexity#21
fix: refactor switch cases and reduce method complexity#21sonarqube-agent[bot] wants to merge 1 commit into
Conversation
Fixed issues: - AZZmth6t2HDYqP_Xynw9 for java:S6208 rule - AZZmth7p2HDYqP_XynyL for java:S6208 rule - AZZmth6y2HDYqP_XynxQ for java:S6541 rule - AZZmth6y2HDYqP_XynxJ for java:S6208 rule - AZZmth6y2HDYqP_XynxK for java:S6208 rule Generated by SonarQube Agent (task: f000afe8-2c7a-404f-b2ec-e9f1820c8ab0)
SonarQube reviewer guideSummary: Refactor switch statements to use modern Java syntax (pattern matching and switch expressions) and extract complex logic into helper methods for improved readability. Review Focus: The major refactoring in Start review at:
|




This PR fixes 5 SonarQube issues by merging switch case labels using comma-separated syntax and refactoring the JsonTypeFactory.findSpecifiedType method to reduce cognitive complexity, LOC, and nesting levels. These changes improve code maintainability and align the codebase with modern Java best practices.
View Project in SonarCloud
Fixed Issues
java:S6208 - Merge the previous cases into this one using comma-separated label. • INFO • View issue
Location:
jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/EnumTypeDefinition.java:64Why is this an issue?
In Java 14 there is a new way to write cases in Switch Statement and Expression when the same action should be performed for different cases. Instead of declaring multiples branches with the same action, you can combine all of them in a single case group, separated with commas. It will result in a more concise code and improved readability.
What changed
This hunk merges the separate
case FLOAT:andcase DOUBLE:labels into a single comma-separatedcase FLOAT, DOUBLE:label. This directly fixes the code smell where multiple switch cases with the same action should be combined into a single comma-separated case group, as reported at line 64 of EnumTypeDefinition.java.java:S6208 - Merge the previous cases into this one using comma-separated label. • INFO • View issue
Location:
jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonPrimitiveType.java:67Why is this an issue?
In Java 14 there is a new way to write cases in Switch Statement and Expression when the same action should be performed for different cases. Instead of declaring multiples branches with the same action, you can combine all of them in a single case group, separated with commas. It will result in a more concise code and improved readability.
What changed
This hunk merges the separate
case INT:,case LONG:,case SHORT:, andcase BYTE:labels into a single comma-separatedcase INT, LONG, SHORT, BYTE:label. This directly fixes the code smell about multiple switch cases that perform the same action needing to be combined into a single comma-separated case group, as recommended for Java 14+.java:S6208 - Merge the previous cases into this one using comma-separated label. • INFO • View issue
Location:
jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java:70Why is this an issue?
In Java 14 there is a new way to write cases in Switch Statement and Expression when the same action should be performed for different cases. Instead of declaring multiples branches with the same action, you can combine all of them in a single case group, separated with commas. It will result in a more concise code and improved readability.
What changed
This hunk removes the inline switch statement (which had separate fall-through cases like
case NUMBER:/case NUMBER_FLOAT:andcase STRING:/case SCALAR:) from the main method and moves the adapted-type check up. This reduces the LOC, complexity, nesting, and variable count of the Brain Method. It also eliminates the old-style fall-through cases that triggered the warnings about merging cases using comma-separated labels.java:S6208 - Merge the previous cases into this one using comma-separated label. • INFO • View issue
Location:
jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java:77Why is this an issue?
In Java 14 there is a new way to write cases in Switch Statement and Expression when the same action should be performed for different cases. Instead of declaring multiples branches with the same action, you can combine all of them in a single case group, separated with commas. It will result in a more concise code and improved readability.
What changed
This hunk removes the inline switch statement (which had separate fall-through cases like
case NUMBER:/case NUMBER_FLOAT:andcase STRING:/case SCALAR:) from the main method and moves the adapted-type check up. This reduces the LOC, complexity, nesting, and variable count of the Brain Method. It also eliminates the old-style fall-through cases that triggered the warnings about merging cases using comma-separated labels.java:S6541 - A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 77 to 64, Complexity from 30 to 14, Nesting Level from 7 to 2, Number of Variables from 19 to 6. • INFO • View issue
Location:
jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java:49Why is this an issue?
This issue is raised when Sonar considers that a method is a 'Brain Method'.
A Brain Method is a method that tends to centralize its owner’s class logic and generally performs too many operations. This can include checking too many conditions, using lots of variables, and ultimately making it difficult to understand, maintain and reuse.
It is characterized by high LOC number, high cyclomatic and cognitive complexity, and a large number of variables being used.
What changed
This hunk begins the refactoring of the
findSpecifiedTypemethod (a Brain Method) by extracting the TypeHint logic into a separate helper methodfindSpecifiedTypeForAccessor. It also uses pattern matching for instanceof, reducing code lines and variables in the main method.SonarQube Remediation Agent uses AI. Check for mistakes.