-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
SilentVoidResolver.php
205 lines (169 loc) · 5.82 KB
/
SilentVoidResolver.php
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
<?php
declare(strict_types=1);
namespace Rector\TypeDeclaration\TypeInferer;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Exit_;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Expr\YieldFrom;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Case_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Continue_;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Finally_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Goto_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\Node\Stmt\TryCatch;
use PHPStan\Reflection\ClassReflection;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Reflection\ReflectionResolver;
final readonly class SilentVoidResolver
{
public function __construct(
private BetterNodeFinder $betterNodeFinder,
private ReflectionResolver $reflectionResolver,
) {
}
public function hasExclusiveVoid(ClassMethod | Closure | Function_ $functionLike): bool
{
$classReflection = $this->reflectionResolver->resolveClassReflection($functionLike);
if ($classReflection instanceof ClassReflection && $classReflection->isInterface()) {
return false;
}
if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped(
$functionLike,
[Yield_::class, YieldFrom::class]
)) {
return false;
}
$return = $this->betterNodeFinder->findFirstInFunctionLikeScoped(
$functionLike,
static fn (Node $node): bool => $node instanceof Return_ && $node->expr instanceof Expr
);
return ! $return instanceof Return_;
}
public function hasSilentVoid(FunctionLike $functionLike): bool
{
if ($functionLike instanceof ArrowFunction) {
return false;
}
$stmts = (array) $functionLike->getStmts();
return ! $this->hasStmtsAlwaysReturnOrExit($stmts);
}
/**
* @param Stmt[]|Expression[] $stmts
*/
private function hasStmtsAlwaysReturnOrExit(array $stmts): bool
{
foreach ($stmts as $stmt) {
if ($stmt instanceof Expression) {
$stmt = $stmt->expr;
}
if ($this->isStopped($stmt)) {
return true;
}
// has switch with always return
if ($stmt instanceof Switch_ && $this->isSwitchWithAlwaysReturnOrExit($stmt)) {
return true;
}
if ($stmt instanceof TryCatch && $this->isTryCatchAlwaysReturnOrExit($stmt)) {
return true;
}
if ($this->isIfReturn($stmt)) {
return true;
}
if ($stmt instanceof Do_ && $this->isDoWithAlwaysReturnOrExit($stmt)) {
return true;
}
}
return false;
}
private function isDoWithAlwaysReturnOrExit(Do_ $do): bool
{
if (! $this->hasStmtsAlwaysReturnOrExit($do->stmts)) {
return false;
}
return ! (bool) $this->betterNodeFinder->findFirst(
$do->stmts,
static fn (Node $node): bool => $node instanceof Break_ || $node instanceof Continue_ || $node instanceof Goto_
);
}
private function isIfReturn(Stmt|Expr $stmt): bool
{
if (! $stmt instanceof If_) {
return false;
}
foreach ($stmt->elseifs as $elseIf) {
if (! $this->hasStmtsAlwaysReturnOrExit($elseIf->stmts)) {
return false;
}
}
if (! $stmt->else instanceof Else_) {
return false;
}
if (! $this->hasStmtsAlwaysReturnOrExit($stmt->stmts)) {
return false;
}
return $this->hasStmtsAlwaysReturnOrExit($stmt->else->stmts);
}
private function isStopped(Stmt|Expr $stmt): bool
{
return $stmt instanceof Throw_
|| $stmt instanceof Exit_
|| ($stmt instanceof Return_ && $stmt->expr instanceof Expr)
|| $stmt instanceof Yield_
|| $stmt instanceof YieldFrom;
}
private function isSwitchWithAlwaysReturnOrExit(Switch_ $switch): bool
{
$hasDefault = false;
foreach ($switch->cases as $case) {
if (! $case->cond instanceof Expr) {
$hasDefault = $case->stmts !== [];
break;
}
}
if (! $hasDefault) {
return false;
}
$casesWithReturnOrExitCount = $this->resolveReturnOrExitCount($switch);
$cases = array_filter($switch->cases, static fn (Case_ $case): bool => $case->stmts !== []);
// has same amount of first return or exit nodes as switches
return count($cases) === $casesWithReturnOrExitCount;
}
private function isTryCatchAlwaysReturnOrExit(TryCatch $tryCatch): bool
{
if (! $this->hasStmtsAlwaysReturnOrExit($tryCatch->stmts)) {
return false;
}
foreach ($tryCatch->catches as $catch) {
if (! $this->hasStmtsAlwaysReturnOrExit($catch->stmts)) {
return false;
}
}
return ! ($tryCatch->finally instanceof Finally_ && ! $this->hasStmtsAlwaysReturnOrExit(
$tryCatch->finally->stmts
));
}
private function resolveReturnOrExitCount(Switch_ $switch): int
{
$casesWithReturnCount = 0;
foreach ($switch->cases as $case) {
if ($this->hasStmtsAlwaysReturnOrExit($case->stmts)) {
++$casesWithReturnCount;
}
}
return $casesWithReturnCount;
}
}