-
-
Notifications
You must be signed in to change notification settings - Fork 688
/
ContinueToBreakInSwitchRector.php
152 lines (150 loc) · 4.65 KB
/
ContinueToBreakInSwitchRector.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
<?php
declare (strict_types=1);
namespace Rector\Php52\Rector\Switch_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Continue_;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\While_;
use PhpParser\NodeVisitor;
use PHPStan\Type\Constant\ConstantIntegerType;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\PhpParser\Node\Value\ValueResolver;
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\Php52\Rector\Switch_\ContinueToBreakInSwitchRector\ContinueToBreakInSwitchRectorTest
*/
final class ContinueToBreakInSwitchRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
*/
private ValueResolver $valueResolver;
private bool $hasChanged = \false;
public function __construct(ValueResolver $valueResolver)
{
$this->valueResolver = $valueResolver;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::CONTINUE_TO_BREAK;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Use break instead of continue in switch statements', [new CodeSample(<<<'CODE_SAMPLE'
function some_run($value)
{
switch ($value) {
case 1:
echo 'Hi';
continue;
case 2:
echo 'Hello';
break;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
function some_run($value)
{
switch ($value) {
case 1:
echo 'Hi';
break;
case 2:
echo 'Hello';
break;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Switch_::class];
}
/**
* @param Switch_ $node
*/
public function refactor(Node $node) : ?Switch_
{
$this->hasChanged = \false;
foreach ($node->cases as $case) {
$this->processContinueStatement($case);
}
if (!$this->hasChanged) {
return null;
}
return $node;
}
/**
* @param \PhpParser\Node\Stmt|\Rector\Contract\PhpParser\Node\StmtsAwareInterface $stmt
*/
private function processContinueStatement($stmt) : void
{
$this->traverseNodesWithCallable($stmt, function (Node $subNode) {
if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) {
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
// continue is belong to loop
if ($subNode instanceof Foreach_ || $subNode instanceof While_ || $subNode instanceof Do_ || $subNode instanceof For_) {
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if (!$subNode instanceof Continue_) {
return null;
}
if (!$subNode->num instanceof Expr) {
$this->hasChanged = \true;
return new Break_();
}
if ($subNode->num instanceof Int_) {
$continueNumber = $this->valueResolver->getValue($subNode->num);
if ($continueNumber <= 1) {
$this->hasChanged = \true;
return new Break_();
}
} elseif ($subNode->num instanceof Variable) {
$processVariableNum = $this->processVariableNum($subNode, $subNode->num);
if ($processVariableNum instanceof Break_) {
$this->hasChanged = \true;
return $processVariableNum;
}
}
return null;
});
}
/**
* @return \PhpParser\Node\Stmt\Continue_|\PhpParser\Node\Stmt\Break_
*/
private function processVariableNum(Continue_ $continue, Variable $numVariable)
{
$staticType = $this->getType($numVariable);
if (!$staticType->isConstantValue()->yes()) {
return $continue;
}
if (!$staticType instanceof ConstantIntegerType) {
return $continue;
}
if ($staticType->getValue() > 1) {
return $continue;
}
return new Break_();
}
}