Repository navigation
SONARPHP-1975 Replace imperative loops with stream pipelines in check classes - #1858
Conversation
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)
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 Verification Rules 2/2 metFunctional validation No results Rules 2 passed 1 not applicable
Auto-approval Not enabledFor more verification details, comment OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|




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:225Why 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'.
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:123Why 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.mapcasts and extracts the name text, and.toList()collects the result. This removes the boilerplate result list and makes the intent explicit.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:44Why 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.
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:46Why 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.
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:78Why 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.
SonarQube Remediation Agent uses AI. Check for mistakes.