44
55namespace Rector \Symfony \Symfony61 \Rector \StaticPropertyFetch ;
66
7+ use PhpParser \Modifiers ;
78use PhpParser \Node ;
9+ use PhpParser \Node \Const_ ;
10+ use PhpParser \Node \Expr \ClassConstFetch ;
811use PhpParser \Node \Expr \StaticPropertyFetch ;
12+ use PhpParser \Node \Stmt \Class_ ;
13+ use PhpParser \Node \Stmt \ClassConst ;
14+ use PhpParser \Node \Stmt \Property ;
915use PHPStan \Reflection \ClassReflection ;
1016use Rector \Rector \AbstractRector ;
1117use Rector \Reflection \ReflectionResolver ;
18+ use Rector \Symfony \Enum \SymfonyClass ;
1219use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
1320use 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}
0 commit comments