Skip to content

Commit 92fdfd2

Browse files
committed
Hooks/AlwaysReturnInFilter: remove redundant condition
Given that: * `$insideIfConditionalReturn` has a default value of `0`; * And that value is only ever increased; * The `$insideIfConditionalReturn >= 0` condition will always be `true`. So this condition can be safely removed, just like the - now unused - assignments to the variable. The original condition was introduced with the introduction of the sniff in 177. The condition was adjusted in 291, which made the logic redundant. Looking at the sniff, I believe the intention was to only flag the "return outside condition missing" when not all control structure paths had a `return` statement, but this was never really properly checked as the only control structures taken into account are `if` control structures. I believe it would be good to improve the sniff to handle more control structures (`switch`, `while` etc) and to not throw the "return outside condition missing" error if all possible paths have a `return` statement, but that is outside the scope of the current PR. I will add a note to this effect to the review ticket for this sniff - 520.
1 parent 0fd1c85 commit 92fdfd2

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,10 @@ private function processFunctionBody( $stackPtr ) {
223223
$functionBodyScopeEnd
224224
);
225225

226-
$insideIfConditionalReturn = 0;
227-
$outsideConditionalReturn = 0;
226+
$outsideConditionalReturn = 0;
228227

229228
while ( $returnTokenPtr ) {
230-
if ( $this->isInsideIfConditonal( $returnTokenPtr ) ) {
231-
++$insideIfConditionalReturn;
232-
} else {
229+
if ( $this->isInsideIfConditonal( $returnTokenPtr ) === false ) {
233230
++$outsideConditionalReturn;
234231
}
235232
if ( $this->isReturningVoid( $returnTokenPtr ) ) {
@@ -244,11 +241,10 @@ private function processFunctionBody( $stackPtr ) {
244241
);
245242
}
246243

247-
if ( $insideIfConditionalReturn >= 0 && $outsideConditionalReturn === 0 ) {
244+
if ( $outsideConditionalReturn === 0 ) {
248245
$message = 'Please, make sure that a callback to `%s` filter is always returning some value.';
249246
$data = [ $filterName ];
250247
$this->phpcsFile->addError( $message, $functionBodyScopeStart, 'MissingReturnStatement', $data );
251-
252248
}
253249
}
254250

0 commit comments

Comments
 (0)