-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathAddAssertArrayFromClassMethodDocblockRector.php
More file actions
247 lines (201 loc) · 6.92 KB
/
AddAssertArrayFromClassMethodDocblockRector.php
File metadata and controls
247 lines (201 loc) · 6.92 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
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
<?php
declare(strict_types=1);
namespace Rector\Assert\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Rector\Assert\Enum\AssertClassName;
use Rector\Assert\NodeAnalyzer\ExistingAssertStaticCallResolver;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @experimental Check generic array key/value types in runtime with assert. Generics for impatient people.
*
* @see \Rector\Tests\Assert\Rector\ClassMethod\AddAssertArrayFromClassMethodDocblockRector\AddAssertArrayFromClassMethodDocblockRectorTest
*/
final class AddAssertArrayFromClassMethodDocblockRector extends AbstractRector implements ConfigurableRectorInterface
{
private string $assertClass = AssertClassName::WEBMOZART;
public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly ExistingAssertStaticCallResolver $existingAssertStaticCallResolver
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add key and value assert based on docblock @param type declarations (pick from "webmozart" or "beberlei" asserts)',
[
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
/**
* @param int[] $items
*/
public function run(array $items)
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
<?php
use Webmozart\Assert\Assert;
class SomeClass
{
/**
* @param int[] $items
*/
public function run(array $items)
{
Assert::allInteger($items);
}
}
CODE_SAMPLE
,
[AssertClassName::WEBMOZART]
),
]
);
}
public function getNodeTypes(): array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?ClassMethod
{
$scope = ScopeFetcher::fetch($node);
if (! $scope->isInClass()) {
return null;
}
if ($node->getStmts() === [] || $node->isAbstract()) {
return null;
}
$methodPhpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if (! $methodPhpDocInfo instanceof PhpDocInfo) {
return null;
}
$paramTagValueNodes = $methodPhpDocInfo->getParamTagValueNodes();
if ($paramTagValueNodes === []) {
return null;
}
$assertStaticCallStmts = [];
foreach ($node->getParams() as $param) {
if (! $param->type instanceof Identifier) {
continue;
}
// handle arrays only
if (! $this->isName($param->type, 'array')) {
continue;
}
if (! $param->var instanceof Variable) {
continue;
}
$paramName = $param->var->name;
if (! is_string($paramName)) {
continue;
}
$paramDocType = $methodPhpDocInfo->getParamType($paramName);
if (! $paramDocType instanceof ArrayType) {
continue;
}
$valueAssertMethod = $this->matchTypeToAssertMethod($paramDocType->getItemType());
if (is_string($valueAssertMethod)) {
$assertStaticCallStmts[] = $this->createAssertExpression($param->var, $valueAssertMethod);
}
$keyAssertMethod = $this->matchTypeToAssertMethod($paramDocType->getKeyType());
if (is_string($keyAssertMethod)) {
$arrayKeys = new FuncCall(new Name('array_keys'), [new Arg($param->var)]);
$assertStaticCallStmts[] = $this->createAssertExpression($arrayKeys, $keyAssertMethod);
}
}
// filter existing assert to avoid duplication
if ($assertStaticCallStmts === []) {
return null;
}
$existingAssertCallHashes = $this->existingAssertStaticCallResolver->resolve($node);
$assertStaticCallStmts = $this->filterOutExistingStaticCall($assertStaticCallStmts, $existingAssertCallHashes);
if ($assertStaticCallStmts === []) {
return null;
}
$node->stmts = array_merge($assertStaticCallStmts, $node->stmts);
return $node;
}
/**
* @param array<string> $configuration
*/
public function configure(array $configuration): void
{
if ($configuration === []) {
// default
return;
}
Assert::count($configuration, 1);
Assert::inArray($configuration[0], [AssertClassName::BEBERLEI, AssertClassName::WEBMOZART]);
$this->assertClass = $configuration[0];
}
private function createAssertExpression(Expr $expr, string $methodName): Expression
{
$assertFullyQualified = new FullyQualified($this->assertClass);
$staticCall = new StaticCall($assertFullyQualified, $methodName, [new Arg($expr)]);
return new Expression($staticCall);
}
/**
* @param Expression[] $assertStaticCallStmts
* @param string[] $existingAssertCallHashes
* @return Expression[]
*/
private function filterOutExistingStaticCall(array $assertStaticCallStmts, array $existingAssertCallHashes): array
{
$standard = new Standard();
return array_filter($assertStaticCallStmts, function (Expression $assertStaticCallExpression) use (
$standard,
$existingAssertCallHashes
): bool {
$currentStaticCallHash = $standard->prettyPrintExpr($assertStaticCallExpression->expr);
return ! in_array($currentStaticCallHash, $existingAssertCallHashes, true);
});
}
private function matchTypeToAssertMethod(Type $type): ?string
{
if ($type instanceof IntegerType) {
return 'allInteger';
}
if ($type instanceof StringType) {
return 'allString';
}
if ($type instanceof FloatType) {
return 'allFloat';
}
if ($type instanceof BooleanType) {
return 'allBoolean';
}
return null;
}
}