Skip to content

Commit

Permalink
VariableAnalysisSniff::checkForCatchBlock(): fix comment tolerance
Browse files Browse the repository at this point in the history
PHP ignores comments in unexpected/unconventional places and so should the sniff.

Includes adjusting an existing unit test.

Without the fix, the adjusted unit test would cause test failures.
  • Loading branch information
jrfnl committed Feb 14, 2020
1 parent 2976bca commit cbb04a5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use VariableAnalysis\Lib\Helpers;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;

class VariableAnalysisSniff implements Sniff {
/**
Expand Down Expand Up @@ -575,7 +576,7 @@ protected function checkForCatchBlock(File $phpcsFile, $stackPtr, $varName, $cur
return false;
}

$catchPtr = $phpcsFile->findPrevious(T_WHITESPACE, $openPtr - 1, null, true, null, true);
$catchPtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $openPtr - 1, null, true, null, true);
if (($catchPtr !== false) && ($tokens[$catchPtr]['code'] === T_CATCH)) {
// Scope of the exception var is actually the function, not just the catch block.
$this->markVariableDeclaration($varName, 'local', null, $stackPtr, $currScope, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function function_with_try_catch() {
try {
echo $e;
echo $var;
} catch (Exception $e) {
} catch /* comment */ (Exception $e) {
echo $e;
echo $var;
}
Expand Down

0 comments on commit cbb04a5

Please sign in to comment.