forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostIncDecToPreIncDecRector.php
135 lines (121 loc) · 3.46 KB
/
PostIncDecToPreIncDecRector.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
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Rector\PostInc;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\PostDec;
use PhpParser\Node\Expr\PostInc;
use PhpParser\Node\Expr\PreDec;
use PhpParser\Node\Expr\PreInc;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\For_;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector\PostIncDecToPreIncDecRectorTest
*/
final class PostIncDecToPreIncDecRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Use ++$value or --$value instead of `$value++` or `$value--`',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($value = 1)
{
$value++; echo $value;
$value--; echo $value;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($value = 1)
{
++$value; echo $value;
--$value; echo $value;
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [PostInc::class, PostDec::class];
}
/**
* @param PostInc|PostDec $node
*/
public function refactor(Node $node): ?Node
{
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($this->isAnExpression($parentNode)) {
return $this->processPrePost($node);
}
if ($parentNode instanceof ArrayDimFetch && $this->nodeComparator->areNodesEqual($parentNode->dim, $node)) {
return $this->processPreArray($node, $parentNode);
}
if (! $parentNode instanceof For_) {
return null;
}
if (count($parentNode->loop) !== 1) {
return null;
}
if (! $this->nodeComparator->areNodesEqual($parentNode->loop[0], $node)) {
return null;
}
return $this->processPreFor($node, $parentNode);
}
private function isAnExpression(?Node $node = null): bool
{
if (! $node instanceof Node) {
return false;
}
return $node instanceof Expression;
}
/**
* @param PostInc|PostDec $node
*/
private function processPrePost(Node $node): Expr
{
if ($node instanceof PostInc) {
return new PreInc($node->var);
}
return new PreDec($node->var);
}
/**
* @param PostInc|PostDec $node
*/
private function processPreArray(Node $node, ArrayDimFetch $arrayDimFetch): ?Expr
{
$parentOfArrayDimFetch = $arrayDimFetch->getAttribute(AttributeKey::PARENT_NODE);
if (! $this->isAnExpression($parentOfArrayDimFetch)) {
return null;
}
$arrayDimFetch->dim = $node->var;
$this->addNodeAfterNode($this->processPrePost($node), $arrayDimFetch);
return $arrayDimFetch->dim;
}
/**
* @param PostInc|PostDec $node
*/
private function processPreFor(Node $node, For_ $for): Expr
{
$for->loop = [$this->processPrePost($node)];
return $for->loop[0];
}
}