Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Sergio Vera <sergio.vera@gmail.com>
  • Loading branch information
rmsundar1 and svera authored Dec 15, 2021
commit b9c0aed441edd4a7164862934586a13c0535c62f
20 changes: 18 additions & 2 deletions Magento2/Sniffs/Exceptions/DirectThrowSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,23 @@ public function process(File $phpcsFile, $stackPtr)
$exceptionString = 'Exception';
$customExceptionFound = false;
foreach ($tokens as $key => $token) {
if ($token['code'] === T_USE) {
if ($token['code'] !== T_USE) {
continue;
}
$endOfUse = $phpcsFile->findEndOfStatement($key);
$useStatementValue = $this->getFullClassNameAndAlias($tokens, $key, $endOfUse);
// we safely consider use statement has alias will not be a direct exception class
if (!empty($useStatementValue['alias'])) {
continue;
}
if (substr($useStatementValue['name'], 0, strlen($exceptionString)) !== $exceptionString
&& substr($useStatementValue['name'], -strlen($exceptionString)) === $exceptionString
&& $useStatementValue['name'] !== $exceptionString
) {
$customExceptionFound = true;
break;
}
}
$endOfUse = $phpcsFile->findEndOfStatement($key);
$useStatementValue = $this->getFullClassNameAndAlias($tokens, $key, $endOfUse);
//we safely consider use statement has alias will not be a direct exception class
Expand Down Expand Up @@ -84,7 +100,7 @@ public function process(File $phpcsFile, $stackPtr)
* @param int $end
* @return array
*/
private function getFullClassNameAndAlias($tokens, $start, $end)
private function getFullClassNameAndAlias($tokens, $start, $end): array
{
$fullName = $alias = '';
$foundAlias = false;
Expand Down