Skip to content

Commit

Permalink
Merge pull request #594 from PHPCSStandards/feature/controlstructures…
Browse files Browse the repository at this point in the history
…-getcaughtexceptions-no-exception-on-parse-error

ControlStructures::getCaughtExceptions(): don't throw an exception for a parse error
  • Loading branch information
jrfnl authored May 12, 2024
2 parents 567d785 + 0e0ad89 commit 473cf6c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
4 changes: 1 addition & 3 deletions PHPCSUtils/Utils/ControlStructures.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ public static function isElseIf(File $phpcsFile, $stackPtr)
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified `$stackPtr` is not of
* type `T_CATCH` or doesn't exist.
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If no parenthesis opener or closer can be
* determined (parse error).
*/
public static function getCaughtExceptions(File $phpcsFile, $stackPtr)
{
Expand All @@ -228,7 +226,7 @@ public static function getCaughtExceptions(File $phpcsFile, $stackPtr)
}

if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) {
throw new RuntimeException('Parentheses opener/closer of the T_CATCH could not be determined');
return [];
}

$opener = $tokens[$stackPtr]['parenthesis_opener'];
Expand Down
17 changes: 4 additions & 13 deletions Tests/Utils/ControlStructures/GetCaughtExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,6 @@ public function testNotCatch()
ControlStructures::getCaughtExceptions(self::$phpcsFile, $target);
}

/**
* Test receiving an expected exception when a parse error is encountered.
*
* @return void
*/
public function testParseError()
{
$this->expectPhpcsException('Parentheses opener/closer of the T_CATCH could not be determined');

$target = $this->getTargetToken('/* testLiveCoding */', \T_CATCH);
ControlStructures::getCaughtExceptions(self::$phpcsFile, $target);
}

/**
* Test retrieving the exceptions caught in a `catch` condition.
*
Expand Down Expand Up @@ -220,6 +207,10 @@ public static function dataGetCaughtExceptions()
'testMarker' => '/* testMultiMissingExceptionNames */',
'expected' => [],
],
'live coding / parse error' => [
'testMarker' => '/* testLiveCoding */',
'expected' => [],
],
];
}
}

0 comments on commit 473cf6c

Please sign in to comment.