-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
ReturnEarlyIfVariableRector.php
148 lines (123 loc) · 3.64 KB
/
ReturnEarlyIfVariableRector.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
<?php
declare(strict_types=1);
namespace Rector\EarlyReturn\Rector\StmtsAwareInterface;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector\ReturnEarlyIfVariableRectorTest
*/
final class ReturnEarlyIfVariableRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Replace if conditioned variable override with direct return', [
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run($value)
{
if ($value === 50) {
$value = 100;
}
return $value;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run($value)
{
if ($value === 50) {
return 100;
}
return $value;
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StmtsAwareInterface::class];
}
/**
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node): ?Node
{
$stmts = (array) $node->stmts;
foreach ($stmts as $key => $stmt) {
$returnVariable = $this->matchNextStmtReturnVariable($node, $key);
if (! $returnVariable instanceof Variable) {
continue;
}
if ($stmt instanceof If_ && $stmt->else === null && $stmt->elseifs === []) {
// is single condition if
$if = $stmt;
if (count($if->stmts) !== 1) {
continue;
}
$onlyIfStmt = $if->stmts[0];
$assignedExpr = $this->matchOnlyIfStmtReturnExpr($onlyIfStmt, $returnVariable);
if (! $assignedExpr instanceof Expr) {
continue;
}
$if->stmts[0] = new Return_($assignedExpr);
return $node;
}
}
return null;
}
private function matchOnlyIfStmtReturnExpr(Stmt $onlyIfStmt, Variable $returnVariable): Expr|null
{
if (! $onlyIfStmt instanceof Expression) {
return null;
}
if (! $onlyIfStmt->expr instanceof Assign) {
return null;
}
$assign = $onlyIfStmt->expr;
// assign to same variable that is returned
if (! $assign->var instanceof Variable) {
return null;
}
if (! $this->nodeComparator->areNodesEqual($assign->var, $returnVariable)) {
return null;
}
// return directly
return $assign->expr;
}
private function matchNextStmtReturnVariable(StmtsAwareInterface $stmtsAware, int $key): Variable|null
{
$nextStmt = $stmtsAware->stmts[$key + 1] ?? null;
// last item → stop
if ($nextStmt === null) {
return null;
}
if (! $nextStmt instanceof Return_) {
return null;
}
// next return must be variable
if (! $nextStmt->expr instanceof Variable) {
return null;
}
return $nextStmt->expr;
}
}