Skip to content

Commit b387875

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 76ebdef commit b387875

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ The file documents changes to the PHP_CodeSniffer project.
175175
- Thanks to @simonsan for the patch
176176
- Fixed bug #3893 : Generic/DocComment : the SpacingAfterTagGroup fixer could accidentally remove ignore annotations
177177
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
178+
- Fixed bug #3898 : Squiz/NonExecutableCode : the sniff could get confused over comments in unexpected places
179+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
178180
- Fixed bug #3904 : Squiz/FunctionSpacing : prevent potential fixer conflict
179181
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
180182
- Fixed bug #3906 : Tokenizer/CSS: fixed a bug related to the unsupported slash comment syntax

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

8789
case 'NonExecutableCodeUnitTest.2.inc':

0 commit comments

Comments
 (0)