Skip to content

Commit a470609

Browse files
authored
add property support to ErrorNamesPropertyToConstantRector (#860)
1 parent 7ea6950 commit a470609

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\Symfony61\Rector\StaticPropertyFetch\ErrorNamesPropertyToConstantRector\Fixture;
4+
5+
use Symfony\Component\Validator\Constraints\NotBlank;
6+
7+
final class ClassProperty extends NotBlank
8+
{
9+
/**
10+
* @var string[]
11+
*/
12+
protected static $errorNames = [];
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Symfony\Tests\Symfony61\Rector\StaticPropertyFetch\ErrorNamesPropertyToConstantRector\Fixture;
20+
21+
use Symfony\Component\Validator\Constraints\NotBlank;
22+
23+
final class ClassProperty extends NotBlank
24+
{
25+
/**
26+
* @var string[]
27+
*/
28+
protected const ERROR_NAMES = [];
29+
}
30+
31+
?>

rules/Symfony61/Rector/StaticPropertyFetch/ErrorNamesPropertyToConstantRector.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44

55
namespace Rector\Symfony\Symfony61\Rector\StaticPropertyFetch;
66

7+
use PhpParser\Modifiers;
78
use PhpParser\Node;
9+
use PhpParser\Node\Const_;
10+
use PhpParser\Node\Expr\ClassConstFetch;
811
use PhpParser\Node\Expr\StaticPropertyFetch;
12+
use PhpParser\Node\Stmt\Class_;
13+
use PhpParser\Node\Stmt\ClassConst;
14+
use PhpParser\Node\Stmt\Property;
915
use PHPStan\Reflection\ClassReflection;
1016
use Rector\Rector\AbstractRector;
1117
use Rector\Reflection\ReflectionResolver;
18+
use Rector\Symfony\Enum\SymfonyClass;
1219
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1320
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1421

@@ -61,21 +68,52 @@ class SomeClass
6168
*/
6269
public function getNodeTypes(): array
6370
{
64-
return [StaticPropertyFetch::class];
71+
return [StaticPropertyFetch::class, Class_::class];
6572
}
6673

6774
/**
68-
* @param StaticPropertyFetch $node
75+
* @param StaticPropertyFetch|Class_ $node
6976
*/
7077
public function refactor(Node $node): ?Node
7178
{
7279
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
7380
if (! $classReflection instanceof ClassReflection) {
7481
return null;
7582
}
76-
if (! $classReflection->is('Symfony\Component\Validator\Constraint')) {
83+
84+
if (! $classReflection->is(SymfonyClass::VALIDATOR_CONSTRAINT)) {
7785
return null;
7886
}
87+
88+
if ($node instanceof StaticPropertyFetch) {
89+
return $this->refactorStaticPropertyFetch($node, $classReflection);
90+
}
91+
92+
foreach ($node->stmts as $key => $stmt) {
93+
if (! $stmt instanceof Property) {
94+
continue;
95+
}
96+
97+
if (! $stmt->isStatic()) {
98+
continue;
99+
}
100+
101+
if (! $this->isName($stmt->props[0], 'errorNames')) {
102+
continue;
103+
}
104+
105+
$node->stmts[$key] = $this->createClassConst($stmt, $stmt);
106+
107+
return $node;
108+
}
109+
110+
return null;
111+
}
112+
113+
private function refactorStaticPropertyFetch(
114+
StaticPropertyFetch $node,
115+
ClassReflection $classReflection
116+
): ?ClassConstFetch {
79117
if (! $this->isName($node->name, 'errorNames')) {
80118
return null;
81119
}
@@ -87,4 +125,17 @@ public function refactor(Node $node): ?Node
87125

88126
return $this->nodeFactory->createClassConstFetch($parentClass->getName(), 'ERROR_NAMES');
89127
}
128+
129+
private function createClassConst(Property $property, Property $stmt): ClassConst
130+
{
131+
$propertyItem = $property->props[0];
132+
133+
$const = new Const_('ERROR_NAMES', $propertyItem->default);
134+
135+
$classConst = new ClassConst([$const], $stmt->flags & ~Modifiers::STATIC);
136+
137+
$classConst->setDocComment($property->getDocComment());
138+
139+
return $classConst;
140+
}
90141
}

src/Enum/SymfonyClass.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ final class SymfonyClass
166166
*/
167167
public const SYMFONY_VALIDATOR_CONSTRAINTS_COLLECTION = 'Symfony\Component\Validator\Constraints\Collection';
168168

169+
/**
170+
* @var string
171+
*/
172+
public const VALIDATOR_CONSTRAINT = 'Symfony\Component\Validator\Constraint';
173+
169174
/**
170175
* @var string
171176
*/

0 commit comments

Comments
 (0)