Skip to content

Commit 54ca471

Browse files
committed
Fix DirectThrowSniff false positive
1 parent 0e141b0 commit 54ca471

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Magento2/Sniffs/Exceptions/DirectThrowSniff.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,19 @@ public function process(File $phpcsFile, $stackPtr)
4444
$tokens = $phpcsFile->getTokens();
4545
$endOfStatement = $phpcsFile->findEndOfStatement($stackPtr);
4646
$posOfException = $phpcsFile->findNext(T_STRING, $stackPtr, $endOfStatement);
47-
if ($tokens[$posOfException]['content'] === 'Exception') {
47+
$content = $tokens[$posOfException]['content'];
48+
$exceptionClassInUseStatement = false;
49+
foreach ($tokens as $key => $token) {
50+
if ($token['code'] === T_USE) {
51+
$endOfUse = $phpcsFile->findEndOfStatement($key);
52+
$posOfException = $phpcsFile->findNext(T_STRING, $key, $key + 3, false, 'Exception');
53+
if ($posOfException && $phpcsFile->findNext(T_SEMICOLON, $posOfException+1, $endOfUse + 1)) {
54+
$exceptionClassInUseStatement = true;
55+
break;
56+
}
57+
}
58+
}
59+
if ($content === '\Exception' || ($content === 'Exception' && $exceptionClassInUseStatement)) {
4860
$phpcsFile->addWarning(
4961
$this->warningMessage,
5062
$stackPtr,

0 commit comments

Comments
 (0)