Skip to content

Commit 6593f61

Browse files
committed
ControlStructures::getCaughtExceptions(): allow for PHP8 non-capturing catch
PHP 8 will introduce non-capturing catch statements. This tiny fix makes sure the method can handle these correctly. Includes unit test. Ref: https://wiki.php.net/rfc/non-capturing_catches
1 parent e411e6b commit 6593f61

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

PHPCSUtils/Utils/ControlStructures.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public static function getCaughtExceptions(File $phpcsFile, $stackPtr)
383383
$firstToken = null;
384384
$lastToken = null;
385385

386-
for ($i = ($opener + 1); $i < $closer; $i++) {
386+
for ($i = ($opener + 1); $i <= $closer; $i++) {
387387
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']])) {
388388
continue;
389389
}

Tests/Utils/ControlStructures/GetCaughtExceptionsTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ try {
2727
/* testMultiCatchCompoundNames */
2828
} catch (\NS\RuntimeException | My\ParseErrorException | namespace \ AnotherException $e) {
2929

30+
/* testPHP8NonCapturingCatch */
31+
} catch (RuntimeException | AnotherException) {
32+
3033
/* testLiveCoding */
3134
// Intentional parse error.
3235
} catch (

Tests/Utils/ControlStructures/GetCaughtExceptionsTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,21 @@ public function dataGetCaughtExceptions()
197197
],
198198
],
199199
],
200+
'non-capturing-catch' => [
201+
'target' => '/* testPHP8NonCapturingCatch */',
202+
'expected' => [
203+
[
204+
'type' => 'RuntimeException',
205+
'type_token' => 3,
206+
'type_end_token' => 3,
207+
],
208+
[
209+
'type' => 'AnotherException',
210+
'type_token' => 7,
211+
'type_end_token' => 7,
212+
],
213+
],
214+
],
200215
];
201216
}
202217
}

0 commit comments

Comments
 (0)