Skip to content

Reduce cognitive complexity in client and Jackson code#24

Open
sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20260622-010147-91a2b751
Open

Reduce cognitive complexity in client and Jackson code#24
sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20260622-010147-91a2b751

Conversation

@sonarqube-agent

Copy link
Copy Markdown

This PR was automatically created by the Remediation Agent's Scheduled backlog remediation feature.

Why these issues? I prioritized the highest-severity SonarQube findings that were most directly addressed by the code changes: multiple Cognitive Complexity violations plus one duplicated literal issue. The selected issues are tightly related to local refactors in a small set of modules, which makes them well-suited for a focused PR and increases the likelihood of a clean, coherent fix.

Refactored several oversized methods into smaller helpers across the C# and Java XML clients and Jackson modules, and replaced repeated PHP JSON client type literals with shared constants. This lowers SonarQube cognitive complexity and duplication warnings, making the code easier to read, test, and maintain.

View Project in SonarCloud


Fixed Issues

java:S3776 - Refactor this method to reduce its Cognitive Complexity from 123 to the 15 allowed. • CRITICALView issue

Location: csharp-xml-client/src/main/java/com/webcohesion/enunciate/modules/csharp_client/CSharpXMLClientModule.java:190

Why is this an issue?

Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.

What changed

This hunk turns the top-level method into a simple coordinator: it now delegates the JAX-WS checks and JAXB checks to separate helper methods and returns the accumulated result. That directly helps address the reported excessive cognitive complexity by removing large blocks of nested logic from the flagged method.

--- a/csharp-xml-client/src/main/java/com/webcohesion/enunciate/modules/csharp_client/CSharpXMLClientModule.java
+++ b/csharp-xml-client/src/main/java/com/webcohesion/enunciate/modules/csharp_client/CSharpXMLClientModule.java
@@ -191,0 +192,4 @@ public class CSharpXMLClientModule extends BasicGeneratingModule implements ApiF
+    usesUnmappableElements = checkUnmappableJaxwsElements() || usesUnmappableElements;
+    usesUnmappableElements = checkUnmappableJaxbElements() || usesUnmappableElements;
+    return usesUnmappableElements;
+  }
java:S3776 - Refactor this method to reduce its Cognitive Complexity from 130 to the 15 allowed. • CRITICALView issue

Location: java-xml-client/src/main/java/com/webcohesion/enunciate/modules/java_xml_client/JavaXMLClientModule.java:146

Why is this an issue?

Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.

What changed

This hunk starts extracting setup and control-flow work out of the oversized source-generation method. It moves facet-filter creation into a helper call, keeps the up-to-date check at the top level, and delegates the heavy generation logic to a new method. That directly helps the reported excessive cognitive complexity problem by reducing branching and nesting in the original method.

