@@ -34,7 +34,7 @@ final class AnnotationWithValueToAttributeRector extends AbstractRector implemen
3434 */
3535 private array $ annotationWithValueToAttributes = [];
3636
37- private ? Class_ $ currentClass = null ;
37+ private bool $ hasChanged = false ;
3838
3939 public function __construct (
4040 private readonly PhpDocTagRemover $ phpDocTagRemover ,
@@ -86,7 +86,7 @@ final class SomeTest extends TestCase
8686 */
8787 public function getNodeTypes (): array
8888 {
89- return [Class_::class, ClassMethod::class ];
89+ return [Class_::class];
9090 }
9191
9292 public function provideMinPhpVersion (): int
@@ -95,24 +95,61 @@ public function provideMinPhpVersion(): int
9595 }
9696
9797 /**
98- * @param Class_|ClassMethod $node
98+ * @param Class_ $node
9999 */
100100 public function refactor (Node $ node ): ?Node
101101 {
102+
102103 if (! $ this ->testsNodeAnalyzer ->isInTestClass ($ node )) {
103104 return null ;
104105 }
105106
106- if ($ node instanceof Class_) {
107- $ this ->currentClass = $ node ;
107+ $ this ->hasChanged = false ;
108+
109+ // handle class level
110+ // $classPhpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
111+ $ this ->refactorClassMethodOrClass ($ node , $ node );
112+
113+ foreach ($ node ->getMethods () as $ classMethod ) {
114+ $ this ->refactorClassMethodOrClass ($ classMethod , $ node );
108115 }
109116
110- $ phpDocInfo = $ this ->phpDocInfoFactory ->createFromNode ($ node );
111- if (! $ phpDocInfo instanceof PhpDocInfo) {
117+ if (! $ this ->hasChanged ) {
112118 return null ;
113119 }
114120
115- $ hasChanged = false ;
121+ return $ node ;
122+ }
123+
124+ /**
125+ * @param mixed[] $configuration
126+ */
127+ public function configure (array $ configuration ): void
128+ {
129+ Assert ::allIsInstanceOf ($ configuration , AnnotationWithValueToAttribute::class);
130+ $ this ->annotationWithValueToAttributes = $ configuration ;
131+ }
132+
133+ private function resolveAttributeValue (
134+ GenericTagValueNode $ genericTagValueNode ,
135+ AnnotationWithValueToAttribute $ annotationWithValueToAttribute
136+ ): mixed {
137+ $ valueMap = $ annotationWithValueToAttribute ->getValueMap ();
138+ if ($ valueMap === []) {
139+ // no map? convert value as it is
140+ return $ genericTagValueNode ->value ;
141+ }
142+
143+ $ originalValue = strtolower ($ genericTagValueNode ->value );
144+ return $ valueMap [$ originalValue ];
145+ }
146+
147+ private function refactorClassMethodOrClass (ClassMethod |Class_ $ classOrClass , Class_ $ class ): void
148+ {
149+ $ phpDocInfo = $ this ->phpDocInfoFactory ->createFromNode ($ classOrClass );
150+ if (! $ phpDocInfo instanceof PhpDocInfo) {
151+ return ;
152+ }
116153
117154 foreach ($ this ->annotationWithValueToAttributes as $ annotationWithValueToAttribute ) {
118155 /** @var PhpDocTagNode[] $desiredTagValueNodes */
@@ -133,47 +170,18 @@ public function refactor(Node $node): ?Node
133170 [$ attributeValue ]
134171 );
135172
136- if ($ node instanceof ClassMethod && $ annotationWithValueToAttribute ->getIsOnClassLevel () && $ this ->currentClass instanceof Class_) {
137- Assert ::isAOf ($ this ->currentClass , Class_::class);
138- $ this ->currentClass ->attrGroups = array_merge ($ this ->currentClass ->attrGroups , [$ attributeGroup ]);
173+ if ($ annotationWithValueToAttribute ->getIsOnClassLevel ()) {
174+ $ class ->attrGroups = array_merge ($ class ->attrGroups , [$ attributeGroup ]);
139175 } else {
140- $ node ->attrGroups = array_merge ($ node ->attrGroups , [$ attributeGroup ]);
176+ $ classOrClass ->attrGroups = array_merge ($ classOrClass ->attrGroups , [$ attributeGroup ]);
141177 }
142178
143179 // cleanup
144180 $ this ->phpDocTagRemover ->removeTagValueFromNode ($ phpDocInfo , $ desiredTagValueNode );
145- $ hasChanged = true ;
146- }
147- }
148-
149- if ($ hasChanged ) {
150- $ this ->docBlockUpdater ->updateRefactoredNodeWithPhpDocInfo ($ node );
151- return $ node ;
152- }
153-
154- return null ;
155- }
181+ $ this ->docBlockUpdater ->updateRefactoredNodeWithPhpDocInfo ($ classOrClass );
156182
157- /**
158- * @param mixed[] $configuration
159- */
160- public function configure (array $ configuration ): void
161- {
162- Assert ::allIsInstanceOf ($ configuration , AnnotationWithValueToAttribute::class);
163- $ this ->annotationWithValueToAttributes = $ configuration ;
164- }
165-
166- private function resolveAttributeValue (
167- GenericTagValueNode $ genericTagValueNode ,
168- AnnotationWithValueToAttribute $ annotationWithValueToAttribute
169- ): mixed {
170- $ valueMap = $ annotationWithValueToAttribute ->getValueMap ();
171- if ($ valueMap === []) {
172- // no map? convert value as it is
173- return $ genericTagValueNode ->value ;
183+ $ this ->hasChanged = true ;
184+ }
174185 }
175-
176- $ originalValue = strtolower ($ genericTagValueNode ->value );
177- return $ valueMap [$ originalValue ];
178186 }
179187}
0 commit comments