Skip to content

SONARPHP-1975 Replace imperative loops with stream pipelines in check classes - #1858

Merged
felix-pauck-sonarsource merged 1 commit into
masterfrom
remediate-master-20261020-230058-b404f9ae
Oct 21, 2026
Merged

felix-pauck-sonarsource merged 1 commit into
masterfrom
remediate-master-20261020-230058-b404f9ae

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:S9391 (Use a stream instead of this loop) represents high-impact violations with consistent automated fixes. These 5 instances span distinct files in the php-checks module, forming a coherent refactoring group that improves code clarity and maintainability without altering functionality.

This PR fixes 5 SonarQube violations of rule S9391 by replacing imperative for-loops with declarative stream pipelines across multiple check classes. Stream-based implementations are more expressive, reduce boilerplate code, and align with modern Java best practices.

View Project in SonarCloud


Fixed Issues

java:S9391 - Use a stream instead of this loop. • MAJOR • View issue

Location: php:php-checks/src/main/java/org/sonar/php/checks/HardCodedCredentialsInFunctionCallsCheck.java:225

Why is this an issue?

Modern programming languages provide declarative, functional interfaces for processing collections. Traditional imperative loops that perform filtering, mapping, or collecting operations can be harder to read and understand at a glance because:

What changed

Adds the java.util.stream.Stream import, which is needed by the stream-based replacement of the imperative loop that the scanner flagged with 'Use a stream instead of this loop'.

--- a/php-checks/src/main/java/org/sonar/php/checks/HardCodedCredentialsInFunctionCallsCheck.java
+++ b/php-checks/src/main/java/org/sonar/php/checks/HardCodedCredentialsInFunctionCallsCheck.java
@@ -30,0 +31,1 @@ import java.util.stream.Collectors;
+import java.util.stream.Stream;
java:S9391 - Use a stream instead of this loop. • MAJOR • View issue

Location: php:php-checks/src/main/java/org/sonar/php/checks/ImmediatelyReturnedVariableCheck.java:123

Why is this an issue?

Modern programming languages provide declarative, functional interfaces for processing collections. Traditional imperative loops that perform filtering, mapping, or collecting operations can be harder to read and understand at a glance because:

What changed

This addresses the code smell about an imperative loop that filters and collects elements, which the rule says should be a stream. The manual for-loop is replaced with a stream pipeline. The loop created an ArrayList, checked each Optional element for presence and for a variable identifier, then added the text. The pipeline expresses the same steps declaratively: .flatMap(Optional::stream) drops empty elements, .map(ArrayAssignmentPatternElementTree::variable) extracts the variable, .filter(variable -> variable.is(Kind.VARIABLE_IDENTIFIER)) keeps only identifiers, the next .map casts and extracts the name text, and .toList() collects the result. This removes the boilerplate result list and makes the intent explicit.

