Skip to content

Commit 7ad58f6

Browse files
committed
Fix AlwaysReturnSniff for missing return statement
Now properly catches filter callbacks that don't `return` _anywhere_ inside the function body
1 parent 7920531 commit 7ad58f6

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

WordPressVIPMinimum/Sniffs/Filters/AlwaysReturnSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ private function processFunctionBody( $stackPtr ) {
228228
);
229229
}
230230

231-
if ( 0 < $insideIfConditionalReturn && 0 === $outsideConditionalReturn ) {
231+
if ( 0 <= $insideIfConditionalReturn && 0 === $outsideConditionalReturn ) {
232232
$this->phpcsFile->AddWarning( sprintf( 'Please, make sure that a callback to `%s` filter is always returning some value.', $filterName ), $functionBodyScopeStart, 'missingReturnStatement' );
233233
}
234234
}

WordPressVIPMinimum/Tests/Filters/AlwaysReturnUnitTest.inc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,10 @@ if ( 1 === 1 ) {
100100
add_filter( 'another_good_example_closure', function () {
101101
return 'hello';
102102
} );
103-
}
103+
}
104+
105+
add_filter( 'bad_example_closure_with_no_returns', function( $input ) {
106+
$input = $input . ' hello!';
107+
108+
// But we never return _anywhere_
109+
}, 10, 2 );

WordPressVIPMinimum/Tests/Filters/AlwaysReturnUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function getWarningList() {
3535
49 => 1,
3636
88 => 1,
3737
95 => 1,
38+
105 => 1,
3839
);
3940
}
4041

0 commit comments

Comments
 (0)