-
-
Notifications
You must be signed in to change notification settings - Fork 441
Expand file tree
/
Copy pathTernaryToNullCoalescingRector.php
More file actions
166 lines (136 loc) · 4.84 KB
/
TernaryToNullCoalescingRector.php
File metadata and controls
166 lines (136 loc) · 4.84 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
<?php
declare(strict_types=1);
namespace Rector\Php70\Rector\Ternary;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\Ternary;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\Application\File;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Php70\Rector\Ternary\TernaryToNullCoalescingRector\TernaryToNullCoalescingRectorTest
*/
final class TernaryToNullCoalescingRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ValueResolver $valueResolver
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes unneeded null check to ?? operator',
[
new CodeSample('$value === null ? 10 : $value;', '$value ?? 10;'),
new CodeSample('isset($value) ? $value : 10;', '$value ?? 10;'),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Ternary::class];
}
/**
* @param Ternary $node
*/
public function refactor(Node $node): ?Node
{
if ($node->cond instanceof Isset_) {
return $this->processTernaryWithIsset($node, $node->cond);
}
if ($node->cond instanceof Identical) {
$checkedNode = $node->else;
$fallbackNode = $node->if;
} elseif ($node->cond instanceof NotIdentical) {
$checkedNode = $node->if;
$fallbackNode = $node->else;
} else {
// not a match
return null;
}
if (! $checkedNode instanceof Expr) {
return null;
}
if (! $fallbackNode instanceof Expr) {
return null;
}
$ternaryCompareNode = $node->cond;
if ($this->isNullMatch($ternaryCompareNode->left, $ternaryCompareNode->right, $checkedNode)) {
return new Coalesce($checkedNode, $fallbackNode);
}
if ($this->isNullMatch($ternaryCompareNode->right, $ternaryCompareNode->left, $checkedNode)) {
return new Coalesce($checkedNode, $fallbackNode);
}
return null;
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::NULL_COALESCE;
}
private function processTernaryWithIsset(Ternary $ternary, Isset_ $isset): ?Coalesce
{
if (! $ternary->if instanceof Expr) {
return null;
}
if ($isset->vars === []) {
return null;
}
// none or multiple isset values cannot be handled here
if (count($isset->vars) > 1) {
return null;
}
if (! $this->nodeComparator->areNodesEqual($ternary->if, $isset->vars[0])) {
return null;
}
if (($ternary->else instanceof Ternary || $ternary->else instanceof BinaryOp) && $this->isTernaryParenthesized(
$this->file,
$ternary->cond,
$ternary
)) {
$ternary->else->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
}
return new Coalesce($ternary->if, $ternary->else);
}
private function isTernaryParenthesized(File $file, Expr $expr, Ternary $ternary): bool
{
$oldTokens = $file->getOldTokens();
$endTokenPost = $ternary->getEndTokenPos();
if (isset($oldTokens[$endTokenPost]) && (string) $oldTokens[$endTokenPost] === ')') {
$startTokenPos = $ternary->else->getStartTokenPos();
$previousEndTokenPost = $expr->getEndTokenPos();
while ($startTokenPos > $previousEndTokenPost) {
--$startTokenPos;
if (! isset($oldTokens[$startTokenPos])) {
return false;
}
// handle space before open parentheses
if (trim((string) $oldTokens[$startTokenPos]) === '') {
continue;
}
return (string) $oldTokens[$startTokenPos] === '(';
}
return false;
}
return false;
}
private function isNullMatch(Expr $possibleNullExpr, Expr $firstNode, Expr $secondNode): bool
{
if (! $this->valueResolver->isNull($possibleNullExpr)) {
return false;
}
return $this->nodeComparator->areNodesEqual($firstNode, $secondNode);
}
}