-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathConstraintOptionsToNamedArgumentsRector.php
More file actions
179 lines (143 loc) · 4.94 KB
/
ConstraintOptionsToNamedArgumentsRector.php
File metadata and controls
179 lines (143 loc) · 4.94 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
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony73\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\Symfony\Enum\SymfonyClass;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Symfony\Tests\Symfony73\Rector\Class_\ConstraintOptionsToNamedArgumentsRector\ConstraintOptionsToNamedArgumentsRectorTest
*/
final class ConstraintOptionsToNamedArgumentsRector extends AbstractRector
{
public function __construct(
private readonly ValueResolver $valueResolver,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Refactor Symfony constraints using array options to named arguments syntax for better readability and type safety.',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\Validator\Constraints\NotBlank;
$constraint = new NotBlank(['message' => 'This field should not be blank.']);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\Validator\Constraints\NotBlank;
$constraint = new NotBlank(message: 'This field should not be blank.');
CODE_SAMPLE
)]
);
}
public function getNodeTypes(): array
{
return [New_::class];
}
public function refactor(Node $node): ?Node
{
if (! $node instanceof New_) {
return null;
}
if ($node->isFirstClassCallable()) {
return null;
}
// Match classes starting with Symfony\Component\Validator\Constraints\
if (! $node->class instanceof FullyQualified && ! $node->class instanceof Name) {
return null;
}
$className = $this->getName($node->class);
if (! is_string($className)) {
return null;
}
if (
! str_starts_with($className, 'Symfony\Component\Validator\Constraints\\')
&& ! str_starts_with($className, 'Symfony\Bridge\Doctrine\Validator\Constraints\\')
) {
return null;
}
if (
$node->args === [] ||
! $node->args[0] instanceof Arg ||
! $node->args[0]->value instanceof Array_
) {
return null;
}
$argName = $node->args[0]->name;
if ($argName instanceof Identifier && $argName->name !== 'options') {
return null;
}
$args = $node->getArgs();
if ($className === SymfonyClass::SYMFONY_VALIDATOR_CONSTRAINTS_COLLECTION && $args[0]->value instanceof Array_) {
if ($args[0]->name instanceof Identifier) {
return null;
}
$args[0]->name = new Identifier('fields');
foreach ($args as $key => $arg) {
if (! $arg->name instanceof Identifier) {
continue;
}
if ($arg->name->toString() !== 'allowExtraFields') {
continue;
}
if (! $this->valueResolver->isFalse($arg->value)) {
continue;
}
unset($args[$key]);
}
$node->args = array_values($args);
return $node;
}
$array = $node->args[0]->value;
$namedArgs = [];
$oldTokens = $this->file->getOldTokens();
foreach ($array->items as $item) {
if (! $item instanceof ArrayItem) {
continue;
}
if (! $item->key instanceof Expr) {
// handle nested array
if ($item->value instanceof New_) {
return null;
}
continue;
}
$keyValue = $this->valueResolver->getValue($item->key);
if (! is_string($keyValue)) {
continue;
}
$lastTokenKey = $item->key->getEndTokenPos();
$startTokenValue = $item->value->getStartTokenPos();
while ($lastTokenKey < $startTokenValue) {
++$lastTokenKey;
if (! isset($oldTokens[$lastTokenKey])) {
break;
}
$token = $oldTokens[$lastTokenKey];
if ($token->is([T_DOC_COMMENT, T_COMMENT])) {
return null;
}
}
$arg = new Arg($item->value);
$arg->name = new Identifier($keyValue);
$namedArgs[] = $arg;
}
if ($namedArgs === []) {
return null;
}
$node->args = $namedArgs;
return $node;
}
}