-
-
Notifications
You must be signed in to change notification settings - Fork 708
/
Copy pathCountArrayToEmptyArrayComparisonRector.php
172 lines (170 loc) · 5.74 KB
/
CountArrayToEmptyArrayComparisonRector.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
declare (strict_types=1);
namespace Rector\CodingStyle\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp\Greater;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\If_;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector\CountArrayToEmptyArrayComparisonRectorTest
*/
final class CountArrayToEmptyArrayComparisonRector extends AbstractRector
{
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Change count array comparison to empty array comparison to improve performance', [new CodeSample(<<<'CODE_SAMPLE'
count($array) === 0;
count($array) > 0;
! count($array);
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$array === [];
$array !== [];
$array === [];
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Identical::class, NotIdentical::class, BooleanNot::class, Greater::class, Smaller::class, If_::class, ElseIf_::class];
}
/**
* @param Identical|NotIdentical|BooleanNot|Greater|Smaller|If_|ElseIf_ $node
*/
public function refactor(Node $node) : ?Node
{
if ($node instanceof BooleanNot) {
return $this->refactorBooleanNot($node);
}
if ($node instanceof Identical || $node instanceof NotIdentical) {
if ($node->left instanceof FuncCall) {
$expr = $this->matchCountFuncCallArgExpr($node->left);
} elseif ($node->right instanceof FuncCall) {
$expr = $this->matchCountFuncCallArgExpr($node->right);
} else {
return null;
}
if (!$expr instanceof Expr) {
return null;
}
// not pass array type, skip
if (!$this->isArray($expr)) {
return null;
}
return $this->refactorIdenticalOrNotIdentical($node, $expr);
}
if ($node instanceof Smaller || $node instanceof Greater) {
return $this->refactorGreaterOrSmaller($node);
}
return $this->refactorIfElseIf($node);
}
private function refactorBooleanNot(BooleanNot $booleanNot) : ?Identical
{
$expr = $this->matchCountFuncCallArgExpr($booleanNot->expr);
if (!$expr instanceof Expr) {
return null;
}
// not pass array type, skip
if (!$this->isArray($expr)) {
return null;
}
return new Identical($expr, new Array_([]));
}
private function isArray(Expr $expr) : bool
{
return $this->nodeTypeResolver->getNativeType($expr)->isArray()->yes();
}
/**
* @param \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical $binaryOp
* @return \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical|null
*/
private function refactorIdenticalOrNotIdentical($binaryOp, Expr $expr)
{
if ($this->isZeroLNumber($binaryOp->right)) {
$binaryOp->left = $expr;
$binaryOp->right = new Array_([]);
return $binaryOp;
}
if ($this->isZeroLNumber($binaryOp->left)) {
$binaryOp->left = new Array_([]);
$binaryOp->right = $expr;
return $binaryOp;
}
return null;
}
/**
* @param \PhpParser\Node\Expr\BinaryOp\Greater|\PhpParser\Node\Expr\BinaryOp\Smaller $binaryOp
*/
private function refactorGreaterOrSmaller($binaryOp) : ?\PhpParser\Node\Expr\BinaryOp\NotIdentical
{
if ($binaryOp instanceof Greater) {
$leftExpr = $this->matchCountFuncCallArgExpr($binaryOp->left);
if (!$leftExpr instanceof Expr) {
return null;
}
if (!$this->isZeroLNumber($binaryOp->right)) {
return null;
}
return new NotIdentical($leftExpr, new Array_([]));
}
$rightExpr = $this->matchCountFuncCallArgExpr($binaryOp->right);
if (!$rightExpr instanceof Expr) {
return null;
}
if (!$this->isZeroLNumber($binaryOp->left)) {
return null;
}
return new NotIdentical(new Array_([]), $rightExpr);
}
/**
* @param \PhpParser\Node\Stmt\If_|\PhpParser\Node\Stmt\ElseIf_ $ifElseIf
* @return \PhpParser\Node\Stmt\If_|\PhpParser\Node\Stmt\ElseIf_|null
*/
private function refactorIfElseIf($ifElseIf)
{
$expr = $this->matchCountFuncCallArgExpr($ifElseIf->cond);
if (!$expr instanceof Expr) {
return null;
}
$ifElseIf->cond = new NotIdentical($expr, new Array_([]));
return $ifElseIf;
}
private function matchCountFuncCallArgExpr(Expr $expr) : ?Expr
{
if (!$expr instanceof FuncCall) {
return null;
}
if (!$this->isName($expr, 'count')) {
return null;
}
if ($expr->isFirstClassCallable()) {
return null;
}
$firstArg = $expr->getArgs()[0];
if (!$this->isArray($firstArg->value)) {
return null;
}
return $firstArg->value;
}
private function isZeroLNumber(Expr $expr) : bool
{
if (!$expr instanceof Int_) {
return \false;
}
return $expr->value === 0;
}
}