Skip to content

Commit fb9447f

Browse files
committed
Squiz/NonExecutableCode: flag redundant return statements in closures too
A return statement which doesn't return a value at the end of a function body would be flagged as "not required" for named functions, but not so for anonymous functions. Fixed now. Includes tests.
1 parent b387875 commit fb9447f

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ The file documents changes to the PHP_CodeSniffer project.
9090
- Thanks to Dan Wallis (@fredden) for the patch
9191
- Squiz.PHP.InnerFunctions sniff no longer reports on OO methods for OO structures declared within a function or closure
9292
- Thanks to @Daimona for the patch
93+
- Squiz.PHP.NonExecutableCode will now also flag redundant return statements just before a closure close brace
94+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
9395
- Runtime performance improvement for PHPCS CLI users. The improvement should be most noticeable for users on Windows.
9496
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
9597
- The -e (explain) command will now list sniffs in natural order

src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ public function process(File $phpcsFile, $stackPtr)
122122
// then this return statement doesn't return anything
123123
// and is not required anyway.
124124
$owner = $tokens[$next]['scope_condition'];
125-
if ($tokens[$owner]['code'] === T_FUNCTION) {
125+
if ($tokens[$owner]['code'] === T_FUNCTION
126+
|| $tokens[$owner]['code'] === T_CLOSURE
127+
) {
126128
$warning = 'Empty return statement not required here';
127129
$phpcsFile->addWarning($warning, $stackPtr, 'ReturnNotRequired');
128130
return;

src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.1.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,5 +413,11 @@ function returnNotRequiredIgnoreCommentsB()
413413
/*comment*/
414414
}
415415

416+
$closure = function ()
417+
{
418+
echo 'foo';
419+
return; // This return should be flagged as not required.
420+
};
421+
416422
// Intentional syntax error.
417423
return array_map(

src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function getWarningList($testFile='')
8484
396 => 1,
8585
406 => 1,
8686
412 => 1,
87+
419 => 1,
8788
];
8889

8990
case 'NonExecutableCodeUnitTest.2.inc':

0 commit comments

Comments
 (0)