--- a/java-xml-client/src/main/java/com/webcohesion/enunciate/modules/java_xml_client/JavaXMLClientModule.java
+++ b/java-xml-client/src/main/java/com/webcohesion/enunciate/modules/java_xml_client/JavaXMLClientModule.java
@@ -160,0 +161,16 @@ public class JavaXMLClientModule extends BasicGeneratingModule implements ApiFea
+    FacetFilter facetFilter = createFacetFilter();
+    model.put("isFacetExcluded", new IsFacetExcludedMethod(facetFilter));
+
+    if (isUpToDateWithSources(sourceDir)) {
+      info("Skipping generation of Java client sources as everything appears up-to-date...");
+    }
+    else {
+      generateClientSources(sourceDir, model, conversions, jaxbContext, facetFilter);
+    }
+
+    context.setProperty(LIRBARY_DESCRIPTION_PROPERTY, readLibraryDescription(model));
+
+    return sourceDir;
+  }
+
+  private FacetFilter createFacetFilter() {
java:S3776 - Refactor this method to reduce its Cognitive Complexity from 161 to the 15 allowed. • CRITICALView issue

Location: jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/api/impl/DataTypeExampleImpl.java:138

Why is this an issue?

Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.

What changed

This hunk starts extracting logic out of the overly complex build method by replacing nested type-id and override handling with helper method calls. That directly helps address the reported excessive cognitive complexity by flattening control flow and making the main method easier to read.

--- a/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/api/impl/DataTypeExampleImpl.java
+++ b/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/api/impl/DataTypeExampleImpl.java
@@ -144,4 +144,3 @@ public class DataTypeExampleImpl extends ExampleImpl {
-    if (type.getTypeIdInclusion() == JsonTypeInfo.As.PROPERTY) {
-      if (type.getTypeIdProperty() != null) {
-        node.put(type.getTypeIdProperty(), sourceType.getTypeIdValue());
-      }
+    addTypeIdProperty(node, type, sourceType);
+    if (applyTypeOverride(node, type)) {
+      return;
java:S3776 - Refactor this method to reduce its Cognitive Complexity from 85 to the 15 allowed. • CRITICALView issue

Location: jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java:49

Why is this an issue?

Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.

What changed

This hunk starts refactoring the overly complex method by removing the inlined Accessor-specific logic and delegating it to a new helper, findSpecifiedType(accessor, context). That directly helps with the reported excessive cognitive complexity because the main method now has less branching and nesting, while preserving the same early-return behavior when a specific type is found.

--- a/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java
+++ b/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java
@@ -50,10 +50,4 @@ public class JsonTypeFactory {
-    JsonType jsonType = null;
-
-    if (adaptable instanceof Accessor) {
-      Accessor accessor = (Accessor) adaptable;
-      TypeHint typeHint = accessor.getAnnotation(TypeHint.class);
-      if (typeHint != null) {
-        TypeMirror hint = TypeHintUtils.getTypeHint(typeHint, context.getContext().getProcessingEnvironment(), null);
-        if (hint != null) {
-          return getJsonType(hint, context);
-        }
+    if (adaptable instanceof Accessor accessor) {
+      JsonType specifiedType = findSpecifiedType(accessor, context);
+      if (specifiedType != null) {
+        return specifiedType;
java:S1192 - Define a constant instead of duplicating this literal "String" 17 times. • CRITICALView issue

Location: php-json-client/src/main/java/com/webcohesion/enunciate/modules/php_json_client/ClientClassnameForMethod.java:67

Why is this an issue?

Duplicated string literals make the process of refactoring complex and error-prone, as any change would need to be propagated on all occurrences.

What changed

This hunk introduces shared constants for repeated type-name literals such as String, Boolean, Integer, Object, and Array. It is the foundation of the fix for the duplicated string literal code smell because later hunks can now reference a single definition instead of repeating the same text throughout the class.

--- a/php-json-client/src/main/java/com/webcohesion/enunciate/modules/php_json_client/ClientClassnameForMethod.java
+++ b/php-json-client/src/main/java/com/webcohesion/enunciate/modules/php_json_client/ClientClassnameForMethod.java
@@ -57,0 +58,6 @@ public class ClientClassnameForMethod extends com.webcohesion.enunciate.util.fre
+  private static final String BOOLEAN_TYPE = "Boolean";
+  private static final String INTEGER_TYPE = "Integer";
+  private static final String STRING_TYPE = "String";
+  private static final String OBJECT_TYPE = "Object";
+  private static final String ARRAY_TYPE = "Array";
+

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


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AZZmth6y2HDYqP_XynxM for java:S3776 rule
- AZZmtiC92HDYqP_Xyn2H for java:S1192 rule
- AZZmth2O2HDYqP_Xynvj for java:S3776 rule
- AZZmthz_2HDYqP_Xyntj for java:S3776 rule
- AZZmth912HDYqP_XynzY for java:S3776 rule

Generated by SonarQube Agent (task: ec7c10b5-c52f-4ac6-96b0-824b7ec02fb0)
@sonarqube-agent

Copy link
Copy Markdown
Author

⚠️ This repository does not have a CODEOWNERS file. The PR has been created but has not been automatically assigned to any reviewer. To ensure PRs are reviewed promptly, consider adding a CODEOWNERS file to your repository.

@sonarqubecloud

Copy link
Copy Markdown

SonarQube reviewer guide

Review in SonarQube

Summary: Large-scale refactoring across multiple modules to improve code maintainability through method extraction and increased testability, with no functional changes to the public API.

Review Focus: These are significant refactorings that break down complex methods into smaller, focused helper methods. Pay particular attention to:

  1. Logic preservation—ensure extracted conditions and loops are logically equivalent
  2. Early returns—verify that new guard clauses don't alter control flow
  3. Variable scope changes—confirm variables are properly passed/returned between extracted methods
  4. Java 16+ pattern matching—new instanceof patterns need correct type casting

Start review at: CSharpXMLClientModule.java. This file has the most extensive refactoring (126 to 236 lines) with numerous helper methods extracted from usesUnmappableElements(). Start here because the complexity is highest and proper decomposition of validation logic is critical for maintainability.

💬 Please send your feedback

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues
0 New dependency risks

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

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.

1 participant