-
-
Notifications
You must be signed in to change notification settings - Fork 687
/
ExplicitBoolCompareRector.php
265 lines (263 loc) · 9.05 KB
/
ExplicitBoolCompareRector.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
declare (strict_types=1);
namespace Rector\CodeQuality\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Greater;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use Rector\NodeTypeResolver\TypeAnalyzer\ArrayTypeAnalyzer;
use Rector\NodeTypeResolver\TypeAnalyzer\StringTypeAnalyzer;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\If_\ExplicitBoolCompareRector\ExplicitBoolCompareRectorTest
*/
final class ExplicitBoolCompareRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\NodeTypeResolver\TypeAnalyzer\StringTypeAnalyzer
*/
private $stringTypeAnalyzer;
/**
* @readonly
* @var \Rector\NodeTypeResolver\TypeAnalyzer\ArrayTypeAnalyzer
*/
private $arrayTypeAnalyzer;
/**
* @readonly
* @var \Rector\PhpParser\Node\Value\ValueResolver
*/
private $valueResolver;
public function __construct(StringTypeAnalyzer $stringTypeAnalyzer, ArrayTypeAnalyzer $arrayTypeAnalyzer, ValueResolver $valueResolver)
{
$this->stringTypeAnalyzer = $stringTypeAnalyzer;
$this->arrayTypeAnalyzer = $arrayTypeAnalyzer;
$this->valueResolver = $valueResolver;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Make if conditions more explicit', [new CodeSample(<<<'CODE_SAMPLE'
final class SomeController
{
public function run($items)
{
if (!count($items)) {
return 'no items';
}
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeController
{
public function run($items)
{
if (count($items) === 0) {
return 'no items';
}
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [If_::class, ElseIf_::class, Ternary::class];
}
/**
* @param If_|ElseIf_|Ternary $node
* @return null|Stmt[]|Node
*/
public function refactor(Node $node)
{
// skip short ternary
if ($node instanceof Ternary && !$node->if instanceof Expr) {
return null;
}
if ($node->cond instanceof BooleanNot) {
$conditionNode = $node->cond->expr;
$isNegated = \true;
} else {
$conditionNode = $node->cond;
$isNegated = \false;
}
if ($conditionNode instanceof Bool_) {
return null;
}
$conditionStaticType = $this->nodeTypeResolver->getNativeType($conditionNode);
if ($conditionStaticType instanceof MixedType || $conditionStaticType->isBoolean()->yes()) {
return null;
}
$binaryOp = $this->resolveNewConditionNode($conditionNode, $isNegated);
if (!$binaryOp instanceof BinaryOp) {
return null;
}
if ($node instanceof If_ && $node->cond instanceof Assign && $binaryOp->left instanceof NotIdentical && $binaryOp->right instanceof NotIdentical) {
$expression = new Expression($node->cond);
$binaryOp->left->left = $node->cond->var;
$binaryOp->right->left = $node->cond->var;
$node->cond = $binaryOp;
return [$expression, $node];
}
$node->cond = $binaryOp;
return $node;
}
private function resolveNewConditionNode(Expr $expr, bool $isNegated) : ?BinaryOp
{
if ($expr instanceof FuncCall && $this->nodeNameResolver->isName($expr, 'count')) {
return $this->resolveCount($isNegated, $expr);
}
if ($this->arrayTypeAnalyzer->isArrayType($expr)) {
return $this->resolveArray($isNegated, $expr);
}
if ($this->stringTypeAnalyzer->isStringOrUnionStringOnlyType($expr)) {
return $this->resolveString($isNegated, $expr);
}
$exprType = $this->getType($expr);
if ($exprType->isInteger()->yes()) {
return $this->resolveInteger($isNegated, $expr);
}
if ($exprType->isFloat()->yes()) {
return $this->resolveFloat($isNegated, $expr);
}
if ($this->nodeTypeResolver->isNullableTypeOfSpecificType($expr, ObjectType::class)) {
return $this->resolveNullable($isNegated, $expr);
}
return null;
}
/**
* @return \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\Greater|null
*/
private function resolveCount(bool $isNegated, FuncCall $funcCall)
{
if ($funcCall->isFirstClassCallable()) {
return null;
}
$countedType = $this->getType($funcCall->getArgs()[0]->value);
if ($countedType->isArray()->yes()) {
return null;
}
$lNumber = new LNumber(0);
// compare === 0, assumption
if ($isNegated) {
return new Identical($funcCall, $lNumber);
}
return new Greater($funcCall, $lNumber);
}
/**
* @return Identical|NotIdentical|null
*/
private function resolveArray(bool $isNegated, Expr $expr) : ?BinaryOp
{
if (!$expr instanceof Variable) {
return null;
}
$array = new Array_([]);
// compare === []
if ($isNegated) {
return new Identical($expr, $array);
}
return new NotIdentical($expr, $array);
}
/**
* @return \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical|\PhpParser\Node\Expr\BinaryOp\BooleanAnd|\PhpParser\Node\Expr\BinaryOp\BooleanOr
*/
private function resolveString(bool $isNegated, Expr $expr)
{
$emptyString = new String_('');
$identical = $this->resolveIdentical($expr, $isNegated, $emptyString);
$value = $this->valueResolver->getValue($expr);
// unknown value. may be from parameter
if ($value === null) {
return $this->resolveZeroIdenticalstring($identical, $isNegated, $expr);
}
$length = \strlen((string) $value);
if ($length === 1) {
$zeroString = new String_('0');
return $this->resolveIdentical($expr, $isNegated, $zeroString);
}
return $identical;
}
/**
* @return \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical
*/
private function resolveIdentical(Expr $expr, bool $isNegated, String_ $string)
{
/**
* // compare === ''
*
* @var Identical|NotIdentical $identical
*/
$identical = $isNegated ? new Identical($expr, $string) : new NotIdentical($expr, $string);
return $identical;
}
/**
* @param \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical $identical
* @return \PhpParser\Node\Expr\BinaryOp\BooleanAnd|\PhpParser\Node\Expr\BinaryOp\BooleanOr
*/
private function resolveZeroIdenticalstring($identical, bool $isNegated, Expr $expr)
{
$string = new String_('0');
$zeroIdentical = $isNegated ? new Identical($expr, $string) : new NotIdentical($expr, $string);
return $isNegated ? new BooleanOr($identical, $zeroIdentical) : new BooleanAnd($identical, $zeroIdentical);
}
/**
* @return \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical
*/
private function resolveInteger(bool $isNegated, Expr $expr)
{
$lNumber = new LNumber(0);
if ($isNegated) {
return new Identical($expr, $lNumber);
}
return new NotIdentical($expr, $lNumber);
}
/**
* @return \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical
*/
private function resolveFloat(bool $isNegated, Expr $expr)
{
$dNumber = new DNumber(0.0);
if ($isNegated) {
return new Identical($expr, $dNumber);
}
return new NotIdentical($expr, $dNumber);
}
/**
* @return \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical
*/
private function resolveNullable(bool $isNegated, Expr $expr)
{
$constFetch = $this->nodeFactory->createNull();
if ($isNegated) {
return new Identical($expr, $constFetch);
}
return new NotIdentical($expr, $constFetch);
}
}