-
-
Notifications
You must be signed in to change notification settings - Fork 708
/
Copy pathSimplifyIfElseToTernaryRector.php
175 lines (173 loc) · 4.92 KB
/
SimplifyIfElseToTernaryRector.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
<?php
declare (strict_types=1);
namespace Rector\CodeQuality\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Printer\BetterStandardPrinter;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector\SimplifyIfElseToTernaryRectorTest
*/
final class SimplifyIfElseToTernaryRector extends AbstractRector
{
/**
* @readonly
*/
private BetterStandardPrinter $betterStandardPrinter;
/**
* @readonly
*/
private BetterNodeFinder $betterNodeFinder;
/**
* @var int
*/
private const LINE_LENGTH_LIMIT = 120;
public function __construct(BetterStandardPrinter $betterStandardPrinter, BetterNodeFinder $betterNodeFinder)
{
$this->betterStandardPrinter = $betterStandardPrinter;
$this->betterNodeFinder = $betterNodeFinder;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Changes if/else for same value as assign to ternary', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
if (empty($value)) {
$this->arrayBuilt[][$key] = true;
} else {
$this->arrayBuilt[][$key] = $value;
}
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$this->arrayBuilt[][$key] = empty($value) ? true : $value;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [If_::class];
}
/**
* @param If_ $node
*/
public function refactor(Node $node) : ?Node
{
if (!$node->else instanceof Else_) {
return null;
}
if ($node->elseifs !== []) {
return null;
}
$ifAssignVarExpr = $this->resolveOnlyStmtAssignVar($node->stmts);
if (!$ifAssignVarExpr instanceof Expr) {
return null;
}
$elseAssignExpr = $this->resolveOnlyStmtAssignVar($node->else->stmts);
if (!$elseAssignExpr instanceof Expr) {
return null;
}
if (!$this->nodeComparator->areNodesEqual($ifAssignVarExpr, $elseAssignExpr)) {
return null;
}
$ternaryIfExpr = $this->resolveOnlyStmtAssignExpr($node->stmts);
$expr = $this->resolveOnlyStmtAssignExpr($node->else->stmts);
if (!$ternaryIfExpr instanceof Expr) {
return null;
}
if (!$expr instanceof Expr) {
return null;
}
// has nested ternary → skip, it's super hard to read
if ($this->haveNestedTernary([$node->cond, $ternaryIfExpr, $expr])) {
return null;
}
$ternary = new Ternary($node->cond, $ternaryIfExpr, $expr);
$assign = new Assign($ifAssignVarExpr, $ternary);
// do not create super long lines
if ($this->isNodeTooLong($assign)) {
return null;
}
$expression = new Expression($assign);
$this->mirrorComments($expression, $node);
return $expression;
}
/**
* @param Stmt[] $stmts
*/
private function resolveOnlyStmtAssignVar(array $stmts) : ?Expr
{
if (\count($stmts) !== 1) {
return null;
}
$stmt = $stmts[0];
if (!$stmt instanceof Expression) {
return null;
}
$stmtExpr = $stmt->expr;
if (!$stmtExpr instanceof Assign) {
return null;
}
return $stmtExpr->var;
}
/**
* @param Stmt[] $stmts
*/
private function resolveOnlyStmtAssignExpr(array $stmts) : ?Expr
{
if (\count($stmts) !== 1) {
return null;
}
$stmt = $stmts[0];
if (!$stmt instanceof Expression) {
return null;
}
if ($stmt->getComments() !== []) {
return null;
}
$stmtExpr = $stmt->expr;
if (!$stmtExpr instanceof Assign) {
return null;
}
return $stmtExpr->expr;
}
/**
* @param Node[] $nodes
*/
private function haveNestedTernary(array $nodes) : bool
{
foreach ($nodes as $node) {
$ternary = $this->betterNodeFinder->findFirstInstanceOf($node, Ternary::class);
if ($ternary instanceof Ternary) {
return \true;
}
}
return \false;
}
private function isNodeTooLong(Assign $assign) : bool
{
$assignContent = $this->betterStandardPrinter->print($assign);
return \strlen($assignContent) > self::LINE_LENGTH_LIMIT;
}
}