Repository navigation
Expand file tree
/
Copy pathAttributeKeyToClassConstFetchRector.php
More file actions
184 lines (157 loc) · 5.06 KB
/
Copy pathAttributeKeyToClassConstFetchRector.php
File metadata and controls
184 lines (157 loc) · 5.06 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
<?php
declare(strict_types=1);
namespace Rector\Transform\Rector\Attribute;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Identifier;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\Transform\ValueObject\AttributeKeyToClassConstFetch;
use Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector\AttributeKeyToClassConstFetchRectorTest
*/
final class AttributeKeyToClassConstFetchRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var AttributeKeyToClassConstFetch[]
*/
private array $attributeKeysToClassConstFetches = [];
public function __construct(
private readonly ValueResolver $valueResolver
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Replace key value on specific attribute to class constant', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
use Doctrine\ORM\Mapping\Column;
class SomeClass
{
#[Column(type: "string")]
public $name;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Doctrine\ORM\Mapping\Column;
use Doctrine\DBAL\Types\Types;
class SomeClass
{
#[Column(type: Types::STRING)]
public $name;
}
CODE_SAMPLE
,
[
new AttributeKeyToClassConstFetch('Doctrine\ORM\Mapping\Column', 'type', 'Doctrine\DBAL\Types\Types', [
'string' => 'STRING',
]),
]
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [
Class_::class,
Property::class,
Param::class,
ClassMethod::class,
Function_::class,
Closure::class,
ArrowFunction::class,
Interface_::class,
];
}
/**
* @param Class_|Property|Param|ClassMethod|Function_|Closure|ArrowFunction|Interface_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node->attrGroups === []) {
return null;
}
$hasChanged = false;
foreach ($this->attributeKeysToClassConstFetches as $attributeKeyToClassConstFetch) {
foreach ($node->attrGroups as $attrGroup) {
if ($this->processToClassConstFetch($attrGroup, $attributeKeyToClassConstFetch)) {
$hasChanged = true;
}
}
}
if ($hasChanged) {
return $node;
}
return null;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, AttributeKeyToClassConstFetch::class);
$this->attributeKeysToClassConstFetches = $configuration;
}
private function processToClassConstFetch(
AttributeGroup $attributeGroup,
AttributeKeyToClassConstFetch $attributeKeyToClassConstFetch
): bool {
$hasChanged = false;
foreach ($attributeGroup->attrs as $attribute) {
if (! $this->isName($attribute->name, $attributeKeyToClassConstFetch->getAttributeClass())) {
continue;
}
foreach ($attribute->args as $arg) {
$argName = $arg->name;
if (! $argName instanceof Identifier) {
continue;
}
if (! $this->isName($argName, $attributeKeyToClassConstFetch->getAttributeKey())) {
continue;
}
if ($this->processArg($arg, $attributeKeyToClassConstFetch)) {
$hasChanged = true;
}
}
}
return $hasChanged;
}
private function processArg(Arg $arg, AttributeKeyToClassConstFetch $attributeKeyToClassConstFetch): bool
{
$value = $this->valueResolver->getValue($arg->value);
$constName = $attributeKeyToClassConstFetch->getValuesToConstantsMap()[$value] ?? null;
if ($constName === null) {
return false;
}
$classConstFetch = $this->nodeFactory->createClassConstFetch(
$attributeKeyToClassConstFetch->getConstantClass(),
$constName
);
if (
$arg->value instanceof ClassConstFetch
&& $this->getName($arg->value) === $this->getName($classConstFetch)
) {
return false;
}
$arg->value = $classConstFetch;
return true;
}
}