Skip to content

Commit bf6552a

Browse files
authored
[DeadCode] Skip dead catch before catch-all Throwable/Exception in RemoveDeadCatchRector (#8462)
1 parent b9d85b9 commit bf6552a

4 files changed

Lines changed: 79 additions & 10 deletions

File tree

‎rules-tests/DeadCode/Rector/TryCatch/RemoveDeadCatchRector/Fixture/drop_with_many_dead_catches.php.inc‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ class DropWithManyDeadCatches
88
{
99
try {
1010
// some code
11-
} catch (SomeException $exception) {
12-
throw new InvalidArgumentException($exception->getMessage());
13-
} catch (DeadException $exception) {
11+
} catch (\RangeException $exception) {
12+
throw new \InvalidArgumentException($exception->getMessage());
13+
} catch (\DomainException $exception) {
1414
throw $exception;
15-
} catch (RuntimeException $exception) {
16-
throw new InvalidArgumentException($exception->getMessage());
15+
} catch (\RuntimeException $exception) {
16+
throw new \InvalidArgumentException($exception->getMessage());
1717
} catch (\Throwable $throwable) {
1818
throw $throwable;
1919
}
@@ -32,10 +32,10 @@ class DropWithManyDeadCatches
3232
{
3333
try {
3434
// some code
35-
} catch (SomeException $exception) {
36-
throw new InvalidArgumentException($exception->getMessage());
37-
} catch (RuntimeException $exception) {
38-
throw new InvalidArgumentException($exception->getMessage());
35+
} catch (\RangeException $exception) {
36+
throw new \InvalidArgumentException($exception->getMessage());
37+
} catch (\RuntimeException $exception) {
38+
throw new \InvalidArgumentException($exception->getMessage());
3939
}
4040
}
4141
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\TryCatch\RemoveDeadCatchRector\Fixture;
4+
5+
class SkipDeadCatchBeforeCatchAllException
6+
{
7+
public function run()
8+
{
9+
try {
10+
// some code
11+
} catch (ValidationException $exception) {
12+
throw $exception;
13+
} catch (OAuthServerException $exception) {
14+
echo 'oauth';
15+
} catch (\Exception $exception) {
16+
echo 'general';
17+
}
18+
}
19+
}
20+
21+
?>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\TryCatch\RemoveDeadCatchRector\Fixture;
4+
5+
class SkipDeadCatchChildOfLaterCatch
6+
{
7+
public function run()
8+
{
9+
try {
10+
// some code
11+
} catch (\App\ChildException $exception) {
12+
throw $exception;
13+
} catch (\App\ParentException $exception) {
14+
echo 'parent';
15+
}
16+
}
17+
}
18+
19+
?>

‎rules/DeadCode/Rector/TryCatch/RemoveDeadCatchRector.php‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpParser\Node\Stmt\Expression;
1313
use PhpParser\Node\Stmt\Nop;
1414
use PhpParser\Node\Stmt\TryCatch;
15+
use PHPStan\Reflection\ReflectionProvider;
1516
use PHPStan\Type\ObjectType;
1617
use Rector\Rector\AbstractRector;
1718
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -22,6 +23,11 @@
2223
*/
2324
final class RemoveDeadCatchRector extends AbstractRector
2425
{
26+
public function __construct(
27+
private readonly ReflectionProvider $reflectionProvider
28+
) {
29+
}
30+
2531
public function getRuleDefinition(): RuleDefinition
2632
{
2733
return new RuleDefinition('Remove dead catches', [new CodeSample(
@@ -154,7 +160,17 @@ private function shouldSkipNextCatchClassParentWithSpecialTreatment(
154160
return true;
155161
}
156162

157-
if (! $this->isObjectType($fullyQualified, new ObjectType($nextCatchType->toString()))) {
163+
// Throwable/Exception catch anything, so the current catch is never dead against them,
164+
// even when the current type cannot be resolved to prove the parent relation
165+
$catchesAnything = in_array(
166+
ltrim($nextCatchType->toString(), '\\'),
167+
['Throwable', 'Exception'],
168+
true
169+
);
170+
171+
// only skip when the next catch is provably unrelated; an unresolvable next type
172+
// might be a parent of the current one, so removing the catch would change behavior
173+
if (! $catchesAnything && $this->isProvablyUnrelated($fullyQualified, $nextCatchType)) {
158174
continue;
159175
}
160176

@@ -166,6 +182,19 @@ private function shouldSkipNextCatchClassParentWithSpecialTreatment(
166182
return false;
167183
}
168184

185+
private function isProvablyUnrelated(FullyQualified $current, FullyQualified $next): bool
186+
{
187+
if (! $this->reflectionProvider->hasClass($current->toString())) {
188+
return false;
189+
}
190+
191+
if (! $this->reflectionProvider->hasClass($next->toString())) {
192+
return false;
193+
}
194+
195+
return ! $this->isObjectType($current, new ObjectType($next->toString()));
196+
}
197+
169198
/**
170199
* @param Stmt[] $stmts
171200
*/

0 commit comments

Comments
 (0)