Skip to content

Commit 20cc1c2

Browse files
Merge pull request #76 from VincentLanglet/fixTryCatch
🐛 Fix return in try catch
2 parents 648174e + c1843e1 commit 20cc1c2

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

SymfonyCustom/Sniffs/Formatting/ConditionalReturnOrThrowSniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,15 @@ public function process(File $phpcsFile, $stackPtr)
5959
&& isset($tokens[$opener]['scope_closer'])
6060
&& $stackPtr <= $tokens[$opener]['scope_closer']) {
6161
$isClosure = $phpcsFile->findPrevious(T_CLOSURE, $stackPtr, $opener);
62-
6362
if (false !== $isClosure) {
6463
return;
6564
}
6665

66+
$isTryCatch = $phpcsFile->findPrevious([T_TRY, T_CATCH], $stackPtr, $opener);
67+
if (false !== $isTryCatch) {
68+
return;
69+
}
70+
6771
$condition = $phpcsFile->findNext($this->conditions, $stackPtr + 1);
6872

6973
if (false !== $condition) {

SymfonyCustom/Tests/Formatting/ConditionalReturnOrThrowUnitTest.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,20 @@ class Test
7979
$b = 2;
8080
}
8181
}
82+
83+
public function ok()
84+
{
85+
if (false) {
86+
try {
87+
$a = 1;
88+
} catch (NumberParseException $e) {
89+
$a = 1;
90+
91+
return;
92+
}
93+
} else {
94+
$a = 1;
95+
}
96+
}
8297
}
8398

0 commit comments

Comments
 (0)