-
-
Notifications
You must be signed in to change notification settings - Fork 687
/
IfIssetToCoalescingRector.php
118 lines (113 loc) · 3.22 KB
/
IfIssetToCoalescingRector.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
<?php
declare (strict_types=1);
namespace Rector\Php70\Rector\StmtsAwareInterface;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Rector\AbstractRector;
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\StmtsAwareInterface\IfIssetToCoalescingRector\IfIssetToCoalescingRectorTest
*/
final class IfIssetToCoalescingRector extends AbstractRector implements MinPhpVersionInterface
{
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Change if with isset and return to coalesce', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
private $items = [];
public function resolve($key)
{
if (isset($this->items[$key])) {
return $this->items[$key];
}
return 'fallback value';
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
private $items = [];
public function resolve($key)
{
return $this->items[$key] ?? 'fallback value';
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [StmtsAwareInterface::class];
}
/**
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node) : ?Node
{
if ($node->stmts === null) {
return null;
}
foreach ($node->stmts as $key => $stmt) {
if (!$stmt instanceof Return_) {
continue;
}
if (!$stmt->expr instanceof Expr) {
continue;
}
$previousStmt = $node->stmts[$key - 1] ?? null;
if (!$previousStmt instanceof If_) {
continue;
}
if (!$previousStmt->cond instanceof Isset_) {
continue;
}
$ifOnlyStmt = $this->matchBareIfOnlyStmt($previousStmt);
if (!$ifOnlyStmt instanceof Return_) {
continue;
}
if (!$ifOnlyStmt->expr instanceof Expr) {
continue;
}
$ifIsset = $previousStmt->cond;
if (!$this->nodeComparator->areNodesEqual($ifOnlyStmt->expr, $ifIsset->vars[0])) {
continue;
}
unset($node->stmts[$key - 1]);
$stmt->expr = new Coalesce($ifOnlyStmt->expr, $stmt->expr);
return $node;
}
return null;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::NULL_COALESCE;
}
private function matchBareIfOnlyStmt(If_ $if) : ?Stmt
{
if ($if->else instanceof Else_) {
return null;
}
if ($if->elseifs !== []) {
return null;
}
if (\count($if->stmts) !== 1) {
return null;
}
return $if->stmts[0];
}
}