-
-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathAttributeKeyToClassConstFetchRector.php
139 lines (135 loc) · 4.85 KB
/
AttributeKeyToClassConstFetchRector.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
<?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\Transform\ValueObject\AttributeKeyToClassConstFetch;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202501\Webmozart\Assert\Assert;
/**
* @api used in rector-doctrine
* @see \Rector\Tests\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector\AttributeKeyToClassConstFetchRectorTest
*/
final class AttributeKeyToClassConstFetchRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @readonly
*/
private ValueResolver $valueResolver;
/**
* @var AttributeKeyToClassConstFetch[]
*/
private array $attributeKeysToClassConstFetches = [];
public function __construct(ValueResolver $valueResolver)
{
$this->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;
}
}