Skip to content

Commit 0aec4c3

Browse files
committed
Merge branch 'feature/3345-generic-inlinecontrolstructures-bugfix-multicatch-finally' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 798ae54 + 59e161a commit 0aec4c3

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ public function process(File $phpcsFile, $stackPtr)
210210
if (isset($tokens[$end]['scope_opener']) === true) {
211211
$type = $tokens[$end]['code'];
212212
$end = $tokens[$end]['scope_closer'];
213-
if ($type === T_DO || $type === T_IF || $type === T_ELSEIF || $type === T_TRY) {
213+
if ($type === T_DO
214+
|| $type === T_IF || $type === T_ELSEIF
215+
|| $type === T_TRY || $type === T_CATCH || $type === T_FINALLY
216+
) {
214217
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true);
215218
if ($next === false) {
216219
break;
@@ -227,15 +230,20 @@ public function process(File $phpcsFile, $stackPtr)
227230
continue;
228231
}
229232

233+
// Account for TRY... CATCH/FINALLY statements.
234+
if (($type === T_TRY
235+
|| $type === T_CATCH
236+
|| $type === T_FINALLY)
237+
&& ($nextType === T_CATCH
238+
|| $nextType === T_FINALLY)
239+
) {
240+
continue;
241+
}
242+
230243
// Account for DO... WHILE conditions.
231244
if ($type === T_DO && $nextType === T_WHILE) {
232245
$end = $phpcsFile->findNext(T_SEMICOLON, ($next + 1));
233246
}
234-
235-
// Account for TRY... CATCH statements.
236-
if ($type === T_TRY && $nextType === T_CATCH) {
237-
$end = $tokens[$next]['scope_closer'];
238-
}
239247
} else if ($type === T_CLOSURE) {
240248
// There should be a semicolon after the closing brace.
241249
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true);

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,22 @@ for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
253253

254254
if ($this->valid(fn(): bool => 2 > 1)) {
255255
}
256+
257+
// Issue 3345.
258+
function testMultiCatch()
259+
{
260+
if (true)
261+
try {
262+
} catch (\LogicException $e) {
263+
} catch (\Exception $e) {
264+
}
265+
}
266+
267+
function testFinally()
268+
{
269+
if (true)
270+
try {
271+
} catch (\LogicException $e) {
272+
} finally {
273+
}
274+
}

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,24 @@ for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
286286

287287
if ($this->valid(fn(): bool => 2 > 1)) {
288288
}
289+
290+
// Issue 3345.
291+
function testMultiCatch()
292+
{
293+
if (true) {
294+
try {
295+
} catch (\LogicException $e) {
296+
} catch (\Exception $e) {
297+
}
298+
}
299+
}
300+
301+
function testFinally()
302+
{
303+
if (true) {
304+
try {
305+
} catch (\LogicException $e) {
306+
} finally {
307+
}
308+
}
309+
}

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public function getErrorList($testFile='InlineControlStructureUnitTest.1.inc')
7171
236 => 1,
7272
238 => 1,
7373
242 => 1,
74+
260 => 1,
75+
269 => 1,
7476
];
7577

7678
case 'InlineControlStructureUnitTest.js':

0 commit comments

Comments
 (0)