Skip to content

Reduce method and constructor parameters to fix SonarQube violations - #1808

Open
sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20260915-230108-5fdbbb98
Open

sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20260915-230108-5fdbbb98

Conversation

@sonarqube-agent

Copy link
Copy Markdown
Contributor

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

Why these issues? Rule java:S107 (excessive method/constructor parameters) represents the highest-value violation group with 5 MAJOR severity issues across 2 files. These violations are highly automatable through parameter refactoring patterns (tuples and helper objects), and fixing all instances in the affected files achieves maximum ROI for a single, coherent PR that improves overall code quality.

This PR fixes 5 MAJOR SonarQube issues (java:S107) by reducing parameter counts in TreeFactory and UseStatementTreeImpl classes. The changes refactor methods to use tuple parameters and helper records (GroupPrefix, FunctionExpressionHeader) to keep parameter counts at or below the 7-parameter threshold, improving code maintainability and compliance with SonarQube standards.

View Project in SonarCloud


Fixed Issues

java:S107 - Method has 8 parameters, which is greater than 7 authorized. • MAJORView issue

Location: php:php-frontend/src/main/java/org/sonar/php/parser/TreeFactory.java:1669

Why is this an issue?

Methods with a long parameter list are difficult to use because maintainers must figure out the role of each parameter and keep track of their position.

What changed

Combines the DOUBLEARROW token and EXPRESSION into a single tuple parameter using f.newTuple(), reducing the number of arguments passed to the arrowFunctionExpression method from 8 to 7. This fixes the too-many-parameters code smell on the arrowFunctionExpression method at line 1669.

--- a/php-frontend/src/main/java/org/sonar/php/parser/PHPGrammar.java
+++ b/php-frontend/src/main/java/org/sonar/php/parser/PHPGrammar.java
@@ -1913,2 +1919,1 @@ public class PHPGrammar {
-        b.token(DOUBLEARROW),
-        EXPRESSION()));
+        f.newTuple(b.token(DOUBLEARROW), EXPRESSION())));
java:S107 - Constructor has 9 parameters, which is greater than 7 authorized. • MAJORView issue

Location: php:php-frontend/src/main/java/org/sonar/php/tree/impl/statement/UseStatementTreeImpl.java:44

Why is this an issue?

Methods with a long parameter list are difficult to use because maintainers must figure out the role of each parameter and keep track of their position.

What changed

Changes the fields prefix, nsSeparatorToken, and openCurlyBraceToken from final to non-final. This is necessary because these fields are no longer set via the constructor (which previously had 9 parameters). Instead, they are set after construction in the createGroupUseStatement factory method, allowing the constructor's parameter count to be reduced below the threshold of 7.

--- a/php-frontend/src/main/java/org/sonar/php/tree/impl/statement/UseStatementTreeImpl.java
+++ b/php-frontend/src/main/java/org/sonar/php/tree/impl/statement/UseStatementTreeImpl.java
@@ -37,3 +37,3 @@ public class UseStatementTreeImpl extends PHPTree implements UseStatementTree {
-  private final NamespaceNameTree prefix;
-  private final InternalSyntaxToken nsSeparatorToken;
-  private final InternalSyntaxToken openCurlyBraceToken;
+  private NamespaceNameTree prefix;
+  private InternalSyntaxToken nsSeparatorToken;
+  private InternalSyntaxToken openCurlyBraceToken;
java:S107 - Method has 8 parameters, which is greater than 7 authorized. • MAJORView issue

Location: php:php-frontend/src/main/java/org/sonar/php/tree/impl/statement/UseStatementTreeImpl.java:74

Why is this an issue?

Methods with a long parameter list are difficult to use because maintainers must figure out the role of each parameter and keep track of their position.

What changed

Updates the createUseStatement factory method call to use the reduced constructor (5 parameters instead of 9, removing the null arguments for group-specific fields). Also introduces a GroupPrefix record that encapsulates prefix and nsSeparatorToken into a single parameter, which helps reduce the parameter count of the createGroupUseStatement method from 8 to 7 (at the threshold).

--- a/php-frontend/src/main/java/org/sonar/php/tree/impl/statement/UseStatementTreeImpl.java
+++ b/php-frontend/src/main/java/org/sonar/php/tree/impl/statement/UseStatementTreeImpl.java
@@ -71,1 +62,4 @@ public class UseStatementTreeImpl extends PHPTree implements UseStatementTree {
-    return new UseStatementTreeImpl(Kind.USE_STATEMENT, useToken, useTypeToken, null, null, null, clauses, null, eosToken);
+    return new UseStatementTreeImpl(Kind.USE_STATEMENT, useToken, useTypeToken, clauses, eosToken);
+  }
+
+  public record GroupPrefix(NamespaceNameTree prefix, InternalSyntaxToken nsSeparatorToken) {
java:S107 - Method has 8 parameters, which is greater than 7 authorized. • MAJORView issue

Location: php:php-frontend/src/main/java/org/sonar/php/parser/TreeFactory.java:1648

Why is this an issue?

Methods with a long parameter list are difficult to use because maintainers must figure out the role of each parameter and keep track of their position.

What changed

Adds the import for FunctionExpressionHeader, a new helper type used to group the 'attribute groups' and 'static keyword' parameters of functionExpression into a single object. This supports reducing the parameter count of the functionExpression method from 8 to 7, fixing the too-many-parameters code smell on that method.

--- a/php-frontend/src/main/java/org/sonar/php/parser/PHPGrammar.java
+++ b/php-frontend/src/main/java/org/sonar/php/parser/PHPGrammar.java
@@ -25,0 +26,1 @@ import org.sonar.php.tree.impl.statement.DeclareStatementTreeImpl.DeclareStateme
+import org.sonar.php.tree.impl.expression.FunctionExpressionTreeImpl.FunctionExpressionHeader;
java:S107 - Method has 8 parameters, which is greater than 7 authorized. • MAJORView issue

Location: php:php-frontend/src/main/java/org/sonar/php/parser/TreeFactory.java:1001

Why is this an issue?

Methods with a long parameter list are difficult to use because maintainers must figure out the role of each parameter and keep track of their position.

What changed

Combines the ENDIF token and EOS into a single tuple parameter using f.newTuple(), reducing the number of arguments passed to the alternativeIfStatement method from 8 to 7. This fixes the too-many-parameters code smell on the alternativeIfStatement method at line 1001.

--- a/php-frontend/src/main/java/org/sonar/php/parser/PHPGrammar.java
+++ b/php-frontend/src/main/java/org/sonar/php/parser/PHPGrammar.java
@@ -996,2 +997,1 @@ public class PHPGrammar {
-        b.token(PHPKeyword.ENDIF),
-        EOS()));
+        f.newTuple(b.token(PHPKeyword.ENDIF), EOS())));

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


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AZkoVAEhIsbR56mqmhML for java:S107 rule
- AZkoVAEhIsbR56mqmhMM for java:S107 rule
- AZkoU_-OIsbR56mqmhLn for java:S107 rule
- AZkoVAEhIsbR56mqmhMK for java:S107 rule
- AZkoU_-OIsbR56mqmhLm for java:S107 rule

Generated by SonarQube Agent (task: 873984c4-dd4f-419d-b999-651d482614f4)

This branch has not been deployed

No deployments
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