Repository navigation
Expand file tree
/
Copy pathRemoveDeadCatchRector.php
More file actions
213 lines (179 loc) · 5.52 KB
/
Copy pathRemoveDeadCatchRector.php
File metadata and controls
213 lines (179 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Rector\TryCatch;
use PhpParser\Node;
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\TryCatch;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\TryCatch\RemoveDeadCatchRector\RemoveDeadCatchRectorTest
*/
final class RemoveDeadCatchRector extends AbstractRector
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove dead catches', [new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
try {
// some code
} catch (RuntimeException $exception) {
throw new InvalidArgumentException($exception->getMessage());
} catch (Throwable $throwable) {
throw $throwable;
}
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
try {
// some code
} catch (RuntimeException $exception) {
throw new InvalidArgumentException($exception->getMessage());
}
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [TryCatch::class];
}
/**
* @param TryCatch $node
* @return TryCatch|null
*/
public function refactor(Node $node): ?Node
{
$catches = $node->catches;
if (count($catches) === 1) {
return null;
}
$hasChanged = false;
$maxIndexCatches = count($catches) - 1;
foreach ($catches as $key => $catchItem) {
if (! $this->isJustThrownSameVariable($catchItem)) {
continue;
}
/** @var FullyQualified $type */
$type = $catchItem->types[0];
if ($this->shouldSkipNextCatchClassParentWithSpecialTreatment($catches, $type, $key, $maxIndexCatches)) {
continue;
}
unset($catches[$key]);
$hasChanged = true;
}
if (! $hasChanged || $catches === []) {
return null;
}
$node->catches = $catches;
return $node;
}
private function isJustThrownSameVariable(Catch_ $catch): bool
{
if ($this->isEmpty($catch->stmts)) {
return false;
}
$catchItemStmt = $catch->stmts[0];
if (! $catchItemStmt instanceof Expression || ! $catchItemStmt->expr instanceof Throw_) {
return false;
}
if (! $this->nodeComparator->areNodesEqual($catch->var, $catchItemStmt->expr->expr)) {
return false;
}
// too complex to check
if (count($catch->types) !== 1) {
return false;
}
$type = $catch->types[0];
return $type instanceof FullyQualified;
}
/**
* @param Catch_[] $catches
*/
private function shouldSkipNextCatchClassParentWithSpecialTreatment(
array $catches,
FullyQualified $fullyQualified,
int $key,
int $maxIndexCatches
): bool {
for ($index = $key + 1; $index <= $maxIndexCatches; ++$index) {
if (! isset($catches[$index])) {
continue;
}
$nextCatch = $catches[$index];
// too complex to check
if (count($nextCatch->types) !== 1) {
return true;
}
$nextCatchType = $nextCatch->types[0];
if (! $nextCatchType instanceof FullyQualified) {
return true;
}
// Throwable/Exception catch anything, so the current catch is never dead against them,
// even when the current type cannot be resolved to prove the parent relation
$catchesAnything = in_array(
ltrim($nextCatchType->toString(), '\\'),
['Throwable', 'Exception'],
true
);
// only skip when the next catch is provably unrelated; an unresolvable next type
// might be a parent of the current one, so removing the catch would change behavior
if (! $catchesAnything && $this->isProvablyUnrelated($fullyQualified, $nextCatchType)) {
continue;
}
if (! $this->isJustThrownSameVariable($nextCatch)) {
return true;
}
}
return false;
}
private function isProvablyUnrelated(FullyQualified $current, FullyQualified $next): bool
{
if (! $this->reflectionProvider->hasClass($current->toString())) {
return false;
}
if (! $this->reflectionProvider->hasClass($next->toString())) {
return false;
}
return ! $this->isObjectType($current, new ObjectType($next->toString()));
}
/**
* @param Stmt[] $stmts
*/
private function isEmpty(array $stmts): bool
{
if ($stmts === []) {
return \true;
}
if (\count($stmts) > 1) {
return \false;
}
return $stmts[0] instanceof Nop;
}
}