forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountOnNullRector.php
173 lines (147 loc) · 5.13 KB
/
CountOnNullRector.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
<?php
declare(strict_types=1);
namespace Rector\Php71\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\Cast\Array_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Trait_;
use PHPStan\Type\ArrayType;
use PHPStan\Type\NullType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\TypeAnalyzer\CountableTypeAnalyzer;
use Rector\Php71\NodeAnalyzer\CountableAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://3v4l.org/Bndc9
*
* @see \Rector\Tests\Php71\Rector\FuncCall\CountOnNullRector\CountOnNullRectorTest
*/
final class CountOnNullRector extends AbstractRector
{
/**
* @var string
*/
private const ALREADY_CHANGED_ON_COUNT = 'already_changed_on_count';
/**
* @var CountableTypeAnalyzer
*/
private $countableTypeAnalyzer;
/**
* @var CountableAnalyzer
*/
private $countableAnalyzer;
public function __construct(CountableTypeAnalyzer $countableTypeAnalyzer, CountableAnalyzer $countableAnalyzer)
{
$this->countableTypeAnalyzer = $countableTypeAnalyzer;
$this->countableAnalyzer = $countableAnalyzer;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes count() on null to safe ternary check',
[new CodeSample(
<<<'CODE_SAMPLE'
$values = null;
$count = count($values);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$values = null;
$count = count((array) $values);
CODE_SAMPLE
)]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}
$countedNode = $node->args[0]->value;
if ($this->countableTypeAnalyzer->isCountableType($countedNode)) {
return null;
}
// this can lead to false positive by phpstan, but that's best we can do
$onlyValueType = $this->getStaticType($countedNode);
if ($onlyValueType instanceof ArrayType) {
if (! $this->countableAnalyzer->isCastableArrayType($countedNode)) {
return null;
}
return $this->castToArray($countedNode, $node);
}
if ($this->nodeTypeResolver->isNullableTypeOfSpecificType($countedNode, ArrayType::class)) {
return $this->castToArray($countedNode, $node);
}
if ($this->nodeTypeResolver->isNullableType($countedNode) || $this->nodeTypeResolver->isStaticType(
$countedNode,
NullType::class
)) {
$identical = new Identical($countedNode, $this->nodeFactory->createNull());
$ternary = new Ternary($identical, new LNumber(0), $node);
// prevent infinity loop re-resolution
$node->setAttribute(self::ALREADY_CHANGED_ON_COUNT, true);
return $ternary;
}
if ($this->isAtLeastPhpVersion(PhpVersionFeature::IS_COUNTABLE)) {
$conditionNode = new FuncCall(new Name('is_countable'), [new Arg($countedNode)]);
} else {
$instanceof = new Instanceof_($countedNode, new FullyQualified('Countable'));
$conditionNode = new BooleanOr($this->nodeFactory->createFuncCall(
'is_array',
[new Arg($countedNode)]
), $instanceof);
}
// prevent infinity loop re-resolution
$node->setAttribute(self::ALREADY_CHANGED_ON_COUNT, true);
return new Ternary($conditionNode, $node, new LNumber(0));
}
private function shouldSkip(FuncCall $funcCall): bool
{
if (! $this->isName($funcCall, 'count')) {
return true;
}
$alreadyChangedOnCount = $funcCall->getAttribute(self::ALREADY_CHANGED_ON_COUNT);
// check if it has some condition before already, if so, probably it's already handled
if ($alreadyChangedOnCount) {
return true;
}
$parentNode = $funcCall->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Ternary) {
return true;
}
if (! isset($funcCall->args[0])) {
return true;
}
// skip node in trait, as impossible to analyse
$classLike = $funcCall->getAttribute(AttributeKey::CLASS_NODE);
return $classLike instanceof Trait_;
}
private function castToArray(Expr $countedExpr, FuncCall $funcCall): FuncCall
{
$castArray = new Array_($countedExpr);
$funcCall->args = [new Arg($castArray)];
return $funcCall;
}
}