-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathErrorNamesPropertyToConstantRector.php
More file actions
141 lines (113 loc) · 3.67 KB
/
ErrorNamesPropertyToConstantRector.php
File metadata and controls
141 lines (113 loc) · 3.67 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
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony61\Rector\StaticPropertyFetch;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Const_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\Symfony\Enum\SymfonyClass;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* Covers:
* - https://github.com/symfony/symfony/blob/6.1/UPGRADE-6.1.md#validator
*
* @see \Rector\Symfony\Tests\Symfony61\Rector\StaticPropertyFetch\ErrorNamesPropertyToConstantRector\ErrorNamesPropertyToConstantRectorTest
*/
final class ErrorNamesPropertyToConstantRector extends AbstractRector
{
public function __construct(
private readonly ReflectionResolver $reflectionResolver,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Turns old Constraint::$errorNames properties to use Constraint::ERROR_NAMES instead',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\Validator\Constraints\NotBlank;
class SomeClass
{
NotBlank::$errorNames
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\Validator\Constraints\NotBlank;
class SomeClass
{
NotBlank::ERROR_NAMES
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StaticPropertyFetch::class, Class_::class];
}
/**
* @param StaticPropertyFetch|Class_ $node
*/
public function refactor(Node $node): ?Node
{
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}
if (! $classReflection->is(SymfonyClass::VALIDATOR_CONSTRAINT)) {
return null;
}
if ($node instanceof StaticPropertyFetch) {
return $this->refactorStaticPropertyFetch($node, $classReflection);
}
foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Property) {
continue;
}
if (! $stmt->isStatic()) {
continue;
}
if (! $this->isName($stmt->props[0], 'errorNames')) {
continue;
}
$node->stmts[$key] = $this->createClassConst($stmt, $stmt);
return $node;
}
return null;
}
private function refactorStaticPropertyFetch(
StaticPropertyFetch $node,
ClassReflection $classReflection
): ?ClassConstFetch {
if (! $this->isName($node->name, 'errorNames')) {
return null;
}
$parentClass = $classReflection->getParentClass();
if (! $parentClass instanceof ClassReflection) {
return null;
}
return $this->nodeFactory->createClassConstFetch($parentClass->getName(), 'ERROR_NAMES');
}
private function createClassConst(Property $property, Property $stmt): ClassConst
{
$propertyItem = $property->props[0];
$const = new Const_('ERROR_NAMES', $propertyItem->default);
$classConst = new ClassConst([$const], $stmt->flags & ~Modifiers::STATIC);
$classConst->setDocComment($property->getDocComment());
return $classConst;
}
}