Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ControlStructures::getCaughtExceptions(): don't throw an exception for a parse error #594

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' => [],
],
];
}
}