Skip to content

Squiz/NonExecutableCode: fix undefined index during live coding reviews #1706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,25 +225,34 @@ public function process(File $phpcsFile, $stackPtr)
for ($i = ($start + 1); $i < $end; $i++) {
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true
|| isset(Tokens::$bracketTokens[$tokens[$i]['code']]) === true
|| $tokens[$i]['code'] === T_SEMICOLON
) {
continue;
}

// Skip whole functions and classes/interfaces because they are not
// technically executed code, but rather declarations that may be used.
if ($tokens[$i]['code'] === T_FUNCTION
|| $tokens[$i]['code'] === T_CLASS
|| $tokens[$i]['code'] === T_INTERFACE
if (isset(Tokens::$ooScopeTokens[$tokens[$i]['code']]) === true
|| $tokens[$i]['code'] === T_FUNCTION
|| $tokens[$i]['code'] === T_CLOSURE
) {
if (isset($tokens[$i]['scope_closer']) === false) {
// Parse error/Live coding.
return;
}

$i = $tokens[$i]['scope_closer'];
continue;
}

$line = $tokens[$i]['line'];
if ($line > $lastLine) {
$type = substr($tokens[$stackPtr]['type'], 2);
$warning = 'Code after %s statement cannot be executed';
$data = [$type];
$warning = 'Code after %s statement (line %s) cannot be executed';
$data = [
$type,
$tokens[$stackPtr]['line'],
];
$phpcsFile->addWarning($warning, $i, 'Unreachable', $data);
$lastLine = $line;
}
Expand Down
19 changes: 19 additions & 0 deletions src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,22 @@ class HttpStatus
const CONTINUE = 100;
const SWITCHING_PROTOCOLS = 101;
}

interface ABC {
public function noError($name, $var);
}

trait Something {
function getReturnType() {
echo 'no error';
}
}

$a = new class {
public function log($msg)
{
echo 'no error';
}
};

interface MyInterface {
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function getWarningList()
234 => 1,
235 => 2,
239 => 1,
273 => 2,
];

}//end getWarningList()
Expand Down