Skip to content

Commit cbb04a5

Browse files
committed
VariableAnalysisSniff::checkForCatchBlock(): fix comment tolerance
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.
1 parent 2976bca commit cbb04a5

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use VariableAnalysis\Lib\Helpers;
99
use PHP_CodeSniffer\Sniffs\Sniff;
1010
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Util\Tokens;
1112

1213
class VariableAnalysisSniff implements Sniff {
1314
/**
@@ -575,7 +576,7 @@ protected function checkForCatchBlock(File $phpcsFile, $stackPtr, $varName, $cur
575576
return false;
576577
}
577578

578-
$catchPtr = $phpcsFile->findPrevious(T_WHITESPACE, $openPtr - 1, null, true, null, true);
579+
$catchPtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $openPtr - 1, null, true, null, true);
579580
if (($catchPtr !== false) && ($tokens[$catchPtr]['code'] === T_CATCH)) {
580581
// Scope of the exception var is actually the function, not just the catch block.
581582
$this->markVariableDeclaration($varName, 'local', null, $stackPtr, $currScope, true);

VariableAnalysis/Tests/CodeAnalysis/fixtures/FunctionWithTryCatchFixture.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function function_with_try_catch() {
66
try {
77
echo $e;
88
echo $var;
9-
} catch (Exception $e) {
9+
} catch /* comment */ (Exception $e) {
1010
echo $e;
1111
echo $var;
1212
}

0 commit comments

Comments
 (0)