Repository navigation
Expand file tree
/
Copy pathRepeatedAndNotEqualToNotInArrayRector.php
More file actions
182 lines (149 loc) · 5.49 KB
/
Copy pathRepeatedAndNotEqualToNotInArrayRector.php
File metadata and controls
182 lines (149 loc) · 5.49 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
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
173
174
175
176
177
178
179
180
181
182
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\BooleanAnd;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\CodeQuality\NodeFactory\InArrayFromRepeatedCompareFactory;
use Rector\CodeQuality\ValueObject\ComparedExprAndValueExpr;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\BooleanAnd\RepeatedAndNotEqualToNotInArrayRector\RepeatedAndNotEqualToNotInArrayRectorTest
*/
final class RepeatedAndNotEqualToNotInArrayRector extends AbstractRector
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly InArrayFromRepeatedCompareFactory $inArrayFromRepeatedCompareFactory,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Simplify repeated && compare of same value, to ! in_array() call',
[
new CodeSample(
<<<'CODE_SAMPLE'
if ($value !== 10 && $value !== 20 && $value !== 30) {
// ...
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
if (! in_array($value, [10, 20, 30], true)) {
// ...
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [BooleanAnd::class];
}
/**
* @param BooleanAnd $node
*/
public function refactor(Node $node): ?BooleanNot
{
// in_array() func call is not allowed in a constant expression
if ($this->isInConstantExpression($node)) {
return null;
}
if (! $this->isNotEqualOrNotIdentical($node->right)) {
return null;
}
// match compared variable and expr
if (! $node->left instanceof BooleanAnd && ! $this->isNotEqualOrNotIdentical($node->left)) {
return null;
}
$comparedExprAndValueExprs = $this->matchComparedAndDesiredValues($node);
if ($comparedExprAndValueExprs === null) {
return null;
}
$args = $this->inArrayFromRepeatedCompareFactory->createInArrayArgs($comparedExprAndValueExprs);
if ($args === null) {
return null;
}
if ($this->isStrictComparison($node)) {
$args[] = new Arg(new ConstFetch(new Name('true')));
}
$inArrayFuncCall = new FuncCall(new Name('in_array'), $args);
return new BooleanNot($inArrayFuncCall);
}
private function isInConstantExpression(BooleanAnd $booleanAnd): bool
{
if ($booleanAnd->getAttribute(AttributeKey::IS_DEFAULT_PROPERTY_VALUE) === true) {
return true;
}
if ($booleanAnd->getAttribute(AttributeKey::IS_CLASS_CONST_VALUE) === true) {
return true;
}
return $booleanAnd->getAttribute(AttributeKey::IS_PARAM_DEFAULT) === true;
}
private function isNotEqualOrNotIdentical(Expr $expr): bool
{
if ($expr instanceof NotIdentical) {
return true;
}
return $expr instanceof NotEqual;
}
private function matchComparedExprAndValueExpr(NotIdentical|NotEqual $expr): ComparedExprAndValueExpr
{
return new ComparedExprAndValueExpr($expr->left, $expr->right);
}
/**
* @return null|ComparedExprAndValueExpr[]
*/
private function matchComparedAndDesiredValues(BooleanAnd $booleanAnd): ?array
{
/** @var NotIdentical|NotEqual $rightCompare */
$rightCompare = $booleanAnd->right;
// match compared expr and desired value
$comparedExprAndValueExprs = [$this->matchComparedExprAndValueExpr($rightCompare)];
$currentBooleanAnd = $booleanAnd;
while ($currentBooleanAnd->left instanceof BooleanAnd) {
if (! $this->isNotEqualOrNotIdentical($currentBooleanAnd->left->right)) {
return null;
}
/** @var NotIdentical|NotEqual $leftRight */
$leftRight = $currentBooleanAnd->left->right;
$comparedExprAndValueExprs[] = $this->matchComparedExprAndValueExpr($leftRight);
$currentBooleanAnd = $currentBooleanAnd->left;
}
if (! $this->isNotEqualOrNotIdentical($currentBooleanAnd->left)) {
return null;
}
/** @var NotIdentical|NotEqual $leftCompare */
$leftCompare = $currentBooleanAnd->left;
$comparedExprAndValueExprs[] = $this->matchComparedExprAndValueExpr($leftCompare);
// keep original natural order, as left/right goes from bottom up
return array_reverse($comparedExprAndValueExprs);
}
private function isStrictComparison(BooleanAnd $booleanAnd): bool
{
$notIdenticals = $this->betterNodeFinder->findInstanceOf($booleanAnd, NotIdentical::class);
if ($notIdenticals === []) {
return false;
}
$notEquals = $this->betterNodeFinder->findInstanceOf($booleanAnd, NotEqual::class);
// mix not identical and not equals, keep as is
// @see https://3v4l.org/2SoHZ
return $notEquals === [];
}
}