forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVarInlineAnnotationToAssertRector.php
224 lines (187 loc) · 6.56 KB
/
VarInlineAnnotationToAssertRector.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
<?php
declare(strict_types=1);
namespace Rector\CodeQualityStrict\Rector\Stmt;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\Type\BooleanType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQualityStrict\Rector\Stmt\VarInlineAnnotationToAssertRector\VarInlineAnnotationToAssertRectorTest
*/
final class VarInlineAnnotationToAssertRector extends AbstractRector
{
/**
* @var string
*/
private const ASSERT = 'assert';
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Turn @var inline checks above code to assert() of the type',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
/** @var SpecificClass $value */
$value->call();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
/** @var SpecificClass $value */
assert($value instanceof SpecificClass);
$value->call();
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Stmt::class];
}
/**
* @param Stmt $node
*/
public function refactor(Node $node): ?Node
{
// skip properties
if ($node instanceof Property) {
return null;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$docVariableName = $this->getVarDocVariableName($phpDocInfo);
if ($docVariableName === null) {
return null;
}
$variable = $this->findVariableByName($node, $docVariableName);
if (! $variable instanceof Variable) {
return null;
}
$isVariableJustCreated = $this->isVariableJustCreated($node, $docVariableName);
if (! $isVariableJustCreated) {
return $this->refactorFreshlyCreatedNode($node, $phpDocInfo, $variable);
}
return $this->refactorAlreadyCreatedNode($node, $phpDocInfo, $variable);
}
private function getVarDocVariableName(PhpDocInfo $phpDocInfo): ?string
{
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (! $varTagValueNode instanceof VarTagValueNode) {
return null;
}
$variableName = $varTagValueNode->variableName;
// no variable
if ($variableName === '') {
return null;
}
return ltrim($variableName, '$');
}
private function findVariableByName(Stmt $stmt, string $docVariableName): ?Node
{
return $this->betterNodeFinder->findFirst($stmt, function (Node $node) use ($docVariableName): bool {
if (! $node instanceof Variable) {
return false;
}
return $this->nodeNameResolver->isName($node, $docVariableName);
});
}
private function isVariableJustCreated(Stmt $stmt, string $docVariableName): bool
{
if (! $stmt instanceof Expression) {
return false;
}
if (! $stmt->expr instanceof Assign) {
return false;
}
$assign = $stmt->expr;
if (! $assign->var instanceof Variable) {
return false;
}
// the variable is on the left side = just created
return $this->nodeNameResolver->isName($assign->var, $docVariableName);
}
private function refactorFreshlyCreatedNode(Stmt $stmt, PhpDocInfo $phpDocInfo, Variable $variable): ?Node
{
$stmt->setAttribute(AttributeKey::COMMENTS, null);
$type = $phpDocInfo->getVarType();
$assertFuncCall = $this->createFuncCallBasedOnType($type, $variable);
if (! $assertFuncCall instanceof FuncCall) {
return null;
}
$phpDocInfo->removeByType(VarTagValueNode::class);
$this->addNodeBeforeNode($assertFuncCall, $stmt);
return $stmt;
}
private function refactorAlreadyCreatedNode(Stmt $stmt, PhpDocInfo $phpDocInfo, Variable $variable): ?Node
{
$varTagValue = $phpDocInfo->getVarTagValueNode();
if (! $varTagValue instanceof VarTagValueNode) {
throw new ShouldNotHappenException();
}
$phpStanType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType(
$varTagValue->type,
$variable
);
$assertFuncCall = $this->createFuncCallBasedOnType($phpStanType, $variable);
if (! $assertFuncCall instanceof FuncCall) {
return null;
}
$this->addNodeAfterNode($assertFuncCall, $stmt);
return $stmt;
}
private function createFuncCallBasedOnType(Type $type, Variable $variable): ?FuncCall
{
if ($type instanceof ObjectType) {
$instanceOf = new Instanceof_($variable, new FullyQualified($type->getClassName()));
return $this->nodeFactory->createFuncCall(self::ASSERT, [$instanceOf]);
}
if ($type instanceof IntegerType) {
$isInt = $this->nodeFactory->createFuncCall('is_int', [$variable]);
return $this->nodeFactory->createFuncCall(self::ASSERT, [$isInt]);
}
if ($type instanceof FloatType) {
$funcCall = $this->nodeFactory->createFuncCall('is_float', [$variable]);
return $this->nodeFactory->createFuncCall(self::ASSERT, [$funcCall]);
}
if ($type instanceof StringType) {
$isString = $this->nodeFactory->createFuncCall('is_string', [$variable]);
return $this->nodeFactory->createFuncCall(self::ASSERT, [$isString]);
}
if ($type instanceof BooleanType) {
$isInt = $this->nodeFactory->createFuncCall('is_bool', [$variable]);
return $this->nodeFactory->createFuncCall(self::ASSERT, [$isInt]);
}
return null;
}
}