Skip to content

Commit 8284121

Browse files
committed
DO NOT PULL - see #149 | Squiz/NonExecutableCode: add extra defensive coding
THIS SHOULD BE FIXED IN THE TOKENIZER INSTEAD. Includes unit test.
1 parent 26bfadc commit 8284121

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ public function process(File $phpcsFile, $stackPtr)
109109
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
110110
if ($tokens[$next]['code'] === T_SEMICOLON) {
111111
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
112-
if ($tokens[$next]['code'] === T_CLOSE_CURLY_BRACKET) {
112+
if ($tokens[$next]['code'] === T_CLOSE_CURLY_BRACKET
113+
&& isset($tokens[$next]['scope_condition']) === true
114+
) {
113115
// If this is the closing brace of a function
114116
// then this return statement doesn't return anything
115117
// and is not required anyway.
@@ -123,7 +125,7 @@ public function process(File $phpcsFile, $stackPtr)
123125
}
124126
}
125127
}
126-
}
128+
}//end if
127129

128130
if (isset($tokens[$stackPtr]['scope_opener']) === true) {
129131
$owner = $tokens[$stackPtr]['scope_condition'];

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

+8
Original file line numberDiff line numberDiff line change
@@ -418,3 +418,11 @@ $closure = function ()
418418
echo 'foo';
419419
return; // This return should be flagged as not required.
420420
};
421+
422+
// Safeguard against an "Undefined array key "scope_condition"" notice.
423+
if (foo(<<<EOD
424+
foobar!
425+
EOD
426+
)) {
427+
return;
428+
}

0 commit comments

Comments
 (0)