-
-
Notifications
You must be signed in to change notification settings - Fork 680
/
CompleteMissingIfElseBracketRector.php
108 lines (106 loc) · 3.04 KB
/
CompleteMissingIfElseBracketRector.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
<?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 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
{
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 mixed[] $oldTokens
* @param \PhpParser\Node\Stmt\If_|\PhpParser\Node\Stmt\ElseIf_|\PhpParser\Node\Stmt\Else_ $if
*/
private function isIfConditionFollowedByOpeningCurlyBracket($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($oldTokens[$i], ['{', ':'], \true)) {
// all good
return \true;
}
if ($i === $startTokenPos) {
return \false;
}
--$i;
}
return \false;
}
/**
* @param \PhpParser\Node\Stmt\If_|\PhpParser\Node\Stmt\ElseIf_|\PhpParser\Node\Stmt\Else_ $if
*/
private function isBareNewNode($if) : bool
{
$originalNode = $if->getAttribute(AttributeKey::ORIGINAL_NODE);
if (!$originalNode instanceof Node) {
return \true;
}
// not defined, probably new if
return $if->getStartTokenPos() === -1;
}
}