7
7
use PhpParser \Node ;
8
8
use PhpParser \Node \Arg ;
9
9
use PhpParser \Node \Attribute ;
10
+ use PhpParser \Node \Expr ;
10
11
use PhpParser \Node \Expr \MethodCall ;
11
12
use PhpParser \Node \Expr \Variable ;
12
13
use PhpParser \Node \Identifier ;
19
20
use PhpParser \NodeVisitor ;
20
21
use PHPStan \Reflection \ReflectionProvider ;
21
22
use PHPStan \Type \ObjectType ;
23
+ use Rector \Doctrine \NodeAnalyzer \AttributeFinder ;
22
24
use Rector \NodeTypeResolver \Node \AttributeKey ;
23
25
use Rector \Rector \AbstractRector ;
24
26
use Rector \Symfony \Enum \CommandMethodName ;
@@ -38,6 +40,7 @@ final class CommandHelpToAttributeRector extends AbstractRector implements MinPh
38
40
{
39
41
public function __construct (
40
42
private readonly ReflectionProvider $ reflectionProvider ,
43
+ private readonly AttributeFinder $ attributeFinder
41
44
) {
42
45
}
43
46
@@ -105,8 +108,10 @@ public function refactor(Node $node): ?Node
105
108
return null ;
106
109
}
107
110
108
- $ asCommandAttribute = $ this ->getAsCommandAttribute ($ node );
109
- if ($ asCommandAttribute === null ) {
111
+ $ asCommandAttribute = $ this ->attributeFinder ->findAttributeByClass ($ node , SymfonyAttribute::AS_COMMAND );
112
+
113
+ // $asCommandAttribute = $this->getAsCommandAttribute($node);
114
+ if (! $ asCommandAttribute instanceof Attribute) {
110
115
return null ;
111
116
}
112
117
@@ -126,15 +131,15 @@ public function refactor(Node $node): ?Node
126
131
return null ;
127
132
}
128
133
129
- $ wrappedHelp = new String_ (
134
+ $ string = new String_ (
130
135
$ helpExpr ->value ,
131
136
[
132
137
Attributekey::KIND => String_::KIND_NOWDOC ,
133
138
AttributeKey::DOC_LABEL => 'TXT ' ,
134
139
]
135
140
);
136
141
137
- $ asCommandAttribute ->args [] = new Arg ($ wrappedHelp , false , false , [], new Identifier ('help ' ));
142
+ $ asCommandAttribute ->args [] = new Arg ($ string , false , false , [], new Identifier ('help ' ));
138
143
139
144
if ($ configureClassMethod ->stmts === []) {
140
145
unset($ configureClassMethod );
@@ -143,29 +148,16 @@ public function refactor(Node $node): ?Node
143
148
return $ node ;
144
149
}
145
150
146
- private function getAsCommandAttribute (Class_ $ class ): ?Attribute
147
- {
148
- foreach ($ class ->attrGroups as $ attrGroup ) {
149
- foreach ($ attrGroup ->attrs as $ attribute ) {
150
- if ($ this ->nodeNameResolver ->isName ($ attribute ->name , SymfonyAttribute::AS_COMMAND )) {
151
- return $ attribute ;
152
- }
153
- }
154
- }
155
-
156
- return null ;
157
- }
158
-
159
151
/**
160
152
* Returns the argument passed to setHelp() and removes the MethodCall node.
161
153
*/
162
- private function findAndRemoveSetHelpExpr (ClassMethod $ configureMethod ): ?String_
154
+ private function findAndRemoveSetHelpExpr (ClassMethod $ classMethod ): ?String_
163
155
{
164
156
$ helpString = null ;
165
157
166
158
$ this ->traverseNodesWithCallable (
167
- (array ) $ configureMethod ->stmts ,
168
- function (Node $ node ) use (&$ helpString ) {
159
+ (array ) $ classMethod ->stmts ,
160
+ function (Node $ node ) use (&$ helpString ): int | null | Expr {
169
161
if ($ node instanceof Class_ || $ node instanceof Function_) {
170
162
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN ;
171
163
}
@@ -196,9 +188,9 @@ function (Node $node) use (&$helpString) {
196
188
}
197
189
);
198
190
199
- foreach ((array ) $ configureMethod ->stmts as $ key => $ stmt ) {
191
+ foreach ((array ) $ classMethod ->stmts as $ key => $ stmt ) {
200
192
if ($ this ->isExpressionVariableThis ($ stmt )) {
201
- unset($ configureMethod ->stmts [$ key ]);
193
+ unset($ classMethod ->stmts [$ key ]);
202
194
}
203
195
}
204
196
0 commit comments