Skip to content

Commit d6f12b6

Browse files
authored
VariableAnalysisSniff::checkForCatchBlock(): fix comment tolerance (#146)
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 0bd16bc commit d6f12b6

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ protected function checkForCatchBlock(File $phpcsFile, $stackPtr, $varName, $cur
576576
return false;
577577
}
578578

579-
$catchPtr = $phpcsFile->findPrevious(T_WHITESPACE, $openPtr - 1, null, true, null, true);
579+
$catchPtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $openPtr - 1, null, true, null, true);
580580
if (($catchPtr !== false) && ($tokens[$catchPtr]['code'] === T_CATCH)) {
581581
// Scope of the exception var is actually the function, not just the catch block.
582582
$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)