--- a/php-checks/src/main/java/org/sonar/php/checks/ImmediatelyReturnedVariableCheck.java
+++ b/php-checks/src/main/java/org/sonar/php/checks/ImmediatelyReturnedVariableCheck.java
@@ -122,7 +122,6 @@ public class ImmediatelyReturnedVariableCheck extends AbstractStatementsCheck {
-    List<String> names = new ArrayList<>();
-    for (Optional<ArrayAssignmentPatternElementTree> element : listExpressionTree.elements()) {
-      if (element.isPresent() && element.get().variable().is(Kind.VARIABLE_IDENTIFIER)) {
-        names.add(((VariableIdentifierTree) element.get().variable()).variableExpression().text());
-      }
-    }
-    return names;
+    return listExpressionTree.elements().stream()
+      .flatMap(Optional::stream)
+      .map(ArrayAssignmentPatternElementTree::variable)
+      .filter(variable -> variable.is(Kind.VARIABLE_IDENTIFIER))
+      .map(variable -> ((VariableIdentifierTree) variable).variableExpression().text())
+      .toList();
java:S9391 - Use a stream instead of this loop. • MAJOR • View issue

Location: php:php-checks/src/main/java/org/sonar/php/checks/phpini/PhpIniFiles.java:44

Why is this an issue?

Modern programming languages provide declarative, functional interfaces for processing collections. Traditional imperative loops that perform filtering, mapping, or collecting operations can be harder to read and understand at a glance because:

What changed

Adds the import for java.util.stream.Collectors, which the new stream pipeline needs for its collect step when replacing the imperative filtering loop in PhpIniFiles.

--- a/php-checks/src/main/java/org/sonar/php/checks/phpini/PhpIniFiles.java
+++ b/php-checks/src/main/java/org/sonar/php/checks/phpini/PhpIniFiles.java
@@ -20,0 +21,1 @@ import java.util.List;
+import java.util.stream.Collectors;
java:S9391 - Use a stream instead of this loop. • MAJOR • View issue

Location: php:php-checks/src/main/java/org/sonar/php/checks/DuplicateConditionCheck.java:46

Why is this an issue?

Modern programming languages provide declarative, functional interfaces for processing collections. Traditional imperative loops that perform filtering, mapping, or collecting operations can be harder to read and understand at a glance because:

What changed

Removes the now-unused SwitchCaseClauseTree import. After the for loop over switch case clauses is replaced by a stream pipeline, the type is no longer named explicitly; the lambda parameter's type is inferred. This cleanup supports the loop-to-stream refactoring.

--- a/php-checks/src/main/java/org/sonar/php/checks/DuplicateConditionCheck.java
+++ b/php-checks/src/main/java/org/sonar/php/checks/DuplicateConditionCheck.java
@@ -31,1 +30,0 @@ import org.sonar.plugins.php.api.tree.statement.IfStatementTree;
-import org.sonar.plugins.php.api.tree.statement.SwitchCaseClauseTree;
java:S9391 - Use a stream instead of this loop. • MAJOR • View issue

Location: php:php-checks/src/main/java/org/sonar/php/checks/ArgumentWithDefaultValueNotLastCheck.java:78

Why is this an issue?

Modern programming languages provide declarative, functional interfaces for processing collections. Traditional imperative loops that perform filtering, mapping, or collecting operations can be harder to read and understand at a glance because:

What changed

Adds the import for java.util.stream.Collectors, which the rewritten stream pipeline needs for Collectors.joining. This supports replacing the imperative loop with a stream.

--- a/php-checks/src/main/java/org/sonar/php/checks/ArgumentWithDefaultValueNotLastCheck.java
+++ b/php-checks/src/main/java/org/sonar/php/checks/ArgumentWithDefaultValueNotLastCheck.java
@@ -20,0 +21,1 @@ import java.util.List;
+import java.util.stream.Collectors;

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


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AaDv9RMs32uFrWcL249I for java:S9391 rule
- AaDv9RSE32uFrWcL249O for java:S9391 rule
- AaDv9RNj32uFrWcL249J for java:S9391 rule
- AaDv9RS632uFrWcL249P for java:S9391 rule
- AaDv9RQA32uFrWcL249N for java:S9391 rule

Generated by SonarQube Agent (task: b155dd55-6453-4e1d-91ed-599b84d3bee0)
@gitar-bot

gitar-bot Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Code Review ✅ Approved

🟡 Medium risk · Replacing loops with stream pipelines across five PHP checks could alter rule-detection results if filtering or collection semantics differ.

Replaces imperative for-loops with stream pipelines across five check classes, including ImmediatelyReturnedVariableCheck and PhpIniFiles, with supporting import changes. No issues found.

Verification Rules 2/2 met

Functional validation No results

Rules 2 passed 1 not applicable

✅ Internal artifact registries
✅ Repository license compliance

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@jonas-wielage-sonarsource
jonas-wielage-sonarsource removed their request for review October 21, 2026 07:04
@hashicorp-vault-sonar-prod hashicorp-vault-sonar-prod Bot changed the title Replace imperative loops with stream pipelines in check classes SONARPHP-1975 Replace imperative loops with stream pipelines in check classes Oct 21, 2026
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

SONARPHP-1975

@felix-pauck-sonarsource felix-pauck-sonarsource left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! 👍

@sonarqube-next

Copy link
Copy Markdown

@felix-pauck-sonarsource
felix-pauck-sonarsource merged commit 63a80f4 into master Oct 21, 2026
25 checks passed
@felix-pauck-sonarsource
felix-pauck-sonarsource deleted the remediate-master-20261020-230058-b404f9ae branch October 21, 2026 08:44
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.

2 participants