-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathCompleteMissingIfElseBracketRector.php
More file actions
129 lines (110 loc) · 3.09 KB
/
CompleteMissingIfElseBracketRector.php
File metadata and controls
129 lines (110 loc) · 3.09 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
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Token;
use Rector\Contract\Rector\HTMLAverseRectorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector\CompleteMissingIfElseBracketRectorTest
*/
final class CompleteMissingIfElseBracketRector extends AbstractRector implements HTMLAverseRectorInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Complete missing if/else brackets', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($value)
{
if ($value)
return 1;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($value)
{
if ($value) {
return 1;
}
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [If_::class, ElseIf_::class, Else_::class];
}
/**
* @param If_|ElseIf_|Else_ $node
*/
public function refactor(Node $node): ?Node
{
if ($this->isBareNewNode($node)) {
return null;
}
$oldTokens = $this->file->getOldTokens();
if ($this->isIfConditionFollowedByOpeningCurlyBracket($node, $oldTokens)) {
return null;
}
// invoke reprint with brackets
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
return $node;
}
/**
* @param Token[] $oldTokens
*/
private function isIfConditionFollowedByOpeningCurlyBracket(If_|ElseIf_|Else_ $if, array $oldTokens): bool
{
$startStmt = current($if->stmts);
if (! $startStmt instanceof Stmt) {
return true;
}
$startTokenPos = $if->getStartTokenPos();
$i = $startStmt->getStartTokenPos() - 1;
$condEndTokenPos = $if instanceof Else_
? $startTokenPos
: $if->cond->getEndTokenPos();
while (isset($oldTokens[$i])) {
if ($i === $condEndTokenPos) {
return false;
}
if (in_array((string) $oldTokens[$i], ['{', ':'], true)) {
// all good
return true;
}
if ($i === $startTokenPos) {
return false;
}
--$i;
}
return false;
}
private function isBareNewNode(If_|ElseIf_|Else_ $if): bool
{
$originalNode = $if->getAttribute(AttributeKey::ORIGINAL_NODE);
if (! $originalNode instanceof Node) {
return true;
}
// not defined, probably new if
return $if->getStartTokenPos() === -1;
}
}