Skip to content
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
15 changes: 9 additions & 6 deletions PHPCSUtils/Utils/ControlStructures.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public static function isElseIf(File $phpcsFile, $stackPtr)
* 'type_end_token' => integer, // The stack pointer to the end of the type declaration.
* )
* ```
* In case of an invalid catch structure, the array may be empty.
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified `$stackPtr` is not of
* type `T_CATCH` or doesn't exist.
Expand Down Expand Up @@ -243,12 +244,14 @@ public static function getCaughtExceptions(File $phpcsFile, $stackPtr)
}

if (isset(Collections::namespacedNameTokens()[$tokens[$i]['code']]) === false) {
// Add the current exception to the result array.
$exceptions[] = [
'type' => $foundName,
'type_token' => $firstToken,
'type_end_token' => $lastToken,
];
// Add the current exception to the result array if one was found.
if ($foundName !== '') {
$exceptions[] = [
'type' => $foundName,
'type_token' => $firstToken,
'type_end_token' => $lastToken,
];
}

if ($tokens[$i]['code'] === \T_BITWISE_OR) {
// Multi-catch. Reset and continue.
Expand Down
6 changes: 6 additions & 0 deletions Tests/Utils/ControlStructures/GetCaughtExceptionsTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ try {
/* testPHP8NonCapturingCatch */
} catch (RuntimeException | AnotherException) {

/* testMissingExceptionName */
} catch ($e) {

/* testMultiMissingExceptionNames */
} catch ( | $e) {

/* testLiveCoding */
// Intentional parse error.
} catch (
8 changes: 8 additions & 0 deletions Tests/Utils/ControlStructures/GetCaughtExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ public function dataGetCaughtExceptions()
],
],
],
'catch-without-named-exception' => [
'testMarker' => '/* testMissingExceptionName */',
'expected' => [],
],
'multi-catch-without-named-exceptions' => [
'testMarker' => '/* testMultiMissingExceptionNames */',
'expected' => [],
],
];
}
}