Skip to content

Commit 4244aa8

Browse files
committed
Squiz/NonExecutableCode: make sniff more code style independent
When determining whether a `return` statement is the last code token in a function body, comments should be ignored, but weren't. Fixed now. Includes tests.
1 parent 5ef2b66 commit 4244aa8

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ public function process(File $phpcsFile, $stackPtr)
114114
}
115115

116116
if ($tokens[$stackPtr]['code'] === T_RETURN) {
117-
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
117+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
118118
if ($tokens[$next]['code'] === T_SEMICOLON) {
119-
$next = $phpcsFile->findNext(T_WHITESPACE, ($next + 1), null, true);
119+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
120120
if ($tokens[$next]['code'] === T_CLOSE_CURLY_BRACKET) {
121121
// If this is the closing brace of a function
122122
// then this return statement doesn't return anything

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,5 +396,22 @@ function executionStoppingThrowExpressionsE() {
396396
echo 'non-executable';
397397
}
398398

399+
function returnNotRequiredIgnoreCommentsA()
400+
{
401+
if ($something === TRUE) {
402+
return /*comment*/;
403+
}
404+
405+
echo 'foo';
406+
return /*comment*/;
407+
}
408+
409+
function returnNotRequiredIgnoreCommentsB()
410+
{
411+
echo 'foo';
412+
return;
413+
/*comment*/
414+
}
415+
399416
// Intentional syntax error.
400417
return array_map(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public function getWarningList($testFile='')
8282
386 => 1,
8383
391 => 1,
8484
396 => 1,
85+
406 => 1,
86+
412 => 1,
8587
];
8688
break;
8789
case 'NonExecutableCodeUnitTest.2.inc':

0 commit comments

Comments
 (0)