Skip to content

Commit 8947251

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 4244aa8 commit 8947251

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

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
break;
8990
case 'NonExecutableCodeUnitTest.2.inc':

0 commit comments

Comments
 (0)