-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathNestedTernaryToMatchRector.php
More file actions
161 lines (134 loc) · 4.5 KB
/
NestedTernaryToMatchRector.php
File metadata and controls
161 lines (134 loc) · 4.5 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
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Rector\Assign;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\MatchArm;
use Rector\CodingStyle\ValueObject\ConditionAndResult;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodingStyle\Rector\Assign\NestedTernaryToMatchRector\NestedTernaryToMatchRectorTest
*/
final class NestedTernaryToMatchRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Convert nested ternary expressions to match(true) statements', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function getValue($input)
{
return $input > 100 ? 'more than 100' : ($input > 5 ? 'more than 5' : 'less');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function getValue($input)
{
return match (true) {
$input > 100 => 'more than 100',
$input > 5 => 'more than 5',
default => 'less',
};
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Assign::class];
}
/**
* @param Assign $node
*/
public function refactor(Node $node): ?Assign
{
if (! $node->expr instanceof Ternary) {
return null;
}
$ternary = $node->expr;
// traverse nested ternaries to collect them all
$currentTernary = $ternary;
/** @var ConditionAndResult[] $conditionsAndResults */
$conditionsAndResults = [];
$defaultExpr = null;
while ($currentTernary instanceof Ternary) {
if (! $currentTernary->if instanceof Expr) {
// short ternary, skip
return null;
}
$conditionsAndResults[] = new ConditionAndResult($currentTernary->cond, $currentTernary->if);
$currentTernary = $currentTernary->else;
if (! $currentTernary instanceof Ternary) {
$defaultExpr = $currentTernary;
}
}
// nothing long enough
if (count($conditionsAndResults) < 2 || ! $defaultExpr instanceof Expr) {
return null;
}
$match = $this->createMatch($conditionsAndResults, $defaultExpr);
$node->expr = $match;
return $node;
}
/**
* @param ConditionAndResult[] $conditionsAndResults
*/
private function createMatch(array $conditionsAndResults, Expr $defaultExpr): Match_
{
$singleVariableName = $this->matchAlwaysIdenticalVariableName($conditionsAndResults);
if (is_string($singleVariableName)) {
$isVariableIdentical = true;
$match = new Match_(new Variable($singleVariableName));
} else {
$isVariableIdentical = false;
$match = new Match_($this->nodeFactory->createTrue());
}
foreach ($conditionsAndResults as $conditionAndResult) {
$match->arms[] = new MatchArm([
$isVariableIdentical ? $conditionAndResult->getIdenticalExpr() : $conditionAndResult->getConditionExpr(),
], $conditionAndResult->getResultExpr());
}
$match->arms[] = new MatchArm(null, $defaultExpr);
return $match;
}
/**
* @param ConditionAndResult[] $conditionsAndResults
*/
private function matchAlwaysIdenticalVariableName(array $conditionsAndResults): mixed
{
$identicalVariableNames = [];
foreach ($conditionsAndResults as $conditionAndResult) {
if (! $conditionAndResult->isIdenticalCompare()) {
return null;
}
$variableName = $conditionAndResult->getIdenticalVariableName();
if (! is_string($variableName)) {
return null;
}
$identicalVariableNames[] = $variableName;
}
$uniqueIdenticalVariableNames = array_unique($identicalVariableNames);
$uniqueIdenticalVariableNames = array_values($uniqueIdenticalVariableNames);
if (count($uniqueIdenticalVariableNames) === 1) {
return $uniqueIdenticalVariableNames[0];
}
return null;
}
}