Skip to content

Commit b9c0aed

Browse files
rmsundar1svera
andauthored
Apply suggestions from code review
Co-authored-by: Sergio Vera <[email protected]>
1 parent 0994f92 commit b9c0aed

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Magento2/Sniffs/Exceptions/DirectThrowSniff.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,23 @@ public function process(File $phpcsFile, $stackPtr)
4949
$exceptionString = 'Exception';
5050
$customExceptionFound = false;
5151
foreach ($tokens as $key => $token) {
52-
if ($token['code'] === T_USE) {
52+
if ($token['code'] !== T_USE) {
53+
continue;
54+
}
55+
$endOfUse = $phpcsFile->findEndOfStatement($key);
56+
$useStatementValue = $this->getFullClassNameAndAlias($tokens, $key, $endOfUse);
57+
// we safely consider use statement has alias will not be a direct exception class
58+
if (!empty($useStatementValue['alias'])) {
59+
continue;
60+
}
61+
if (substr($useStatementValue['name'], 0, strlen($exceptionString)) !== $exceptionString
62+
&& substr($useStatementValue['name'], -strlen($exceptionString)) === $exceptionString
63+
&& $useStatementValue['name'] !== $exceptionString
64+
) {
65+
$customExceptionFound = true;
66+
break;
67+
}
68+
}
5369
$endOfUse = $phpcsFile->findEndOfStatement($key);
5470
$useStatementValue = $this->getFullClassNameAndAlias($tokens, $key, $endOfUse);
5571
//we safely consider use statement has alias will not be a direct exception class
@@ -84,7 +100,7 @@ public function process(File $phpcsFile, $stackPtr)
84100
* @param int $end
85101
* @return array
86102
*/
87-
private function getFullClassNameAndAlias($tokens, $start, $end)
103+
private function getFullClassNameAndAlias($tokens, $start, $end): array
88104
{
89105
$fullName = $alias = '';
90106
$foundAlias = false;

0 commit comments

Comments
 (0)